This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* inertia hook
*
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
*/
const inertia = require('./private/inertia-middleware')
module.exports = function defineInertiaHook(sails) {
let hook
let sharedProps = {}
let sharedViewData = {}
let rootView = 'app'
routesToBindInertiaTo = [
'GET r|^((?![^?]*\\/[^?\\/]+\\.[^?\\/]+(\\?.*)?).)*$|',
// (^^Leave out assets)
'POST /*',
'PATCH /*',
'PUT /*',
'DELETE /*',
]
return {
defaults: {
inertia: {
version: 1,
},
},
initialize: async function () {
hook = this
sails.inertia = hook
hook.share('flash', {
success: null,
error: null,
})
hook.share('errors', {})
sails.on('router:before', function routerBefore() {
routesToBindInertiaTo.forEach(function iterator(routeAddress) {
sails.router.bind(
routeAddress,
inertia(sails, { hook, sharedProps, sharedViewData, rootView })
)
})
})
},
share: (key, value = null) => (sharedProps[key] = value),
getShared: (key = null) => sharedProps[key] ?? sharedProps,
flushShared: (key) => {
key ? delete sharedProps[key] : (sharedProps = {})
},
viewData: (key, value) => (sharedViewData[key] = value),
getViewData: (key) => sharedViewData[key] ?? sharedViewData,
setRootView: (newRootView) => (rootView = newRootView),
getRootView: () => rootView,
}
}