create-sails-generator 0.0.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,95 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
* badRequest.js
|
|
3
|
-
*
|
|
4
|
-
* A custom response.
|
|
5
|
-
*
|
|
6
|
-
* Example usage:
|
|
7
|
-
* ```
|
|
8
|
-
* return res.badRequest();
|
|
9
|
-
* // -or-
|
|
10
|
-
* return res.badRequest(optionalData);
|
|
11
|
-
* ```
|
|
12
|
-
*
|
|
13
|
-
* Or with actions2:
|
|
14
|
-
* ```
|
|
15
|
-
* exits: {
|
|
16
|
-
* somethingHappened: {
|
|
17
|
-
* responseType: 'badRequest'
|
|
18
|
-
* }
|
|
19
|
-
* }
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* ```
|
|
23
|
-
* throw 'somethingHappened';
|
|
24
|
-
* // -or-
|
|
25
|
-
* throw { somethingHappened: optionalData }
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
|
|
1
|
+
// @ts-nocheck
|
|
29
2
|
module.exports = function badRequest(optionalData) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const statusCodeToSet = 400
|
|
36
|
-
|
|
37
|
-
// Check if it's an Inertia request
|
|
38
|
-
if (req.header('X-Inertia')) {
|
|
39
|
-
if (optionalData && optionalData.problems) {
|
|
40
|
-
const errors = {}
|
|
41
|
-
optionalData.problems.forEach((problem) => {
|
|
42
|
-
if (typeof problem === 'object') {
|
|
43
|
-
Object.keys(problem).forEach((propertyName) => {
|
|
44
|
-
const sanitizedProblem = problem[propertyName].replace(/\.$/, '') // Trim trailing dot
|
|
45
|
-
if (!errors[propertyName]) {
|
|
46
|
-
errors[propertyName] = [sanitizedProblem]
|
|
47
|
-
} else {
|
|
48
|
-
errors[propertyName].push(sanitizedProblem)
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
} else {
|
|
52
|
-
const regex = /"(.*?)"/
|
|
53
|
-
const matches = problem.match(regex)
|
|
54
|
-
|
|
55
|
-
if (matches && matches.length > 1) {
|
|
56
|
-
const propertyName = matches[1]
|
|
57
|
-
const sanitizedProblem = problem
|
|
58
|
-
.replace(/"([^"]+)"/, '$1')
|
|
59
|
-
.replace('\n', '')
|
|
60
|
-
.replace('·', '')
|
|
61
|
-
.trim()
|
|
62
|
-
if (!errors[propertyName]) {
|
|
63
|
-
errors[propertyName] = [sanitizedProblem]
|
|
64
|
-
} else {
|
|
65
|
-
errors[propertyName].push(sanitizedProblem)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
req.session.errors = errors
|
|
71
|
-
return res.redirect(303, 'back')
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// If not an Inertia request, perform the normal badRequest response
|
|
76
|
-
if (optionalData === undefined) {
|
|
77
|
-
sails.log.info('Ran custom response: res.badRequest()')
|
|
78
|
-
return res.sendStatus(statusCodeToSet)
|
|
79
|
-
} else if (_.isError(optionalData)) {
|
|
80
|
-
sails.log.info(
|
|
81
|
-
'Custom response `res.badRequest()` called with an Error:',
|
|
82
|
-
optionalData
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
if (!_.isFunction(optionalData.toJSON)) {
|
|
86
|
-
if (process.env.NODE_ENV === 'production') {
|
|
87
|
-
return res.sendStatus(statusCodeToSet)
|
|
88
|
-
} else {
|
|
89
|
-
return res.status(statusCodeToSet).send(optionalData.stack)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
return res.status(statusCodeToSet).send(optionalData)
|
|
94
|
-
}
|
|
3
|
+
return this.req._sails.inertia.handleBadRequest(
|
|
4
|
+
this.req,
|
|
5
|
+
this.res,
|
|
6
|
+
optionalData
|
|
7
|
+
)
|
|
95
8
|
}
|
|
@@ -1,72 +1,4 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
|
-
const { encode } = require('querystring')
|
|
3
2
|
module.exports = function inertia(data) {
|
|
4
|
-
|
|
5
|
-
const res = this.res
|
|
6
|
-
const sails = req._sails
|
|
7
|
-
|
|
8
|
-
const sharedProps = sails.inertia.sharedProps
|
|
9
|
-
const sharedViewData = sails.inertia.sharedViewData
|
|
10
|
-
const rootView = sails.config.inertia.rootView
|
|
11
|
-
|
|
12
|
-
const allProps = {
|
|
13
|
-
...sharedProps,
|
|
14
|
-
...data.props
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const allViewData = {
|
|
18
|
-
...sharedViewData,
|
|
19
|
-
...data.viewData
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let url = req.url || req.originalUrl
|
|
23
|
-
const assetVersion = sails.config.inertia.version
|
|
24
|
-
const currentVersion =
|
|
25
|
-
typeof assetVersion === 'function' ? assetVersion() : assetVersion
|
|
26
|
-
|
|
27
|
-
const page = {
|
|
28
|
-
component: data.page,
|
|
29
|
-
version: currentVersion,
|
|
30
|
-
props: allProps,
|
|
31
|
-
url
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Implements inertia partial reload. See https://inertiajs.com/partial-reload
|
|
35
|
-
if (
|
|
36
|
-
req.get(inertiaHeaders.PARTIAL_DATA) &&
|
|
37
|
-
req.get(inertiaHeaders.PARTIAL_COMPONENT) === page.component
|
|
38
|
-
) {
|
|
39
|
-
const only = req.get(inertiaHeaders.PARTIAL_DATA).split(',')
|
|
40
|
-
page.props = only.length ? getPartialData(data.props, only) : page.props
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const queryParams = req.query
|
|
44
|
-
if (req.method === 'GET' && Object.keys(queryParams).length) {
|
|
45
|
-
// Keep original request query params
|
|
46
|
-
url += `?${encode(queryParams)}`
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (req.get(inertiaHeaders.INERTIA)) {
|
|
50
|
-
res.set(inertiaHeaders.INERTIA, true)
|
|
51
|
-
res.set('Vary', 'Accept')
|
|
52
|
-
return res.json(page)
|
|
53
|
-
} else {
|
|
54
|
-
// Implements full page reload
|
|
55
|
-
return res.view(rootView, {
|
|
56
|
-
page,
|
|
57
|
-
viewData: allViewData
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function getPartialData(props, only = []) {
|
|
63
|
-
return Object.assign({}, ...only.map((key) => ({ [key]: props[key] })))
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const inertiaHeaders = {
|
|
67
|
-
INERTIA: 'X-Inertia',
|
|
68
|
-
VERSION: 'X-Inertia-Version',
|
|
69
|
-
PARTIAL_DATA: 'X-Inertia-Partial-Data',
|
|
70
|
-
PARTIAL_COMPONENT: 'X-Inertia-Partial-Component',
|
|
71
|
-
LOCATION: 'X-Inertia-Location'
|
|
3
|
+
return this.req._sails.inertia.render(this.req, this.res, data)
|
|
72
4
|
}
|
|
@@ -1,20 +1,4 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
|
-
|
|
3
|
-
const inertiaHeaders = {
|
|
4
|
-
INERTIA: 'X-Inertia',
|
|
5
|
-
LOCATION: 'X-Inertia-Location'
|
|
6
|
-
}
|
|
7
|
-
|
|
8
2
|
module.exports = function inertiaRedirect(url) {
|
|
9
|
-
|
|
10
|
-
const res = this.res
|
|
11
|
-
|
|
12
|
-
if (req.get(inertiaHeaders.INERTIA)) {
|
|
13
|
-
res.set(inertiaHeaders.LOCATION, url)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return res.redirect(
|
|
17
|
-
['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409,
|
|
18
|
-
url
|
|
19
|
-
)
|
|
3
|
+
return this.req._sails.inertia.location(this.req, this.res, url)
|
|
20
4
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-sails-generator",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Sails generator for The Boring JavaScript Stack.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test"
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"sails",
|
|
11
11
|
"senerator"
|
|
12
12
|
],
|
|
13
|
-
"author": "Kelvin Omereshone<kelvin@sailscasts.com>",
|
|
13
|
+
"author": "Kelvin Omereshone <kelvin@sailscasts.com>",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"sails": ">=1"
|
|
@@ -18,5 +18,15 @@
|
|
|
18
18
|
"bugs": {
|
|
19
19
|
"url": "https://github.com/sailscastshq/boring-stack/issues"
|
|
20
20
|
},
|
|
21
|
-
"homepage": "https://github.com/sailscastshq/boring-stack/create-sails-generator#readme"
|
|
21
|
+
"homepage": "https://github.com/sailscastshq/boring-stack/tree/main/create-sails-generator#readme",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/sailscastshq/boring-stack.git",
|
|
25
|
+
"directory": "packages/create-sails-generator"
|
|
26
|
+
},
|
|
27
|
+
"main": "index.js",
|
|
28
|
+
"directories": {
|
|
29
|
+
"test": "tests"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {}
|
|
22
32
|
}
|