inertia-sails 0.1.7 → 0.1.8

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inertia-sails",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "The Sails adapter for Inertia.",
5
5
  "main": "index.js",
6
6
  "sails": {
@@ -13,10 +13,12 @@
13
13
  },
14
14
  "repository": {
15
15
  "type": "git",
16
- "url": "git+https://github.com/sailscastshq/inertia-sails.git"
16
+ "url": "git+https://github.com/sailscastshq/boring-stack.git",
17
+ "directory": "packages/inertia-sails"
17
18
  },
18
19
  "peerDependencies": {
19
- "sails": ">=1"
20
+ "sails": ">=1",
21
+ "sails-flash": ">=0.0.1"
20
22
  },
21
23
  "keywords": [
22
24
  "Inertia",
@@ -11,7 +11,27 @@ const getPartialData = require('./get-partial-data')
11
11
  const resolveValidationErrors = require('./resolve-validation-errors')
12
12
  function inertia(sails, { hook, sharedProps, sharedViewData, rootView }) {
13
13
  return function inertiaMiddleware(req, res, next) {
14
- hook.share('errors', resolveValidationErrors(req))
14
+ /**
15
+ * Flash messages stored in the session.
16
+ * @typedef {Object} FlashMessages
17
+ * @property {Array} message - Flash message(s).
18
+ * @property {Array} error - Error message(s).
19
+ * @property {Array} success - Success message(s).
20
+ */
21
+ const flash = {
22
+ message: req.flash('message'),
23
+ error: req.flash('error'),
24
+ success: req.flash('success')
25
+ }
26
+ hook.share('flash', flash) // Share the flash object as props
27
+ /**
28
+ * Validation errors stored in the session, resolved and formatted for Inertia.js.
29
+ * @type {Object}
30
+ */
31
+ const validationErrors = resolveValidationErrors(req)
32
+ req.flash('errors', validationErrors) // Flash the validation error so we can share it later
33
+
34
+ hook.share('errors', req.flash('errors')[0] || {}) // Share validation errors as props
15
35
 
16
36
  hook.render = function (component, props = {}, viewData = {}) {
17
37
  const allProps = {
@@ -49,14 +69,16 @@ function inertia(sails, { hook, sharedProps, sharedViewData, rootView }) {
49
69
  }
50
70
 
51
71
  if (isInertiaRequest(req)) {
72
+ res.set(INERTIA, true)
73
+ res.set('Vary', 'Accept')
52
74
  return res.json(page)
75
+ } else {
76
+ // Implements full page reload
77
+ return sails.hooks.views.render(rootView, {
78
+ page,
79
+ viewData: allViewData
80
+ })
53
81
  }
54
-
55
- // Implements full page reload
56
- return sails.hooks.views.render(rootView, {
57
- page,
58
- viewData: allViewData
59
- })
60
82
  }
61
83
  /**
62
84
  * Handle 303 and external redirects
@@ -73,11 +95,6 @@ function inertia(sails, { hook, sharedProps, sharedViewData, rootView }) {
73
95
  )
74
96
  }
75
97
 
76
- if (isInertiaRequest(req)) {
77
- res.set(INERTIA, true)
78
- res.set('Vary', 'Accept')
79
- }
80
-
81
98
  return next()
82
99
  }
83
100
  }
@@ -35,6 +35,6 @@ module.exports = function resolveValidationErrors(req) {
35
35
  const defaultErrors = { default: collectedErrors.default }
36
36
  return defaultErrors
37
37
  }
38
-
38
+ delete req.session.errors
39
39
  return collectedErrors
40
40
  }
Binary file