@tolki/types 1.1.0 → 1.1.2
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 +18 -18
- package/package.json +1 -1
- package/src/enums.d.ts +17 -9
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ The full documentation for the TypeScript type utilities can be found at [https:
|
|
|
8
8
|
|
|
9
9
|
<!-- AUTO-GENERATED-DOCS:START -->
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## TypeScript Type Utilities Installation
|
|
12
12
|
|
|
13
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
14
|
|
|
@@ -24,7 +24,7 @@ yarn add @tolki/types
|
|
|
24
24
|
pnpm add @tolki/types
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
### Importing the Package
|
|
28
28
|
|
|
29
29
|
Example usage of types in a Vue SFC setup script section:
|
|
30
30
|
|
|
@@ -45,15 +45,15 @@ const { users } = defineProps<{
|
|
|
45
45
|
|
|
46
46
|
See the [TypeScript Types Documentation](/typescript/typescript-utilities-list) for a full list of available types and their descriptions.
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
## Tolki TypeScript Type Utilities List
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
### Paginator Utilities Types
|
|
51
51
|
|
|
52
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
53
|
|
|
54
54
|
[LengthAwarePaginator](#lengthawarepaginator) [SimplePaginator](#simplepaginator) [CursorPaginator](#cursorpaginator) [Pagination Components](#pagination-components)
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
#### LengthAwarePaginator
|
|
57
57
|
|
|
58
58
|
Imagine you have a Laravel controller with an Inertia response of your users table paginated.
|
|
59
59
|
|
|
@@ -139,7 +139,7 @@ Example in Vue template:
|
|
|
139
139
|
</template>
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
#### SimplePaginator
|
|
143
143
|
|
|
144
144
|
If you use the `simplePaginate` response function as show below, you can use the `SimplePaginator` type to define the response structure.
|
|
145
145
|
|
|
@@ -185,7 +185,7 @@ defineProps<{
|
|
|
185
185
|
</script>
|
|
186
186
|
```
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
#### CursorPaginator
|
|
189
189
|
|
|
190
190
|
The last pagination result that Laravel provides is the `cursor` pagination. For that response structure you can use the `CursorPaginator` type.
|
|
191
191
|
|
|
@@ -231,7 +231,7 @@ defineProps<{
|
|
|
231
231
|
</script>
|
|
232
232
|
```
|
|
233
233
|
|
|
234
|
-
|
|
234
|
+
#### Pagination components
|
|
235
235
|
|
|
236
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
237
|
|
|
@@ -278,13 +278,13 @@ defineProps<{
|
|
|
278
278
|
</template>
|
|
279
279
|
```
|
|
280
280
|
|
|
281
|
-
|
|
281
|
+
### Model Utilities Types
|
|
282
282
|
|
|
283
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
284
|
|
|
285
285
|
The purpose of the following model utility types is to simplify and standardize the way you define these models.
|
|
286
286
|
|
|
287
|
-
|
|
287
|
+
#### Model Types
|
|
288
288
|
|
|
289
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
290
|
|
|
@@ -344,7 +344,7 @@ notification.updated_at; // inferred as string
|
|
|
344
344
|
notification.deleted_at; // inferred as string
|
|
345
345
|
```
|
|
346
346
|
|
|
347
|
-
|
|
347
|
+
#### Model Relationship Types
|
|
348
348
|
|
|
349
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
350
|
|
|
@@ -370,7 +370,7 @@ For these type of situations, you can use the following helpers to quickly defin
|
|
|
370
370
|
|
|
371
371
|
[WithCount](#withcount) [WithMax](#withmax) [WithMin](#withmin) [WithSum](#withsum) [WithAvg](#withavg) [WithExists](#withexists)
|
|
372
372
|
|
|
373
|
-
|
|
373
|
+
#### WithCount
|
|
374
374
|
|
|
375
375
|
```TypeScript
|
|
376
376
|
import type { WithCount } from "@tolki/types";
|
|
@@ -387,7 +387,7 @@ post.comments_count // inferred as number | null
|
|
|
387
387
|
post.authors_count // inferred as number | null
|
|
388
388
|
```
|
|
389
389
|
|
|
390
|
-
|
|
390
|
+
#### WithMax
|
|
391
391
|
|
|
392
392
|
```TypeScript
|
|
393
393
|
import type { WithMax } from "@tolki/types";
|
|
@@ -404,7 +404,7 @@ post.comments_max // inferred as number | null
|
|
|
404
404
|
post.authors_max // inferred as number | null
|
|
405
405
|
```
|
|
406
406
|
|
|
407
|
-
|
|
407
|
+
#### WithMin
|
|
408
408
|
|
|
409
409
|
```TypeScript
|
|
410
410
|
import type { WithMin } from "@tolki/types";
|
|
@@ -421,7 +421,7 @@ post.comments_min // inferred as number | null
|
|
|
421
421
|
post.authors_min // inferred as number | null
|
|
422
422
|
```
|
|
423
423
|
|
|
424
|
-
|
|
424
|
+
#### WithSum
|
|
425
425
|
|
|
426
426
|
```TypeScript
|
|
427
427
|
import type { WithSum } from "@tolki/types";
|
|
@@ -438,7 +438,7 @@ post.comments_sum // inferred as number | null
|
|
|
438
438
|
post.authors_sum // inferred as number | null
|
|
439
439
|
```
|
|
440
440
|
|
|
441
|
-
|
|
441
|
+
#### WithAvg
|
|
442
442
|
|
|
443
443
|
```TypeScript
|
|
444
444
|
import type { WithAvg } from "@tolki/types";
|
|
@@ -455,7 +455,7 @@ post.comments_avg // inferred as number | null
|
|
|
455
455
|
post.authors_avg // inferred as number | null
|
|
456
456
|
```
|
|
457
457
|
|
|
458
|
-
|
|
458
|
+
#### WithExists
|
|
459
459
|
|
|
460
460
|
```TypeScript
|
|
461
461
|
import type { WithExists } from "@tolki/types";
|
|
@@ -472,7 +472,7 @@ post.comments_exists // inferred as boolean
|
|
|
472
472
|
post.authors_exists // inferred as boolean
|
|
473
473
|
```
|
|
474
474
|
|
|
475
|
-
|
|
475
|
+
### JsonResource Utilities Types
|
|
476
476
|
|
|
477
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
478
|
|
package/package.json
CHANGED
package/src/enums.d.ts
CHANGED
|
@@ -1,37 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Extract the case key names from the _cases tuple.
|
|
3
3
|
*/
|
|
4
|
-
export type CaseKeys<TEnum extends EnumConst> =
|
|
4
|
+
export type CaseKeys<TEnum extends EnumConst> = NonNullable<
|
|
5
|
+
TEnum["_cases"]
|
|
6
|
+
>[number];
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Extracts the instance method key names from the `_methods` tuple.
|
|
8
10
|
*/
|
|
9
|
-
export type MethodKeys<TEnum extends EnumConst> =
|
|
11
|
+
export type MethodKeys<TEnum extends EnumConst> = NonNullable<
|
|
12
|
+
TEnum["_methods"]
|
|
13
|
+
>[number];
|
|
10
14
|
|
|
11
15
|
/**
|
|
12
16
|
* Extracts the static helper key names from the `_static` tuple.
|
|
13
17
|
*/
|
|
14
|
-
export type StaticKeys<TEnum extends EnumConst> =
|
|
18
|
+
export type StaticKeys<TEnum extends EnumConst> = NonNullable<
|
|
19
|
+
TEnum["_static"]
|
|
20
|
+
>[number];
|
|
15
21
|
|
|
16
22
|
/**
|
|
17
23
|
* Extracts the helper key names from the `_helpers` tuple.
|
|
18
24
|
*/
|
|
19
|
-
export type HelperKeys<TEnum extends EnumConst> =
|
|
25
|
+
export type HelperKeys<TEnum extends EnumConst> = NonNullable<
|
|
26
|
+
TEnum["_helpers"]
|
|
27
|
+
>[number];
|
|
20
28
|
|
|
21
29
|
/**
|
|
22
30
|
* Constraint for all generated enum const objects.
|
|
23
31
|
*
|
|
24
32
|
* - `_cases` lists the case key names (matches PHP enum cases).
|
|
25
33
|
* - `_methods` lists instance method names whose values are per-case maps.
|
|
26
|
-
* Only these properties are resolved per-case by `
|
|
34
|
+
* Only these properties are resolved per-case by `from`; everything else
|
|
27
35
|
* passes through as-is. Use an empty array when there are no instance methods.
|
|
28
36
|
* - `_helpers` lists helper method names added to the enum.
|
|
29
37
|
* - `_static` lists static helper method names added to the enum.
|
|
30
38
|
*/
|
|
31
39
|
export type EnumConst = {
|
|
32
|
-
readonly _cases
|
|
33
|
-
readonly _methods
|
|
34
|
-
readonly _static
|
|
40
|
+
readonly _cases?: readonly string[];
|
|
41
|
+
readonly _methods?: readonly string[];
|
|
42
|
+
readonly _static?: readonly string[];
|
|
35
43
|
readonly [key: string]: unknown;
|
|
36
44
|
};
|
|
37
45
|
|
|
@@ -92,7 +100,7 @@ export type CaseValue<TEnum extends EnumConst> = {
|
|
|
92
100
|
}[CaseKeys<TEnum>];
|
|
93
101
|
|
|
94
102
|
/**
|
|
95
|
-
* The fully inferred return type of
|
|
103
|
+
* The fully inferred return type of `from`.
|
|
96
104
|
*/
|
|
97
105
|
export type ToEnumResult<TEnum extends EnumConst, TValue> =
|
|
98
106
|
MatchedCaseKey<TEnum, TValue> extends never
|