Skip to content

Commit

Permalink
feat: add support for view props
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Nov 21, 2023
1 parent 1171fbe commit aa411d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ export class Inertia {
/**
* Render a page using Inertia
*/
async render<TPageProps extends Record<string, any>>(component: string, pageProps?: TPageProps) {
async render<
TPageProps extends Record<string, any> = PageProps,
TViewProps extends Record<string, any> = PageProps,
>(component: string, pageProps?: TPageProps, viewProps?: TViewProps) {
const pageObject = await this.#buildPageObject(component, pageProps)
const isInertiaRequest = !!this.ctx.request.header('x-inertia')

if (!isInertiaRequest) {
return this.ctx.view.render(this.config.rootView, { page: pageObject })
return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject })
}

return pageObject
Expand Down
14 changes: 14 additions & 0 deletions tests/inertia.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { test } from '@japa/runner'
import { HttpContext } from '@adonisjs/core/http'
import { HttpContextFactory, RequestFactory } from '@adonisjs/core/factories/http'

import { setupViewMacroMock } from '../tests_helpers/index.js'
Expand Down Expand Up @@ -182,4 +183,17 @@ test.group('Inertia', () => {

assert.deepEqual(result.url, '/foo?bar=baz&test[]=32&12&bla=42')
})

test('view props are passed to the root view', async ({ assert }) => {
// @ts-expect-error mock
HttpContext.getter('view', () => ({ render: (view: any, props: any) => ({ view, props }) }))

const inertia = await new InertiaFactory().create()
const result: any = await inertia.render('foo', { data: 42 }, { metaTitle: 'foo' })

assert.deepEqual(result.props.metaTitle, 'foo')

// @ts-expect-error mock
delete HttpContext.prototype.view
})
})
27 changes: 22 additions & 5 deletions tests/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,38 @@ test.group('Types', () => {
test('assign interface to render', async () => {
const inertia = await new InertiaFactory().create()

interface Response {
interface MyRouteResponse {
foo: string
}

inertia.render<Response>('foo', null as any).catch(() => {})
inertia.render<MyRouteResponse>('foo', null as any).catch(() => {})
})

test('ts error if props doesnt match generic', async () => {
test('no ts error if generic is not passed', async () => {
const inertia = await new InertiaFactory().create()

interface Response {
inertia.render('foo', { foo: 1 }).catch(() => {})
})

test('ts error if page props doesnt match generic', async () => {
const inertia = await new InertiaFactory().create()

interface MyRouteResponse {
foo: string
}

// @ts-expect-error props doesn't match generic
inertia.render<Response>('foo', { foo: 1 }).catch(() => {})
inertia.render<MyRouteResponse>('foo', { foo: 1 }).catch(() => {})
})

test('ts error if view props doesnt match generic', async () => {
const inertia = await new InertiaFactory().create()

interface MyViewProps {
metaTitle: string
}

// @ts-expect-error props doesn't match generic
inertia.render<any, MyViewProps>('foo', { foo: 1 }, { foo: 32 }).catch(() => {})
})
})

0 comments on commit aa411d0

Please sign in to comment.