inertia-sails 0.1.9 → 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 +1 -1
- package/private/inertia-middleware.js +5 -85
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,100 +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
6
|
if (isInertiaRequest(req)) {
|
|
15
|
-
/**
|
|
16
|
-
* Flash messages stored in the session.
|
|
17
|
-
* @typedef {Object} FlashMessages
|
|
18
|
-
* @property {Array} message - Flash message(s).
|
|
19
|
-
* @property {Array} error - Error message(s).
|
|
20
|
-
* @property {Array} success - Success message(s).
|
|
21
|
-
*/
|
|
22
7
|
const flash = {
|
|
23
8
|
message: req.flash('message'),
|
|
24
9
|
error: req.flash('error'),
|
|
25
10
|
success: req.flash('success')
|
|
26
11
|
}
|
|
27
|
-
hook.share('flash', flash)
|
|
28
|
-
/**
|
|
29
|
-
* Validation errors stored in the session, resolved and formatted for Inertia.js.
|
|
30
|
-
* @type {Object}
|
|
31
|
-
*/
|
|
32
|
-
const validationErrors = resolveValidationErrors(req)
|
|
33
|
-
req.flash('errors', validationErrors) // Flash the validation error so we can share it later
|
|
34
|
-
|
|
35
|
-
hook.share('errors', req.flash('errors')[0] || {}) // Share validation errors as props
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
hook.render = function (component, props = {}, viewData = {}) {
|
|
39
|
-
const allProps = {
|
|
40
|
-
...sharedProps,
|
|
41
|
-
...props
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const allViewData = {
|
|
45
|
-
...sharedViewData,
|
|
46
|
-
...viewData
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let url = req.url || req.originalUrl
|
|
50
|
-
const assetVersion = sails.config.inertia.version
|
|
51
|
-
const currentVersion =
|
|
52
|
-
typeof assetVersion == 'function' ? assetVersion() : assetVersion
|
|
53
|
-
|
|
54
|
-
const page = {
|
|
55
|
-
component,
|
|
56
|
-
version: currentVersion,
|
|
57
|
-
props: allProps,
|
|
58
|
-
url
|
|
59
|
-
}
|
|
12
|
+
hook.share('flash', flash)
|
|
60
13
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const only = req.get(PARTIAL_DATA).split(',')
|
|
64
|
-
page.props = only.length ? getPartialData(props, only) : page.props
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const queryParams = req.query
|
|
68
|
-
if (req.method == 'GET' && Object.keys(queryParams).length) {
|
|
69
|
-
// Keep original request query params
|
|
70
|
-
url += `?${encode(queryParams)}`
|
|
71
|
-
}
|
|
14
|
+
const validationErrors = resolveValidationErrors(req)
|
|
15
|
+
req.flash('errors', validationErrors)
|
|
72
16
|
|
|
73
|
-
|
|
74
|
-
res.set(INERTIA, true)
|
|
75
|
-
res.set('Vary', 'Accept')
|
|
76
|
-
return res.json(page)
|
|
77
|
-
} else {
|
|
78
|
-
// Implements full page reload
|
|
79
|
-
return sails.hooks.views.render(rootView, {
|
|
80
|
-
page,
|
|
81
|
-
viewData: allViewData
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Handle 303 and external redirects
|
|
87
|
-
* see https://inertiajs.com/redirects#303-response-code
|
|
88
|
-
* @param {string} url - The URL to redirect to.
|
|
89
|
-
*/
|
|
90
|
-
hook.location = function (url) {
|
|
91
|
-
if (isInertiaRequest(req)) {
|
|
92
|
-
res.set('X-Inertia-Location', url)
|
|
93
|
-
}
|
|
94
|
-
return res.redirect(
|
|
95
|
-
['PUT', 'PATCH', 'DELETE'].includes(req.method) ? 303 : 409,
|
|
96
|
-
url
|
|
97
|
-
)
|
|
17
|
+
hook.share('errors', req.flash('errors')[0] || {})
|
|
98
18
|
}
|
|
99
19
|
|
|
100
20
|
return next()
|