inertia-sails 1.4.0 → 1.4.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/README.md CHANGED
@@ -90,6 +90,11 @@ Return a URL string to redirect:
90
90
  return '/dashboard'
91
91
  ```
92
92
 
93
+ `inertiaRedirect` performs an Inertia location visit. It does not return a
94
+ page object, so `sails.inertia.preserveFragment()` does not apply to this
95
+ response type. If you already know the fragment you want, include it in the
96
+ returned URL.
97
+
93
98
  #### Preserving URL fragments
94
99
 
95
100
  When a standard Inertia redirect should carry the current hash to the next
@@ -222,6 +227,22 @@ return {
222
227
  }
223
228
  ```
224
229
 
230
+ Or pass the rescue option inline:
231
+
232
+ ```js
233
+ return {
234
+ page: 'dashboard',
235
+ props: {
236
+ analytics: sails.inertia.defer(
237
+ async () => {
238
+ return await Analytics.getExpensiveReport()
239
+ },
240
+ { rescue: true }
241
+ )
242
+ }
243
+ }
244
+ ```
245
+
225
246
  When a rescued deferred prop throws, it is omitted from `props` and its key is
226
247
  reported in `rescuedProps`, allowing the client `<Deferred>` component to show
227
248
  its rescue slot instead of failing the whole deferred response.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inertia-sails",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "The Sails adapter for Inertia.",
5
5
  "main": "index.js",
6
6
  "sails": {
@@ -53,6 +53,17 @@ function runWithContext(req, callback) {
53
53
  }
54
54
 
55
55
  describe('preserveFragment', function () {
56
+ it('does not preserve fragments unless explicitly requested', async function () {
57
+ const hook = createHook()
58
+ const req = { session: {} }
59
+
60
+ await runWithContext(req, async () => {
61
+ assert.equal(requestContext.getPreserveFragment(), false)
62
+ assert.equal(hook.consumePreserveFragment(req), false)
63
+ assert.equal(hasOwn(req.session, '_inertiaPreserveFragment'), false)
64
+ })
65
+ })
66
+
56
67
  it('stores preserveFragment in request context and session', async function () {
57
68
  const hook = createHook()
58
69
  /** @type {{ session: Record<string, any> }} */
@@ -66,6 +77,16 @@ describe('preserveFragment', function () {
66
77
  })
67
78
  })
68
79
 
80
+ it('keeps the chainable helper shape when passed a boolean', async function () {
81
+ const hook = createHook()
82
+ /** @type {{ session: Record<string, any> }} */
83
+ const req = { session: {} }
84
+
85
+ await runWithContext(req, async () => {
86
+ assert.equal(hook.preserveFragment(true), hook)
87
+ })
88
+ })
89
+
69
90
  it('consumes and clears session-backed preserveFragment metadata', async function () {
70
91
  const hook = createHook()
71
92
  const req = {
@@ -1,8 +1,22 @@
1
1
  const { describe, it } = require('node:test')
2
2
  const assert = require('node:assert/strict')
3
+ const defineInertiaHook = require('../..')
3
4
  const resolvePageProps = require('../../lib/props/resolve-page-props')
4
5
  const DeferProp = require('../../lib/props/defer-prop')
5
6
 
7
+ function createHook() {
8
+ /** @type {Record<string, any>} */
9
+ const sails = {
10
+ config: {},
11
+ log: {
12
+ warn() {}
13
+ }
14
+ }
15
+ const hook = defineInertiaHook(/** @type {any} */ (sails))
16
+ sails.inertia = hook
17
+ return hook
18
+ }
19
+
6
20
  describe('resolvePageProps', function () {
7
21
  it('keeps the existing resolved props return shape', async function () {
8
22
  const props = await resolvePageProps({
@@ -44,6 +58,21 @@ describe('resolvePageProps', function () {
44
58
  assert.deepEqual(result.rescuedProps, ['permissions'])
45
59
  })
46
60
 
61
+ it('rescues deferred props from the hook options shorthand', async function () {
62
+ const hook = createHook()
63
+ const result = await resolvePageProps.withMetadata({
64
+ analytics: hook.defer(
65
+ async () => {
66
+ throw new Error('Analytics service unavailable')
67
+ },
68
+ { rescue: true }
69
+ )
70
+ })
71
+
72
+ assert.deepEqual(result.props, {})
73
+ assert.deepEqual(result.rescuedProps, ['analytics'])
74
+ })
75
+
47
76
  it('throws deferred prop failures unless rescue is enabled', async function () {
48
77
  await assert.rejects(
49
78
  () =>