@milkio/stargate 1.0.0-alpha.99 → 1.0.0-beta.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) 2023 akirarika
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/index.d.ts ADDED
@@ -0,0 +1,284 @@
1
+ export type MilkioStargateOptions = {
2
+ baseUrl: string | (() => string) | (() => Promise<string>);
3
+ timeout?: number;
4
+ fetch?: typeof fetch;
5
+ abort?: typeof AbortController;
6
+ };
7
+ export type Mixin<T, U> = U & Omit<T, keyof U>;
8
+ export type ExecuteOptions = {
9
+ params?: Record<any, any>;
10
+ headers?: Record<string, string>;
11
+ timeout?: number;
12
+ type?: "action" | "stream";
13
+ baseUrl?: string | (() => string) | (() => Promise<string>);
14
+ };
15
+ export type ExecuteResultsOption = {
16
+ executeId: string;
17
+ };
18
+ export type Ping = [
19
+ {
20
+ connect: false;
21
+ delay: number;
22
+ error: any;
23
+ },
24
+ null
25
+ ] | [
26
+ null,
27
+ {
28
+ connect: true;
29
+ delay: number;
30
+ serverTimestamp: number;
31
+ }
32
+ ];
33
+ export declare function createStargate<Generated extends {
34
+ routeSchema: any;
35
+ rejectCode: any;
36
+ }>(stargateOptions: MilkioStargateOptions): Promise<{
37
+ $types: {
38
+ generated: Generated;
39
+ };
40
+ options: MilkioStargateOptions;
41
+ execute<Path extends keyof Generated["routeSchema"]>(path: Path, options?: Mixin<ExecuteOptions, {
42
+ params?: Generated["routeSchema"][Path]["types"]["params"];
43
+ }>): Promise<Generated["routeSchema"][Path]["types"]["\uD83D\uDC23"] extends boolean ? // action
44
+ [Partial<Generated["rejectCode"]>, null, ExecuteResultsOption] | [null, Generated["routeSchema"][Path]["types"]["result"], ExecuteResultsOption] : // stream
45
+ [Partial<Generated["rejectCode"]>, null, ExecuteResultsOption] | [null, AsyncGenerator<[Partial<Generated["rejectCode"]>, null] | [null, GeneratorGeneric<Generated["routeSchema"][Path]["types"]["result"]>], ExecuteResultsOption>]>;
46
+ __cookbook: {
47
+ subscribe: (baseUrl: string) => Promise<any>;
48
+ };
49
+ ping(options?: {
50
+ timeout?: number;
51
+ }): Promise<Ping>;
52
+ on: <Key extends keyof {
53
+ "milkio:executeBefore": {
54
+ path: string;
55
+ options: Mixin<ExecuteOptions, {
56
+ headers: Record<string, string>;
57
+ baseUrl: string;
58
+ }>;
59
+ };
60
+ "milkio:fetchBefore": {
61
+ path: string;
62
+ options: Mixin<ExecuteOptions, {
63
+ headers: Record<string, string>;
64
+ baseUrl: string;
65
+ }>;
66
+ body: string;
67
+ };
68
+ "milkio:executeError": {
69
+ path: string;
70
+ options: Mixin<ExecuteOptions, {
71
+ headers: Record<string, string>;
72
+ baseUrl: string;
73
+ }>;
74
+ error: Partial<Generated["rejectCode"]>;
75
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => boolean | Promise<boolean>) => Promise<void>;
76
+ };
77
+ }, Handler extends (event: {
78
+ "milkio:executeBefore": {
79
+ path: string;
80
+ options: Mixin<ExecuteOptions, {
81
+ headers: Record<string, string>;
82
+ baseUrl: string;
83
+ }>;
84
+ };
85
+ "milkio:fetchBefore": {
86
+ path: string;
87
+ options: Mixin<ExecuteOptions, {
88
+ headers: Record<string, string>;
89
+ baseUrl: string;
90
+ }>;
91
+ body: string;
92
+ };
93
+ "milkio:executeError": {
94
+ path: string;
95
+ options: Mixin<ExecuteOptions, {
96
+ headers: Record<string, string>;
97
+ baseUrl: string;
98
+ }>;
99
+ error: Partial<Generated["rejectCode"]>;
100
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => boolean | Promise<boolean>) => Promise<void>;
101
+ };
102
+ }[Key]) => void>(key: Key, handler: Handler) => () => void;
103
+ off: <Key extends keyof {
104
+ "milkio:executeBefore": {
105
+ path: string;
106
+ options: Mixin<ExecuteOptions, {
107
+ headers: Record<string, string>;
108
+ baseUrl: string;
109
+ }>;
110
+ };
111
+ "milkio:fetchBefore": {
112
+ path: string;
113
+ options: Mixin<ExecuteOptions, {
114
+ headers: Record<string, string>;
115
+ baseUrl: string;
116
+ }>;
117
+ body: string;
118
+ };
119
+ "milkio:executeError": {
120
+ path: string;
121
+ options: Mixin<ExecuteOptions, {
122
+ headers: Record<string, string>;
123
+ baseUrl: string;
124
+ }>;
125
+ error: Partial<Generated["rejectCode"]>;
126
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => boolean | Promise<boolean>) => Promise<void>;
127
+ };
128
+ }, Handler extends (event: {
129
+ "milkio:executeBefore": {
130
+ path: string;
131
+ options: Mixin<ExecuteOptions, {
132
+ headers: Record<string, string>;
133
+ baseUrl: string;
134
+ }>;
135
+ };
136
+ "milkio:fetchBefore": {
137
+ path: string;
138
+ options: Mixin<ExecuteOptions, {
139
+ headers: Record<string, string>;
140
+ baseUrl: string;
141
+ }>;
142
+ body: string;
143
+ };
144
+ "milkio:executeError": {
145
+ path: string;
146
+ options: Mixin<ExecuteOptions, {
147
+ headers: Record<string, string>;
148
+ baseUrl: string;
149
+ }>;
150
+ error: Partial<Generated["rejectCode"]>;
151
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => boolean | Promise<boolean>) => Promise<void>;
152
+ };
153
+ }[Key]) => void>(key: Key, handler: Handler) => void;
154
+ emit: <Key extends keyof {
155
+ "milkio:executeBefore": {
156
+ path: string;
157
+ options: Mixin<ExecuteOptions, {
158
+ headers: Record<string, string>;
159
+ baseUrl: string;
160
+ }>;
161
+ };
162
+ "milkio:fetchBefore": {
163
+ path: string;
164
+ options: Mixin<ExecuteOptions, {
165
+ headers: Record<string, string>;
166
+ baseUrl: string;
167
+ }>;
168
+ body: string;
169
+ };
170
+ "milkio:executeError": {
171
+ path: string;
172
+ options: Mixin<ExecuteOptions, {
173
+ headers: Record<string, string>;
174
+ baseUrl: string;
175
+ }>;
176
+ error: Partial<Generated["rejectCode"]>;
177
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => boolean | Promise<boolean>) => Promise<void>;
178
+ };
179
+ }, Value extends {
180
+ "milkio:executeBefore": {
181
+ path: string;
182
+ options: Mixin<ExecuteOptions, {
183
+ headers: Record<string, string>;
184
+ baseUrl: string;
185
+ }>;
186
+ };
187
+ "milkio:fetchBefore": {
188
+ path: string;
189
+ options: Mixin<ExecuteOptions, {
190
+ headers: Record<string, string>;
191
+ baseUrl: string;
192
+ }>;
193
+ body: string;
194
+ };
195
+ "milkio:executeError": {
196
+ path: string;
197
+ options: Mixin<ExecuteOptions, {
198
+ headers: Record<string, string>;
199
+ baseUrl: string;
200
+ }>;
201
+ error: Partial<Generated["rejectCode"]>;
202
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => boolean | Promise<boolean>) => Promise<void>;
203
+ };
204
+ }[Key]>(key: Key, value: Value) => Promise<void>;
205
+ }>;
206
+ export interface ExecuteStreamOptions {
207
+ headers?: Record<string, string>;
208
+ timeout?: number;
209
+ }
210
+ export interface ApiSchemaExtend {
211
+ apiValidator: {
212
+ generatedAt: number;
213
+ validate: Record<any, any>;
214
+ };
215
+ apiMethodsSchema: Record<any, any>;
216
+ apiMethodsTypeSchema: Record<any, any>;
217
+ apiTestsSchema: Record<any, any>;
218
+ }
219
+ export type FailCodeExtend = Record<any, (...args: Array<any>) => any>;
220
+ export type BootstrapMiddleware = (data: {
221
+ storage: ClientStorage;
222
+ }) => Promise<void> | void;
223
+ export type BeforeExecuteMiddleware = (data: {
224
+ path: string;
225
+ params: any;
226
+ headers: Record<string, string>;
227
+ storage: ClientStorage;
228
+ }) => Promise<void> | void;
229
+ export type AfterExecuteMiddleware = (data: {
230
+ path: string;
231
+ result: {
232
+ value: any;
233
+ };
234
+ storage: ClientStorage;
235
+ }) => Promise<void> | void;
236
+ export interface MiddlewareOptions {
237
+ bootstrap?: BootstrapMiddleware;
238
+ beforeExecute?: BeforeExecuteMiddleware;
239
+ afterExecute?: AfterExecuteMiddleware;
240
+ }
241
+ export interface ClientStorage {
242
+ getItem: (key: string) => Promise<string | null>;
243
+ setItem: (key: string, value: string) => Promise<void>;
244
+ removeItem: (key: string) => Promise<void>;
245
+ }
246
+ export interface ExecuteResultSuccess<Result> {
247
+ executeId: string;
248
+ success: true;
249
+ data: Result;
250
+ }
251
+ export type GeneratorGeneric<T> = T extends AsyncGenerator<infer I> ? I : never;
252
+ export type FlattenKeys<T, Prefix extends string = ""> = {
253
+ [K in keyof T]: T[K] extends object ? FlattenKeys<T[K], `${Prefix}${Exclude<K, symbol>}.`> : `$input.${Prefix}${Exclude<K, symbol>}`;
254
+ }[keyof T];
255
+ /**
256
+ * Represents a message sent in an event stream
257
+ * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
258
+ */
259
+ export interface EventSourceMessage {
260
+ data: string;
261
+ }
262
+ /**
263
+ * Converts a ReadableStream into a callback pattern.
264
+ * @param stream The input ReadableStream.
265
+ * @param onChunk A function that will be called on each new byte chunk in the stream.
266
+ * @returns {Promise<void>} A promise that will be resolved when the stream closes.
267
+ */
268
+ export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void): Promise<void>;
269
+ /**
270
+ * Parses arbitary byte chunks into EventSource line buffers.
271
+ * Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
272
+ * @param onLine A function that will be called on each new EventSource line.
273
+ * @returns A function that should be called for each incoming byte chunk.
274
+ */
275
+ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number) => void): (arr: Uint8Array) => void;
276
+ /**
277
+ * Parses line buffers into EventSourceMessages.
278
+ * @param onId A function that will be called on each `id` field.
279
+ * @param onRetry A function that will be called on each `retry` field.
280
+ * @param onMessage A function that will be called on each message.
281
+ * @returns A function that should be called for each incoming line buffer.
282
+ */
283
+ export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void): (line: Uint8Array, fieldLength: number) => void;
284
+ export declare function withResolvers<T = any>(): PromiseWithResolvers<T>;