-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
523 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* 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 } | ||
* ``` | ||
*/ | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* expired.js | ||
* | ||
* A custom response that content-negotiates the current request to either: | ||
* • serve an HTML error page about the specified token being invalid or expired | ||
* • or send back 498 (Token Expired/Invalid) with no response body. | ||
* | ||
* Example usage: | ||
* ``` | ||
* return res.expired(); | ||
* ``` | ||
* | ||
* Or with actions2: | ||
* ``` | ||
* exits: { | ||
* badToken: { | ||
* description: 'Provided token was expired, invalid, or already used up.', | ||
* responseType: 'expired' | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
module.exports = function expired() { | ||
sails.log.verbose('Ran custom response: res.expired()') | ||
|
||
return this.res.status(400).redirect('/link-expired') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// @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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @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 | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.