@tolki/types 1.8.0 → 1.10.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 +4 -1
- package/src/index.d.ts +2 -0
- package/src/json-api.d.ts +149 -0
- package/src/lang.d.ts +256 -0
- package/src/objects.d.ts +65 -12
- package/src/pagination.d.ts +30 -4
- package/src/path-resolve.d.ts +191 -48
- 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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tolki/types",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.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",
|
|
@@ -42,6 +42,9 @@
|
|
|
42
42
|
"./data": {
|
|
43
43
|
"types": "./src/data.d.ts"
|
|
44
44
|
},
|
|
45
|
+
"./lang": {
|
|
46
|
+
"types": "./src/lang.d.ts"
|
|
47
|
+
},
|
|
45
48
|
"./models": {
|
|
46
49
|
"types": "./src/models.d.ts"
|
|
47
50
|
},
|
package/src/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ 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";
|
|
6
|
+
export type * from "./lang";
|
|
5
7
|
export type * from "./models";
|
|
6
8
|
export type * from "./objects";
|
|
7
9
|
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/lang.d.ts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import type { Countable } from "./data";
|
|
2
|
+
import type {
|
|
3
|
+
ObjectDotPath,
|
|
4
|
+
ObjectDotPathValue,
|
|
5
|
+
ObjectResolvePath,
|
|
6
|
+
} from "./path-resolve";
|
|
7
|
+
|
|
8
|
+
/** A tag callback receives the text between `<key>` and `</key>`. */
|
|
9
|
+
type TagCallback = (children: string) => unknown;
|
|
10
|
+
|
|
11
|
+
/** A value a placeholder can take; what PHP cannot cast to a string throws. */
|
|
12
|
+
type ReplacementValue =
|
|
13
|
+
| string
|
|
14
|
+
| number
|
|
15
|
+
| boolean
|
|
16
|
+
| null
|
|
17
|
+
| undefined
|
|
18
|
+
| object
|
|
19
|
+
| TagCallback;
|
|
20
|
+
|
|
21
|
+
/** Placeholder values keyed by name, or a list keyed by position. */
|
|
22
|
+
type Replacements =
|
|
23
|
+
| Readonly<Record<string, ReplacementValue>>
|
|
24
|
+
| readonly ReplacementValue[];
|
|
25
|
+
|
|
26
|
+
/** Any class, so a handler can be registered for it. */
|
|
27
|
+
type StringableType<TValue = unknown> = abstract new (
|
|
28
|
+
...args: never[]
|
|
29
|
+
) => TValue;
|
|
30
|
+
|
|
31
|
+
/** Handlers that turn an instance of the exact class into a replacement. */
|
|
32
|
+
type StringableHandlers = ReadonlyMap<
|
|
33
|
+
StringableType,
|
|
34
|
+
(value: never) => unknown
|
|
35
|
+
>;
|
|
36
|
+
|
|
37
|
+
/** A group of lines, as a PHP lang file's array holds them. */
|
|
38
|
+
type LangGroup = { [key: string]: LangValue } | LangValue[];
|
|
39
|
+
|
|
40
|
+
/** A line, or a group of lines. */
|
|
41
|
+
type LangValue = string | LangGroup;
|
|
42
|
+
|
|
43
|
+
/** A line, or a group of lines, as the caller hands them over; the translator never writes to them. */
|
|
44
|
+
type LangInputValue =
|
|
45
|
+
| string
|
|
46
|
+
| { readonly [key: string]: LangInputValue }
|
|
47
|
+
| readonly LangInputValue[];
|
|
48
|
+
|
|
49
|
+
/** One locale's lines: a group under its file's name (`ns::group` for a package), any other value a JSON line. */
|
|
50
|
+
type LocaleLines = Readonly<Record<string, LangInputValue>>;
|
|
51
|
+
|
|
52
|
+
/** Every locale's lines, keyed by locale. */
|
|
53
|
+
type LangLines = Readonly<Record<string, LocaleLines>>;
|
|
54
|
+
|
|
55
|
+
/** What `get()` answers: a line, or a copy of a group. */
|
|
56
|
+
type TranslationValue = string | LangGroup;
|
|
57
|
+
|
|
58
|
+
/** Whether some locale's keys are typed only as `string`, so the lines name no key in particular. */
|
|
59
|
+
type HasUntypedLocale<TLines> = true extends {
|
|
60
|
+
[TLocale in keyof TLines]: string extends keyof TLines[TLocale]
|
|
61
|
+
? true
|
|
62
|
+
: false;
|
|
63
|
+
}[keyof TLines]
|
|
64
|
+
? true
|
|
65
|
+
: false;
|
|
66
|
+
|
|
67
|
+
/** The depth left one level further down. */
|
|
68
|
+
type NextDepth = [never, 0, 1, 2, 3, 4, 5, 6, 7];
|
|
69
|
+
|
|
70
|
+
/** Each locale's keys to a `TValue`: JSON lines, groups, and paths at most 4 levels below a group. */
|
|
71
|
+
type LocalePaths<TLines extends LangLines, TValue> = {
|
|
72
|
+
[TLocale in keyof TLines]: ObjectDotPath<TLines[TLocale], TValue, 5>;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** Every key the lines name: each locale's JSON lines and groups, and the paths at most 4 levels below a group. */
|
|
76
|
+
// Indexed here rather than inside LocalePaths: a key union returned through an alias is far slower to check.
|
|
77
|
+
type TranslationKey<TLines extends LangLines> =
|
|
78
|
+
HasUntypedLocale<TLines> extends true
|
|
79
|
+
? string
|
|
80
|
+
: LocalePaths<TLines, unknown>[keyof TLines];
|
|
81
|
+
|
|
82
|
+
/** A key without a namespace split at its first dot into the group and the item, null for a whole group. */
|
|
83
|
+
type BasicSegments<TKey extends string> =
|
|
84
|
+
TKey extends `${infer TGroup}.${infer TItem}`
|
|
85
|
+
? [TGroup, TItem]
|
|
86
|
+
: [TKey, null];
|
|
87
|
+
|
|
88
|
+
/** A key split as `parseKey()` splits it: the namespace (`*` for none), the group and the item. */
|
|
89
|
+
type ParsedKey<TKey extends string> =
|
|
90
|
+
TKey extends `${infer TNamespace}::${infer TRest}`
|
|
91
|
+
? [
|
|
92
|
+
TNamespace,
|
|
93
|
+
...BasicSegments<
|
|
94
|
+
TRest extends `${infer TFirst}::${string}` ? TFirst : TRest
|
|
95
|
+
>,
|
|
96
|
+
]
|
|
97
|
+
: ["*", ...BasicSegments<TKey>];
|
|
98
|
+
|
|
99
|
+
/** The lines and groups a key reaches in every locale's groups; a JSON line is a string, which every answer holds. */
|
|
100
|
+
type LocaleValues<
|
|
101
|
+
TLines extends LangLines,
|
|
102
|
+
TName extends string,
|
|
103
|
+
TItem extends string | null,
|
|
104
|
+
> = {
|
|
105
|
+
[TLocale in keyof TLines]: TName extends keyof TLines[TLocale]
|
|
106
|
+
? ReachedValue<
|
|
107
|
+
GroupItem<Exclude<TLines[TLocale][TName], string>, TItem>
|
|
108
|
+
>
|
|
109
|
+
: never;
|
|
110
|
+
}[keyof TLines];
|
|
111
|
+
|
|
112
|
+
/** A group's item, or the whole group for none; only a pattern's paths, such as a list's `${number}`, are templates. */
|
|
113
|
+
// Distributed so a JSON line, which leaves no group, reaches nothing: ObjectResolvePath reads `never` as unknown.
|
|
114
|
+
type GroupItem<TGroup, TItem extends string | null> = TGroup extends unknown
|
|
115
|
+
? TItem extends string
|
|
116
|
+
? Record<never, never> extends Record<TItem, unknown>
|
|
117
|
+
? ObjectDotPathValue<TGroup, TItem>
|
|
118
|
+
: ObjectResolvePath<TGroup, TItem, never>
|
|
119
|
+
: TGroup
|
|
120
|
+
: never;
|
|
121
|
+
|
|
122
|
+
/** PHP skips an empty group, so `get()` answers the key for it. */
|
|
123
|
+
// An unknown value stands for any line or group, which keyof alone would read as an empty group.
|
|
124
|
+
type ReachedValue<TValue> = unknown extends TValue
|
|
125
|
+
? TranslationValue
|
|
126
|
+
: TValue extends string
|
|
127
|
+
? TValue
|
|
128
|
+
: TValue extends readonly never[]
|
|
129
|
+
? string
|
|
130
|
+
: [keyof TValue] extends [never]
|
|
131
|
+
? string
|
|
132
|
+
: TValue;
|
|
133
|
+
|
|
134
|
+
/** A value as `get()` copies it, every line a string once replaced; a loosely typed group copies as a `LangGroup`. */
|
|
135
|
+
// A mapped type over a list expands at once, so a recursive type such as LangValue needs the guards and the depth.
|
|
136
|
+
type Translated<TValue, TDepth extends number = 8> = TValue extends string
|
|
137
|
+
? string
|
|
138
|
+
: TDepth extends 0
|
|
139
|
+
? TValue
|
|
140
|
+
: { [key: string]: LangValue } extends TValue
|
|
141
|
+
? string extends keyof TValue
|
|
142
|
+
? { [key: string]: LangValue }
|
|
143
|
+
: TValue
|
|
144
|
+
: LangValue[] extends TValue
|
|
145
|
+
? LangValue[]
|
|
146
|
+
: {
|
|
147
|
+
-readonly [TKey in keyof TValue]: Translated<
|
|
148
|
+
TValue[TKey],
|
|
149
|
+
NextDepth[TDepth]
|
|
150
|
+
>;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/** The groups a key reaches, with their lines, or the key itself, which a locale without them answers. */
|
|
154
|
+
type FoundResult<TValues> = Translated<TValues> | string;
|
|
155
|
+
|
|
156
|
+
/** What `get()` answers for a key spelled out in full. */
|
|
157
|
+
type LiteralKeyResult<TLines extends LangLines, TKey extends string> =
|
|
158
|
+
ParsedKey<TKey> extends [
|
|
159
|
+
infer TNamespace extends string,
|
|
160
|
+
infer TGroup extends string,
|
|
161
|
+
infer TItem extends string | null,
|
|
162
|
+
]
|
|
163
|
+
? TNamespace extends "*"
|
|
164
|
+
? TGroup extends "*"
|
|
165
|
+
? TItem extends null
|
|
166
|
+
? LangGroup | string
|
|
167
|
+
: string
|
|
168
|
+
: FoundResult<LocaleValues<TLines, TGroup, TItem>>
|
|
169
|
+
: FoundResult<
|
|
170
|
+
LocaleValues<TLines, `${TNamespace}::${TGroup}`, TItem>
|
|
171
|
+
>
|
|
172
|
+
: never;
|
|
173
|
+
|
|
174
|
+
/** What `get()` answers for each of several keys spelled out in full. */
|
|
175
|
+
// KeyResult must not call itself: for a generic key the Extract never resolves, and the recursion hits TS2589.
|
|
176
|
+
type LiteralKeysResult<
|
|
177
|
+
TLines extends LangLines,
|
|
178
|
+
TKeys extends string,
|
|
179
|
+
> = TKeys extends string ? LiteralKeyResult<TLines, TKeys> : never;
|
|
180
|
+
|
|
181
|
+
/** What `get()` answers for each key in a union; a pattern answers the key itself, or any known group it matches. */
|
|
182
|
+
// Only a pattern such as `a.${string}` makes Record<TKey, unknown> an index signature, which an empty object satisfies.
|
|
183
|
+
type KeyResult<
|
|
184
|
+
TLines extends LangLines,
|
|
185
|
+
TKey extends string,
|
|
186
|
+
> = TKey extends string
|
|
187
|
+
? Record<never, never> extends Record<TKey, unknown>
|
|
188
|
+
?
|
|
189
|
+
| string
|
|
190
|
+
| LiteralKeysResult<
|
|
191
|
+
TLines,
|
|
192
|
+
Extract<LocalePaths<TLines, object>[keyof TLines], TKey>
|
|
193
|
+
>
|
|
194
|
+
: LiteralKeyResult<TLines, TKey>
|
|
195
|
+
: never;
|
|
196
|
+
|
|
197
|
+
/** What `get()` answers: the group a key names in any locale or the key, otherwise a string; either if untyped. */
|
|
198
|
+
type TranslationResult<
|
|
199
|
+
TLines extends LangLines,
|
|
200
|
+
TKey extends string,
|
|
201
|
+
> = string extends TKey
|
|
202
|
+
? TranslationValue
|
|
203
|
+
: HasUntypedLocale<TLines> extends true
|
|
204
|
+
? TranslationValue
|
|
205
|
+
: KeyResult<TLines, TKey>;
|
|
206
|
+
|
|
207
|
+
/** What `array()` answers for a key: the group it names, or any group when it names none. */
|
|
208
|
+
type TranslationArray<TLines extends LangLines, TKey extends string> = [
|
|
209
|
+
Extract<TranslationResult<TLines, TKey>, LangGroup>,
|
|
210
|
+
] extends [never]
|
|
211
|
+
? LangGroup
|
|
212
|
+
: Extract<TranslationResult<TLines, TKey>, LangGroup>;
|
|
213
|
+
|
|
214
|
+
/** Answers the line for a key no locale has; `null` or `undefined` keeps the key. */
|
|
215
|
+
type MissingKeyHandler = (
|
|
216
|
+
key: string,
|
|
217
|
+
replace: Replacements,
|
|
218
|
+
locale: string,
|
|
219
|
+
fallback: boolean,
|
|
220
|
+
) => string | null | undefined;
|
|
221
|
+
|
|
222
|
+
/** Turns the locales a lookup would read into the ones it reads. */
|
|
223
|
+
type LocalesResolver = (locales: string[]) => string[];
|
|
224
|
+
|
|
225
|
+
/** What PHP's `count()` accepts: an array, a `Countable`, or here a `Map`, a `Set` or an object. */
|
|
226
|
+
type LangCountable =
|
|
227
|
+
| readonly unknown[]
|
|
228
|
+
| ReadonlyMap<unknown, unknown>
|
|
229
|
+
| ReadonlySet<unknown>
|
|
230
|
+
| Countable
|
|
231
|
+
| Readonly<Record<string, unknown>>;
|
|
232
|
+
|
|
233
|
+
/** A number, or a value whose count is the number. */
|
|
234
|
+
type ChoiceNumber = number | LangCountable;
|
|
235
|
+
|
|
236
|
+
// An export list keeps the helpers above private: a .d.ts module without one exports every declaration.
|
|
237
|
+
export type {
|
|
238
|
+
ChoiceNumber,
|
|
239
|
+
LangCountable,
|
|
240
|
+
LangGroup,
|
|
241
|
+
LangInputValue,
|
|
242
|
+
LangLines,
|
|
243
|
+
LangValue,
|
|
244
|
+
LocaleLines,
|
|
245
|
+
LocalesResolver,
|
|
246
|
+
MissingKeyHandler,
|
|
247
|
+
Replacements,
|
|
248
|
+
ReplacementValue,
|
|
249
|
+
StringableHandlers,
|
|
250
|
+
StringableType,
|
|
251
|
+
TagCallback,
|
|
252
|
+
TranslationArray,
|
|
253
|
+
TranslationKey,
|
|
254
|
+
TranslationResult,
|
|
255
|
+
TranslationValue,
|
|
256
|
+
};
|
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,35 +384,37 @@ 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[]
|
|
379
|
-
? K extends `${
|
|
380
|
-
?
|
|
381
|
-
|
|
391
|
+
? K extends `${number}`
|
|
392
|
+
? // A tuple declares its indices as string keys, so the segment matches one and its number never does.
|
|
393
|
+
K extends KnownObjectKeys<T>
|
|
394
|
+
? [T[K & keyof T], false]
|
|
382
395
|
: [T[number], true]
|
|
383
396
|
: [never, true]
|
|
384
|
-
: T extends
|
|
385
|
-
|
|
386
|
-
| number
|
|
387
|
-
| boolean
|
|
388
|
-
| bigint
|
|
389
|
-
| symbol
|
|
390
|
-
| ((...args: never[]) => unknown)
|
|
391
|
-
? [never, true]
|
|
397
|
+
: T extends ReadonlyMap<unknown, infer V>
|
|
398
|
+
? [V, true]
|
|
392
399
|
: T extends
|
|
393
|
-
|
|
|
394
|
-
|
|
|
395
|
-
|
|
|
396
|
-
|
|
|
397
|
-
|
|
|
398
|
-
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
400
|
+
| string
|
|
401
|
+
| number
|
|
402
|
+
| boolean
|
|
403
|
+
| bigint
|
|
404
|
+
| symbol
|
|
405
|
+
| ((...args: never[]) => unknown)
|
|
406
|
+
? [never, true]
|
|
407
|
+
: T extends
|
|
408
|
+
| Date
|
|
409
|
+
| RegExp
|
|
410
|
+
| Promise<unknown>
|
|
411
|
+
| ReadonlySet<unknown>
|
|
412
|
+
| WeakMap<object, unknown>
|
|
413
|
+
| WeakSet<object>
|
|
414
|
+
? [T[K & keyof T], true]
|
|
415
|
+
: IsBareObject<T> extends true
|
|
416
|
+
? [unknown, true]
|
|
417
|
+
: ObjectKeyStep<T, K>;
|
|
404
418
|
|
|
405
419
|
/** One segment through an object with declared keys or index signatures. */
|
|
406
420
|
type ObjectKeyStep<T, K extends string> =
|
|
@@ -468,17 +482,20 @@ type ObjectPathTop<T, P> = T extends unknown
|
|
|
468
482
|
|
|
469
483
|
/** Whether `get()` reads dotted path `P` as one literal key of `T`: always, never, or maybe (then it walks). */
|
|
470
484
|
type ObjectPathLiteral<T, P extends string> =
|
|
471
|
-
|
|
472
|
-
|
|
485
|
+
// get() tries a dotted key on a Map before walking it, and a Map's type never promises which keys it holds.
|
|
486
|
+
T extends ReadonlyMap<unknown, unknown>
|
|
487
|
+
? boolean
|
|
488
|
+
: P extends KnownObjectKeys<T>
|
|
489
|
+
? Record<never, never> extends Pick<T, P & keyof T>
|
|
490
|
+
? boolean
|
|
491
|
+
: true
|
|
492
|
+
: string extends keyof T
|
|
473
493
|
? boolean
|
|
474
|
-
:
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
? boolean
|
|
480
|
-
: false
|
|
481
|
-
: false;
|
|
494
|
+
: P extends `${number}`
|
|
495
|
+
? number extends keyof T
|
|
496
|
+
? boolean
|
|
497
|
+
: false
|
|
498
|
+
: false;
|
|
482
499
|
|
|
483
500
|
/**
|
|
484
501
|
* Every value a dot path through `T` can reach, to a bounded depth. Used when
|
|
@@ -494,20 +511,24 @@ export type ObjectPathValue<T, D extends number = 5> = [D] extends [never]
|
|
|
494
511
|
?
|
|
495
512
|
| Exclude<E, undefined>
|
|
496
513
|
| ObjectPathValue<NonNullable<E>, ObjectPathDepth[D]>
|
|
497
|
-
: T extends
|
|
498
|
-
?
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
514
|
+
: T extends ReadonlyMap<unknown, infer V>
|
|
515
|
+
?
|
|
516
|
+
| Exclude<V, undefined>
|
|
517
|
+
| ObjectPathValue<NonNullable<V>, ObjectPathDepth[D]>
|
|
518
|
+
: T extends (...args: never[]) => unknown
|
|
519
|
+
? never
|
|
520
|
+
: T extends object
|
|
521
|
+
? [keyof T] extends [never]
|
|
522
|
+
? unknown
|
|
523
|
+
: {
|
|
524
|
+
[K in keyof T]-?:
|
|
525
|
+
| Exclude<T[K], undefined>
|
|
526
|
+
| ObjectPathValue<
|
|
527
|
+
NonNullable<T[K]>,
|
|
528
|
+
ObjectPathDepth[D]
|
|
529
|
+
>;
|
|
530
|
+
}[keyof T]
|
|
531
|
+
: never;
|
|
511
532
|
|
|
512
533
|
/**
|
|
513
534
|
* Resolves the value at a dot path within an object, adding `TDefault`
|
|
@@ -535,3 +556,125 @@ export type ObjectResolvePath<T, P, TDefault = null> = P extends
|
|
|
535
556
|
: ObjectPathTop<T, P> extends [infer V, infer M]
|
|
536
557
|
? V | (true extends M ? TDefault : never)
|
|
537
558
|
: TDefault;
|
|
559
|
+
|
|
560
|
+
/** A depth `ObjectDotPath` can count down from; a larger one would index past `ObjectDotPathDepth` and lift the cap. */
|
|
561
|
+
type ObjectDotPathMaxDepth = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
|
|
562
|
+
|
|
563
|
+
/** The depth left one segment further down. */
|
|
564
|
+
type ObjectDotPathDepth = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
565
|
+
|
|
566
|
+
/** Whether a member of `T` can be a function, as on a DOM node, a class instance with methods or a Collection. */
|
|
567
|
+
// An `any` member is not a method, so a loosely typed group still lists its keys.
|
|
568
|
+
type ObjectHasMethod<T> = true extends {
|
|
569
|
+
[TKey in keyof T]-?: 0 extends 1 & T[TKey]
|
|
570
|
+
? false
|
|
571
|
+
: [Extract<T[TKey], (...args: never[]) => unknown>] extends [never]
|
|
572
|
+
? false
|
|
573
|
+
: true;
|
|
574
|
+
}[keyof T]
|
|
575
|
+
? true
|
|
576
|
+
: false;
|
|
577
|
+
|
|
578
|
+
/** The paths below a member: an object with a method is listed but never walked into. */
|
|
579
|
+
// Distributed, so `HTMLElement | null` ends too; a list's own methods do not count.
|
|
580
|
+
type ObjectDotPathBelow<
|
|
581
|
+
T,
|
|
582
|
+
TValue,
|
|
583
|
+
TDepth extends ObjectDotPathMaxDepth,
|
|
584
|
+
> = T extends readonly unknown[]
|
|
585
|
+
? ObjectDotPath<T, TValue, TDepth>
|
|
586
|
+
: ObjectHasMethod<T> extends true
|
|
587
|
+
? never
|
|
588
|
+
: ObjectDotPath<T, TValue, TDepth>;
|
|
589
|
+
|
|
590
|
+
/** A path, when the value under it can be a `TValue`. */
|
|
591
|
+
type ObjectDotPathTo<TPath extends string, TChild, TValue> = [
|
|
592
|
+
Extract<TChild, TValue>,
|
|
593
|
+
] extends [never]
|
|
594
|
+
? never
|
|
595
|
+
: TPath;
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Every dot path into `T`, at most `TDepth` segments deep (0 to 10), whose value can be a `TValue`. An object's paths
|
|
599
|
+
* go by its string and number keys and a list's by index. A member holding a scalar, a function, an object with a
|
|
600
|
+
* method such as a DOM node, or a `Map`, `Set`, `WeakMap`, `WeakSet`, `Date`, `RegExp` or `Promise` ends the path.
|
|
601
|
+
*
|
|
602
|
+
* @example
|
|
603
|
+
* ObjectDotPath<{ a: { b: string }; c: string }> // "a" | "a.b" | "c"
|
|
604
|
+
* ObjectDotPath<{ l: { x: string }[] }> // "l" | `l.${number}` | `l.${number}.x`
|
|
605
|
+
* ObjectDotPath<{ a: { b: string }; c: string }, object> // "a"
|
|
606
|
+
*/
|
|
607
|
+
export type ObjectDotPath<
|
|
608
|
+
T,
|
|
609
|
+
TValue = unknown,
|
|
610
|
+
TDepth extends ObjectDotPathMaxDepth = 5,
|
|
611
|
+
> = TDepth extends 0
|
|
612
|
+
? never
|
|
613
|
+
: T extends
|
|
614
|
+
| string
|
|
615
|
+
| number
|
|
616
|
+
| boolean
|
|
617
|
+
| bigint
|
|
618
|
+
| symbol
|
|
619
|
+
| ((...args: never[]) => unknown)
|
|
620
|
+
| ReadonlyMap<unknown, unknown>
|
|
621
|
+
| ReadonlySet<unknown>
|
|
622
|
+
| WeakMap<object, unknown>
|
|
623
|
+
| WeakSet<object>
|
|
624
|
+
| Date
|
|
625
|
+
| RegExp
|
|
626
|
+
| Promise<unknown>
|
|
627
|
+
? never
|
|
628
|
+
: T extends readonly (infer TItem)[]
|
|
629
|
+
?
|
|
630
|
+
| ObjectDotPathTo<`${number}`, TItem, TValue>
|
|
631
|
+
| `${number}.${ObjectDotPathBelow<TItem, TValue, ObjectDotPathDepth[TDepth]>}`
|
|
632
|
+
: T extends object
|
|
633
|
+
? {
|
|
634
|
+
[TKey in keyof T & (string | number)]:
|
|
635
|
+
| ObjectDotPathTo<`${TKey}`, T[TKey], TValue>
|
|
636
|
+
| `${TKey}.${ObjectDotPathBelow<T[TKey], TValue, ObjectDotPathDepth[TDepth]>}`;
|
|
637
|
+
}[keyof T & (string | number)]
|
|
638
|
+
: never;
|
|
639
|
+
|
|
640
|
+
/** One segment of a path `ObjectDotPath` lists: a pattern reads any member of a list, or an index signature's value. */
|
|
641
|
+
type ObjectDotPathStep<T, K extends string> =
|
|
642
|
+
Record<never, never> extends Record<K, unknown>
|
|
643
|
+
? T extends readonly unknown[]
|
|
644
|
+
? T[number]
|
|
645
|
+
: T extends object
|
|
646
|
+
? K extends `${number}`
|
|
647
|
+
? number extends keyof T
|
|
648
|
+
? T[number & keyof T]
|
|
649
|
+
: never
|
|
650
|
+
: string extends keyof T
|
|
651
|
+
? T[string & keyof T]
|
|
652
|
+
: never
|
|
653
|
+
: never
|
|
654
|
+
: ObjectPathStep<T, K>[0];
|
|
655
|
+
|
|
656
|
+
/** Walks a path `ObjectDotPath` lists, one segment at a time. */
|
|
657
|
+
type ObjectDotPathWalk<
|
|
658
|
+
T,
|
|
659
|
+
P extends string,
|
|
660
|
+
> = P extends `${infer Head}.${infer Rest}`
|
|
661
|
+
? ObjectDotPathStep<T, Head> extends infer V
|
|
662
|
+
? unknown extends V
|
|
663
|
+
? unknown
|
|
664
|
+
: ObjectDotPathWalk<NonNullable<V>, Rest>
|
|
665
|
+
: never
|
|
666
|
+
: ObjectDotPathStep<T, P>;
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* The value at a path {@link ObjectDotPath} lists, where `${number}` is any one list index and `${string}` any one key.
|
|
670
|
+
* As with `Arr::get()` and `data_get()`, a dotted key is read whole only at the top level, so `"g.a.b"`, which
|
|
671
|
+
* `ObjectDotPath` lists for `{ g: { "a.b": number } }`, reaches nothing.
|
|
672
|
+
*
|
|
673
|
+
* @example
|
|
674
|
+
* ObjectDotPathValue<{ l: { x: string }[] }, `l.${number}`> // { x: string }
|
|
675
|
+
*/
|
|
676
|
+
export type ObjectDotPathValue<T, P extends string> = P extends string
|
|
677
|
+
? Record<never, never> extends Record<P, unknown>
|
|
678
|
+
? Exclude<ObjectDotPathWalk<T, P>, undefined>
|
|
679
|
+
: ObjectResolvePath<T, P, never>
|
|
680
|
+
: never;
|
|
@@ -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
|
+
}
|