@tolki/types 1.2.0 → 1.3.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.3.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,13 @@ 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,
258
263
  > = RouteCallableWithArgs<TMethods[0], TArgs> & {
259
264
  url: RouteUrlWithArgs<TArgs>;
260
- definition: RouteMetadata<TMethods, TArgs, TComponent>;
265
+ definition: RouteMetadata<TMethods, TArgs, TComponent, TPageProps>;
261
266
  form: FormWithArgs<TMethods, TArgs>;
262
267
  toString(): string;
268
+ readonly _pageProps?: TPageProps;
263
269
  } & VerbMethodsWithArgs<TMethods, TArgs> &
264
270
  ComponentMixin<TComponent, TMethods, TArgs>;
265
271
 
@@ -269,25 +275,35 @@ export type DefineRouteResultWithArgs<
269
275
  export type DefineRouteResultNoArgs<
270
276
  TMethods extends readonly string[],
271
277
  TComponent extends RouteComponentType | undefined = undefined,
278
+ TPageProps = never,
272
279
  > = RouteCallableNoArgs<TMethods[0]> & {
273
280
  url: RouteUrlNoArgs;
274
- definition: RouteMetadata<TMethods, readonly [], TComponent>;
281
+ definition: RouteMetadata<TMethods, readonly [], TComponent, TPageProps>;
275
282
  form: FormNoArgs<TMethods>;
276
283
  toString(): string;
284
+ readonly _pageProps?: TPageProps;
277
285
  } & VerbMethodsNoArgs<TMethods> &
278
286
  ComponentMixin<TComponent, TMethods, readonly []>;
279
287
 
280
288
  /**
281
289
  * The full return type of `defineRoute`.
282
290
  * Discriminates on whether `args` is present in metadata.
291
+ *
292
+ * `TPageProps` is a phantom generic for Inertia page prop typing.
293
+ * It defaults to `never` and is populated when a generated route file
294
+ * emits an explicit type annotation:
295
+ * ```typescript
296
+ * export const dashboard: DefineRouteResult<readonly ['get'], readonly [], 'Dashboard', DashboardPageProps> = defineRoute({...})
297
+ * ```
283
298
  */
284
299
  export type DefineRouteResult<
285
300
  TMethods extends readonly string[] = readonly string[],
286
301
  TArgs extends readonly RouteArgMeta[] = readonly RouteArgMeta[],
287
302
  TComponent extends RouteComponentType | undefined = undefined,
303
+ TPageProps = never,
288
304
  > = [TArgs] extends [readonly []] | [readonly never[]]
289
- ? DefineRouteResultNoArgs<TMethods, TComponent>
290
- : DefineRouteResultWithArgs<TMethods, TArgs, TComponent>;
305
+ ? DefineRouteResultNoArgs<TMethods, TComponent, TPageProps>
306
+ : DefineRouteResultWithArgs<TMethods, TArgs, TComponent, TPageProps>;
291
307
 
292
308
  /**
293
309
  * The result of calling `.form()` — contains action (URL), method, and toString().
@@ -556,3 +572,28 @@ type FormNoArgs<TMethods extends readonly string[]> = FormCallableNoArgs<
556
572
  TMethods[0]
557
573
  > &
558
574
  FormVerbMethodsNoArgs<TMethods>;
575
+
576
+ /**
577
+ * Infer the Inertia page props type from a `DefineRouteResult`.
578
+ *
579
+ * Returns the `TPageProps` generic. Returns `never` when the route const was
580
+ * not annotated with an explicit page props type (i.e., the default).
581
+ *
582
+ * Reads the `_pageProps` phantom property directly to avoid TypeScript's
583
+ * deferred conditional-type inference limitations with `DefineRouteResult`.
584
+ *
585
+ * @example
586
+ * ```typescript
587
+ * import type { DashboardPageProps } from './inertia-controller';
588
+ * import type { InferPageProps } from '@tolki/ts';
589
+ *
590
+ * type Props = InferPageProps<typeof dashboard>; // DashboardPageProps
591
+ * ```
592
+ */
593
+ export type InferPageProps<T> = T extends {
594
+ readonly _pageProps?: infer TPageProps;
595
+ }
596
+ ? [TPageProps] extends [never]
597
+ ? never
598
+ : TPageProps
599
+ : never;
package/src/utils.d.ts CHANGED
@@ -0,0 +1 @@
1
+ export {};