@aklinker1/zeta 2.1.2 → 2.2.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.
Files changed (41) hide show
  1. package/dist/adapters/zod-schema-adapter.d.mts +17 -0
  2. package/dist/adapters/zod-schema-adapter.mjs +726 -0
  3. package/dist/app-Bc9Kn3KA.mjs +1225 -0
  4. package/dist/client.d.mts +71 -0
  5. package/dist/client.mjs +73 -0
  6. package/dist/index.d.mts +317 -0
  7. package/dist/index.mjs +3 -0
  8. package/dist/schema-DKqL09oQ.d.mts +168 -0
  9. package/dist/schema.d.mts +2 -0
  10. package/dist/schema.mjs +151 -0
  11. package/dist/serialization-0dai2wUm.mjs +56 -0
  12. package/dist/testing.d.mts +26 -0
  13. package/dist/testing.mjs +52 -0
  14. package/dist/transports/bun-transport.d.mts +47 -0
  15. package/dist/transports/bun-transport.mjs +58 -0
  16. package/dist/transports/deno-transport.d.mts +48 -0
  17. package/dist/transports/deno-transport.mjs +57 -0
  18. package/dist/transports/fetch-transport.d.mts +6 -0
  19. package/dist/transports/fetch-transport.mjs +25 -0
  20. package/dist/types-BvjPE9EM.d.mts +712 -0
  21. package/dist/types.d.mts +2 -0
  22. package/dist/types.mjs +1 -0
  23. package/package.json +51 -19
  24. package/src/adapters/zod-schema-adapter.ts +0 -29
  25. package/src/app.ts +0 -479
  26. package/src/client.ts +0 -184
  27. package/src/errors.ts +0 -529
  28. package/src/index.ts +0 -5
  29. package/src/internal/compile-fetch-function.ts +0 -166
  30. package/src/internal/compile-route-handler.ts +0 -194
  31. package/src/internal/context.ts +0 -65
  32. package/src/internal/serialization.ts +0 -91
  33. package/src/internal/utils.ts +0 -191
  34. package/src/meta.ts +0 -14
  35. package/src/open-api.ts +0 -273
  36. package/src/schema.ts +0 -271
  37. package/src/status.ts +0 -143
  38. package/src/testing.ts +0 -62
  39. package/src/transports/bun-transport.ts +0 -17
  40. package/src/transports/deno-transport.ts +0 -13
  41. package/src/types.ts +0 -1102
@@ -0,0 +1,712 @@
1
+ import { l as HttpStatus } from "./schema-DKqL09oQ.mjs";
2
+ import { StandardSchemaV1 } from "@standard-schema/spec";
3
+ import { OpenAPI } from "openapi-types";
4
+
5
+ //#region src/internal/utils.d.ts
6
+ declare const IsStatusResult: unique symbol;
7
+ //#endregion
8
+ //#region src/types.d.ts
9
+ /**
10
+ * Represents an App object. TAppData represents additional type information not
11
+ * always stored on the app object itself.
12
+ */
13
+ interface App<TAppData extends AppData = AppData> {
14
+ /**
15
+ * Internal references for implementing routing and calling registered
16
+ * handlers. Subject to breaking changes outside of major versions.
17
+ * @internal
18
+ */
19
+ "~zeta": {
20
+ /**
21
+ * Used for deduplication of hooks.
22
+ */
23
+ id: string;
24
+ /**
25
+ * When true, hooks defined on this app should be added to any app that
26
+ * imports this app.
27
+ */
28
+ exported?: boolean;
29
+ /**
30
+ * Path prefix from `CreateAppOptions.prefix`.
31
+ */
32
+ prefix: string;
33
+ /**
34
+ * List of routes registered with the app.
35
+ */
36
+ routes: {
37
+ [method: string]: {
38
+ [path: string]: RouterData;
39
+ };
40
+ };
41
+ /**
42
+ * Stores arrays of hooks registered on the app.
43
+ */
44
+ hooks: LifeCycleHooks;
45
+ };
46
+ /**
47
+ * Merge and simplify all the app routes into a single fetch function.
48
+ */
49
+ build: () => ServerSideFetch<TAppData["transport"]>;
50
+ /**
51
+ * Returns your application's OpenAPI spec. You do not need to listen to a
52
+ * port to call this method.
53
+ */
54
+ getOpenApiSpec: () => OpenAPI.Document;
55
+ /**
56
+ * Mark the app as "exported". When an exported app is `use`d by a
57
+ * parent app, the parent app will inherit all of it's hooks and modifiers.
58
+ *
59
+ * Regular, non-exported apps isolate their hooks and modifiers from the
60
+ * parent app (except for the global hooks, `onGlobalRequest`, `onGlobalError`, and
61
+ * `onGlobalAfterResponse`, which are always inherited by the parent app).
62
+ *
63
+ * The basic example is you can't access a decorated value from a parent app
64
+ * unless the child app is exported.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const child = createApp()
69
+ * .decorate("a", "A");
70
+ *
71
+ * const bad = createApp()
72
+ * .use(child)
73
+ * .get("/", ({ a }) => {
74
+ * console.log(a); // => undefined
75
+ * });
76
+ *
77
+ * const good = createApp()
78
+ * .use(child.export())
79
+ * .get("/", ({ a }) => {
80
+ * console.log(a); // => "A"
81
+ * });
82
+ * ```
83
+ */
84
+ export: () => App<MergeAppData<TAppData, {
85
+ exported: true;
86
+ }>>;
87
+ /**
88
+ * Detect the current environment and use `Bun.serve` or `Deno.serve` to serve the app over a port.
89
+ * @param port The port to listen on.
90
+ * @param cb Optional callback to be called when the server is ready.
91
+ */
92
+ listen: (port: number, cb?: () => void) => this;
93
+ /**
94
+ * Add a static value to the handler context.
95
+ */
96
+ decorate<TKey extends string, TValue>(key: TKey, value: TValue): App<Simplify<MergeAppData<TAppData, {
97
+ ctx: { [key in TKey]: Readonly<TValue> };
98
+ }>>>;
99
+ /**
100
+ * Add multiple static values to the handler context.
101
+ */
102
+ decorate<TValues extends Record<string, any>>(values: TValues): App<Simplify<MergeAppData<TAppData, {
103
+ ctx: TValues;
104
+ }>>>;
105
+ /**
106
+ * Add a callback that is called before the route is matched. If the callback
107
+ * returns a value, it will be merged into the `ctx` object. If the callback
108
+ * returns a `Response`, it will be returned immediately.
109
+ *
110
+ * @param callback The function to call.
111
+ */
112
+ onGlobalRequest(callback: (ctx: OnGlobalRequestContext<GetAppDataCtx<TAppData>>) => MaybePromise<Response | void>): this;
113
+ onGlobalRequest<TNewCtx extends Record<string, any>>(callback: (ctx: OnGlobalRequestContext<GetAppDataCtx<TAppData>>) => MaybePromise<TNewCtx>): App<MergeAppData<TAppData, {
114
+ ctx: TNewCtx;
115
+ }>>;
116
+ /**
117
+ * Add a callback that is called after the route is matched and before the
118
+ * inputs are validated. If the callback returns a value, it will be merged
119
+ * into the `ctx` object. If the callback returns a `Response`, it will be
120
+ * returned immediately.
121
+ *
122
+ * @param callback The function to call.
123
+ */
124
+ onTransform(callback: (ctx: OnTransformContext<GetAppDataCtx<TAppData>>) => MaybePromise<Response | void>): this;
125
+ onTransform<TNewCtx extends Record<string, any>>(callback: (ctx: OnTransformContext<GetAppDataCtx<TAppData>>) => MaybePromise<TNewCtx>): App<MergeAppData<TAppData, {
126
+ ctx: TNewCtx;
127
+ }>>;
128
+ /**
129
+ * Add a callback that is called after inputs are validated and before the
130
+ * handler is called. If the callback returns a value, it will be merged into
131
+ * the `ctx` object. If the callback returns a `Response`, it will be returned
132
+ * immediately.
133
+ *
134
+ * @param callback The function to call.
135
+ */
136
+ onBeforeHandle(callback: (ctx: OnBeforeHandleContext<GetAppDataCtx<TAppData>>) => MaybePromise<Response | void>): this;
137
+ onBeforeHandle<TNewCtx extends Record<string, any>>(callback: (ctx: OnBeforeHandleContext<GetAppDataCtx<TAppData>>) => MaybePromise<TNewCtx>): App<MergeAppData<TAppData, {
138
+ ctx: TNewCtx;
139
+ }>>;
140
+ /**
141
+ * Add a callback that is called after the handler is called and before the
142
+ * response is validated. If the callback returns a value, it replaces the
143
+ * response with it.
144
+ *
145
+ * @param callback The function to call.
146
+ */
147
+ onAfterHandle(callback: (ctx: AfterHandleContext<GetAppDataCtx<TAppData>>) => MaybePromise<unknown | void>): this;
148
+ /**
149
+ * Add a callback that is called after the response is validated and before it
150
+ * is sent to the client. The callback can return a `Response` if you want to
151
+ * change how the response is built.
152
+ *
153
+ * @param callback The function to call.
154
+ */
155
+ onMapResponse(callback: (ctx: AfterHandleContext<GetAppDataCtx<TAppData>>) => MaybePromise<unknown | void>): this;
156
+ /**
157
+ * Add a callback that is called when an error is thrown. The callback can
158
+ * optionally return a `Response`, which will be used to respond to the
159
+ * client.
160
+ *
161
+ * @param callback The function to call.
162
+ */
163
+ onGlobalError(callback: (ctx: OnGlobalErrorContext<GetAppDataCtx<TAppData>>) => MaybePromise<void>): this;
164
+ /**
165
+ * Add a callback that is called after the response is sent.
166
+ * @param callback The function to call.
167
+ */
168
+ onGlobalAfterResponse(callback: (ctx: AfterResponseContext<GetAppDataCtx<TAppData>>) => MaybePromise<void>): this;
169
+ /**
170
+ * Add an undocumented GET route to the app.
171
+ */
172
+ get<TPath extends BasePath>(path: TPath, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
173
+ routes: {
174
+ GET: { [path in TPath]: AnyDef };
175
+ };
176
+ }>>;
177
+ /**
178
+ * Add a documented GET route to the app.
179
+ */
180
+ get<TPath extends BasePath, TRouteDef extends RouteDef>(path: TPath, def: TRouteDef, handler: RouteHandler<TAppData, TPath, TRouteDef>): App<MergeAppData<TAppData, {
181
+ routes: {
182
+ GET: { [path in TPath]: TRouteDef };
183
+ };
184
+ }>>;
185
+ /**
186
+ * Add an undocumented POST route to the app.
187
+ */
188
+ post<TPath extends BasePath>(path: TPath, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
189
+ routes: {
190
+ POST: { [path in TPath]: AnyDef };
191
+ };
192
+ }>>;
193
+ /**
194
+ * Add a documented POST route to the app.
195
+ */
196
+ post<TPath extends BasePath, TRouteDef extends RouteDef>(path: TPath, def: TRouteDef, handler: RouteHandler<TAppData, TPath, TRouteDef>): App<MergeAppData<TAppData, {
197
+ routes: {
198
+ POST: { [path in TPath]: TRouteDef };
199
+ };
200
+ }>>;
201
+ /**
202
+ * Add an undocumented PUT route to the app.
203
+ */
204
+ put<TPath extends BasePath>(path: TPath, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
205
+ routes: {
206
+ PUT: { [path in TPath]: AnyDef };
207
+ };
208
+ }>>;
209
+ /**
210
+ * Add a documented PUT route to the app.
211
+ */
212
+ put<TPath extends BasePath, TRouteDef extends RouteDef>(path: TPath, def: TRouteDef, handler: RouteHandler<TAppData, TPath, TRouteDef>): App<MergeAppData<TAppData, {
213
+ routes: {
214
+ PUT: { [path in TPath]: TRouteDef };
215
+ };
216
+ }>>;
217
+ /**
218
+ * Add an undocumented DELETE route to the app.
219
+ */
220
+ delete<TPath extends BasePath>(path: TPath, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
221
+ routes: {
222
+ DELETE: { [path in TPath]: AnyDef };
223
+ };
224
+ }>>;
225
+ /**
226
+ * Add a documented DELETE route to the app.
227
+ */
228
+ delete<TPath extends BasePath, TRouteDef extends RouteDef>(path: TPath, def: TRouteDef, handler: RouteHandler<TAppData, TPath, TRouteDef>): App<MergeAppData<TAppData, {
229
+ routes: {
230
+ DELETE: { [path in TPath]: TRouteDef };
231
+ };
232
+ }>>;
233
+ /**
234
+ * Add an undocumented route to the app that responds to any method used.
235
+ */
236
+ any<TPath extends BasePath>(path: TPath, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
237
+ routes: {
238
+ ANY: { [path in TPath]: AnyDef };
239
+ };
240
+ }>>;
241
+ /**
242
+ * Add an documented route to the app that responds to any method used.
243
+ */
244
+ any<TPath extends BasePath, TRouteDef extends RouteDef>(path: TPath, def: TRouteDef, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
245
+ routes: {
246
+ ANY: { [path in TPath]: TRouteDef };
247
+ };
248
+ }>>;
249
+ /**
250
+ * Add an undocumented route to the app using a custom method.
251
+ */
252
+ method<TMethod extends string, TPath extends BasePath>(method: TMethod, path: TPath, handler: RouteHandler<TAppData, TPath, AnyDef>): App<MergeAppData<TAppData, {
253
+ routes: { [method in TMethod]: { [path in TPath]: AnyDef } };
254
+ }>>;
255
+ /**
256
+ * Add a documented route to the app using a custom method.
257
+ */
258
+ method<TMethod extends string, TPath extends BasePath, TRouteDef extends RouteDef>(method: TMethod, path: TPath, def: TRouteDef, handler: RouteHandler<TAppData, TPath, TRouteDef>): App<MergeAppData<TAppData, {
259
+ routes: { [method in TMethod]: { [path in TPath]: TRouteDef } };
260
+ }>>;
261
+ /**
262
+ * Mount another fetch function at `/**`.
263
+ */
264
+ mount(fetch: ServerSideFetch): App<MergeAppData<TAppData, {
265
+ routes: {
266
+ ANY: {
267
+ "/**": AnyDef;
268
+ };
269
+ };
270
+ }>>;
271
+ /**
272
+ * Mount another fetch function at `${path}/**`.
273
+ */
274
+ mount<TPath extends BasePath>(path: TPath, fetch: ServerSideFetch): App<MergeAppData<TAppData, {
275
+ routes: {
276
+ ANY: { [path in `${TPath}/**`]: AnyDef };
277
+ };
278
+ }>>;
279
+ /**
280
+ * Mount another fetch function at `${path}/**`.
281
+ */
282
+ mount<TPath extends BasePath, TRouteDef extends RouteDef>(path: TPath, def: TRouteDef, fetch: ServerSideFetch): App<MergeAppData<TAppData, {
283
+ routes: {
284
+ ANY: { [path in `${TPath}/**`]: TRouteDef };
285
+ };
286
+ }>>;
287
+ /**
288
+ * Add a subapp to the app.
289
+ */
290
+ use<TNewApp extends App>(app: TNewApp): App<UseAppData<TAppData, GetAppData<TNewApp>>>;
291
+ }
292
+ /**
293
+ * Given an `App`, return it's `AppData`.
294
+ */
295
+ type GetAppData<TApp extends App> = TApp extends App<infer TAppData> ? TAppData : never;
296
+ /**
297
+ * Given an `App`, return the routes defined on it.
298
+ */
299
+ type GetAppRoutes<TApp extends App> = GetAppData<TApp>["routes"];
300
+ /**
301
+ * Data stored internally for each route inside the `rou3` router.
302
+ */
303
+ type RouterData = {
304
+ def?: RouteDef;
305
+ route: string;
306
+ hooks: LifeCycleHooks;
307
+ compiledHandler: CompiledRouteHandler;
308
+ } & ({
309
+ fetch: ServerSideFetch;
310
+ } | {
311
+ handler: (ctx: OnBeforeHandleContext) => Promise<any>;
312
+ });
313
+ /**
314
+ * Function type called internally once a route is matched for a request.
315
+ */
316
+ type CompiledRouteHandler = (request: Request, ctx: any) => MaybePromise<Response>;
317
+ /**
318
+ * Type of the callback function used for each route.
319
+ */
320
+ type RouteHandler<TAppData extends AppData, TPath extends BasePath, TRouteDef extends RouteDef> = (ctx: BuildHandlerContext<TAppData, TPath, TRouteDef>) => MaybePromise<GetRouteHandlerReturnType<TRouteDef>>;
321
+ type GetRouteHandlerReturnType<TRouteDef extends RouteDef> = TRouteDef extends {
322
+ responses: symbol;
323
+ } ? any : TRouteDef extends {
324
+ responses: infer TResponses;
325
+ } ? TResponses extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TResponses> : TRouteDef["responses"] extends Record<number, StandardSchemaV1<any>> ? StatusResult : never : void;
326
+ /**
327
+ * Given an `App`, a method, and a route, return the handler function's type.
328
+ */
329
+ type GetRouteHandler<TApp extends App, TMethod extends keyof GetAppRoutes<TApp>, TRoute extends keyof GetAppRoutes<TApp>[TMethod]> = TRoute extends BasePath ? GetAppRoutes<TApp>[TMethod][TRoute] extends RouteDef ? RouteHandler<GetAppData<TApp>, TRoute, GetAppRoutes<TApp>[TMethod][TRoute]> : never : never;
330
+ /**
331
+ * Internal type used to store a hook on an app.
332
+ */
333
+ type LifeCycleHook<TCallback extends Function> = {
334
+ /**
335
+ * Used for deducplication.
336
+ */
337
+ id: string;
338
+ /**
339
+ * Where this plugin should be applied.
340
+ * - `global`: Global plugins are hoisted to the top-level app.
341
+ * - `local`: Local plugins are applied to the app they were added to.
342
+ * @default "local"
343
+ */
344
+ applyTo: "global" | "local";
345
+ /**
346
+ * The function called when the hook is triggered.
347
+ */
348
+ callback: TCallback;
349
+ };
350
+ /**
351
+ * Called immediately after receiving the request. Returned record is merged
352
+ * into the handler context.
353
+ */
354
+ type OnGlobalRequestHook = LifeCycleHook<(ctx: Simplify<OnGlobalRequestContext>) => Record<string, any> | void>;
355
+ /**
356
+ * Called before validating the request inputs. Returned record is merged into
357
+ * the handler context.
358
+ */
359
+ type OnTransformHook = LifeCycleHook<(ctx: Simplify<OnTransformContext>) => MaybePromise<Record<string, any> | void>>;
360
+ /**
361
+ * Called before calling the route handler. Returned record is merged into the
362
+ * handler context.
363
+ */
364
+ type OnBeforeHandleHook = LifeCycleHook<(ctx: Simplify<OnBeforeHandleContext>) => MaybePromise<Record<string, any> | void>>;
365
+ /**
366
+ * Called after calling the route handler. If there is a return value, it
367
+ * replaces the return value from the handler. Similar to the `onTransform` hook,
368
+ * but for the response.
369
+ */
370
+ type OnAfterHandleHook = LifeCycleHook<(ctx: Simplify<AfterHandleContext>) => MaybePromise<unknown | void>>;
371
+ /**
372
+ * Called after validating the handler return value. Used to transform the
373
+ * return value into a `Response`.
374
+ */
375
+ type OnMapResponseHook = LifeCycleHook<(ctx: Simplify<OnMapResponseContext>) => MaybePromise<Response | void>>;
376
+ /**
377
+ * Called if an error is thrown in any other hook other than `onGlobalAfterResponse`.
378
+ *
379
+ * Zeta will handle any `HttpError`s thrown, but you can handle your own errors
380
+ * here.
381
+ */
382
+ type OnGlobalErrorHook = LifeCycleHook<(ctx: Simplify<OnGlobalErrorContext>) => void>;
383
+ /**
384
+ * Called after the response is sent back to the client.
385
+ */
386
+ type OnGlobalAfterResponseHook = LifeCycleHook<(ctx: Simplify<AfterResponseContext>) => void>;
387
+ type LifeCycleHooks = {
388
+ onGlobalRequest?: OnGlobalRequestHook[];
389
+ onTransform?: OnTransformHook[];
390
+ onBeforeHandle?: OnBeforeHandleHook[];
391
+ onAfterHandle?: OnAfterHandleHook[];
392
+ onMapResponse?: OnMapResponseHook[];
393
+ onGlobalError?: OnGlobalErrorHook[];
394
+ onGlobalAfterResponse?: OnGlobalAfterResponseHook[];
395
+ };
396
+ type LifeCycleHookName = keyof LifeCycleHooks;
397
+ /**
398
+ * Base data type associated with each app.
399
+ */
400
+ type AppData = {
401
+ exported: boolean;
402
+ prefix: BasePrefix;
403
+ ctx: BaseCtx;
404
+ routes: BaseRoutes;
405
+ transport: AnyTransport;
406
+ };
407
+ /**
408
+ * Minimal data type that works with `AppData`.
409
+ */
410
+ type DefaultAppData = {
411
+ exported: false;
412
+ prefix: "";
413
+ ctx: {};
414
+ routes: {};
415
+ transport: AnyTransport;
416
+ };
417
+ /**
418
+ * Minimal data type that matches `AppData["ctx"]`.
419
+ */
420
+ type BaseCtx = Record<string, any>;
421
+ /**
422
+ * Minimal data type that matches `AppData["routes"]`.
423
+ */
424
+ type BaseRoutes = {
425
+ [method: string]: {
426
+ [path: BasePath]: RouteDef;
427
+ };
428
+ };
429
+ /**
430
+ * Minimal data type that matches `AppData["routes"][method][path]`, containing information about the route.
431
+ */
432
+ type RouteDef = Simplify<Omit<OpenAPI.Operation, "parameters" | "responses"> & {
433
+ headers?: StandardSchemaV1<Record<string, any>>;
434
+ params?: StandardSchemaV1<Record<string, any>>;
435
+ query?: StandardSchemaV1<Record<string, any>>;
436
+ body?: StandardSchemaV1;
437
+ responses?: StandardSchemaV1 | Record<number, StandardSchemaV1>;
438
+ }>;
439
+ /**
440
+ * Used for `TDef` when a route definition is not passed. Essentially removes type-safety from a route.
441
+ */
442
+ type AnyDef = {
443
+ headers: StandardSchemaV1<Record<string, string>>;
444
+ params: StandardSchemaV1<Record<string, string>>;
445
+ query: StandardSchemaV1<Record<string, string>>;
446
+ body: StandardSchemaV1<any>;
447
+ responses: any;
448
+ };
449
+ /**
450
+ * Base type representing what strings can be passed as a `prefix` when creating an app.
451
+ */
452
+ type BasePrefix = BasePath | "";
453
+ /**
454
+ * Base type representing what a route's string must look like.
455
+ */
456
+ type BasePath = `/${string}`;
457
+ interface RequestContext {
458
+ request: Request;
459
+ url: URL;
460
+ path: string;
461
+ method: string;
462
+ set: Setter;
463
+ }
464
+ /**
465
+ * `ctx` type used in the `onGlobalRequest` hook.
466
+ */
467
+ type OnGlobalRequestContext<TCtx extends BaseCtx = {}> = TCtx & RequestContext;
468
+ /**
469
+ * `ctx` type used in the `onTransform` hook.
470
+ */
471
+ type OnTransformContext<TCtx extends BaseCtx = {}> = OnGlobalRequestContext<TCtx> & {
472
+ route: string;
473
+ params?: Record<string, string>;
474
+ query?: Record<string, string>;
475
+ headers?: Record<string, string>;
476
+ body?: any;
477
+ };
478
+ /**
479
+ * `ctx` type used in the `onBeforeHandle` hook.
480
+ */
481
+ type OnBeforeHandleContext<TCtx extends BaseCtx = {}> = OnTransformContext<TCtx>;
482
+ /**
483
+ * `ctx` type used in the `onAfterHandle` hook.
484
+ */
485
+ type AfterHandleContext<TCtx extends BaseCtx = {}> = OnTransformContext<TCtx> & {
486
+ response?: unknown;
487
+ };
488
+ /**
489
+ * `ctx` type used in the `onMapResponse` hook.
490
+ */
491
+ type OnMapResponseContext<TCtx extends BaseCtx = {}> = AfterHandleContext<TCtx> & {};
492
+ /**
493
+ * `ctx` type used in the `onGlobalError` hook.
494
+ */
495
+ type OnGlobalErrorContext<TCtx extends BaseCtx = {}> = OnGlobalRequestContext<TCtx> & Partial<OnMapResponseContext> & {
496
+ error: unknown;
497
+ };
498
+ /**
499
+ * `ctx` type used in the `onGlobalAfterResponse` hook.
500
+ */
501
+ type AfterResponseContext<TCtx extends BaseCtx = {}> = OnGlobalRequestContext<TCtx> & Partial<OnMapResponseContext> & {
502
+ response: Response;
503
+ };
504
+ /**
505
+ * Given an `AppData` type, return the type of it's `ctx`.
506
+ */
507
+ type GetAppDataCtx<TAppData extends AppData> = TAppData extends {
508
+ ctx: infer TCtx;
509
+ } ? TCtx : never;
510
+ type StatusFn<TMap extends Record<any, any>> = TMap extends never ? never : <TStatus extends keyof TMap>(status: TStatus, body: StandardSchemaV1.InferInput<TMap[TStatus]>) => StatusResult;
511
+ type GetResponseStatusMap<TRouteDef extends RouteDef> = TRouteDef extends {
512
+ responses: unknown;
513
+ } ? TRouteDef["responses"] extends symbol ? Record<number, StandardSchemaV1<any, any>> : TRouteDef["responses"] extends StandardSchemaV1 ? {
514
+ 200: TRouteDef["responses"];
515
+ } : TRouteDef["responses"] extends Record<number | string, StandardSchemaV1> ? TRouteDef["responses"] : any : never;
516
+ type StatusResult = {
517
+ [IsStatusResult]: true;
518
+ status: number;
519
+ body: unknown;
520
+ };
521
+ /**
522
+ * Build the `ctx` type used for request handlers.
523
+ */
524
+ type BuildHandlerContext<TAppData extends AppData, TPath extends BasePath, TRouteDef extends RouteDef> = Simplify<Omit<OnBeforeHandleContext<GetAppDataCtx<TAppData>>, InputParams> & {
525
+ route: TPath;
526
+ } & GetRequestParamsOutputFromDef<TRouteDef> & {
527
+ status: StatusFn<GetResponseStatusMap<TRouteDef>>;
528
+ }>;
529
+ /**
530
+ * Given two `App`s, merge their data to match the behavior of `TParent.use(TChild)`.
531
+ */
532
+ type UseApp<TParent extends App, TChild extends App> = App<Simplify<UseAppData<GetAppData<TParent>, GetAppData<TChild>>>>;
533
+ /**
534
+ * Same as `UseApp`, but instead of app instances, it merges the `AppData` of each.
535
+ */
536
+ type UseAppData<TParentData extends AppData, TChildData extends AppData> = TChildData extends {
537
+ exported: true;
538
+ } ? MergeAppData<TParentData, Pick<ApplyAppDataPrefix<TChildData>, "ctx" | "routes">> : MergeAppData<TParentData, Pick<ApplyAppDataPrefix<TChildData>, "routes">>;
539
+ /**
540
+ * Merge two `App` types together. See `MergeAppData` for details.
541
+ */
542
+ type MergeApp<T1, T2> = T1 extends App<infer D1> ? T2 extends App<infer D2> ? App<Simplify<MergeAppData<D1, D2>>> : never : never;
543
+ /**
544
+ * Merge two `AppData` types together.
545
+ * - `prefix`: The second app's prefix overrides the first if present.
546
+ * - `ctx`: The second app's context gets merged with the first if present. Any
547
+ * existing keys are overwritten to match the second app's context.
548
+ * - `exported`: The second app's exported status overrides the first if
549
+ * present.
550
+ * - `routes`: See `MergeRoutes` for details.
551
+ * - `transport`: Use the original transport, it cannot be overridden
552
+ */
553
+ type MergeAppData<T1 extends AppData, T2 extends Partial<AppData>> = Simplify<{
554
+ prefix: T2["prefix"] extends string ? T2["prefix"] : T1["prefix"];
555
+ ctx: T2["ctx"] extends BaseCtx ? Simplify<Spread<T1["ctx"], T2["ctx"]>> : T1["ctx"];
556
+ exported: T2["exported"] extends boolean ? T2["exported"] : T1["exported"];
557
+ routes: T2["routes"] extends BaseRoutes ? Simplify<MergeRoutes<T1["routes"], T2["routes"]>> : T1["routes"];
558
+ transport: T1["transport"];
559
+ }>;
560
+ /**
561
+ * Merges two route objects together, 2 levels deep. If the same method/path
562
+ * combination exists in both apps, the second app's route overrides the first.
563
+ */
564
+ type MergeRoutes<A extends Record<string, any>, B extends Record<string, any>> = Simplify<Merge<A, B>>;
565
+ /**
566
+ * Given an app and a new prefix, return a new app type with app's original
567
+ * prefix applied to each route, and with the new prefix stored in the
568
+ * `AppData`.
569
+ */
570
+ type ApplyAppPrefix<TApp extends App, TNewPrefix extends BasePrefix = ""> = App<Simplify<ApplyAppDataPrefix<GetAppData<TApp>, TNewPrefix>>>;
571
+ /**
572
+ * Same as `ApplyAppPrefix`, but at the `AppData` level.
573
+ */
574
+ type ApplyAppDataPrefix<TAppData extends AppData, TNewPrefix extends BasePrefix = ""> = {
575
+ ctx: TAppData["ctx"];
576
+ exported: TAppData["exported"];
577
+ prefix: TNewPrefix;
578
+ routes: TAppData["prefix"] extends BasePath ? { [TMethod in keyof TAppData["routes"]]: Simplify<PrefixObjectKeys<TAppData["prefix"], TAppData["routes"][TMethod]>> } : TAppData["routes"];
579
+ transport: TAppData["transport"];
580
+ };
581
+ /**
582
+ * Given a route definition, return the input types of all the params.
583
+ */
584
+ type GetRequestParamsInputFromDef<TRouteDef extends RouteDef> = TRouteDef extends AnyDef ? {
585
+ headers?: Record<string, string>;
586
+ params?: Record<string, string>;
587
+ query?: Record<string, string>;
588
+ body?: any;
589
+ } : Simplify<{ [key in keyof GetDefParams<TRouteDef>]: TRouteDef[key] extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TRouteDef[key]> : never }>;
590
+ /**
591
+ * Given a set of routes, a method, and a route, return the input types of all
592
+ * the schemas in the route definition.
593
+ */
594
+ type GetRequestParamsInput<TRoutes extends BaseRoutes, TMethod extends keyof TRoutes, TRoute extends keyof TRoutes[TMethod]> = TRoute extends BasePath ? GetRequestParamsInputFromDef<TRoutes[TMethod][TRoute]> : never;
595
+ /**
596
+ * Given a route definition, return the input type of the response schema.
597
+ */
598
+ type GetResponseInputFromDef<TRouteDef extends RouteDef> = TRouteDef["responses"] extends undefined ? undefined : TRouteDef["responses"] extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TRouteDef["responses"]> : never;
599
+ /**
600
+ * Given a set of routes, a method, and a route, return the input type of the
601
+ * response schema in the route definition.
602
+ */
603
+ type GetResponseInput<TRoutes extends BaseRoutes, TMethod extends keyof TRoutes, TPath extends keyof TRoutes[TMethod]> = TPath extends BasePath ? GetResponseInputFromDef<TRoutes[TMethod][TPath]> : never;
604
+ /**
605
+ * Helper type for converting a schema or object containing schemas to their
606
+ * output types.
607
+ */
608
+ type ToStandardSchemaOutputs<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> : T extends { [key in keyof T]: any } ? { [key in keyof T]: ToStandardSchemaOutputs<T[key]> } : never;
609
+ /**
610
+ * Given a route definition, return the output type of the param schemas.
611
+ */
612
+ type GetRequestParamsOutputFromDef<TRouteDef extends RouteDef> = ToStandardSchemaOutputs<GetDefParams<TRouteDef>>;
613
+ /**
614
+ * Given a set of routes, a method, and a route, return the output type of the
615
+ * param schemas.
616
+ */
617
+ type GetRequestParamsOutput<TRoutes extends BaseRoutes, TMethod extends keyof TRoutes, TRoute extends keyof TRoutes[TMethod]> = TRoute extends BasePath ? GetRequestParamsOutputFromDef<TRoutes[TMethod][TRoute]> : never;
618
+ type SuccessStatusCodes = 200 | HttpStatus.Ok | 201 | HttpStatus.Created | 202 | HttpStatus.Accepted | 203 | HttpStatus.NonAuthoritativeInformation | 204 | HttpStatus.NoContent | 205 | HttpStatus.ResetContent | 206 | HttpStatus.PartialContent | 207 | HttpStatus.MultiStatus | 208 | HttpStatus.AlreadyReported | 226 | HttpStatus.ImUsed;
619
+ /**
620
+ * Given a `RouteDef`, return a union of all possible handler return values.
621
+ *
622
+ * If `responses` is defined, it will be a discriminated union of objects
623
+ * containing the status and body.
624
+ *
625
+ * If only `response` is defined, it will be the output of that schema.
626
+ *
627
+ * If neither is defined, it will be `void`.
628
+ */
629
+ type GetResponseOutputFromDef<TRouteDef extends RouteDef> = TRouteDef["responses"] extends undefined ? undefined : TRouteDef["responses"] extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TRouteDef["responses"]> : { [key in SuccessStatusCodes & keyof TRouteDef["responses"]]: TRouteDef["responses"][key] extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TRouteDef["responses"][key]> : unknown }[SuccessStatusCodes & keyof TRouteDef["responses"]];
630
+ /**
631
+ * Given a set of routes, a method, and a route, return the output type of the
632
+ * response schema.
633
+ */
634
+ type GetResponseOutput<TRoutes extends BaseRoutes, TMethod extends keyof TRoutes, TPath extends keyof TRoutes[TMethod]> = TPath extends BasePath ? GetResponseOutputFromDef<TRoutes[TMethod][TPath]> : 1;
635
+ type InputParams = "headers" | "params" | "query" | "body";
636
+ /**
637
+ * Given a route definition, return an object with only the input parameeters.
638
+ */
639
+ type GetDefParams<TRouteDef extends RouteDef> = { [key in InputParams & keyof TRouteDef]: TRouteDef[key] };
640
+ /**
641
+ * To generate open API docs, Zeta needs to know how process and extract
642
+ * some information from your schema. This adapter is responsible for
643
+ * providing additional functions for parsing your schema.
644
+ */
645
+ interface SchemaAdapter {
646
+ /**
647
+ * Convert a standard schema definition to it's JSON schema.
648
+ * @param schema The schema to convert.
649
+ * @returns The JSON schema.
650
+ */
651
+ toJsonSchema: (schema: any) => any;
652
+ getMeta: (schema: StandardSchemaV1) => Record<string, any> | undefined;
653
+ }
654
+ interface Transport<TFetchArgs extends [request: Request, ...params: any[]] = [request: Request]> {
655
+ /**
656
+ * Actually bind the server to a local port for hosting.
657
+ */
658
+ listen: (port: number, fetch: ServerSideFetch, cb?: () => void) => void;
659
+ /**
660
+ * Callback where the ctx object can be modified based on the args provided to the fetch function.
661
+ */
662
+ decorate?: (ctx: any, ...fetchArgs: TFetchArgs) => void;
663
+ }
664
+ type AnyTransport = Transport<[request: Request, ...params: any[]]>;
665
+ /**
666
+ * Basic object type used to store custom status and headers set while handling the request.
667
+ */
668
+ interface Setter {
669
+ /**
670
+ * Set this value to change the status code returned.
671
+ */
672
+ status: number;
673
+ /**
674
+ * Set a value on this header to change which headers are sent in the response.
675
+ */
676
+ headers: Record<string, string>;
677
+ }
678
+ /**
679
+ * Given a union of objects, combine them into a single object that's easy to read.
680
+ */
681
+ type Simplify<T> = T extends {
682
+ [key: string]: any;
683
+ } ? { [K in keyof T]: T[K] } : T;
684
+ /**
685
+ * Returns either a Promise of a type or just the type itself.
686
+ */
687
+ type MaybePromise<T> = Promise<T> | T;
688
+ /**
689
+ * A function that, given a request, returns a response. This type is compliant with WinterCG.
690
+ */
691
+ type ServerSideFetch<TTransport extends AnyTransport = Transport> = TTransport extends Transport<infer Params> ? (...args: Params) => MaybePromise<Response> : never;
692
+ /**
693
+ * Apply a string prefix to all the keys of an object.
694
+ */
695
+ type PrefixObjectKeys<TPrefix extends string, TObject extends Record<string, unknown>> = { [K in keyof TObject as `${TPrefix}${string & K extends "/" ? "" : string & K}`]: TObject[K] };
696
+ /**
697
+ * A helper type that models a single-level object spread: { ...L, ...R }
698
+ * It takes all properties from R, and all properties from L that are not in R.
699
+ */
700
+ type Spread<L, R> = Omit<L, keyof R> & R;
701
+ /**
702
+ * Merges two objects, A and B, two levels deep.
703
+ *
704
+ * It combines the keys from both A and B.
705
+ * - If a key exists only in A or only in B, it's carried over.
706
+ * - If a key exists in *both* A and B, it recursively merges their
707
+ * values using a single-level spread (`Spread<A[K], B[K]>`).
708
+ * This ensures properties from B's inner objects overwrite those from A's.
709
+ */
710
+ type Merge<A, B> = Omit<A, keyof B> & { [K in keyof B]: K extends keyof A ? Simplify<Spread<A[K], B[K]>> : B[K] };
711
+ //#endregion
712
+ export { RouteHandler as $, LifeCycleHook as A, OnBeforeHandleHook as B, GetResponseInput as C, GetResponseStatusMap as D, GetResponseOutputFromDef as E, MergeApp as F, OnGlobalRequestHook as G, OnGlobalErrorContext as H, MergeAppData as I, OnTransformContext as J, OnMapResponseContext as K, MergeRoutes as L, LifeCycleHooks as M, MaybePromise as N, GetRouteHandler as O, Merge as P, RouteDef as Q, OnAfterHandleHook as R, GetRequestParamsOutputFromDef as S, GetResponseOutput as T, OnGlobalErrorHook as U, OnGlobalAfterResponseHook as V, OnGlobalRequestContext as W, PrefixObjectKeys as X, OnTransformHook as Y, RequestContext as Z, GetAppDataCtx as _, App as a, StatusFn as at, GetRequestParamsInputFromDef as b, ApplyAppPrefix as c, UseApp as ct, BasePrefix as d, RouterData as et, BaseRoutes as f, GetAppData as g, DefaultAppData as h, AnyTransport as i, Simplify as it, LifeCycleHookName as j, GetRouteHandlerReturnType as k, BaseCtx as l, UseAppData as lt, CompiledRouteHandler as m, AfterResponseContext as n, ServerSideFetch as nt, AppData as o, StatusResult as ot, BuildHandlerContext as p, OnMapResponseHook as q, AnyDef as r, Setter as rt, ApplyAppDataPrefix as s, Transport as st, AfterHandleContext as t, SchemaAdapter as tt, BasePath as u, GetAppRoutes as v, GetResponseInputFromDef as w, GetRequestParamsOutput as x, GetRequestParamsInput as y, OnBeforeHandleContext as z };