inertia-sails 0.0.0 → 0.0.1
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 +103 -0
- package/package.json +12 -5
- package/private/get-partial-data.js +3 -0
- package/private/inertia-headers.js +7 -0
- package/private/is-inertia-request.js +4 -0
- package/README.md +0 -3
package/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* inertia hook
|
|
3
|
+
*
|
|
4
|
+
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
|
|
5
|
+
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const { encode } = require('querystring')
|
|
9
|
+
const isInertiaRequest = require('./private/is-inertia-request')
|
|
10
|
+
const { INERTIA, PARTIAL_DATA, PARTIAL_COMPONENT } = require('./private/inertia-headers')
|
|
11
|
+
const getPartialData = require('./private/get-partial-data')
|
|
12
|
+
|
|
13
|
+
module.exports = function defineInertiaHook(sails) {
|
|
14
|
+
var hook
|
|
15
|
+
const sharedProps = {}
|
|
16
|
+
const sharedViewData = {}
|
|
17
|
+
let version = 1
|
|
18
|
+
let rootView = 'app'
|
|
19
|
+
return {
|
|
20
|
+
share(key, value = null) {
|
|
21
|
+
sharedProps[key] = value
|
|
22
|
+
},
|
|
23
|
+
getShared(key = null) {
|
|
24
|
+
return sharedProps[key] ?? sharedProps
|
|
25
|
+
},
|
|
26
|
+
viewData(key, value) {
|
|
27
|
+
sharedViewData[key] = value
|
|
28
|
+
},
|
|
29
|
+
getViewData(key) {
|
|
30
|
+
return sharedViewData[key] ?? sharedViewData
|
|
31
|
+
},
|
|
32
|
+
version(newVersion) {
|
|
33
|
+
version = newVersion
|
|
34
|
+
},
|
|
35
|
+
setRootView(newRootView) {
|
|
36
|
+
rootView = newRootView
|
|
37
|
+
},
|
|
38
|
+
getRootView() {
|
|
39
|
+
return rootView
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
initialize: async function(cb) {
|
|
43
|
+
hook = this
|
|
44
|
+
return cb()
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
routes: {
|
|
48
|
+
before: {
|
|
49
|
+
'GET /*': function(req, res, next) {
|
|
50
|
+
hook.render = async function(component, props = {}, viewData = {}) {
|
|
51
|
+
|
|
52
|
+
const allProps = {
|
|
53
|
+
...sharedProps,
|
|
54
|
+
...props
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const allViewData = {
|
|
58
|
+
...sharedViewData,
|
|
59
|
+
...viewData
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const url = req.url || req.originalUrl
|
|
63
|
+
const currentVersion = version
|
|
64
|
+
|
|
65
|
+
const page = {
|
|
66
|
+
component,
|
|
67
|
+
version: currentVersion,
|
|
68
|
+
props: allProps,
|
|
69
|
+
url,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Implements inertia partial reload. See https://inertiajs.com/partial-reload
|
|
73
|
+
if (req.get(PARTIAL_DATA) && req.get(PARTIAL_COMPONENT) === component) {
|
|
74
|
+
const only = req.get(PARTIAL_DATA).split(",")
|
|
75
|
+
page.props = only.length ? getPartialData(props, only) : page.props
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const queryParams = req.query;
|
|
79
|
+
if (req.method == 'GET' && Object.keys(queryParams).length) {
|
|
80
|
+
// Keep original request query params
|
|
81
|
+
url += `?${encode(queryParams)}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Implements inertia requests
|
|
85
|
+
if (isInertiaRequest(req)) {
|
|
86
|
+
return res.status(200).json(page)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Implements full page reload
|
|
90
|
+
return sails.hooks.views.render(rootView, { page, viewData: allViewData });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Set Inertia headers
|
|
94
|
+
if (isInertiaRequest(req)) {
|
|
95
|
+
res.set(INERTIA, true)
|
|
96
|
+
res.set('Vary', 'Accept')
|
|
97
|
+
}
|
|
98
|
+
return next()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inertia-sails",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "The Sails adapter for Inertia.
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "The Sails adapter for Inertia.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"sails": {
|
|
7
|
+
"isHook": true,
|
|
8
|
+
"hookName": "inertia"
|
|
9
|
+
},
|
|
6
10
|
"scripts": {
|
|
7
11
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
12
|
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/
|
|
15
|
+
"url": "git+https://github.com/sailscasts/inertia-sails.git"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"sails": ">=1"
|
|
12
19
|
},
|
|
13
20
|
"keywords": [
|
|
14
21
|
"Inertia",
|
|
@@ -17,7 +24,7 @@
|
|
|
17
24
|
"author": "Kelvin Omereshone Oghenerhoro <kelvin@sailscasts.com>",
|
|
18
25
|
"license": "MIT",
|
|
19
26
|
"bugs": {
|
|
20
|
-
"url": "https://github.com/
|
|
27
|
+
"url": "https://github.com/sailscasts/inertia-sails/issues"
|
|
21
28
|
},
|
|
22
|
-
"homepage": "https://github.com/
|
|
29
|
+
"homepage": "https://github.com/sailscasts/inertia-sails#readme"
|
|
23
30
|
}
|
package/README.md
DELETED