inertia-sails 1.0.1 → 1.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 +21 -2
- package/lib/middleware/inertia-middleware.js +14 -24
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -126,6 +126,23 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
126
126
|
})
|
|
127
127
|
},
|
|
128
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Hook routes - sets up AsyncLocalStorage context early so other hooks
|
|
131
|
+
* can use sails.inertia.share() with proper request-scoped context.
|
|
132
|
+
*/
|
|
133
|
+
routes: {
|
|
134
|
+
before: {
|
|
135
|
+
'GET /*': {
|
|
136
|
+
skipAssets: true,
|
|
137
|
+
fn: (req, res, next) => requestContext.run(req, res, next)
|
|
138
|
+
},
|
|
139
|
+
'POST /*': (req, res, next) => requestContext.run(req, res, next),
|
|
140
|
+
'PUT /*': (req, res, next) => requestContext.run(req, res, next),
|
|
141
|
+
'PATCH /*': (req, res, next) => requestContext.run(req, res, next),
|
|
142
|
+
'DELETE /*': (req, res, next) => requestContext.run(req, res, next)
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
|
|
129
146
|
/**
|
|
130
147
|
* Share a property for the current request.
|
|
131
148
|
* Uses AsyncLocalStorage to ensure data doesn't leak between concurrent requests.
|
|
@@ -172,9 +189,11 @@ module.exports = function defineInertiaHook(sails) {
|
|
|
172
189
|
},
|
|
173
190
|
|
|
174
191
|
/**
|
|
175
|
-
* Flush shared properties
|
|
192
|
+
* Flush shared properties for the current request.
|
|
193
|
+
* Since context is set up early in routes.before, this always works
|
|
194
|
+
* in hooks and middleware.
|
|
176
195
|
* @param {string|null} key - The key of the property to flush, or null to flush all
|
|
177
|
-
* @param {boolean} [global=false] - Whether to flush global props
|
|
196
|
+
* @param {boolean} [global=false] - Whether to also flush global props (rarely needed)
|
|
178
197
|
*/
|
|
179
198
|
flushShared(key, global = false) {
|
|
180
199
|
const context = requestContext.getContext()
|
|
@@ -2,39 +2,29 @@ const resolveValidationErrors = require('../helpers/resolve-validation-errors')
|
|
|
2
2
|
const requestContext = require('../helpers/request-context')
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Inertia middleware that handles validation errors
|
|
5
|
+
* Inertia middleware that handles validation errors.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Note: AsyncLocalStorage context is set up earlier in routes.before
|
|
8
|
+
* (see index.js) so that other hooks can use sails.inertia.share()
|
|
9
|
+
* with proper request context.
|
|
9
10
|
*
|
|
10
|
-
* This
|
|
11
|
-
* -
|
|
12
|
-
* - sails.inertia.flash() - Per-request flash messages
|
|
13
|
-
* - sails.inertia.encryptHistory() - Per-request history encryption
|
|
14
|
-
* - sails.inertia.clearHistory() - Per-request history clearing
|
|
11
|
+
* This middleware handles:
|
|
12
|
+
* - Validation errors from redirects (shared as 'errors' prop)
|
|
15
13
|
*
|
|
16
14
|
* @param {Object} hook - The inertia-sails hook instance
|
|
17
15
|
* @returns {Function} Express/Sails middleware function
|
|
18
16
|
*/
|
|
19
17
|
function inertia(hook) {
|
|
20
18
|
return function inertiaMiddleware(req, res, next) {
|
|
21
|
-
// Skip
|
|
22
|
-
if (req.isSocket)
|
|
23
|
-
return next()
|
|
24
|
-
}
|
|
19
|
+
// Skip for WebSocket requests (they don't have req.flash)
|
|
20
|
+
if (req.isSocket) return next()
|
|
25
21
|
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
// Share errors for this request (request-scoped, not global)
|
|
34
|
-
requestContext.setSharedProp('errors', req.flash('errors')[0] || {})
|
|
35
|
-
|
|
36
|
-
return next()
|
|
37
|
-
})
|
|
22
|
+
// Handle validation errors - share them for this request only
|
|
23
|
+
// Context is already set up by routes.before in index.js
|
|
24
|
+
const validationErrors = resolveValidationErrors(req)
|
|
25
|
+
req.flash('errors', validationErrors)
|
|
26
|
+
requestContext.setSharedProp('errors', req.flash('errors')[0] || {})
|
|
27
|
+
return next()
|
|
38
28
|
}
|
|
39
29
|
}
|
|
40
30
|
|