inertia-sails 0.0.9 → 0.1.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 +66 -81
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -19,13 +19,76 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
19
19
|
let sharedProps = {}
|
|
20
20
|
let sharedViewData = {}
|
|
21
21
|
let rootView = 'app'
|
|
22
|
+
|
|
23
|
+
const inertiaMiddleware = (req, res, next) => {
|
|
24
|
+
hook.render = function (component, props = {}, viewData = {}) {
|
|
25
|
+
const allProps = {
|
|
26
|
+
...sharedProps,
|
|
27
|
+
...props,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const allViewData = {
|
|
31
|
+
...sharedViewData,
|
|
32
|
+
...viewData,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let url = req.url || req.originalUrl
|
|
36
|
+
const assetVersion = sails.config.inertia.version
|
|
37
|
+
const currentVersion =
|
|
38
|
+
typeof assetVersion == 'function' ? assetVersion() : assetVersion
|
|
39
|
+
|
|
40
|
+
const page = {
|
|
41
|
+
component,
|
|
42
|
+
version: currentVersion,
|
|
43
|
+
props: allProps,
|
|
44
|
+
url,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Implements inertia partial reload. See https://inertiajs.com/partial-reload
|
|
48
|
+
if (req.get(PARTIAL_DATA) && req.get(PARTIAL_COMPONENT) === component) {
|
|
49
|
+
const only = req.get(PARTIAL_DATA).split(',')
|
|
50
|
+
page.props = only.length ? getPartialData(props, only) : page.props
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const queryParams = req.query
|
|
54
|
+
if (req.method == 'GET' && Object.keys(queryParams).length) {
|
|
55
|
+
// Keep original request query params
|
|
56
|
+
url += `?${encode(queryParams)}`
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Implements inertia requests
|
|
60
|
+
if (isInertiaRequest(req)) {
|
|
61
|
+
return res.status(200).json(page)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Implements full page reload
|
|
65
|
+
return sails.hooks.views.render(rootView, {
|
|
66
|
+
page,
|
|
67
|
+
viewData: allViewData,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
hook.location = function (url = req.headers['referer']) {
|
|
71
|
+
const statusCode = ['PUT', 'PATCH', 'DELETE'].includes(req.method)
|
|
72
|
+
? 303
|
|
73
|
+
: 409
|
|
74
|
+
res.status(statusCode)
|
|
75
|
+
res.set('X-Inertia-Location', url)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Set Inertia headers
|
|
79
|
+
if (isInertiaRequest(req)) {
|
|
80
|
+
res.set(INERTIA, true)
|
|
81
|
+
res.set('Vary', 'Accept')
|
|
82
|
+
}
|
|
83
|
+
return next()
|
|
84
|
+
}
|
|
22
85
|
return {
|
|
23
86
|
defaults: {
|
|
24
87
|
inertia: {
|
|
25
88
|
version: 1,
|
|
26
89
|
},
|
|
27
90
|
},
|
|
28
|
-
initialize: async function (
|
|
91
|
+
initialize: async function () {
|
|
29
92
|
hook = this
|
|
30
93
|
sails.inertia = hook
|
|
31
94
|
|
|
@@ -36,7 +99,8 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
36
99
|
|
|
37
100
|
hook.share('errors', {})
|
|
38
101
|
|
|
39
|
-
|
|
102
|
+
// Register Inertia middleware
|
|
103
|
+
sails.registerActionMiddleware(inertiaMiddleware, '*')
|
|
40
104
|
},
|
|
41
105
|
|
|
42
106
|
share: (key, value = null) => (sharedProps[key] = value),
|
|
@@ -54,84 +118,5 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
54
118
|
setRootView: (newRootView) => (rootView = newRootView),
|
|
55
119
|
|
|
56
120
|
getRootView: () => rootView,
|
|
57
|
-
|
|
58
|
-
routes: {
|
|
59
|
-
before: {
|
|
60
|
-
'GET /*': {
|
|
61
|
-
skipAssets: true,
|
|
62
|
-
fn: function (req, res, next) {
|
|
63
|
-
hook.render = function (component, props = {}, viewData = {}) {
|
|
64
|
-
const allProps = {
|
|
65
|
-
...sharedProps,
|
|
66
|
-
...props,
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const allViewData = {
|
|
70
|
-
...sharedViewData,
|
|
71
|
-
...viewData,
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let url = req.url || req.originalUrl
|
|
75
|
-
const assetVersion = sails.config.inertia.version
|
|
76
|
-
const currentVersion =
|
|
77
|
-
typeof assetVersion == 'function'
|
|
78
|
-
? assetVersion()
|
|
79
|
-
: assetVersion
|
|
80
|
-
|
|
81
|
-
const page = {
|
|
82
|
-
component,
|
|
83
|
-
version: currentVersion,
|
|
84
|
-
props: {},
|
|
85
|
-
url,
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Implements inertia partial reload. See https://inertiajs.com/partial-reload
|
|
89
|
-
let dataKeys
|
|
90
|
-
if (
|
|
91
|
-
req.get(PARTIAL_DATA) &&
|
|
92
|
-
req.get(PARTIAL_COMPONENT) === component
|
|
93
|
-
) {
|
|
94
|
-
dataKeys = req.get(PARTIAL_DATA).split(',')
|
|
95
|
-
} else {
|
|
96
|
-
dataKeys = Object.keys(allProps)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
page.props = getPageProps(allProps, dataKeys)
|
|
100
|
-
|
|
101
|
-
const queryParams = req.query
|
|
102
|
-
if (req.method == 'GET' && Object.keys(queryParams).length) {
|
|
103
|
-
// Keep original request query params
|
|
104
|
-
url += `?${encode(queryParams)}`
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Implements inertia requests
|
|
108
|
-
if (isInertiaRequest(req)) {
|
|
109
|
-
return res.status(200).json(page)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Implements full page reload
|
|
113
|
-
return sails.hooks.views.render(rootView, {
|
|
114
|
-
page,
|
|
115
|
-
viewData: allViewData,
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
hook.location = function (url = req.headers['referer']) {
|
|
119
|
-
const statusCode = ['PUT', 'PATCH', 'DELETE'].includes(req.method)
|
|
120
|
-
? 303
|
|
121
|
-
: 409
|
|
122
|
-
res.status(statusCode)
|
|
123
|
-
res.set('X-Inertia-Location', url)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Set Inertia headers
|
|
127
|
-
if (isInertiaRequest(req)) {
|
|
128
|
-
res.set(INERTIA, true)
|
|
129
|
-
res.set('Vary', 'Accept')
|
|
130
|
-
}
|
|
131
|
-
return next()
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
121
|
}
|
|
137
122
|
}
|