inertia-sails 0.1.8 → 0.2.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.
- package/index.js +13 -17
- package/inertia-sails-0.1.9.tgz +0 -0
- package/package.json +2 -2
- package/private/inertia-middleware.js +10 -88
- package/inertia-sails-0.1.7.tgz +0 -0
package/index.js
CHANGED
|
@@ -7,9 +7,6 @@
|
|
|
7
7
|
const inertia = require('./private/inertia-middleware')
|
|
8
8
|
module.exports = function defineInertiaHook(sails) {
|
|
9
9
|
let hook
|
|
10
|
-
let sharedProps = {}
|
|
11
|
-
let sharedViewData = {}
|
|
12
|
-
let rootView = 'app'
|
|
13
10
|
const routesToBindInertiaTo = [
|
|
14
11
|
'GET r|^((?![^?]*\\/[^?\\/]+\\.[^?\\/]+(\\?.*)?).)*$|',
|
|
15
12
|
// (^^Leave out assets)
|
|
@@ -22,37 +19,36 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
22
19
|
return {
|
|
23
20
|
defaults: {
|
|
24
21
|
inertia: {
|
|
22
|
+
rootView: 'app',
|
|
25
23
|
version: 1
|
|
26
24
|
}
|
|
27
25
|
},
|
|
28
26
|
initialize: async function () {
|
|
29
27
|
hook = this
|
|
30
28
|
sails.inertia = hook
|
|
31
|
-
|
|
29
|
+
sails.inertia.sharedProps = {}
|
|
30
|
+
sails.inertia.sharedViewData = {}
|
|
32
31
|
sails.on('router:before', function routerBefore() {
|
|
33
32
|
routesToBindInertiaTo.forEach(function iterator(routeAddress) {
|
|
34
|
-
sails.router.bind(
|
|
35
|
-
routeAddress,
|
|
36
|
-
inertia(sails, { hook, sharedProps, sharedViewData, rootView })
|
|
37
|
-
)
|
|
33
|
+
sails.router.bind(routeAddress, inertia(hook))
|
|
38
34
|
})
|
|
39
35
|
})
|
|
40
36
|
},
|
|
41
37
|
|
|
42
|
-
share: (key, value = null) => (sharedProps[key] = value),
|
|
38
|
+
share: (key, value = null) => (sails.inertia.sharedProps[key] = value),
|
|
43
39
|
|
|
44
|
-
getShared: (key = null) =>
|
|
40
|
+
getShared: (key = null) =>
|
|
41
|
+
sails.inertia.sharedProps[key] ?? sails.inertia.sharedProps,
|
|
45
42
|
|
|
46
43
|
flushShared: (key) => {
|
|
47
|
-
key
|
|
44
|
+
key
|
|
45
|
+
? delete sails.inertia.sharedProps[key]
|
|
46
|
+
: (sails.inertia.sharedProps = {})
|
|
48
47
|
},
|
|
49
48
|
|
|
50
|
-
viewData: (key, value) => (sharedViewData[key] = value),
|
|
51
|
-
|
|
52
|
-
getViewData: (key) => sharedViewData[key] ?? sharedViewData,
|
|
53
|
-
|
|
54
|
-
setRootView: (newRootView) => (rootView = newRootView),
|
|
49
|
+
viewData: (key, value) => (sails.inertia.sharedViewData[key] = value),
|
|
55
50
|
|
|
56
|
-
|
|
51
|
+
getViewData: (key) =>
|
|
52
|
+
sails.inertia.sharedViewData[key] ?? sails.inertia.sharedViewData
|
|
57
53
|
}
|
|
58
54
|
}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inertia-sails",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "The Sails adapter for Inertia.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"sails": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "git+https://github.com/sailscastshq/boring-stack.git",
|
|
17
|
-
"directory": "
|
|
17
|
+
"directory": "inertia-sails"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"sails": ">=1",
|
|
@@ -1,98 +1,20 @@
|
|
|
1
|
-
const { encode } = require('querystring')
|
|
2
1
|
const isInertiaRequest = require('./is-inertia-request')
|
|
3
2
|
|
|
4
|
-
const {
|
|
5
|
-
INERTIA,
|
|
6
|
-
PARTIAL_DATA,
|
|
7
|
-
PARTIAL_COMPONENT
|
|
8
|
-
} = require('./inertia-headers')
|
|
9
|
-
|
|
10
|
-
const getPartialData = require('./get-partial-data')
|
|
11
3
|
const resolveValidationErrors = require('./resolve-validation-errors')
|
|
12
|
-
function inertia(
|
|
4
|
+
function inertia(hook) {
|
|
13
5
|
return function inertiaMiddleware(req, res, next) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* @property {Array} success - Success message(s).
|
|
20
|
-
*/
|
|
21
|
-
const flash = {
|
|
22
|
-
message: req.flash('message'),
|
|
23
|
-
error: req.flash('error'),
|
|
24
|
-
success: req.flash('success')
|
|
25
|
-
}
|
|
26
|
-
hook.share('flash', flash) // Share the flash object as props
|
|
27
|
-
/**
|
|
28
|
-
* Validation errors stored in the session, resolved and formatted for Inertia.js.
|
|
29
|
-
* @type {Object}
|
|
30
|
-
*/
|
|
31
|
-
const validationErrors = resolveValidationErrors(req)
|
|
32
|
-
req.flash('errors', validationErrors) // Flash the validation error so we can share it later
|
|
33
|
-
|
|
34
|
-
hook.share('errors', req.flash('errors')[0] || {}) // Share validation errors as props
|
|
35
|
-
|
|
36
|
-
hook.render = function (component, props = {}, viewData = {}) {
|
|
37
|
-
const allProps = {
|
|
38
|
-
...sharedProps,
|
|
39
|
-
...props
|
|
6
|
+
if (isInertiaRequest(req)) {
|
|
7
|
+
const flash = {
|
|
8
|
+
message: req.flash('message'),
|
|
9
|
+
error: req.flash('error'),
|
|
10
|
+
success: req.flash('success')
|
|
40
11
|
}
|
|
12
|
+
hook.share('flash', flash)
|
|
41
13
|
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
...viewData
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let url = req.url || req.originalUrl
|
|
48
|
-
const assetVersion = sails.config.inertia.version
|
|
49
|
-
const currentVersion =
|
|
50
|
-
typeof assetVersion == 'function' ? assetVersion() : assetVersion
|
|
51
|
-
|
|
52
|
-
const page = {
|
|
53
|
-
component,
|
|
54
|
-
version: currentVersion,
|
|
55
|
-
props: allProps,
|
|
56
|
-
url
|
|
57
|
-
}
|
|
14
|
+
const validationErrors = resolveValidationErrors(req)
|
|
15
|
+
req.flash('errors', validationErrors)
|
|
58
16
|
|
|
59
|
-
|
|
60
|
-
if (req.get(PARTIAL_DATA) && req.get(PARTIAL_COMPONENT) === component) {
|
|
61
|
-
const only = req.get(PARTIAL_DATA).split(',')
|
|
62
|
-
page.props = only.length ? getPartialData(props, only) : page.props
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const queryParams = req.query
|
|
66
|
-
if (req.method == 'GET' && Object.keys(queryParams).length) {
|
|
67
|
-
// Keep original request query params
|
|
68
|
-
url += `?${encode(queryParams)}`
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (isInertiaRequest(req)) {
|
|
72
|
-
res.set(INERTIA, true)
|
|
73
|
-
res.set('Vary', 'Accept')
|
|
74
|
-
return res.json(page)
|
|
75
|
-
} else {
|
|
76
|
-
// Implements full page reload
|
|
77
|
-
return sails.hooks.views.render(rootView, {
|
|
78
|
-
page,
|
|
79
|
-
viewData: allViewData
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Handle 303 and external redirects
|
|
85
|
-
* see https://inertiajs.com/redirects#303-response-code
|
|
86
|
-
* @param {string} url - The URL to redirect to.
|
|
87
|
-
*/
|
|
88
|
-
hook.location = function (url) {
|
|
89
|
-
if (isInertiaRequest(req)) {
|
|
90
|
-
res.set('X-Inertia-Location', url)
|
|
91
|
-
}
|
|
92
|
-
return res.redirect(
|
|
93
|
-
['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409,
|
|
94
|
-
url
|
|
95
|
-
)
|
|
17
|
+
hook.share('errors', req.flash('errors')[0] || {})
|
|
96
18
|
}
|
|
97
19
|
|
|
98
20
|
return next()
|
package/inertia-sails-0.1.7.tgz
DELETED
|
Binary file
|