This repository was archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathasyncTypes2.ts
152 lines (133 loc) Β· 3.3 KB
/
asyncTypes2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {
SerializedType,
TsonNonce,
TsonType,
TsonTypeTesterCustom,
} from "../sync/syncTypes.js";
import { TsonGuard } from "../tsonAssert.js";
import {
TsonAsyncUnfolderFactory,
createTsonAsyncUnfoldFn,
} from "./createUnfoldAsyncFn.js";
export const ChunkTypes = {
BODY: "BODY",
ERROR: "ERROR",
HEAD: "HEAD",
LEAF: "LEAF",
REF: "REF",
TAIL: "TAIL",
} as const;
export type ChunkTypes = {
[key in keyof typeof ChunkTypes]: (typeof ChunkTypes)[key];
};
export const TsonStatus = {
//MULTI_STATUS: 207,
ERROR: 500,
INCOMPLETE: 203,
OK: 200,
} as const;
export type TsonStatus = {
[key in keyof typeof TsonStatus]: (typeof TsonStatus)[key];
};
export const TsonStructures = {
ARRAY: 0,
ITERABLE: 2,
POJO: 1,
} as const;
export type TsonStructures = typeof TsonStructures;
export interface TsonAsyncChunk<T = unknown> {
chunk: T;
key?: null | number | string | undefined;
}
export interface TsonAsyncMarshaller<
TValue,
TSerializedType extends SerializedType,
> {
async: true;
fold: (
iter: AsyncGenerator<
TsonAsyncChunk<TSerializedType>,
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
number | undefined | void,
undefined
>,
) => Promise<Awaited<TValue>>;
key: string;
unfold: ReturnType<
typeof createTsonAsyncUnfoldFn<TsonAsyncUnfolderFactory<TValue>>
>;
}
export type TsonAsyncType<
/**
* The type of the value
*/
TValue,
/**
* JSON-serializable value how it's stored after it's serialized
*/
TSerializedType extends SerializedType,
> = TsonTypeTesterCustom & TsonAsyncMarshaller<TValue, TSerializedType>;
export interface TsonAsyncOptions {
/**
* A list of guards to apply to every value
*/
guards?: TsonGuard<any>[];
/**
* The nonce function every time we start serializing a new object
* Should return a unique value every time it's called
* @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random`
*/
nonce?: () => string;
/**
* The list of types to use
*/
types: (TsonAsyncType<any, any> | TsonType<any, any>)[];
}
export type TsonAsyncTupleHeader = [
Id: `${TsonNonce}${number}`,
ParentId: `${TsonNonce}${"" | number}`,
Key?: null | number | string | undefined,
];
export type TsonAsyncLeafTuple = [
ChunkType: ChunkTypes["LEAF"],
Header: TsonAsyncTupleHeader,
Value: unknown,
TypeHandlerKey?: string | undefined,
];
export type TsonAsyncBodyTuple = [
ChunkType: ChunkTypes["BODY"],
Header: TsonAsyncTupleHeader,
Head: TsonAsyncHeadTuple,
TypeHandlerKey?: string | undefined,
];
export type TsonAsyncHeadTuple = [
ChunkType: ChunkTypes["HEAD"],
Header: TsonAsyncTupleHeader,
TypeHandlerKey?: TsonStructures[keyof TsonStructures] | string | undefined,
];
export type TsonAsyncReferenceTuple = [
ChunkType: ChunkTypes["REF"],
Header: TsonAsyncTupleHeader,
OriginalNodeId: `${TsonNonce}${number}`,
];
export type TsonAsyncErrorTuple = [
ChunkType: ChunkTypes["ERROR"],
Header: TsonAsyncTupleHeader,
Error: unknown,
];
export type TsonAsyncTailTuple = [
ChunkType: ChunkTypes["TAIL"],
Header: [
Id: TsonAsyncTupleHeader[0],
ParentId: TsonAsyncTupleHeader[1],
Key?: null | undefined,
],
StatusCode: number,
];
export type TsonAsyncTuple =
| TsonAsyncBodyTuple
| TsonAsyncErrorTuple
| TsonAsyncHeadTuple
| TsonAsyncLeafTuple
| TsonAsyncReferenceTuple
| TsonAsyncTailTuple;