@tolki/types 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Abraham Arango
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # Tolki JS Types Package
2
+
3
+ Utility TypeScript types for @tolki packages and Laravel HTTP responses.
4
+
5
+ ## Documentation
6
+
7
+ The full documentation for the TypeScript type utilities can be found at [https://tolki.abe.dev](https://tolki.abe.dev/typescript/).
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@tolki/types",
3
+ "version": "1.0.0",
4
+ "description": "Utility TypeScript types for @tolki packages and Laravel HTTP responses.",
5
+ "author": "Abraham Arango <https://github.com/abetwothree>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/abetwothree/tolki.git",
10
+ "directory": "packages/types"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/abetwothree/tolki/issues"
14
+ },
15
+ "type": "module",
16
+ "types": "src/index.d.ts",
17
+ "keywords": [
18
+ "typescript",
19
+ "types",
20
+ "laravel",
21
+ "models",
22
+ "pagination"
23
+ ],
24
+ "sideEffects": false,
25
+ "files": [
26
+ "src"
27
+ ],
28
+ "exports": {
29
+ ".": {
30
+ "types": "./src/index.d.ts"
31
+ },
32
+ "./arrays": {
33
+ "types": "./src/arrays.d.ts"
34
+ },
35
+ "./collections": {
36
+ "types": "./src/collections.d.ts"
37
+ },
38
+ "./data": {
39
+ "types": "./src/data.d.ts"
40
+ },
41
+ "./models": {
42
+ "types": "./src/models.d.ts"
43
+ },
44
+ "./objects": {
45
+ "types": "./src/objects.d.ts"
46
+ },
47
+ "./pagination": {
48
+ "types": "./src/pagination.d.ts"
49
+ },
50
+ "./path-type": {
51
+ "types": "./src/path-type.d.ts"
52
+ },
53
+ "./responses": {
54
+ "types": "./src/responses.d.ts"
55
+ },
56
+ "./strings": {
57
+ "types": "./src/strings.d.ts"
58
+ },
59
+ "./utils": {
60
+ "types": "./src/utils.d.ts"
61
+ }
62
+ },
63
+ "scripts": {
64
+ "test": "echo \"Error: no test specified\" && exit 1"
65
+ }
66
+ }
@@ -0,0 +1,34 @@
1
+ export interface Arrayable<TValue> {
2
+ toArray(): TValue[];
3
+ }
4
+
5
+ export interface ArrayAccess<TValue> {
6
+ offsetExists(offset: number | string): boolean;
7
+ offsetGet(offset: number | string): TValue | undefined;
8
+ offsetSet(offset: number | string, value: TValue): void;
9
+ offsetUnset(offset: number | string): void;
10
+ }
11
+
12
+ export type ArrayInnerValue<X> = X extends ReadonlyArray<infer U> ? U : never;
13
+
14
+ export type ArrayItems<T> = T[] | Array<T>;
15
+
16
+ /**
17
+ * Helper type to check if an array is mutable (not readonly)
18
+ */
19
+ type IsMutableArray<T> = T extends readonly unknown[]
20
+ ? T extends unknown[]
21
+ ? true
22
+ : false
23
+ : false;
24
+
25
+ /**
26
+ * Helper type to add a value to an array type.
27
+ * Excludes readonly arrays as they cannot be mutated.
28
+ */
29
+ export type AddToArray<T extends unknown[], V> =
30
+ IsMutableArray<T> extends true
31
+ ? T extends Array<infer U>
32
+ ? Array<U | V>
33
+ : never
34
+ : never;
@@ -0,0 +1,2 @@
1
+ export type ProxyTarget = this;
2
+ export type PropertyName = string | symbol;
package/src/data.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ export type DataItems<TValue, TKey extends PropertyKey = PropertyKey> =
2
+ | TValue[]
3
+ | Record<TKey, TValue>;
4
+
5
+ export interface Countable {
6
+ count(): number;
7
+ }
8
+
9
+ export interface IteratorAggregate<TValue, TKey> {
10
+ getIterator(): IterableIterator<[TKey, TValue]>;
11
+ }
12
+
13
+ export interface Jsonable {
14
+ toJson(): string;
15
+ }
16
+
17
+ export interface JsonSerializable {
18
+ jsonSerialize(): unknown;
19
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export type * from "./arrays";
2
+ export type * from "./collections";
3
+ export type * from "./data";
4
+ export type * from "./models";
5
+ export type * from "./objects";
6
+ export type * from "./pagination";
7
+ export type * from "./path-type";
8
+ export type * from "./responses";
9
+ export type * from "./strings";
10
+ export type * from "./utils";
@@ -0,0 +1,43 @@
1
+ export interface Model {
2
+ id?: string | number;
3
+ [key: string]: unknown;
4
+ }
5
+
6
+ export interface Timestamps {
7
+ created_at?: string | null;
8
+ updated_at?: string | null;
9
+ }
10
+
11
+ export interface SoftDeletes {
12
+ deleted_at?: string | null;
13
+ }
14
+
15
+ export interface TimestampModel extends Model, Timestamps {}
16
+ export interface SoftDeleteModel extends Model, SoftDeletes {}
17
+ export interface AllTimestampsModel extends Model, Timestamps, SoftDeletes {}
18
+
19
+ export type AsCount<T extends string> = `${T}_count`;
20
+ export type AsMax<T extends string> = `${T}_max`;
21
+ export type AsMin<T extends string> = `${T}_min`;
22
+ export type AsSum<T extends string> = `${T}_sum`;
23
+ export type AsAvg<T extends string> = `${T}_avg`;
24
+ export type AsExists<T extends string> = `${T}_exists`;
25
+
26
+ export type WithCount<Relations extends string> = {
27
+ [K in Relations as AsCount<K>]: number;
28
+ };
29
+ export type WithMax<Relations extends string> = {
30
+ [K in Relations as AsMax<K>]: number | null;
31
+ };
32
+ export type WithMin<Relations extends string> = {
33
+ [K in Relations as AsMin<K>]: number | null;
34
+ };
35
+ export type WithSum<Relations extends string> = {
36
+ [K in Relations as AsSum<K>]: number | null;
37
+ };
38
+ export type WithAvg<Relations extends string> = {
39
+ [K in Relations as AsAvg<K>]: number | null;
40
+ };
41
+ export type WithExists<Relations extends string> = {
42
+ [K in Relations as AsExists<K>]: boolean;
43
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Helper type to add a key-value pair to an object type
3
+ */
4
+ export type AddToObject<
5
+ T extends Record<PropertyKey, unknown>,
6
+ K extends PropertyKey,
7
+ V,
8
+ > = T & Record<K, V>;
@@ -0,0 +1,43 @@
1
+ export interface SimplePaginator<T> {
2
+ current_page: number;
3
+ data: T[];
4
+ first_page_url: string;
5
+ from: number;
6
+ next_page_url: string | null;
7
+ path: string;
8
+ per_page: number;
9
+ prev_page_url: string | null;
10
+ to: number;
11
+ }
12
+
13
+ export interface LengthAwarePaginator<T> {
14
+ current_page: number;
15
+ data: T[];
16
+ first_page_url: string;
17
+ from: number;
18
+ last_page: number;
19
+ last_page_url: string;
20
+ links: PaginatorLink[];
21
+ next_page_url: string | null;
22
+ path: string;
23
+ per_page: number;
24
+ prev_page_url: string | null;
25
+ to: number;
26
+ total: number;
27
+ }
28
+
29
+ export interface CursorPaginator<T> {
30
+ data: T[];
31
+ path: string;
32
+ per_page: number;
33
+ next_cursor: string | null;
34
+ next_page_url: string | null;
35
+ prev_cursor: string | null;
36
+ prev_page_url: string | null;
37
+ }
38
+
39
+ export interface PaginatorLink {
40
+ url: string | null;
41
+ label: string;
42
+ active: boolean;
43
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * A key used to access a path in an object or array.
3
+ */
4
+ export type PathKey = number | string | null | undefined;
5
+
6
+ /**
7
+ * A set of keys used to access a path in an object or array.
8
+ */
9
+ export type PathKeys = number | string | null | undefined | Array<PathKey>;
10
+
11
+ /**
12
+ * Unwraps a value or function that returns a value.
13
+ */
14
+ export type UnwrapFn<T> = T extends (...args: unknown[]) => infer R ? R : T;
15
+
16
+ /**
17
+ * Helper to get array element type
18
+ */
19
+ type ArrayElement<T> = T extends readonly (infer U)[] ? U : never;
20
+
21
+ /**
22
+ * Helper to check if a type is an array
23
+ */
24
+ type IsArray<T> = T extends readonly unknown[] ? true : false;
25
+
26
+ /**
27
+ * Helper to check if a type is a tuple with at least one element
28
+ */
29
+ type IsTuple<T> = T extends readonly [unknown, ...unknown[]]
30
+ ? true
31
+ : T extends readonly []
32
+ ? true
33
+ : false;
34
+
35
+ /**
36
+ * Helper to get element at specific index in tuple
37
+ * For unions of tuples, returns the union of elements at that index
38
+ */
39
+ type TupleElement<
40
+ T extends readonly unknown[],
41
+ N extends number,
42
+ > = T extends readonly unknown[]
43
+ ? N extends keyof T
44
+ ? T[N]
45
+ : ArrayElement<T>
46
+ : never;
47
+
48
+ /**
49
+ * Gets the type of a value at a given path in an object or array.
50
+ * Supports dot notation (a.b.c) and array indexing (a.0.b).
51
+ */
52
+ export type GetFieldType<
53
+ T,
54
+ P extends string | number,
55
+ TDefault = never,
56
+ > = P extends keyof T
57
+ ? T[P]
58
+ : P extends `${infer First}.${infer Rest}`
59
+ ? First extends keyof T
60
+ ? GetFieldType<T[First], Rest, TDefault>
61
+ : First extends `${infer N extends number}`
62
+ ? IsArray<T> extends true
63
+ ? T extends readonly unknown[]
64
+ ? GetFieldType<T[N], Rest, TDefault>
65
+ : TDefault
66
+ : TDefault
67
+ : TDefault
68
+ : P extends `${infer N extends number}`
69
+ ? IsArray<T> extends true
70
+ ? T extends readonly unknown[]
71
+ ? T[N]
72
+ : TDefault
73
+ : TDefault
74
+ : TDefault;
@@ -0,0 +1,25 @@
1
+ import type { PaginatorLink } from "./pagination";
2
+
3
+ export interface JsonResourceMeta {
4
+ current_page: number;
5
+ from: number;
6
+ last_page: number;
7
+ links: PaginatorLink[];
8
+ path: string;
9
+ per_page: number;
10
+ to: number;
11
+ total: number;
12
+ }
13
+
14
+ export interface JsonResourceLinks {
15
+ first: string | null;
16
+ last: string | null;
17
+ prev: string | null;
18
+ next: string | null;
19
+ }
20
+
21
+ export interface JsonResource<T> {
22
+ data: T[];
23
+ meta: JsonResourceMeta;
24
+ links: JsonResourceLinks;
25
+ }
@@ -0,0 +1,3 @@
1
+ export interface CanBeEscapedWhenCastToString {
2
+ escapeWhenCastToString(escape: boolean): this;
3
+ }
package/src/utils.d.ts ADDED
File without changes