@tolki/types 1.0.2 → 1.1.1
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 +549 -0
- package/package.json +11 -2
- package/src/enums.d.ts +131 -0
- package/src/index.d.ts +1 -0
package/README.md
CHANGED
|
@@ -5,3 +5,552 @@ Utility TypeScript types for @tolki packages and Laravel HTTP responses.
|
|
|
5
5
|
## Documentation
|
|
6
6
|
|
|
7
7
|
The full documentation for the TypeScript type utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/typescript/).
|
|
8
|
+
|
|
9
|
+
<!-- AUTO-GENERATED-DOCS:START -->
|
|
10
|
+
|
|
11
|
+
## TypeScript Type Utilities Installation
|
|
12
|
+
|
|
13
|
+
The [`@tolki/types`](https://www.npmjs.com/package/@tolki/types) package provides a variety of TypeScript types inspired by Laravel's data structures such as pagination results and models. You can install it via npm, yarn, or pnpm:
|
|
14
|
+
|
|
15
|
+
```bash [npm]
|
|
16
|
+
npm install @tolki/types
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash [yarn]
|
|
20
|
+
yarn add @tolki/types
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash [pnpm]
|
|
24
|
+
pnpm add @tolki/types
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Importing the Package
|
|
28
|
+
|
|
29
|
+
Example usage of types in a Vue SFC setup script section:
|
|
30
|
+
|
|
31
|
+
```vue
|
|
32
|
+
<script lang="ts" setup>
|
|
33
|
+
import type { LengthAwarePaginator, TimestampModel } from "@tolki/types";
|
|
34
|
+
|
|
35
|
+
interface User extends TimestampModel {
|
|
36
|
+
name: string;
|
|
37
|
+
email: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const { users } = defineProps<{
|
|
41
|
+
users: LengthAwarePaginator<User>;
|
|
42
|
+
}>();
|
|
43
|
+
</script>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
See the [TypeScript Types Documentation](/typescript/typescript-utilities-list) for a full list of available types and their descriptions.
|
|
47
|
+
|
|
48
|
+
## Tolki TypeScript Type Utilities List
|
|
49
|
+
|
|
50
|
+
### Paginator Utilities Types
|
|
51
|
+
|
|
52
|
+
Laravel provides 3 built in [pagination modes](https://laravel.com/docs/pagination). Each returns a similar but slightly different structured response. When you're working on the front end and using TypeScript, you'd have to write out what each response structure looks like for each pagination mode. These utility types provide you the type-safe representations of these responses, making it easier to define pagination data structures in a type safe manner.
|
|
53
|
+
|
|
54
|
+
[LengthAwarePaginator](#lengthawarepaginator) [SimplePaginator](#simplepaginator) [CursorPaginator](#cursorpaginator) [Pagination Components](#pagination-components)
|
|
55
|
+
|
|
56
|
+
#### LengthAwarePaginator
|
|
57
|
+
|
|
58
|
+
Imagine you have a Laravel controller with an Inertia response of your users table paginated.
|
|
59
|
+
|
|
60
|
+
```PHP
|
|
61
|
+
<?php
|
|
62
|
+
|
|
63
|
+
namespace App\Http\Controllers;
|
|
64
|
+
|
|
65
|
+
use App\Models\User;
|
|
66
|
+
use Inertia\Inertia;
|
|
67
|
+
use Inertia\Response;
|
|
68
|
+
|
|
69
|
+
class UserController extends Controller
|
|
70
|
+
{
|
|
71
|
+
public function index(): Response
|
|
72
|
+
{
|
|
73
|
+
return Inertia::render('Users', [
|
|
74
|
+
'users' => User::query()->paginate(15),
|
|
75
|
+
]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
On your TypeScript based front end, you can quickly define what the pagination response structure looks like by importing the `LengthAwarePaginator` type.
|
|
81
|
+
|
|
82
|
+
The `LengthAwarePaginator` type receives a single generic type parameter which represents the type of the individual items in the paginated data.
|
|
83
|
+
|
|
84
|
+
```vue
|
|
85
|
+
<script setup lang="ts">
|
|
86
|
+
import type { LengthAwarePaginator } from "@tolki/types";
|
|
87
|
+
|
|
88
|
+
interface User {
|
|
89
|
+
id: number;
|
|
90
|
+
name: string;
|
|
91
|
+
email: string;
|
|
92
|
+
created_at: string;
|
|
93
|
+
updated_at: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
defineProps<{
|
|
97
|
+
users: LengthAwarePaginator<User>;
|
|
98
|
+
}>();
|
|
99
|
+
</script>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The example above is a script section of a Vue single file component, but it can be applied to React or Svelte just as easily.
|
|
103
|
+
|
|
104
|
+
With that definition above, you can now use the structure with type safety of the pagination result in your front-end.
|
|
105
|
+
|
|
106
|
+
Example in Vue template:
|
|
107
|
+
|
|
108
|
+
```vue
|
|
109
|
+
<template>
|
|
110
|
+
<div>
|
|
111
|
+
<table v-if="users.data.length > 0">
|
|
112
|
+
<thead>
|
|
113
|
+
<tr>
|
|
114
|
+
<th>ID</th>
|
|
115
|
+
<th>Name</th>
|
|
116
|
+
<th>Email</th>
|
|
117
|
+
<th>Created At</th>
|
|
118
|
+
<th>Updated At</th>
|
|
119
|
+
</tr>
|
|
120
|
+
</thead>
|
|
121
|
+
<tbody>
|
|
122
|
+
<tr v-for="user in users.data" :key="user.id">
|
|
123
|
+
<td>{{ user.id }}</td>
|
|
124
|
+
<td>{{ user.name }}</td>
|
|
125
|
+
<td>{{ user.email }}</td>
|
|
126
|
+
<td>{{ user.created_at }}</td>
|
|
127
|
+
<td>{{ user.updated_at }}</td>
|
|
128
|
+
</tr>
|
|
129
|
+
</tbody>
|
|
130
|
+
</table>
|
|
131
|
+
|
|
132
|
+
<div v-else>No users found.</div>
|
|
133
|
+
|
|
134
|
+
<div>
|
|
135
|
+
Showing results from {{ users.from }} to {{ users.to }} of
|
|
136
|
+
{{ users.total }}
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
</template>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### SimplePaginator
|
|
143
|
+
|
|
144
|
+
If you use the `simplePaginate` response function as show below, you can use the `SimplePaginator` type to define the response structure.
|
|
145
|
+
|
|
146
|
+
Example Laravel controller:
|
|
147
|
+
|
|
148
|
+
```PHP
|
|
149
|
+
<?php
|
|
150
|
+
|
|
151
|
+
namespace App\Http\Controllers;
|
|
152
|
+
|
|
153
|
+
use App\Models\User;
|
|
154
|
+
use Inertia\Inertia;
|
|
155
|
+
use Inertia\Response;
|
|
156
|
+
|
|
157
|
+
class UserController extends Controller
|
|
158
|
+
{
|
|
159
|
+
public function index(): Response
|
|
160
|
+
{
|
|
161
|
+
return Inertia::render('Users', [
|
|
162
|
+
'users' => User::query()->simplePaginate(15),
|
|
163
|
+
]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
In your TypeScript based front end, you can use the `SimplePaginator` type, which also accepts a single generic parameter to define the data structure.
|
|
169
|
+
|
|
170
|
+
```vue
|
|
171
|
+
<script setup lang="ts">
|
|
172
|
+
import type { SimplePaginator } from "@tolki/types";
|
|
173
|
+
|
|
174
|
+
interface User {
|
|
175
|
+
id: number;
|
|
176
|
+
name: string;
|
|
177
|
+
email: string;
|
|
178
|
+
created_at: string;
|
|
179
|
+
updated_at: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
defineProps<{
|
|
183
|
+
users: SimplePaginator<User>;
|
|
184
|
+
}>();
|
|
185
|
+
</script>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
#### CursorPaginator
|
|
189
|
+
|
|
190
|
+
The last pagination result that Laravel provides is the `cursor` pagination. For that response structure you can use the `CursorPaginator` type.
|
|
191
|
+
|
|
192
|
+
Example Laravel controller:
|
|
193
|
+
|
|
194
|
+
```PHP
|
|
195
|
+
<?php
|
|
196
|
+
|
|
197
|
+
namespace App\Http\Controllers;
|
|
198
|
+
|
|
199
|
+
use App\Models\User;
|
|
200
|
+
use Inertia\Inertia;
|
|
201
|
+
use Inertia\Response;
|
|
202
|
+
|
|
203
|
+
class UserController extends Controller
|
|
204
|
+
{
|
|
205
|
+
public function index(): Response
|
|
206
|
+
{
|
|
207
|
+
return Inertia::render('Users', [
|
|
208
|
+
'users' => User::query()->cursorPaginate(15),
|
|
209
|
+
]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
In your TypeScript based front end, you can now use the `CursorPaginator` type to define the response structure, which also accepts a single generic parameter to define the data structure.
|
|
215
|
+
|
|
216
|
+
```vue
|
|
217
|
+
<script setup lang="ts">
|
|
218
|
+
import type { CursorPaginator } from "@tolki/types";
|
|
219
|
+
|
|
220
|
+
interface User {
|
|
221
|
+
id: number;
|
|
222
|
+
name: string;
|
|
223
|
+
email: string;
|
|
224
|
+
created_at: string;
|
|
225
|
+
updated_at: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
defineProps<{
|
|
229
|
+
users: CursorPaginator<User>;
|
|
230
|
+
}>();
|
|
231
|
+
</script>
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### Pagination components
|
|
235
|
+
|
|
236
|
+
You can also use the pagination types to write type safe pagination components for your front end UI. A rough incomplete pagination example that supports all three pagination response types would look something like this.
|
|
237
|
+
|
|
238
|
+
```vue
|
|
239
|
+
<script setup lang="ts" generic="T">
|
|
240
|
+
import type {
|
|
241
|
+
CursorPaginator,
|
|
242
|
+
LengthAwarePaginator,
|
|
243
|
+
SimplePaginator,
|
|
244
|
+
} from "@tolki/types";
|
|
245
|
+
|
|
246
|
+
defineProps<{
|
|
247
|
+
data: LengthAwarePaginator<T> | SimplePaginator<T> | CursorPaginator<T>;
|
|
248
|
+
}>;
|
|
249
|
+
</script>
|
|
250
|
+
|
|
251
|
+
<template>
|
|
252
|
+
<div>
|
|
253
|
+
<!-- pagination UI implementation -->
|
|
254
|
+
</div>
|
|
255
|
+
</template>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
Then you'd use your pagination component like this:
|
|
259
|
+
|
|
260
|
+
```vue
|
|
261
|
+
<script setup lang="ts">
|
|
262
|
+
import type { LengthAwarePaginator } from "@tolki/types";
|
|
263
|
+
|
|
264
|
+
interface User {
|
|
265
|
+
/* .. */
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
defineProps<{
|
|
269
|
+
users: LengthAwarePaginator<User>;
|
|
270
|
+
}>();
|
|
271
|
+
</script>
|
|
272
|
+
|
|
273
|
+
<template>
|
|
274
|
+
<div>
|
|
275
|
+
<!-- T is inferred as User -->
|
|
276
|
+
<Pagination :data="users" />
|
|
277
|
+
</div>
|
|
278
|
+
</template>
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Model Utilities Types
|
|
282
|
+
|
|
283
|
+
Unless you're using a package like [Model Typer](https://github.com/fumeapp/modeltyper) or [Laravel Wayfinder](https://github.com/laravel/wayfinder), you probably find yourself defining models manually on a TypeScript based Laravel project.
|
|
284
|
+
|
|
285
|
+
The purpose of the following model utility types is to simplify and standardize the way you define these models.
|
|
286
|
+
|
|
287
|
+
#### Model Types
|
|
288
|
+
|
|
289
|
+
You can use the simplest `Model` type which only defines a possibly undefined id key and any number of string keyed unknown values.
|
|
290
|
+
|
|
291
|
+
The `Model` type receives a generic parameter to define the type of ID it should be. With that defined, you can now use any number of columns on a `User` type, however, they'll simply register as `unknown`.
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
import type { Model } from "@tolki/types";
|
|
295
|
+
|
|
296
|
+
interface User extends Model<number> {
|
|
297
|
+
title: string;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const user: User = getUser();
|
|
301
|
+
|
|
302
|
+
user.id; // inferred as number
|
|
303
|
+
user.title; // inferred as string
|
|
304
|
+
user.created_at; // inferred as unknown
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
If you'd like to auto define time stamp columns, you can use the `TimestampModel` type, the `SoftDeleteModel`, or the `AllTimestampsModel` type to define `created_at`, `updated_at`, and `deleted_at` in one go.
|
|
308
|
+
|
|
309
|
+
```typescript
|
|
310
|
+
import type {
|
|
311
|
+
TimestampModel,
|
|
312
|
+
SoftDeleteModel,
|
|
313
|
+
AllTimestampsModel,
|
|
314
|
+
} from "@tolki/types";
|
|
315
|
+
|
|
316
|
+
interface User extends TimestampModel<number> {
|
|
317
|
+
title: string;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const user: User = getUser();
|
|
321
|
+
|
|
322
|
+
user.id; // inferred as number
|
|
323
|
+
user.title; // inferred as string
|
|
324
|
+
user.created_at; // inferred as string
|
|
325
|
+
user.updated_at; // inferred as string
|
|
326
|
+
|
|
327
|
+
interface Post extends SoftDeleteModel<string> {}
|
|
328
|
+
|
|
329
|
+
const post: Post = getPost();
|
|
330
|
+
|
|
331
|
+
post.id; // inferred as string
|
|
332
|
+
post.deleted_at; // inferred as string
|
|
333
|
+
|
|
334
|
+
interface Notification extends AllTimestampsModel<number> {
|
|
335
|
+
message: string;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const notification: Notification = getNotification();
|
|
339
|
+
|
|
340
|
+
notification.id; // inferred as number
|
|
341
|
+
notification.message; // inferred as string
|
|
342
|
+
notification.created_at; // inferred as string
|
|
343
|
+
notification.updated_at; // inferred as string
|
|
344
|
+
notification.deleted_at; // inferred as string
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
#### Model Relationship Types
|
|
348
|
+
|
|
349
|
+
Many times when you load eloquent results, it is common to use [relationship aggregation eloquent methods](https://laravel.com/docs/12.x/eloquent-relationships#aggregating-related-models) like count, max, min, sum, average or if a relationship exists using eloquent methods like `withCount`. Those methods create a dynamic column on an eloquent result.
|
|
350
|
+
|
|
351
|
+
For example, let's say you queried this:
|
|
352
|
+
|
|
353
|
+
```PHP
|
|
354
|
+
Post::select(['title', 'body'])
|
|
355
|
+
->withCount('comments')
|
|
356
|
+
->get();
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
That creates the following result structure
|
|
360
|
+
|
|
361
|
+
```TypeScript
|
|
362
|
+
interface Post {
|
|
363
|
+
title: string;
|
|
364
|
+
body: string;
|
|
365
|
+
comments_count: number;
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
For these type of situations, you can use the following helpers to quickly define one or more relationships.
|
|
370
|
+
|
|
371
|
+
[WithCount](#withcount) [WithMax](#withmax) [WithMin](#withmin) [WithSum](#withsum) [WithAvg](#withavg) [WithExists](#withexists)
|
|
372
|
+
|
|
373
|
+
#### WithCount
|
|
374
|
+
|
|
375
|
+
```TypeScript
|
|
376
|
+
import type { WithCount } from "@tolki/types";
|
|
377
|
+
|
|
378
|
+
interface Post extends WithCount<"comments" | "authors">{
|
|
379
|
+
title: string;
|
|
380
|
+
body: string;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const post: Post = getPost();
|
|
384
|
+
|
|
385
|
+
post.title // inferred as string
|
|
386
|
+
post.comments_count // inferred as number | null
|
|
387
|
+
post.authors_count // inferred as number | null
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
#### WithMax
|
|
391
|
+
|
|
392
|
+
```TypeScript
|
|
393
|
+
import type { WithMax } from "@tolki/types";
|
|
394
|
+
|
|
395
|
+
interface Post extends WithMax<"comments" | "authors">{
|
|
396
|
+
title: string;
|
|
397
|
+
body: string;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const post: Post = getPost();
|
|
401
|
+
|
|
402
|
+
post.title // inferred as string
|
|
403
|
+
post.comments_max // inferred as number | null
|
|
404
|
+
post.authors_max // inferred as number | null
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
#### WithMin
|
|
408
|
+
|
|
409
|
+
```TypeScript
|
|
410
|
+
import type { WithMin } from "@tolki/types";
|
|
411
|
+
|
|
412
|
+
interface Post extends WithMin<"comments" | "authors">{
|
|
413
|
+
title: string;
|
|
414
|
+
body: string;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const post: Post = getPost();
|
|
418
|
+
|
|
419
|
+
post.title // inferred as string
|
|
420
|
+
post.comments_min // inferred as number | null
|
|
421
|
+
post.authors_min // inferred as number | null
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
#### WithSum
|
|
425
|
+
|
|
426
|
+
```TypeScript
|
|
427
|
+
import type { WithSum } from "@tolki/types";
|
|
428
|
+
|
|
429
|
+
interface Post extends WithSum<"comments" | "authors">{
|
|
430
|
+
title: string;
|
|
431
|
+
body: string;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const post: Post = getPost();
|
|
435
|
+
|
|
436
|
+
post.title // inferred as string
|
|
437
|
+
post.comments_sum // inferred as number | null
|
|
438
|
+
post.authors_sum // inferred as number | null
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
#### WithAvg
|
|
442
|
+
|
|
443
|
+
```TypeScript
|
|
444
|
+
import type { WithAvg } from "@tolki/types";
|
|
445
|
+
|
|
446
|
+
interface Post extends WithAvg<"comments" | "authors">{
|
|
447
|
+
title: string;
|
|
448
|
+
body: string;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const post: Post = getPost();
|
|
452
|
+
|
|
453
|
+
post.title // inferred as string
|
|
454
|
+
post.comments_avg // inferred as number | null
|
|
455
|
+
post.authors_avg // inferred as number | null
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
#### WithExists
|
|
459
|
+
|
|
460
|
+
```TypeScript
|
|
461
|
+
import type { WithExists } from "@tolki/types";
|
|
462
|
+
|
|
463
|
+
interface Post extends WithExists<"comments" | "authors">{
|
|
464
|
+
title: string;
|
|
465
|
+
body: string;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const post: Post = getPost();
|
|
469
|
+
|
|
470
|
+
post.title // inferred as string
|
|
471
|
+
post.comments_exists // inferred as boolean
|
|
472
|
+
post.authors_exists // inferred as boolean
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
### JsonResource Utilities Types
|
|
476
|
+
|
|
477
|
+
When working with Laravel's [JsonResource](https://laravel.com/docs/eloquent-resources), you often need to define the structure of the JSON response in your TypeScript front end. By default, JSON resources either return a single resource object or a collection of resource objects, which do not have a different data structure from your models' properties.
|
|
478
|
+
|
|
479
|
+
However, when you paginate results in JSON sources, the structure changes. The following utility types help you define these structures in a type-safe manner.
|
|
480
|
+
|
|
481
|
+
For example, given this Laravel controller snippet:
|
|
482
|
+
|
|
483
|
+
```PHP
|
|
484
|
+
use App\Http\Resources\UserCollection;
|
|
485
|
+
use App\Models\User;
|
|
486
|
+
|
|
487
|
+
Route::get('/users', function () {
|
|
488
|
+
return new UserCollection(User::paginate());
|
|
489
|
+
});
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
The JSON structure returned will look like this:
|
|
493
|
+
|
|
494
|
+
```json
|
|
495
|
+
{
|
|
496
|
+
"data": [
|
|
497
|
+
{
|
|
498
|
+
"id": 1,
|
|
499
|
+
"name": "Eladio Schroeder Sr.",
|
|
500
|
+
"email": "therese28@example.com"
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
"id": 2,
|
|
504
|
+
"name": "Liliana Mayert",
|
|
505
|
+
"email": "evandervort@example.com"
|
|
506
|
+
}
|
|
507
|
+
],
|
|
508
|
+
"links": {
|
|
509
|
+
"first": "http://example.com/users?page=1",
|
|
510
|
+
"last": "http://example.com/users?page=1",
|
|
511
|
+
"prev": null,
|
|
512
|
+
"next": null
|
|
513
|
+
},
|
|
514
|
+
"meta": {
|
|
515
|
+
"current_page": 1,
|
|
516
|
+
"from": 1,
|
|
517
|
+
"last_page": 1,
|
|
518
|
+
"path": "http://example.com/users",
|
|
519
|
+
"per_page": 15,
|
|
520
|
+
"to": 10,
|
|
521
|
+
"total": 10
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
To define this structure in TypeScript, you can use the `JsonResourcePaginator` utility type as shown below:
|
|
527
|
+
|
|
528
|
+
```typescript
|
|
529
|
+
import type { JsonResourcePaginator } from "@tolki/types";
|
|
530
|
+
|
|
531
|
+
interface User {
|
|
532
|
+
id: number;
|
|
533
|
+
name: string;
|
|
534
|
+
email: string;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
defineProps<{
|
|
538
|
+
users: JsonResourcePaginator<User>;
|
|
539
|
+
}>();
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
That utility type accepts a single generic parameter which represents the type of the individual items in the paginated data.
|
|
543
|
+
|
|
544
|
+
The `JsonResourcePaginator` type itself uses two additional utility types: `JsonResourceLinks` and `JsonResourceMeta`, which represent the `links` and `meta` sections of the JSON response respectively.
|
|
545
|
+
|
|
546
|
+
```typescript
|
|
547
|
+
export interface JsonResourcePaginator<T> {
|
|
548
|
+
data: T[];
|
|
549
|
+
meta: JsonResourceMeta;
|
|
550
|
+
links: JsonResourceLinks;
|
|
551
|
+
}
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
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.
|
|
555
|
+
|
|
556
|
+
<!-- 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.1.1",
|
|
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",
|
|
@@ -36,6 +36,9 @@
|
|
|
36
36
|
"./collections": {
|
|
37
37
|
"types": "./src/collections.d.ts"
|
|
38
38
|
},
|
|
39
|
+
"./enums": {
|
|
40
|
+
"types": "./src/enums.d.ts"
|
|
41
|
+
},
|
|
39
42
|
"./data": {
|
|
40
43
|
"types": "./src/data.d.ts"
|
|
41
44
|
},
|
|
@@ -61,7 +64,13 @@
|
|
|
61
64
|
"types": "./src/utils.d.ts"
|
|
62
65
|
}
|
|
63
66
|
},
|
|
67
|
+
"tolki": {
|
|
68
|
+
"docs": [
|
|
69
|
+
"typescript/index.md",
|
|
70
|
+
"typescript/typescript-utilities-list.md"
|
|
71
|
+
]
|
|
72
|
+
},
|
|
64
73
|
"scripts": {
|
|
65
|
-
"
|
|
74
|
+
"readme:build": "ts-node ../../scripts/readme-build.ts --filter @tolki/types"
|
|
66
75
|
}
|
|
67
76
|
}
|
package/src/enums.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract the case key names from the _cases tuple.
|
|
3
|
+
*/
|
|
4
|
+
export type CaseKeys<TEnum extends EnumConst> = TEnum["_cases"][number];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the instance method key names from the `_methods` tuple.
|
|
8
|
+
*/
|
|
9
|
+
export type MethodKeys<TEnum extends EnumConst> = TEnum["_methods"][number];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the static helper key names from the `_static` tuple.
|
|
13
|
+
*/
|
|
14
|
+
export type StaticKeys<TEnum extends EnumConst> = TEnum["_static"][number];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Extracts the helper key names from the `_helpers` tuple.
|
|
18
|
+
*/
|
|
19
|
+
export type HelperKeys<TEnum extends EnumConst> = TEnum["_helpers"][number];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Constraint for all generated enum const objects.
|
|
23
|
+
*
|
|
24
|
+
* - `_cases` lists the case key names (matches PHP enum cases).
|
|
25
|
+
* - `_methods` lists instance method names whose values are per-case maps.
|
|
26
|
+
* Only these properties are resolved per-case by `from`; everything else
|
|
27
|
+
* passes through as-is. Use an empty array when there are no instance methods.
|
|
28
|
+
* - `_helpers` lists helper method names added to the enum.
|
|
29
|
+
* - `_static` lists static helper method names added to the enum.
|
|
30
|
+
*/
|
|
31
|
+
export type EnumConst = {
|
|
32
|
+
readonly _cases: readonly string[];
|
|
33
|
+
readonly _methods: readonly string[];
|
|
34
|
+
readonly _static: readonly string[];
|
|
35
|
+
readonly [key: string]: unknown;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the case key name whose value matches the provided value.
|
|
40
|
+
* Uses a mapped type to find case keys where the value type matches bidirectionally.
|
|
41
|
+
*/
|
|
42
|
+
export type MatchedCaseKey<TEnum extends EnumConst, TValue> = {
|
|
43
|
+
[K in CaseKeys<TEnum>]: TEnum[K] extends TValue
|
|
44
|
+
? TValue extends TEnum[K]
|
|
45
|
+
? K
|
|
46
|
+
: never
|
|
47
|
+
: never;
|
|
48
|
+
}[CaseKeys<TEnum>];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves the value of an instance method for a given case value.
|
|
52
|
+
* Iterates case keys to find the one whose value matches TValue bidirectionally,
|
|
53
|
+
* then looks up the method's value at that case key.
|
|
54
|
+
*/
|
|
55
|
+
export type ResolveMethodForValue<
|
|
56
|
+
TEnum extends EnumConst,
|
|
57
|
+
K extends keyof TEnum,
|
|
58
|
+
TValue,
|
|
59
|
+
> = {
|
|
60
|
+
[C in CaseKeys<TEnum>]: TEnum[C] extends TValue
|
|
61
|
+
? TValue extends TEnum[C]
|
|
62
|
+
? C extends keyof TEnum[K]
|
|
63
|
+
? TEnum[K][C]
|
|
64
|
+
: never
|
|
65
|
+
: never
|
|
66
|
+
: never;
|
|
67
|
+
}[CaseKeys<TEnum>];
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Builds the resolved instance type by iterating over all non-case, non-meta keys.
|
|
71
|
+
*
|
|
72
|
+
* - value: set directly to TValue.
|
|
73
|
+
* - Instance methods (listed in _methods): resolved per-case via ResolveMethodForValue.
|
|
74
|
+
* - Static methods / other values: passed through unchanged.
|
|
75
|
+
*/
|
|
76
|
+
export type ResolvedEnumInstance<TEnum extends EnumConst, TValue> = {
|
|
77
|
+
readonly value: TValue;
|
|
78
|
+
} & {
|
|
79
|
+
readonly [K in Exclude<
|
|
80
|
+
keyof TEnum,
|
|
81
|
+
CaseKeys<TEnum> | "_cases" | "_methods" | "_helpers" | "_static"
|
|
82
|
+
>]: K extends MethodKeys<TEnum>
|
|
83
|
+
? ResolveMethodForValue<TEnum, K, TValue>
|
|
84
|
+
: TEnum[K];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Extracts the union of all case values from an enum const.
|
|
89
|
+
*/
|
|
90
|
+
export type CaseValue<TEnum extends EnumConst> = {
|
|
91
|
+
[K in CaseKeys<TEnum>]: TEnum[K];
|
|
92
|
+
}[CaseKeys<TEnum>];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The fully inferred return type of `from`.
|
|
96
|
+
*/
|
|
97
|
+
export type ToEnumResult<TEnum extends EnumConst, TValue> =
|
|
98
|
+
MatchedCaseKey<TEnum, TValue> extends never
|
|
99
|
+
? never
|
|
100
|
+
: ResolvedEnumInstance<TEnum, TValue>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Overrides properties in T with properties in U.
|
|
104
|
+
* Equivalent to a spread: `{ ...T, ...U }`.
|
|
105
|
+
* Keys to omit are derived automatically from U — no manual list needed.
|
|
106
|
+
*/
|
|
107
|
+
type Override<T, U> = Omit<T, keyof U> & U;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The return type of `defineEnum`.
|
|
111
|
+
*
|
|
112
|
+
* Enriches a raw enum const object with bound `from`, `tryFrom`, and `cases`
|
|
113
|
+
* methods so callers don't need to pass the enum object every time.
|
|
114
|
+
*
|
|
115
|
+
* Uses `Omit` to strip any existing `from`, `tryFrom`, or `cases` properties
|
|
116
|
+
* from the original enum before intersecting, avoiding `never` from conflicting
|
|
117
|
+
* property types (e.g. when the enum has `from: null` as a helper stub).
|
|
118
|
+
*/
|
|
119
|
+
export type DefineEnumResult<TEnum extends EnumConst> = Override<
|
|
120
|
+
TEnum,
|
|
121
|
+
{
|
|
122
|
+
readonly _helpers: string[];
|
|
123
|
+
readonly from: <const TValue extends CaseValue<TEnum>>(
|
|
124
|
+
value: TValue,
|
|
125
|
+
) => ToEnumResult<TEnum, TValue>;
|
|
126
|
+
readonly tryFrom: <const TValue extends CaseValue<TEnum>>(
|
|
127
|
+
value: TValue,
|
|
128
|
+
) => ToEnumResult<TEnum, TValue> | null;
|
|
129
|
+
readonly cases: () => Array<ToEnumResult<TEnum, CaseValue<TEnum>>>;
|
|
130
|
+
}
|
|
131
|
+
>;
|