Skip to content

Commit d9088f4

Browse files
committed
feat(types): add InertiaPage type helper
1 parent 3024d46 commit d9088f4

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

adonis-typings/inertia.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ declare module '@ioc:EidelLev/Inertia' {
22
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
33
import { ResponseContract } from '@ioc:Adonis/Core/Response';
44

5-
export type ResponseProps = Record<string, unknown>;
6-
export type RenderResponse = Promise<Record<string, unknown> | string | ResponseContract>;
5+
type AdonisControllerMethod = (ctx: HttpContextContract, ...args: any) => any;
6+
export type ResponseProps = Record<string, any>;
7+
type RenderedResponseProps<T extends ResponseProps> = {
8+
component: string;
9+
version: VersionValue;
10+
props: T;
11+
url: string;
12+
}
13+
export type RenderResponse<T extends ResponseProps> = Promise<RenderedResponseProps<T> | string | ResponseContract>;
14+
export type InertiaPage<T extends AdonisControllerMethod> = Exclude<Awaited<ReturnType<T>>, string | ResponseContract>['props'];
715

816
/**
917
* Shared data types
@@ -31,7 +39,7 @@ declare module '@ioc:EidelLev/Inertia' {
3139
* @param {string} component Page component
3240
* @param {ResponseProps} responseProps Props
3341
*/
34-
render(component: string, responseProps?: ResponseProps, pageOnlyProps?: ResponseProps): RenderResponse;
42+
render<T extends ResponseProps>(component: string, responseProps?: T, pageOnlyProps?: ResponseProps): RenderResponse<T>;
3543

3644
/**
3745
* Redirect back with the correct HTTP status code

src/Inertia.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ export class Inertia implements InertiaContract {
4949
return new LazyProp(callback);
5050
}
5151

52-
public async render(
52+
public async render<T extends ResponseProps>(
5353
component: string,
54-
responseProps: ResponseProps = {},
54+
responseProps?: T,
5555
pageOnlyProps: ResponseProps = {},
56-
): RenderResponse {
56+
): RenderResponse<T> {
5757
const { view: inertiaView, ssr = { enabled: false } } = this.config;
5858
const { request, response, view, session } = this.ctx;
5959
const isInertia = request.inertia();
6060
const partialData = this.resolvePartialData(request.header(HEADERS.INERTIA_PARTIAL_DATA));
6161
const partialDataComponentHeader = request.header(HEADERS.INERTIA_PARTIAL_DATA_COMPONENT);
6262
const requestAssetVersion = request.header(HEADERS.INERTIA_VERSION);
63-
const props: ResponseProps = await this.resolveProps(
64-
{ ...Inertia.sharedData, ...responseProps },
63+
const props: T = await this.resolveProps(
64+
{ ...Inertia.sharedData, ...Object(responseProps) },
6565
partialData,
6666
component,
6767
partialDataComponentHeader,

0 commit comments

Comments
 (0)