@tolki/types 1.7.0 → 1.9.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/README.md CHANGED
@@ -141,6 +141,8 @@ Example in Vue template:
141
141
  </template>
142
142
  ```
143
143
 
144
+ `from` and `to` are `number | null`, because Laravel sends `null` for both on a page with no items. Each entry of `links` is a `PaginatorLink`. Its `page` field is optional, because Laravel 12.23 and later send it, and never on the `...` separator.
145
+
144
146
  #### SimplePaginator
145
147
 
146
148
  If you use the `simplePaginate` response function as show below, you can use the `SimplePaginator` type to define the response structure.
@@ -187,6 +189,8 @@ defineProps<{
187
189
  </script>
188
190
  ```
189
191
 
192
+ `current_page_url` is optional, because Laravel 12.15 and later send it. `from` and `to` are `number | null`, as on `LengthAwarePaginator`.
193
+
190
194
  #### CursorPaginator
191
195
 
192
196
  The last pagination result that Laravel provides is the `cursor` pagination. For that response structure you can use the `CursorPaginator` type.
@@ -555,4 +559,84 @@ export interface JsonResourcePaginator<T> {
555
559
 
556
560
  If needed, you can also use these types individually to define the structure of the `links` and `meta` sections separately. You can also compose your own JSON resource structures if your PHP resources return custom structures.
557
561
 
562
+ #### Simple and Cursor Paginated Resources
563
+
564
+ A collection over `simplePaginate()` or `cursorPaginate()` sends a smaller `meta` block. A simple paginator's `meta` block has no `last_page`, `links`, or `total`, and a cursor paginator's `meta` block has only `path`, `per_page`, `next_cursor`, and `prev_cursor`. Use the types that match the paginator:
565
+
566
+ - `paginate()`: `JsonResourcePaginator<T>` for the whole response, `ResourcePagination` for the `meta` and `links` blocks, and `JsonResourceMeta` for the `meta` block alone.
567
+ - `simplePaginate()`: `JsonResourceSimplePaginator<T>`, `ResourceSimplePagination`, and `JsonResourceSimpleMeta`.
568
+ - `cursorPaginate()`: `JsonResourceCursorPaginator<T>`, `ResourceCursorPagination`, and `JsonResourceCursorMeta`.
569
+
570
+ To type a paginated named collection, join its own interface and the `meta` and `links` type with `&`, as in `UserCollection & ResourceSimplePagination`. `JsonResourceSimpleMeta` has the optional `current_page_url` that `SimplePaginator` has. In `JsonResourceMeta` and `JsonResourceSimpleMeta`, `from` and `to` are `number | null`.
571
+
572
+ ### JSON:API Utilities Types
573
+
574
+ Laravel's [JSON:API resources](https://laravel.com/docs/eloquent-resources#jsonapi-resources), from Laravel 12.45, send a [JSON:API](https://jsonapi.org/format/) document. These types describe each document, given the resource object it sends under `data` and the resource objects its `included` member can hold:
575
+
576
+ | Type | What Laravel sends |
577
+ | ------------------------------ | ---------------------------------------------------------------------------- |
578
+ | `JsonApiDocument<R, I>` | A single resource, as `{ data: R; included?: I[]; jsonapi?: JsonApiObject }` |
579
+ | `JsonApiCollection<R, I>` | `SomeResource::collection()` over items that aren't paginated |
580
+ | `JsonApiPaginator<R, I>` | `SomeResource::collection()` over `paginate()`, with `links` and `meta` |
581
+ | `JsonApiSimplePaginator<R, I>` | The same over `simplePaginate()` |
582
+ | `JsonApiCursorPaginator<R, I>` | The same over `cursorPaginate()` |
583
+
584
+ `I` defaults to `JsonApiResourceObject`, which fits any resource object. The paginator documents take their `meta` block from the matching type above, such as `JsonResourceSimpleMeta`.
585
+
586
+ The parts of a document have types of their own:
587
+
588
+ - `JsonApiResourceObject<TType, TAttributes, TRelationships>`: a resource object, with `id`, `type`, and the optional `attributes`, `relationships`, `links`, and `meta`.
589
+ - `JsonApiResourceIdentifier<TType>`: `{ id: string; type: TType }`, which names a related resource.
590
+ - `JsonApiToOneRelationship<TType>`: a relationship whose `data` is one identifier, or `null`.
591
+ - `JsonApiToManyRelationship<TType>`: a relationship whose `data` is a list of identifiers.
592
+ - `JsonApiRelationship<TType>`: either kind of relationship.
593
+ - `JsonApiObject`: the top-level `jsonapi` member, which Laravel sends once `JsonApiResource::configure()` sets it.
594
+
595
+ Describe a resource object with `JsonApiResourceObject`, then type a response with the document:
596
+
597
+ ```typescript
598
+ import type {
599
+ JsonApiDocument,
600
+ JsonApiResourceObject,
601
+ JsonApiToOneRelationship,
602
+ } from "@tolki/types";
603
+
604
+ type User = JsonApiResourceObject<"users", { name: string }>;
605
+
606
+ type Post = JsonApiResourceObject<
607
+ "posts",
608
+ { title: string; published_at: string | null },
609
+ { author?: JsonApiToOneRelationship<"users"> }
610
+ >;
611
+
612
+ const response = await fetch("/api/posts/1?include=author");
613
+ const post: JsonApiDocument<Post, User> = await response.json();
614
+
615
+ post.data.relationships?.author?.data?.id; // string | undefined
616
+ ```
617
+
618
+ The [Laravel TypeScript Publisher](https://github.com/abetwothree/laravel-ts-publish) writes each resource object interface for you. See [JSON:API Resources](https://tolki.abe.dev/ts/api-resources.html#json-api-resources).
619
+
620
+ #### Sparse Fieldsets
621
+
622
+ A request with a sparse fieldset, such as `?fields[posts]=title`, sends only the attributes it names. `JsonApiSparse<R>` makes a resource object's `attributes` member optional, and every attribute in it:
623
+
624
+ ```typescript
625
+ import type { JsonApiSparse } from "@tolki/types";
626
+
627
+ interface PostResource {
628
+ id: string;
629
+ type: "posts";
630
+ attributes: { title: string; body: string };
631
+ }
632
+
633
+ type SparsePost = JsonApiSparse<PostResource>;
634
+
635
+ const post: SparsePost = { id: "1", type: "posts", attributes: {} };
636
+
637
+ post.attributes?.title; // string | undefined
638
+ ```
639
+
640
+ Over a union of resource objects, `JsonApiSparse` reads each one on its own, so checking `type` still narrows `attributes`.
641
+
558
642
  <!-- AUTO-GENERATED-DOCS:END -->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/types",
3
- "version": "1.7.0",
3
+ "version": "1.9.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",
package/src/arrays.d.ts CHANGED
@@ -126,10 +126,10 @@ export type TruthyArray<T extends readonly unknown[]> =
126
126
  * - a `[key]` single-element tuple, identical to a bare key path, ascending
127
127
  * - a `[key, direction]` tuple: `true`/`'asc'`/`"Ascending"` sorts ascending,
128
128
  * everything else descending, mirroring `Collection::sortByMany`
129
- * - a comparator returning a negative, zero, or positive number
129
+ * - a comparator returning a negative, zero, or positive number, or a bool, which sorts as PHP's `usort()` reads one
130
130
  */
131
131
  export type SortSpec<TValue> =
132
132
  | string
133
133
  | readonly [string]
134
134
  | readonly [string, boolean | "Ascending" | "Descending" | "asc" | "desc"]
135
- | ((a: TValue, b: TValue) => number);
135
+ | ((a: TValue, b: TValue) => number | boolean);
@@ -1,2 +1,13 @@
1
+ /**
2
+ * Any object a `Proxy` can wrap.
3
+ *
4
+ * @deprecated Nothing in @tolki reads it; the next major removes it.
5
+ */
1
6
  export type ProxyTarget = object;
7
+
8
+ /**
9
+ * A property key a `Proxy` trap receives.
10
+ *
11
+ * @deprecated Nothing in @tolki reads it; the next major removes it.
12
+ */
2
13
  export type PropertyName = string | symbol;
package/src/data.d.ts CHANGED
@@ -8,14 +8,9 @@ export type DataItems<TValue, TKey extends PropertyKey = PropertyKey> =
8
8
  | Record<TKey, TValue>;
9
9
 
10
10
  /**
11
- * Data that may also be supplied as one of the iterable structures that stand
12
- * in for a PHP `iterable`: a Map for keyed items, or any other iterable such
13
- * as a generator or a Set for positional items.
14
- *
15
- * No `src/` file references it today; it is kept for `@tolki/collection`, whose constructor
16
- * and `getArrayableItems` accept exactly this set, and it names the shape `@tolki/data`'s
17
- * type tests measure their own widened rows against. Deleting it would be a breaking change
18
- * to a published package for no gain, so it stays until that consumer lands.
11
+ * Data that may also come as one of the iterables standing in for a PHP `iterable`: a Map for keyed items, or any
12
+ * other iterable, such as a generator or a Set, for positional items. `@tolki/collection`'s constructor takes it in
13
+ * the row a subclass's own constructor forwards through.
19
14
  */
20
15
  export type DataIterableItems<TValue, TKey extends PropertyKey = PropertyKey> =
21
16
  | DataItems<TValue, TKey>
package/src/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export type * from "./arrays";
2
2
  export type * from "./collections";
3
3
  export type * from "./data";
4
4
  export type * from "./enums";
5
+ export type * from "./json-api";
5
6
  export type * from "./models";
6
7
  export type * from "./objects";
7
8
  export type * from "./pagination";
@@ -0,0 +1,149 @@
1
+ import type {
2
+ JsonResourceCursorMeta,
3
+ JsonResourceLinks,
4
+ JsonResourceMeta,
5
+ JsonResourceSimpleMeta,
6
+ } from "./resource-responses";
7
+
8
+ /**
9
+ * A resource identifier object: how a relationship's `data` names a related resource.
10
+ *
11
+ * @see https://jsonapi.org/format/#document-resource-identifier-objects
12
+ */
13
+ export interface JsonApiResourceIdentifier<TType extends string = string> {
14
+ /** Always a string, even for an integer key. */
15
+ id: string;
16
+ type: TType;
17
+ }
18
+
19
+ /**
20
+ * A to-one relationship object, whose `data` is `null` when the related model is missing.
21
+ *
22
+ * @see https://jsonapi.org/format/#document-resource-object-relationships
23
+ */
24
+ export interface JsonApiToOneRelationship<TType extends string = string> {
25
+ data: JsonApiResourceIdentifier<TType> | null;
26
+ }
27
+
28
+ /**
29
+ * A to-many relationship object, whose `data` is an empty list when there are no related models.
30
+ *
31
+ * @see https://jsonapi.org/format/#document-resource-object-relationships
32
+ */
33
+ export interface JsonApiToManyRelationship<TType extends string = string> {
34
+ data: JsonApiResourceIdentifier<TType>[];
35
+ }
36
+
37
+ /**
38
+ * A to-one or to-many relationship object.
39
+ */
40
+ export type JsonApiRelationship<TType extends string = string> =
41
+ | JsonApiToOneRelationship<TType>
42
+ | JsonApiToManyRelationship<TType>;
43
+
44
+ /**
45
+ * A resource object, as a JSON:API resource sends it under `data` and in `included`.
46
+ *
47
+ * With its defaults, it fits any resource object, such as an `included` entry of a type you have not declared.
48
+ *
49
+ * @see https://jsonapi.org/format/#document-resource-objects
50
+ */
51
+ export interface JsonApiResourceObject<
52
+ TType extends string = string,
53
+ TAttributes extends object = Record<string, unknown>,
54
+ TRelationships extends object = Record<string, JsonApiRelationship>,
55
+ > {
56
+ /** Always a string, even for an integer key. */
57
+ id: string;
58
+ type: TType;
59
+ attributes?: TAttributes;
60
+ relationships?: TRelationships;
61
+ links?: Record<string, unknown>;
62
+ meta?: Record<string, unknown>;
63
+ }
64
+
65
+ /**
66
+ * The top-level `jsonapi` member, sent once `JsonApiResource::configure()` has set it.
67
+ *
68
+ * @see https://jsonapi.org/format/#document-jsonapi-object
69
+ */
70
+ export interface JsonApiObject {
71
+ version?: string;
72
+ ext?: string[];
73
+ profile?: string[];
74
+ meta?: Record<string, unknown>;
75
+ }
76
+
77
+ /**
78
+ * The document a JSON:API resource sends for a single model.
79
+ *
80
+ * `TIncluded` is the union of the resource objects an `?include=` request can add to `included`.
81
+ *
82
+ * @see https://laravel.com/docs/master/eloquent-resources#jsonapi-resources
83
+ */
84
+ export interface JsonApiDocument<TResource, TIncluded = JsonApiResourceObject> {
85
+ data: TResource;
86
+ included?: TIncluded[];
87
+ jsonapi?: JsonApiObject;
88
+ }
89
+
90
+ /**
91
+ * The document `SomeResource::collection()` sends over a collection that is not paginated.
92
+ *
93
+ * @see https://laravel.com/docs/master/eloquent-resources#jsonapi-resources
94
+ */
95
+ export interface JsonApiCollection<
96
+ TResource,
97
+ TIncluded = JsonApiResourceObject,
98
+ > {
99
+ data: TResource[];
100
+ included?: TIncluded[];
101
+ jsonapi?: JsonApiObject;
102
+ }
103
+
104
+ /**
105
+ * The document `SomeResource::collection()` sends over a `paginate()` paginator.
106
+ */
107
+ export interface JsonApiPaginator<
108
+ TResource,
109
+ TIncluded = JsonApiResourceObject,
110
+ > extends JsonApiCollection<TResource, TIncluded> {
111
+ links: JsonResourceLinks;
112
+ meta: JsonResourceMeta;
113
+ }
114
+
115
+ /**
116
+ * The document `SomeResource::collection()` sends over a `simplePaginate()` paginator.
117
+ */
118
+ export interface JsonApiSimplePaginator<
119
+ TResource,
120
+ TIncluded = JsonApiResourceObject,
121
+ > extends JsonApiCollection<TResource, TIncluded> {
122
+ links: JsonResourceLinks;
123
+ meta: JsonResourceSimpleMeta;
124
+ }
125
+
126
+ /**
127
+ * The document `SomeResource::collection()` sends over a `cursorPaginate()` paginator.
128
+ */
129
+ export interface JsonApiCursorPaginator<
130
+ TResource,
131
+ TIncluded = JsonApiResourceObject,
132
+ > extends JsonApiCollection<TResource, TIncluded> {
133
+ links: JsonResourceLinks;
134
+ meta: JsonResourceCursorMeta;
135
+ }
136
+
137
+ /**
138
+ * A resource object read under a sparse fieldset (`?fields[type]=…`), where any attribute may be left out.
139
+ *
140
+ * Over a union, it reads each resource object on its own, so narrowing on `type` still narrows `attributes`.
141
+ *
142
+ * @see https://laravel.com/docs/master/eloquent-resources#jsonapi-sparse-fieldsets-and-includes
143
+ */
144
+ export type JsonApiSparse<TResource extends { attributes?: object }> =
145
+ TResource extends unknown
146
+ ? Omit<TResource, "attributes"> & {
147
+ attributes?: Partial<NonNullable<TResource["attributes"]>>;
148
+ }
149
+ : never;
package/src/objects.d.ts CHANGED
@@ -527,30 +527,58 @@ export type DeepMergeObjects<T1, T2, D extends number = 5> = [D] extends [never]
527
527
 
528
528
  /**
529
529
  * The object produced by writing `V` at dot path `P`, creating nested objects
530
- * for missing or non-object segments the way `Arr::set` creates arrays.
530
+ * for missing or non-object segments the way `Arr::set` creates arrays. A Map
531
+ * on the path stays a Map, holding the new key.
531
532
  *
532
533
  * @example
533
- * SetObjectPath<Record<never, never>, "a.b", 1> // { a: { b: 1 } }
534
- * SetObjectPath<{ a: { x: 1 } }, "a.y", 2> // { a: { x: 1; y: 2 } }
534
+ * SetObjectPath<Record<never, never>, "a.b", 1> // { a: { b: 1 } }
535
+ * SetObjectPath<{ a: { x: 1 } }, "a.y", 2> // { a: { x: 1; y: 2 } }
536
+ * SetObjectPath<{ m: Map<string, number> }, "m.n", true> // { m: Map<string | number, number | true> }
535
537
  */
536
538
  export type SetObjectPath<T, P extends string, V> = P extends keyof T
537
539
  ? Simplify<Omit<T, P> & { [K in P]: V }>
538
540
  : P extends `${infer Head}.${infer Rest}`
539
541
  ? Simplify<
540
542
  Omit<T, Head> & {
541
- [K in Head]: SetObjectPath<
542
- Head extends keyof T
543
- ? [PlainObjectOf<T[Head]>] extends [never]
544
- ? Record<never, never>
545
- : PlainObjectOf<T[Head]>
546
- : Record<never, never>,
547
- Rest,
548
- V
549
- >;
543
+ [K in Head]: Head extends keyof T
544
+ ? SetPathValue<T[Head], Rest, V>
545
+ : SetObjectPath<Record<never, never>, Rest, V>;
550
546
  }
551
547
  >
552
548
  : Simplify<Omit<T, P> & { [K in P]: V }>;
553
549
 
550
+ /** What a value on a `SetObjectPath` becomes once `Rest` is written into it: a Map stays a Map, the rest an object. */
551
+ type SetPathValue<X, Rest extends string, V> = [X] extends [
552
+ Exclude<X, ReadonlyMap<unknown, unknown>>,
553
+ ]
554
+ ? SetObjectPath<
555
+ [PlainObjectOf<X>] extends [never]
556
+ ? Record<never, never>
557
+ : PlainObjectOf<X>,
558
+ Rest,
559
+ V
560
+ >
561
+ :
562
+ | SetMapPath<Extract<X, ReadonlyMap<unknown, unknown>>, Rest, V>
563
+ | ([Exclude<X, ReadonlyMap<unknown, unknown>>] extends [never]
564
+ ? never
565
+ : SetPathValue<
566
+ Exclude<X, ReadonlyMap<unknown, unknown>>,
567
+ Rest,
568
+ V
569
+ >);
570
+
571
+ /**
572
+ * A Map written into at `Rest`. Its keys widen to any PHP key, since a segment like `"0"` is stored as a number;
573
+ * a deeper path writes inside one entry, which the Map's value type cannot single out.
574
+ */
575
+ type SetMapPath<M, Rest extends string, V> =
576
+ M extends ReadonlyMap<infer MK, infer MV>
577
+ ? Rest extends `${string}.${string}`
578
+ ? Map<MK | string | number, unknown>
579
+ : Map<MK | string | number, MV | V>
580
+ : never;
581
+
554
582
  /**
555
583
  * A value `undot` may produce from leaves of type `V`: the leaf itself, or a
556
584
  * list or object nesting more of them.
@@ -585,6 +613,210 @@ type FlatLeafValue<T, D extends number> = T extends readonly (infer E)[]
585
613
  : ObjectFlatValue<ObjectValue<T>, ObjectDepth[D]>
586
614
  : T;
587
615
 
616
+ /**
617
+ * Every value `flatten()` may push for `T` at some depth: `T` itself, or any value below it. At each level a
618
+ * Collection-like value is read through `all()` first, and a `Date`, `RegExp`, `Map`, `Set`, `Promise` or function is
619
+ * kept whole. `ObjectPathValue` cannot stand in: it drops `undefined` and never reads through `all()`.
620
+ *
621
+ * @example
622
+ * FlattenReach<{ a: [1, { b: "x" }] }> // { a: [1, { b: "x" }] } | [1, { b: "x" }] | 1 | { b: "x" } | "x"
623
+ */
624
+ export type FlattenReach<T, D extends number = 5> = [D] extends [never]
625
+ ? unknown
626
+ : T | FlattenItemReach<T, D>;
627
+
628
+ /**
629
+ * The values flattening the item `T` may push, at any depth: those a list or object holds at every level below it,
630
+ * read through `all()` first, or `T` itself when it is a scalar, `Date`, `RegExp`, `Map`, `Set`, `Promise` or function.
631
+ * TypeScript cannot tell a class instance from a plain object, so a class instance item is typed as walked while the
632
+ * runtime keeps it whole.
633
+ *
634
+ * @example
635
+ * FlattenItemReach<number[][]> // number[] | number
636
+ * FlattenItemReach<{ a: 1 }> // 1
637
+ */
638
+ export type FlattenItemReach<T, D extends number = 5> = FlattenReachOf<
639
+ T extends { all: (...args: never[]) => infer R } ? R : T,
640
+ D
641
+ >;
642
+
643
+ /** What lies below a value `flatten()` has read through `all()`: a list's or object's values, each reached in turn. */
644
+ type FlattenReachOf<T, D extends number> = T extends readonly (infer E)[]
645
+ ? FlattenReach<E, ObjectDepth[D]>
646
+ : T extends NonObjectItems | Date | RegExp | Promise<unknown>
647
+ ? T
648
+ : T extends object
649
+ ? [keyof T] extends [never]
650
+ ? unknown
651
+ : FlattenReach<ObjectValue<T>, ObjectDepth[D]>
652
+ : T;
653
+
654
+ /**
655
+ * Every value `dot()` may keep for `T` at some depth: `T` itself, or any value below it. A list, plain object or `Map`
656
+ * is walked, while a `Date`, `RegExp`, `Set`, `Promise` or function is kept whole. Unlike `FlattenReach`, it walks a
657
+ * `Map`, as `dot()` does where `flatten()` keeps one whole, and never reads through `all()`.
658
+ *
659
+ * @example
660
+ * DotReach<{ m: Map<string, number> }> // { m: Map<string, number> } | Map<string, number> | number
661
+ */
662
+ export type DotReach<T, D extends number = 5> = [D] extends [never]
663
+ ? unknown
664
+ : T | DotReachOf<T, D>;
665
+
666
+ /** What lies below a value `dot()` walks: a list's, plain object's or Map's values, each reached in turn. */
667
+ type DotReachOf<T, D extends number> = T extends readonly (infer E)[]
668
+ ? DotReach<E, ObjectDepth[D]>
669
+ : T extends ReadonlyMap<unknown, infer V>
670
+ ? DotReach<V, ObjectDepth[D]>
671
+ : T extends NonObjectItems | Date | RegExp | Promise<unknown>
672
+ ? T
673
+ : T extends object
674
+ ? [keyof T] extends [never]
675
+ ? unknown
676
+ : DotReach<ObjectValue<T>, ObjectDepth[D]>
677
+ : T;
678
+
679
+ /** collapse reads a Collection-like item through all(), as Arr::collapse unwraps a Collection. */
680
+ type CollapseItem<V> = V extends { all: (...args: never[]) => infer R } ? R : V;
681
+
682
+ /**
683
+ * The items whose own entries collapse copies; a Map, Set, Date, RegExp, Promise or scalar is skipped. The runtime
684
+ * skips a class instance too, as PHP skips an object, but a type can't tell one from a plain object.
685
+ */
686
+ type CollapseEntries<V> = Extract<
687
+ Exclude<V, NonObjectItems | Date | RegExp | Promise<unknown>>,
688
+ object
689
+ >;
690
+
691
+ /** An empty object fits Pick<I, K> only when K is optional in I; distributing checks each shape I may take. */
692
+ type CollapseRequired<I, K extends PropertyKey> = I extends unknown
693
+ ? Record<never, never> extends Pick<I, K & keyof I>
694
+ ? false
695
+ : true
696
+ : never;
697
+
698
+ /** A key is certain only when every shape I may take requires it; a Date, Map or scalar among them adds nothing. */
699
+ type CollapseAlwaysKeys<I> = [I] extends [CollapseEntries<I>]
700
+ ? {
701
+ [K in keyof I]-?: false extends CollapseRequired<I, K> ? never : K;
702
+ }[keyof I]
703
+ : never;
704
+
705
+ /** Only an item under a declared, required key of T is sure to be merged; an index signature may hold none. */
706
+ type CollapseGuaranteed<T> = {
707
+ [P in keyof T]-?: string extends P
708
+ ? never
709
+ : number extends P
710
+ ? never
711
+ : Record<never, never> extends Pick<T, P>
712
+ ? never
713
+ : CollapseAlwaysKeys<CollapseItem<T[P]>>;
714
+ }[keyof T];
715
+
716
+ /** Object.entries skips symbol keys, so collapse never copies one. */
717
+ type CollapseKeys<U> = U extends unknown ? Exclude<keyof U, symbol> : never;
718
+
719
+ /** The last item holding a key wins it, and a union has no order, so the key may hold any of their values. */
720
+ type CollapseValue<U, K extends PropertyKey> = U extends unknown
721
+ ? K extends keyof U
722
+ ? Required<U>[K]
723
+ : never
724
+ : never;
725
+
726
+ /** A string index signature swallows the literal keys beside it, so that result holds any item's value at any key. */
727
+ type CollapseMerge<U, G> =
728
+ string extends CollapseKeys<U>
729
+ ? Record<string, CollapseAnyValue<U>>
730
+ : Simplify<
731
+ { [K in Extract<CollapseKeys<U>, G>]: CollapseValue<U, K> } & {
732
+ [K in Exclude<CollapseKeys<U>, G>]?: CollapseValue<U, K>;
733
+ }
734
+ >;
735
+
736
+ /** Any value any item holds at any of its keys. */
737
+ type CollapseAnyValue<U> = U extends unknown
738
+ ? Required<U>[CollapseKeys<U> & keyof U]
739
+ : never;
740
+
741
+ /**
742
+ * The object `collapse()` merges an object's (or a list's) items into, as `array_merge` does: every key an item may
743
+ * have, required only where an item under a required key must have it, and integer keys renumbered. A list among the
744
+ * items appends under integer keys, so that result holds any key and value.
745
+ *
746
+ * @example
747
+ * CollapsedObject<{ a: { x: 1 }; b: { y: "s" } }> // { x: 1; y: "s" }
748
+ * CollapsedObject<Record<number, { a: number }>> // { a?: number }
749
+ * CollapsedObject<{ a: number[]; b: { y: "s" } }> // Record<string | number, unknown>
750
+ */
751
+ export type CollapsedObject<T> = [
752
+ Extract<CollapseItem<ObjectValue<T>>, readonly unknown[]>,
753
+ ] extends [never]
754
+ ? ReindexedObject<
755
+ CollapseMerge<
756
+ CollapseEntries<CollapseItem<ObjectValue<T>>>,
757
+ CollapseGuaranteed<T>
758
+ >
759
+ >
760
+ : Record<string | number, unknown>;
761
+
762
+ /**
763
+ * One row's items, as `mapSpread` reads it: a plain object or a Map stands for a PHP array, whose values spread, and
764
+ * a null row gives none; an unknown or keyless object row may hold any. A type cannot tell a class instance from a
765
+ * plain object, so an instance's fields spread and a plain object's `all` member unwraps, both unlike the runtime.
766
+ */
767
+ type SpreadRowItems<V> = unknown extends V
768
+ ? unknown[]
769
+ : V extends null
770
+ ? []
771
+ : V extends readonly unknown[]
772
+ ? V
773
+ : V extends (...args: never[]) => unknown
774
+ ? [V]
775
+ : V extends { all: (...args: never[]) => infer R }
776
+ ? SpreadRowItems<R>
777
+ : V extends ReadonlyMap<unknown, infer M>
778
+ ? M[]
779
+ : V extends
780
+ | ReadonlySet<unknown>
781
+ | WeakMap<object, unknown>
782
+ | WeakSet<object>
783
+ | Date
784
+ | RegExp
785
+ | Promise<unknown>
786
+ ? [V]
787
+ : V extends object
788
+ ? [keyof V] extends [never]
789
+ ? unknown[]
790
+ : ObjectValue<V>[]
791
+ : [V];
792
+
793
+ /** Rows of one fixed length zip into one tuple; rows of differing or open length give `false`. */
794
+ type SpreadZip<
795
+ S extends readonly unknown[],
796
+ A extends unknown[] = [],
797
+ > = S["length"] extends A["length"]
798
+ ? A
799
+ : A["length"] extends S["length"]
800
+ ? false
801
+ : SpreadZip<S, [...A, S[A["length"]]]>;
802
+
803
+ /**
804
+ * The arguments `mapSpread` and `eachSpread` hand their callback for one row: the row's items, then its key.
805
+ * Rows of one fixed length spread as a tuple, so a callback may leave off the key; otherwise the key's position
806
+ * varies, so every argument may be any item or the key.
807
+ *
808
+ * @example
809
+ * SpreadArgs<[number, string], "x"> // [number, string, "x"]
810
+ * SpreadArgs<number[], number> // number[]
811
+ * SpreadArgs<{ all(): string[] }, 0> // (string | 0)[]
812
+ */
813
+ export type SpreadArgs<TRow, TKey> =
814
+ SpreadRowItems<TRow> extends infer S extends readonly unknown[]
815
+ ? SpreadZip<S> extends infer Z extends unknown[]
816
+ ? [...Z, TKey]
817
+ : (S[number] | TKey)[]
818
+ : never;
819
+
588
820
  /**
589
821
  * The object members of a resolved type, falling back to a loose record when
590
822
  * it has none — `objectItem` throws for a non-object at runtime.
@@ -1,20 +1,35 @@
1
+ /**
2
+ * What Laravel's `simplePaginate()` paginator sends.
3
+ *
4
+ * @see https://laravel.com/docs/master/pagination#converting-results-to-json
5
+ */
1
6
  export interface SimplePaginator<T> {
2
7
  current_page: number;
8
+ /** Sent from Laravel 12.15. */
9
+ current_page_url?: string;
3
10
  data: T[];
4
11
  first_page_url: string;
5
- from: number;
12
+ /** `null` on a page with no items. */
13
+ from: number | null;
6
14
  next_page_url: string | null;
7
15
  path: string;
8
16
  per_page: number;
9
17
  prev_page_url: string | null;
10
- to: number;
18
+ /** `null` on a page with no items. */
19
+ to: number | null;
11
20
  }
12
21
 
22
+ /**
23
+ * What Laravel's `paginate()` paginator sends.
24
+ *
25
+ * @see https://laravel.com/docs/master/pagination#converting-results-to-json
26
+ */
13
27
  export interface LengthAwarePaginator<T> {
14
28
  current_page: number;
15
29
  data: T[];
16
30
  first_page_url: string;
17
- from: number;
31
+ /** `null` on a page with no items. */
32
+ from: number | null;
18
33
  last_page: number;
19
34
  last_page_url: string;
20
35
  links: PaginatorLink[];
@@ -22,10 +37,16 @@ export interface LengthAwarePaginator<T> {
22
37
  path: string;
23
38
  per_page: number;
24
39
  prev_page_url: string | null;
25
- to: number;
40
+ /** `null` on a page with no items. */
41
+ to: number | null;
26
42
  total: number;
27
43
  }
28
44
 
45
+ /**
46
+ * What Laravel's `cursorPaginate()` paginator sends.
47
+ *
48
+ * @see https://laravel.com/docs/master/pagination#converting-results-to-json
49
+ */
29
50
  export interface CursorPaginator<T> {
30
51
  data: T[];
31
52
  path: string;
@@ -36,8 +57,13 @@ export interface CursorPaginator<T> {
36
57
  prev_page_url: string | null;
37
58
  }
38
59
 
60
+ /**
61
+ * One entry of a length-aware paginator's `links` list.
62
+ */
39
63
  export interface PaginatorLink {
40
64
  url: string | null;
41
65
  label: string;
66
+ /** The page the link points to. Sent from Laravel 12.23, and never on the `...` separator. */
67
+ page?: number | null;
42
68
  active: boolean;
43
69
  }
@@ -28,15 +28,27 @@ export type UnwrapFn<T> = T extends (...args: unknown[]) => infer R ? R : T;
28
28
  * For arrays: numeric keys resolve to the element type.
29
29
  * For objects: string keys resolve to the property type.
30
30
  * For tuples: specific numeric keys resolve to the element at that position.
31
+ * For Maps: any key resolves to the value type or `undefined`, since a Map may not hold it.
31
32
  *
32
33
  * @example
33
34
  * ```ts
34
35
  * type A = ResolveKey<string[], 0>; // string
35
36
  * type B = ResolveKey<{ name: string; age: number }, "name">; // string
36
37
  * type C = ResolveKey<[string, number], 1>; // number
38
+ * type D = ResolveKey<Map<string, number>, "size">; // number | undefined
37
39
  * ```
38
40
  */
39
- export type ResolveKey<T, K> =
41
+ export type ResolveKey<T, K> = [T] extends [
42
+ Exclude<T, ReadonlyMap<unknown, unknown>>,
43
+ ]
44
+ ? ResolveOwnKey<T, K>
45
+ : // A Map stands for a PHP array, so its entries are its keys and `size` or a method never is.
46
+ T extends ReadonlyMap<unknown, infer V>
47
+ ? V | undefined
48
+ : ResolveOwnKey<T, K>;
49
+
50
+ /** {@link ResolveKey} for a value that is not a Map: a property, a list element or a tuple member. */
51
+ type ResolveOwnKey<T, K> =
40
52
  // If K is directly a key of T (handles objects and tuple numeric indices)
41
53
  K extends keyof T
42
54
  ? T[K]
@@ -372,7 +384,7 @@ type ObjectIndexStep<T, K extends string> = K extends keyof T
372
384
  : [never, true];
373
385
 
374
386
  /**
375
- * Resolves one path segment to `[value, mayBeMissing]`. `get()` walks only objects and lists, so a scalar or function
387
+ * Resolves one path segment to `[value, mayBeMissing]`. `get()` walks objects, lists and Maps, so a scalar or function
376
388
  * ends the path; a built-in object's members are mostly on its prototype, which `Object.hasOwn` doesn't see.
377
389
  */
378
390
  type ObjectPathStep<T, K extends string> = T extends readonly unknown[]
@@ -381,26 +393,27 @@ type ObjectPathStep<T, K extends string> = T extends readonly unknown[]
381
393
  ? [T[N], false]
382
394
  : [T[number], true]
383
395
  : [never, true]
384
- : T extends
385
- | string
386
- | number
387
- | boolean
388
- | bigint
389
- | symbol
390
- | ((...args: never[]) => unknown)
391
- ? [never, true]
396
+ : T extends ReadonlyMap<unknown, infer V>
397
+ ? [V, true]
392
398
  : T extends
393
- | Date
394
- | RegExp
395
- | Promise<unknown>
396
- | ReadonlyMap<unknown, unknown>
397
- | ReadonlySet<unknown>
398
- | WeakMap<object, unknown>
399
- | WeakSet<object>
400
- ? [T[K & keyof T], true]
401
- : IsBareObject<T> extends true
402
- ? [unknown, true]
403
- : ObjectKeyStep<T, K>;
399
+ | string
400
+ | number
401
+ | boolean
402
+ | bigint
403
+ | symbol
404
+ | ((...args: never[]) => unknown)
405
+ ? [never, true]
406
+ : T extends
407
+ | Date
408
+ | RegExp
409
+ | Promise<unknown>
410
+ | ReadonlySet<unknown>
411
+ | WeakMap<object, unknown>
412
+ | WeakSet<object>
413
+ ? [T[K & keyof T], true]
414
+ : IsBareObject<T> extends true
415
+ ? [unknown, true]
416
+ : ObjectKeyStep<T, K>;
404
417
 
405
418
  /** One segment through an object with declared keys or index signatures. */
406
419
  type ObjectKeyStep<T, K extends string> =
@@ -468,17 +481,20 @@ type ObjectPathTop<T, P> = T extends unknown
468
481
 
469
482
  /** Whether `get()` reads dotted path `P` as one literal key of `T`: always, never, or maybe (then it walks). */
470
483
  type ObjectPathLiteral<T, P extends string> =
471
- P extends KnownObjectKeys<T>
472
- ? Record<never, never> extends Pick<T, P & keyof T>
484
+ // get() tries a dotted key on a Map before walking it, and a Map's type never promises which keys it holds.
485
+ T extends ReadonlyMap<unknown, unknown>
486
+ ? boolean
487
+ : P extends KnownObjectKeys<T>
488
+ ? Record<never, never> extends Pick<T, P & keyof T>
489
+ ? boolean
490
+ : true
491
+ : string extends keyof T
473
492
  ? boolean
474
- : true
475
- : string extends keyof T
476
- ? boolean
477
- : P extends `${number}`
478
- ? number extends keyof T
479
- ? boolean
480
- : false
481
- : false;
493
+ : P extends `${number}`
494
+ ? number extends keyof T
495
+ ? boolean
496
+ : false
497
+ : false;
482
498
 
483
499
  /**
484
500
  * Every value a dot path through `T` can reach, to a bounded depth. Used when
@@ -494,20 +510,24 @@ export type ObjectPathValue<T, D extends number = 5> = [D] extends [never]
494
510
  ?
495
511
  | Exclude<E, undefined>
496
512
  | ObjectPathValue<NonNullable<E>, ObjectPathDepth[D]>
497
- : T extends (...args: never[]) => unknown
498
- ? never
499
- : T extends object
500
- ? [keyof T] extends [never]
501
- ? unknown
502
- : {
503
- [K in keyof T]-?:
504
- | Exclude<T[K], undefined>
505
- | ObjectPathValue<
506
- NonNullable<T[K]>,
507
- ObjectPathDepth[D]
508
- >;
509
- }[keyof T]
510
- : never;
513
+ : T extends ReadonlyMap<unknown, infer V>
514
+ ?
515
+ | Exclude<V, undefined>
516
+ | ObjectPathValue<NonNullable<V>, ObjectPathDepth[D]>
517
+ : T extends (...args: never[]) => unknown
518
+ ? never
519
+ : T extends object
520
+ ? [keyof T] extends [never]
521
+ ? unknown
522
+ : {
523
+ [K in keyof T]-?:
524
+ | Exclude<T[K], undefined>
525
+ | ObjectPathValue<
526
+ NonNullable<T[K]>,
527
+ ObjectPathDepth[D]
528
+ >;
529
+ }[keyof T]
530
+ : never;
511
531
 
512
532
  /**
513
533
  * Resolves the value at a dot path within an object, adding `TDefault`
@@ -1,23 +1,56 @@
1
1
  import type { PaginatorLink } from "./pagination";
2
2
 
3
3
  /**
4
- * Interface representing the pagination structure of a JSON resource collection response, following Laravel's default JSON Resource format.
4
+ * The `meta` block of a resource collection paginated with `paginate()`.
5
5
  *
6
6
  * @see https://laravel.com/docs/master/eloquent-resources#pagination
7
7
  */
8
8
  export interface JsonResourceMeta {
9
9
  current_page: number;
10
- from: number;
10
+ /** `null` on a page with no items. */
11
+ from: number | null;
11
12
  last_page: number;
12
13
  links: PaginatorLink[];
13
14
  path: string;
14
15
  per_page: number;
15
- to: number;
16
+ /** `null` on a page with no items. */
17
+ to: number | null;
16
18
  total: number;
17
19
  }
18
20
 
19
21
  /**
20
- * Interface representing the structure of a JSON resource collection response with pagination links, following Laravel's default JSON Resource format.
22
+ * The `meta` block of a resource collection paginated with `simplePaginate()`.
23
+ *
24
+ * @see https://laravel.com/docs/master/eloquent-resources#pagination
25
+ */
26
+ export interface JsonResourceSimpleMeta {
27
+ current_page: number;
28
+ /** Sent from Laravel 12.15. */
29
+ current_page_url?: string;
30
+ /** `null` on a page with no items. */
31
+ from: number | null;
32
+ path: string;
33
+ per_page: number;
34
+ /** `null` on a page with no items. */
35
+ to: number | null;
36
+ }
37
+
38
+ /**
39
+ * The `meta` block of a resource collection paginated with `cursorPaginate()`.
40
+ *
41
+ * @see https://laravel.com/docs/master/eloquent-resources#pagination
42
+ */
43
+ export interface JsonResourceCursorMeta {
44
+ path: string;
45
+ per_page: number;
46
+ next_cursor: string | null;
47
+ prev_cursor: string | null;
48
+ }
49
+
50
+ /**
51
+ * The `links` block of a paginated resource collection, following Laravel's default JSON Resource format.
52
+ *
53
+ * `last` is always `null` over `simplePaginate()`, and `first` and `last` are always `null` over `cursorPaginate()`.
21
54
  */
22
55
  export interface JsonResourceLinks {
23
56
  first: string | null;
@@ -27,7 +60,7 @@ export interface JsonResourceLinks {
27
60
  }
28
61
 
29
62
  /**
30
- * Interface representing a paginated JSON resource collection response, following Laravel's default JSON Resource format.
63
+ * A resource collection paginated with `paginate()`, following Laravel's default JSON Resource format.
31
64
  *
32
65
  * This structure includes the paginated data array, along with the pagination metadata and links as defined by Laravel's default JSON Resource responses.
33
66
  */
@@ -37,6 +70,24 @@ export interface JsonResourcePaginator<T> {
37
70
  links: JsonResourceLinks;
38
71
  }
39
72
 
73
+ /**
74
+ * A resource collection paginated with `simplePaginate()`.
75
+ */
76
+ export interface JsonResourceSimplePaginator<
77
+ T,
78
+ > extends ResourceSimplePagination {
79
+ data: T[];
80
+ }
81
+
82
+ /**
83
+ * A resource collection paginated with `cursorPaginate()`.
84
+ */
85
+ export interface JsonResourceCursorPaginator<
86
+ T,
87
+ > extends ResourceCursorPagination {
88
+ data: T[];
89
+ }
90
+
40
91
  /**
41
92
  * Interface representing an anonymous JSON resource collection response, following Laravel's default JSON Resource format.
42
93
  */
@@ -45,7 +96,7 @@ export interface AnonymousResourceCollection<T> {
45
96
  }
46
97
 
47
98
  /**
48
- * Interface representing a paginated JSON resource collection response with pagination metadata and links, following Laravel's default JSON Resource format.
99
+ * The `meta` and `links` blocks Laravel adds to a resource collection paginated with `paginate()`.
49
100
  *
50
101
  * This interface is meant to extend basic collection resources with pagination information.
51
102
  */
@@ -53,3 +104,23 @@ export interface ResourcePagination {
53
104
  meta: JsonResourceMeta;
54
105
  links: JsonResourceLinks;
55
106
  }
107
+
108
+ /**
109
+ * The `meta` and `links` blocks Laravel adds to a resource collection paginated with `simplePaginate()`.
110
+ *
111
+ * Like `ResourcePagination`, it is meant to extend a collection's own interface.
112
+ */
113
+ export interface ResourceSimplePagination {
114
+ meta: JsonResourceSimpleMeta;
115
+ links: JsonResourceLinks;
116
+ }
117
+
118
+ /**
119
+ * The `meta` and `links` blocks Laravel adds to a resource collection paginated with `cursorPaginate()`.
120
+ *
121
+ * Like `ResourcePagination`, it is meant to extend a collection's own interface.
122
+ */
123
+ export interface ResourceCursorPagination {
124
+ meta: JsonResourceCursorMeta;
125
+ links: JsonResourceLinks;
126
+ }