Skip to content

Commit

Permalink
feat/upgrade to inertia 2 (#125)
Browse files Browse the repository at this point in the history
* feat(inertia-sails): extract isInertiaPartialRequest to a module

* chore(inertia-sails): move get-partial-data to props folder

* chore(inertia-sails): move inertia-headers to helpers folder

* chore(inertia-sails): move is-inertia-request to helpers folder

* feat(inertia-sails): extract building of Inertia page object to a module

* chore(inertia-sails): move inertia middleware to middlewares

* chore(inertia-sails): rename middlewares to middleware

* chore(inertia-sails): move resolve-validation-errors to helpers

* feat(inertia-sails): add module to resolveProp

* feat(inertia-sails): add symbol to ignore first load

* feat(inertia-sails): add module to pick props to resolve

* feat(inertia-sails): add AlwaysProp class

* feat(inertia-sails): add DeferProp class

* feat(inertia-sails): add MergeProp class

* feat(inertia-sails): add MergeableProp base class

* feat(inertia-sails): add module to resolve page props

* fix(inertia-sails): remove unnecessary await calls

* feat(inertia-sails): add module to resolve except props

* feat(inertia-sails): add OptionalProp class

* feat(inertia-sails): add resolve-deferred-props module

* feat(inertia-sails): add module to handle merging props

* feat(inertia-sails): add module to resolve only props

* feat(inertia-sails): update hook to include defer and render methods

* feat(inertia-sails): extract rendering logic to a module

* chore(inertia-sails): move render module to root

* feat(inertia-sails): implement Inertia redirect logic

* chore(inertia-sails): change private to lib

* feat(inertia-sails): implement optional prop method

* feat(inertia-sails): implement merge method

* chore(inertia-sails): update link to docs

* feat(inertia-sails): implement always prop method

* chore(inertia-sails): group class imports

* feat(inertia-sails): add clearHistory and encryptHistory methods

* chore(inertia-sails): use method shorthand

* chore(inertia-sails): add docs for clearHistory method

* feat(inertia-sails): change the boolean for clearHistory and encryptHistory

* feat(create-sails-generator): update inertiaRedirect custom response

* feat(create-sails-generator): update inertia response

* fix(inertia-sails): use error bag header constant

* feat(create-sails-generator): simplify badRequest custom response

* feat(inertia-sails): add handleBadRequest method

* feat(inertia-sails): add handleBadRequest method

* feat(mellow-react): simplify badRequest custom response

* fix(mellow-react): remove using 'back" for redirect

* feat(mellow-react): update inertia custom response

* feat(mellow-react): update inertiaRedirect custom response

* chore(mellow-react): upgrade dependencies

* chore(mellow-vue): update dependencies

* chore(create-sails-generator): bump version

* chore(inertia-sails): bump version

* feat(mellow-vue): update custom responses

* feat(mellow-svelte): update setup to Svelte 5

* chore: remove templates from workspaces

* chore: run npm install

* chore(mellow-svelte): update deps

* chore(mellow-react): run npm install

* feat(mellow-svelte): update custom responses

* chore(mellow-vue): update deps
  • Loading branch information
DominusKelvin authored Feb 12, 2025
1 parent 416f0e0 commit 4c74c37
Show file tree
Hide file tree
Showing 48 changed files with 4,540 additions and 9,734 deletions.
10,831 changes: 2,962 additions & 7,869 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"prettier": "2.8.8"
},
"workspaces": [
"packages/*",
"templates/*"
"packages/*"
],
"lint-staged": {
"**/*": "prettier --config ./.prettierrc.js --write --ignore-unknown"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,95 +1,8 @@
/**
* badRequest.js
*
* A custom response.
*
* Example usage:
* ```
* return res.badRequest();
* // -or-
* return res.badRequest(optionalData);
* ```
*
* Or with actions2:
* ```
* exits: {
* somethingHappened: {
* responseType: 'badRequest'
* }
* }
* ```
*
* ```
* throw 'somethingHappened';
* // -or-
* throw { somethingHappened: optionalData }
* ```
*/

// @ts-nocheck
module.exports = function badRequest(optionalData) {
// Get access to `req` and `res`
const req = this.req
const res = this.res

// Define the status code to send in the response.
const statusCodeToSet = 400

// Check if it's an Inertia request
if (req.header('X-Inertia')) {
if (optionalData && optionalData.problems) {
const errors = {}
optionalData.problems.forEach((problem) => {
if (typeof problem === 'object') {
Object.keys(problem).forEach((propertyName) => {
const sanitizedProblem = problem[propertyName].replace(/\.$/, '') // Trim trailing dot
if (!errors[propertyName]) {
errors[propertyName] = [sanitizedProblem]
} else {
errors[propertyName].push(sanitizedProblem)
}
})
} else {
const regex = /"(.*?)"/
const matches = problem.match(regex)

if (matches && matches.length > 1) {
const propertyName = matches[1]
const sanitizedProblem = problem
.replace(/"([^"]+)"/, '$1')
.replace('\n', '')
.replace('·', '')
.trim()
if (!errors[propertyName]) {
errors[propertyName] = [sanitizedProblem]
} else {
errors[propertyName].push(sanitizedProblem)
}
}
}
})
req.session.errors = errors
return res.redirect(303, 'back')
}
}

// If not an Inertia request, perform the normal badRequest response
if (optionalData === undefined) {
sails.log.info('Ran custom response: res.badRequest()')
return res.sendStatus(statusCodeToSet)
} else if (_.isError(optionalData)) {
sails.log.info(
'Custom response `res.badRequest()` called with an Error:',
optionalData
)

if (!_.isFunction(optionalData.toJSON)) {
if (process.env.NODE_ENV === 'production') {
return res.sendStatus(statusCodeToSet)
} else {
return res.status(statusCodeToSet).send(optionalData.stack)
}
}
} else {
return res.status(statusCodeToSet).send(optionalData)
}
return this.req._sails.inertia.handleBadRequest(
this.req,
this.res,
optionalData
)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
// @ts-nocheck

const inertiaHeaders = {
INERTIA: 'X-Inertia',
LOCATION: 'X-Inertia-Location'
}

module.exports = function inertiaRedirect(url) {
const req = this.req
const res = this.res

if (req.get(inertiaHeaders.INERTIA)) {
res.set(inertiaHeaders.LOCATION, url)
}

return res.redirect(
['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409,
url
)
return this.req._sails.inertia.location(this.req, this.res, url)
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,4 @@
// @ts-nocheck
const { encode } = require('querystring')
module.exports = function inertia(data) {
const req = this.req
const res = this.res
const sails = req._sails

const sharedProps = sails.inertia.sharedProps
const sharedViewData = sails.inertia.sharedViewData
const rootView = sails.config.inertia.rootView

const allProps = {
...sharedProps,
...data.props
}

const allViewData = {
...sharedViewData,
...data.viewData
}

let url = req.url || req.originalUrl
const assetVersion = sails.config.inertia.version
const currentVersion =
typeof assetVersion === 'function' ? assetVersion() : assetVersion

const page = {
component: data.page,
version: currentVersion,
props: allProps,
url
}

// Implements inertia partial reload. See https://inertiajs.com/partial-reload
if (
req.get(inertiaHeaders.PARTIAL_DATA) &&
req.get(inertiaHeaders.PARTIAL_COMPONENT) === page.component
) {
const only = req.get(inertiaHeaders.PARTIAL_DATA).split(',')
page.props = only.length ? getPartialData(data.props, only) : page.props
}

const queryParams = req.query
if (req.method === 'GET' && Object.keys(queryParams).length) {
// Keep original request query params
url += `?${encode(queryParams)}`
}

if (req.get(inertiaHeaders.INERTIA)) {
res.set(inertiaHeaders.INERTIA, true)
res.set('Vary', 'Accept')
return res.json(page)
} else {
// Implements full page reload
return res.view(rootView, {
page,
viewData: allViewData
})
}
}

function getPartialData(props, only = []) {
return Object.assign({}, ...only.map((key) => ({ [key]: props[key] })))
}

const inertiaHeaders = {
INERTIA: 'X-Inertia',
VERSION: 'X-Inertia-Version',
PARTIAL_DATA: 'X-Inertia-Partial-Data',
PARTIAL_COMPONENT: 'X-Inertia-Partial-Component',
LOCATION: 'X-Inertia-Location'
return this.req._sails.inertia.render(this.req, this.res, data)
}
2 changes: 1 addition & 1 deletion packages/create-sails-generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-sails-generator",
"version": "0.0.3",
"version": "0.1.0",
"description": "Sails generator for The Boring JavaScript Stack.",
"scripts": {
"test": "node --test"
Expand Down
Loading

0 comments on commit 4c74c37

Please sign in to comment.