@tolki/types 1.2.0 → 1.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/types",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Utility TypeScript types for @tolki packages and Laravel HTTP responses.",
5
5
  "author": "Abraham Arango <https://github.com/abetwothree>",
6
6
  "license": "MIT",
@@ -1,5 +1,10 @@
1
1
  import type { PaginatorLink } from "./pagination";
2
2
 
3
+ /**
4
+ * Interface representing the pagination structure of a JSON resource collection response, following Laravel's default JSON Resource format.
5
+ *
6
+ * @see https://laravel.com/docs/master/eloquent-resources#pagination
7
+ */
3
8
  export interface JsonResourceMeta {
4
9
  current_page: number;
5
10
  from: number;
@@ -11,6 +16,9 @@ export interface JsonResourceMeta {
11
16
  total: number;
12
17
  }
13
18
 
19
+ /**
20
+ * Interface representing the structure of a JSON resource collection response with pagination links, following Laravel's default JSON Resource format.
21
+ */
14
22
  export interface JsonResourceLinks {
15
23
  first: string | null;
16
24
  last: string | null;
@@ -18,8 +26,30 @@ export interface JsonResourceLinks {
18
26
  next: string | null;
19
27
  }
20
28
 
29
+ /**
30
+ * Interface representing a paginated JSON resource collection response, following Laravel's default JSON Resource format.
31
+ *
32
+ * This structure includes the paginated data array, along with the pagination metadata and links as defined by Laravel's default JSON Resource responses.
33
+ */
21
34
  export interface JsonResourcePaginator<T> {
22
35
  data: T[];
23
36
  meta: JsonResourceMeta;
24
37
  links: JsonResourceLinks;
25
38
  }
39
+
40
+ /**
41
+ * Interface representing an anonymous JSON resource collection response, following Laravel's default JSON Resource format.
42
+ */
43
+ export interface AnonymousResourceCollection<T> {
44
+ data: T[];
45
+ }
46
+
47
+ /**
48
+ * Interface representing a paginated JSON resource collection response with pagination metadata and links, following Laravel's default JSON Resource format.
49
+ *
50
+ * This interface is meant to extend basic collection resources with pagination information.
51
+ */
52
+ export interface ResourcePagination {
53
+ meta: JsonResourceMeta;
54
+ links: JsonResourceLinks;
55
+ }
package/src/routes.d.ts CHANGED
@@ -125,11 +125,15 @@ export type RouteComponentType = string | Readonly<Record<string, string>>;
125
125
 
126
126
  /**
127
127
  * Route metadata shape as passed to `defineRoute`.
128
+ *
129
+ * `TPageProps` is a phantom generic — it is not present in the object shape.
130
+ * It is carried by `DefineRouteResult` and readable via `InferPageProps<T>`.
128
131
  */
129
132
  export type RouteMetadata<
130
133
  TMethods extends readonly string[] = readonly string[],
131
134
  TArgs extends readonly RouteArgMeta[] = readonly RouteArgMeta[],
132
135
  TComponent extends RouteComponentType | undefined = undefined,
136
+ TPageProps = never,
133
137
  > = {
134
138
  readonly name?: string | null;
135
139
  readonly url: string;
@@ -255,11 +259,15 @@ export type DefineRouteResultWithArgs<
255
259
  TMethods extends readonly string[],
256
260
  TArgs extends readonly RouteArgMeta[],
257
261
  TComponent extends RouteComponentType | undefined = undefined,
262
+ TPageProps = never,
263
+ TRequestPayload = never,
258
264
  > = RouteCallableWithArgs<TMethods[0], TArgs> & {
259
265
  url: RouteUrlWithArgs<TArgs>;
260
- definition: RouteMetadata<TMethods, TArgs, TComponent>;
266
+ definition: RouteMetadata<TMethods, TArgs, TComponent, TPageProps>;
261
267
  form: FormWithArgs<TMethods, TArgs>;
262
268
  toString(): string;
269
+ readonly _pageProps?: TPageProps;
270
+ readonly _requestPayload?: TRequestPayload;
263
271
  } & VerbMethodsWithArgs<TMethods, TArgs> &
264
272
  ComponentMixin<TComponent, TMethods, TArgs>;
265
273
 
@@ -269,25 +277,51 @@ export type DefineRouteResultWithArgs<
269
277
  export type DefineRouteResultNoArgs<
270
278
  TMethods extends readonly string[],
271
279
  TComponent extends RouteComponentType | undefined = undefined,
280
+ TPageProps = never,
281
+ TRequestPayload = never,
272
282
  > = RouteCallableNoArgs<TMethods[0]> & {
273
283
  url: RouteUrlNoArgs;
274
- definition: RouteMetadata<TMethods, readonly [], TComponent>;
284
+ definition: RouteMetadata<TMethods, readonly [], TComponent, TPageProps>;
275
285
  form: FormNoArgs<TMethods>;
276
286
  toString(): string;
287
+ readonly _pageProps?: TPageProps;
288
+ readonly _requestPayload?: TRequestPayload;
277
289
  } & VerbMethodsNoArgs<TMethods> &
278
290
  ComponentMixin<TComponent, TMethods, readonly []>;
279
291
 
280
292
  /**
281
293
  * The full return type of `defineRoute`.
282
294
  * Discriminates on whether `args` is present in metadata.
295
+ *
296
+ * `TPageProps` is a phantom generic for Inertia page prop typing.
297
+ * It defaults to `never` and is populated when a generated route file
298
+ * emits an explicit type annotation:
299
+ * ```typescript
300
+ * export const dashboard: DefineRouteResult<readonly ['get'], readonly [], 'Dashboard', DashboardPageProps> = defineRoute({...})
301
+ * ```
302
+ *
303
+ * `TRequestPayload` is a phantom generic for FormRequest payload typing.
304
+ * It defaults to `never` and is populated when a generated route file
305
+ * emits an explicit type annotation:
306
+ * ```typescript
307
+ * export const store: DefineRouteResult<readonly ['post'], readonly [], undefined, never, StorePostRequest> = defineRoute({...})
308
+ * ```
283
309
  */
284
310
  export type DefineRouteResult<
285
311
  TMethods extends readonly string[] = readonly string[],
286
312
  TArgs extends readonly RouteArgMeta[] = readonly RouteArgMeta[],
287
313
  TComponent extends RouteComponentType | undefined = undefined,
314
+ TPageProps = never,
315
+ TRequestPayload = never,
288
316
  > = [TArgs] extends [readonly []] | [readonly never[]]
289
- ? DefineRouteResultNoArgs<TMethods, TComponent>
290
- : DefineRouteResultWithArgs<TMethods, TArgs, TComponent>;
317
+ ? DefineRouteResultNoArgs<TMethods, TComponent, TPageProps, TRequestPayload>
318
+ : DefineRouteResultWithArgs<
319
+ TMethods,
320
+ TArgs,
321
+ TComponent,
322
+ TPageProps,
323
+ TRequestPayload
324
+ >;
291
325
 
292
326
  /**
293
327
  * The result of calling `.form()` — contains action (URL), method, and toString().
@@ -556,3 +590,53 @@ type FormNoArgs<TMethods extends readonly string[]> = FormCallableNoArgs<
556
590
  TMethods[0]
557
591
  > &
558
592
  FormVerbMethodsNoArgs<TMethods>;
593
+
594
+ /**
595
+ * Infer the Inertia page props type from a `DefineRouteResult`.
596
+ *
597
+ * Returns the `TPageProps` generic. Returns `never` when the route const was
598
+ * not annotated with an explicit page props type (i.e., the default).
599
+ *
600
+ * Reads the `_pageProps` phantom property directly to avoid TypeScript's
601
+ * deferred conditional-type inference limitations with `DefineRouteResult`.
602
+ *
603
+ * @example
604
+ * ```typescript
605
+ * import type { DashboardPageProps } from './inertia-controller';
606
+ * import type { InferPageProps } from '@tolki/ts';
607
+ *
608
+ * type Props = InferPageProps<typeof dashboard>; // DashboardPageProps
609
+ * ```
610
+ */
611
+ export type InferPageProps<T> = T extends {
612
+ readonly _pageProps?: infer TPageProps;
613
+ }
614
+ ? [TPageProps] extends [never]
615
+ ? never
616
+ : TPageProps
617
+ : never;
618
+
619
+ /**
620
+ * Infer the request payload type from a `DefineRouteResult`.
621
+ *
622
+ * Returns the `TRequestPayload` generic. Returns `never` when the route const
623
+ * was not annotated with an explicit request payload type (i.e., the default).
624
+ *
625
+ * Reads the `_requestPayload` phantom property directly to avoid TypeScript's
626
+ * deferred conditional-type inference limitations with `DefineRouteResult`.
627
+ *
628
+ * @example
629
+ * ```typescript
630
+ * import type { store } from './post-controller';
631
+ * import type { InferRequestPayload } from '@tolki/ts';
632
+ *
633
+ * type Payload = InferRequestPayload<typeof store>; // StorePostRequest
634
+ * ```
635
+ */
636
+ export type InferRequestPayload<T> = T extends {
637
+ readonly _requestPayload?: infer TRequestPayload;
638
+ }
639
+ ? [TRequestPayload] extends [never]
640
+ ? never
641
+ : TRequestPayload
642
+ : never;
package/src/utils.d.ts CHANGED
@@ -0,0 +1 @@
1
+ export {};