@tolki/types 1.8.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 +84 -0
- package/package.json +1 -1
- package/src/index.d.ts +1 -0
- package/src/json-api.d.ts +149 -0
- package/src/objects.d.ts +65 -12
- package/src/pagination.d.ts +30 -4
- package/src/path-resolve.d.ts +65 -45
- package/src/resource-responses.d.ts +77 -6
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
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>
|
|
534
|
-
* SetObjectPath<{ a: { x: 1 } }, "a.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]:
|
|
542
|
-
Head
|
|
543
|
-
|
|
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.
|
|
@@ -623,6 +651,31 @@ type FlattenReachOf<T, D extends number> = T extends readonly (infer E)[]
|
|
|
623
651
|
: FlattenReach<ObjectValue<T>, ObjectDepth[D]>
|
|
624
652
|
: T;
|
|
625
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
|
+
|
|
626
679
|
/** collapse reads a Collection-like item through all(), as Arr::collapse unwraps a Collection. */
|
|
627
680
|
type CollapseItem<V> = V extends { all: (...args: never[]) => infer R } ? R : V;
|
|
628
681
|
|
package/src/pagination.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
package/src/path-resolve.d.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
-
|
|
|
394
|
-
|
|
|
395
|
-
|
|
|
396
|
-
|
|
|
397
|
-
|
|
|
398
|
-
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
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
|
-
|
|
472
|
-
|
|
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
|
-
:
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
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
|
|
498
|
-
?
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
16
|
+
/** `null` on a page with no items. */
|
|
17
|
+
to: number | null;
|
|
16
18
|
total: number;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
+
}
|