Skip to content

Commit

Permalink
feat: add share method
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Jan 16, 2024
1 parent 0479596 commit 7944ab6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <reference types="@adonisjs/core/providers/edge_provider" />

import type { HttpContext } from '@adonisjs/core/http'
import type { MaybePromise, PageProps, ResolvedConfig } from './types.js'
import type { Data, MaybePromise, PageProps, ResolvedConfig, SharedData } from './types.js'

/**
* Symbol used to identify lazy props
Expand All @@ -21,10 +21,14 @@ const kLazySymbol = Symbol('lazy')
* Main class used to interact with Inertia
*/
export class Inertia {
#sharedData: SharedData = {}

constructor(
protected ctx: HttpContext,
protected config: ResolvedConfig
) {}
) {
this.#sharedData = config.sharedData
}

/**
* Check if a value is a lazy prop
Expand Down Expand Up @@ -88,11 +92,19 @@ export class Inertia {
return {
component,
version: this.config.versionCache.getVersion(),
props: await this.#resolvePageProps(component, { ...this.config.sharedData, ...pageProps }),
props: await this.#resolvePageProps(component, { ...this.#sharedData, ...pageProps }),
url: this.ctx.request.url(true),
}
}

/**
* Share data for the current request.
* This data will override any shared data defined in the config.
*/
share(data: Record<string, Data>) {
this.#sharedData = { ...this.#sharedData, ...data }
}

/**
* Render a page using Inertia
*/
Expand Down
37 changes: 37 additions & 0 deletions tests/inertia.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,41 @@ test.group('Inertia', () => {
// @ts-expect-error mock
delete HttpContext.prototype.view
})

test('share data for the current request', async ({ assert }) => {
const inertia = await new InertiaFactory().withXInertiaHeader().create()

inertia.share({ foo: 'bar' })

const result: any = await inertia.render('foo')

assert.deepEqual(result.props, { foo: 'bar' })
})

test('share() data are scoped to current instance', async ({ assert }) => {
const inertia = await new InertiaFactory().withXInertiaHeader().create()
const inertia2 = await new InertiaFactory().withXInertiaHeader().create()

inertia.share({ foo: 'bar' })
inertia2.share({ foo: 'baz' })

const result: any = await inertia.render('foo')
const result2: any = await inertia2.render('foo')

assert.deepEqual(result.props, { foo: 'bar' })
assert.deepEqual(result2.props, { foo: 'baz' })
})

test('share() data override the global shared data', async ({ assert }) => {
const inertia = await new InertiaFactory()
.merge({ config: { sharedData: { foo: 'bar' } } })
.withXInertiaHeader()
.create()

inertia.share({ foo: 'baz' })

const result: any = await inertia.render('foo')

assert.deepEqual(result.props, { foo: 'baz' })
})
})

0 comments on commit 7944ab6

Please sign in to comment.