Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.8.x] [inertia-helpers] add support for fallback page component in resolvePageComponent #252

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/inertia-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
export async function resolvePageComponent<T>(path: string, pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
const page = pages[path]
export async function resolvePageComponent<T>(path: string, pages: Record<string, Promise<T> | (() => Promise<T>)>, fallback?: string|((path: string) => Promise<T>)): Promise<T> {
let page = pages[path]

if (typeof page === 'undefined') {
throw new Error(`Page not found: ${path}`)
if (typeof page === 'undefined' && typeof fallback === 'string') {
page = pages[fallback]
}

return typeof page === 'function' ? page() : page
if (typeof page !== 'undefined') {
return typeof page === 'function' ? page() : page
}

if (typeof fallback === 'function') {
return fallback(path)
}

throw new PageNotFoundError(`Page not found: [${path}].`)
}

export class PageNotFoundError extends Error {
constructor(message: string) {
super(message)
this.name = 'PageNotFoundError'
}
}
23 changes: 22 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import laravel from '../src'
import { resolvePageComponent } from '../src/inertia-helpers';
import { resolvePageComponent, PageNotFoundError } from '../src/inertia-helpers';

describe('laravel-vite-plugin', () => {
afterEach(() => {
Expand Down Expand Up @@ -376,4 +376,25 @@ describe('inertia-helpers', () => {
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts', { eager: true }))
expect(file.default).toBe('Dummy File')
})

it('accepts path as fallback', async () => {
const file = await resolvePageComponent<{ default: string }>('missing-page', import.meta.glob('./__data__/*.ts', { eager: true }), path)
expect(file.default).toBe('Dummy File')
})

it('accepts closure as fallback', async () => {
let capturedPath: string|null = null;
const file = await resolvePageComponent<{ default: string }>('missing-page', import.meta.glob('./__data__/*.ts', { eager: true }), (originalPath) => {
capturedPath = originalPath

return resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts', { eager: true }))
})
expect(file.default).toBe('Dummy File')
expect(capturedPath).toBe('missing-page')
})

it('throws PageNotFoundError when path is not found', async () => {
const callback = () => resolvePageComponent<{ default: string }>('./__data__/_does_not_exist_.ts', import.meta.glob('./__data__/*.ts'))
await expect(callback).rejects.toThrowError(PageNotFoundError)
})
})