@kaito-http/core 4.0.0-beta.10 → 4.0.0-beta.12
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/dist/index.cjs +16 -21
- package/dist/index.d.cts +88 -94
- package/dist/index.d.ts +88 -94
- package/dist/index.js +16 -21
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -147,12 +147,13 @@ var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "un
|
|
|
147
147
|
// src/router/router.ts
|
|
148
148
|
var Router = class _Router {
|
|
149
149
|
state;
|
|
150
|
-
static create = (config) =>
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
150
|
+
static create = (config = {}) => {
|
|
151
|
+
return new _Router({
|
|
152
|
+
through: async (context) => context,
|
|
153
|
+
routes: /* @__PURE__ */ new Set(),
|
|
154
|
+
config
|
|
155
|
+
});
|
|
156
|
+
};
|
|
156
157
|
constructor(state) {
|
|
157
158
|
this.state = state;
|
|
158
159
|
}
|
|
@@ -171,10 +172,7 @@ var Router = class _Router {
|
|
|
171
172
|
routes: /* @__PURE__ */ new Set([...this.state.routes, merged])
|
|
172
173
|
});
|
|
173
174
|
};
|
|
174
|
-
params = (
|
|
175
|
-
...this.state,
|
|
176
|
-
paramsSchema: import_zod.z.object(spec)
|
|
177
|
-
});
|
|
175
|
+
params = () => this;
|
|
178
176
|
merge = (pathPrefix, other) => {
|
|
179
177
|
const newRoutes = [...other.state.routes].map((route) => ({
|
|
180
178
|
...route,
|
|
@@ -235,7 +233,7 @@ var Router = class _Router {
|
|
|
235
233
|
});
|
|
236
234
|
}
|
|
237
235
|
const findRoute = _Router.getFindRoute(methodToRoutesMap);
|
|
238
|
-
const handle = async (req) => {
|
|
236
|
+
const handle = async (req, ...args) => {
|
|
239
237
|
const url = new URL(req.url);
|
|
240
238
|
const method = req.method;
|
|
241
239
|
const { route, params: rawParams } = findRoute(method, url.pathname);
|
|
@@ -252,16 +250,15 @@ var Router = class _Router {
|
|
|
252
250
|
try {
|
|
253
251
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
254
252
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
255
|
-
const params = route.router.state.paramsSchema ? route.router.state.paramsSchema.parse(rawParams) : rawParams;
|
|
256
253
|
const ctx = await route.router.state.through(
|
|
257
|
-
await this.state.config.getContext?.(request, head) ?? null,
|
|
258
|
-
|
|
254
|
+
await this.state.config.getContext?.(request, head, ...args) ?? null,
|
|
255
|
+
rawParams
|
|
259
256
|
);
|
|
260
257
|
const result = await route.run({
|
|
261
258
|
ctx,
|
|
262
259
|
body,
|
|
263
260
|
query,
|
|
264
|
-
params
|
|
261
|
+
params: rawParams
|
|
265
262
|
});
|
|
266
263
|
if (result instanceof Response) {
|
|
267
264
|
if (isNodeLikeDev) {
|
|
@@ -315,7 +312,7 @@ var Router = class _Router {
|
|
|
315
312
|
}
|
|
316
313
|
}
|
|
317
314
|
};
|
|
318
|
-
return async (request) => {
|
|
315
|
+
return async (request, ...args) => {
|
|
319
316
|
if (this.state.config.before) {
|
|
320
317
|
const result = await this.state.config.before(request);
|
|
321
318
|
if (result instanceof Response) {
|
|
@@ -328,7 +325,7 @@ var Router = class _Router {
|
|
|
328
325
|
return result;
|
|
329
326
|
}
|
|
330
327
|
}
|
|
331
|
-
const response = await handle(request);
|
|
328
|
+
const response = await handle(request, ...args);
|
|
332
329
|
if (this.state.config.transform) {
|
|
333
330
|
const transformed = await this.state.config.transform(request, response);
|
|
334
331
|
if (transformed instanceof Response) {
|
|
@@ -429,10 +426,8 @@ var Router = class _Router {
|
|
|
429
426
|
};
|
|
430
427
|
};
|
|
431
428
|
|
|
432
|
-
// src/
|
|
433
|
-
|
|
434
|
-
return Router.create(config);
|
|
435
|
-
}
|
|
429
|
+
// src/index.ts
|
|
430
|
+
var create = Router.create;
|
|
436
431
|
// Annotate the CommonJS export names for ESM import in node:
|
|
437
432
|
0 && (module.exports = {
|
|
438
433
|
KaitoError,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z
|
|
1
|
+
import { z } from 'zod';
|
|
2
2
|
import { KaitoSSEResponse } from './stream/stream.cjs';
|
|
3
3
|
|
|
4
4
|
declare class WrappedError<T> extends Error {
|
|
@@ -72,55 +72,59 @@ declare class KaitoHead {
|
|
|
72
72
|
get touched(): boolean;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
data: null;
|
|
82
|
-
message: string;
|
|
83
|
-
};
|
|
84
|
-
type SuccessfulAPIResponse<T> = {
|
|
85
|
-
success: true;
|
|
86
|
-
data: T;
|
|
87
|
-
};
|
|
88
|
-
type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
|
|
89
|
-
type AnyResponse = APIResponse<unknown>;
|
|
90
|
-
type MakeOptional<T, K extends keyof T> = T extends T ? Omit<T, K> & Partial<Pick<T, K>> : never;
|
|
91
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
92
|
-
type KaitoMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
93
|
-
type NotReadonly<T> = {
|
|
94
|
-
-readonly [K in keyof T]: T[K];
|
|
75
|
+
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextTo, infer Result, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyOutput> ? Route<ContextTo, Result, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyOutput> : never;
|
|
76
|
+
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
77
|
+
type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
|
|
78
|
+
routes: Set<Routes>;
|
|
79
|
+
through: (context: unknown, params: RequiredParams) => Promise<ContextTo>;
|
|
80
|
+
config: KaitoConfig<ContextFrom, Input>;
|
|
95
81
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
82
|
+
declare class Router<ContextFrom, ContextTo, RequiredParams extends string, R extends AnyRoute, Input extends readonly unknown[]> {
|
|
83
|
+
private readonly state;
|
|
84
|
+
static create: <Context = null, Input_1 extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input_1>) => Router<Context, Context, never, never, Input_1>;
|
|
85
|
+
protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, R, Input>);
|
|
86
|
+
get routes(): Set<R>;
|
|
87
|
+
private readonly add;
|
|
88
|
+
readonly params: [R] extends [never] ? <NextParams extends string>() => Router<ContextFrom, ContextTo, NextParams, R, Input> : 'router.params() can only be called before any routes are attached';
|
|
89
|
+
readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends string, OtherRoutes extends AnyRoute>(pathPrefix: [NextRequiredParams] extends [ExtractRouteParams<PathPrefix> | RequiredParams] ? PathPrefix : `Missing ${Exclude<NextRequiredParams, ExtractRouteParams<PathPrefix> | RequiredParams>}`, other: Router<ContextFrom, unknown, NextRequiredParams, OtherRoutes, Input>) => Router<ContextFrom, ContextTo, RequiredParams, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>, Input>;
|
|
90
|
+
protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
|
|
91
|
+
route?: never;
|
|
92
|
+
params?: never;
|
|
93
|
+
} | {
|
|
94
|
+
route: R_1;
|
|
95
|
+
params: Record<string, string>;
|
|
96
|
+
};
|
|
97
|
+
private static buildQuerySchema;
|
|
98
|
+
serve: () => (request: Request, ...args: Input) => Promise<Response>;
|
|
99
|
+
openapi: (highLevelSpec: {
|
|
100
|
+
info: {
|
|
101
|
+
version: string;
|
|
102
|
+
title: string;
|
|
103
|
+
description?: string;
|
|
104
|
+
};
|
|
105
|
+
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
106
|
+
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
107
|
+
private readonly method;
|
|
108
|
+
get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, Input>;
|
|
109
|
+
post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, Input>;
|
|
110
|
+
put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, Input>;
|
|
111
|
+
patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, Input>;
|
|
112
|
+
delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, Input>;
|
|
113
|
+
head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, Input>;
|
|
114
|
+
options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
|
|
115
|
+
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
|
|
116
|
+
}
|
|
113
117
|
|
|
114
|
-
type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
118
|
+
type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
|
|
119
|
+
params: Record<Params, string>;
|
|
115
120
|
ctx: Context;
|
|
116
|
-
body: BodyOutput;
|
|
117
121
|
query: QueryOutput;
|
|
118
|
-
|
|
122
|
+
body: BodyOutput;
|
|
119
123
|
};
|
|
120
124
|
type AnyQuery = {
|
|
121
125
|
[key in string]: any;
|
|
122
126
|
};
|
|
123
|
-
type Through<From, To, RequiredParams extends
|
|
127
|
+
type Through<From, To, RequiredParams extends string> = (context: From, params: Record<RequiredParams, string>) => Promise<To>;
|
|
124
128
|
type SSEOutputSpec<Result> = {
|
|
125
129
|
type: 'sse';
|
|
126
130
|
schema: z.Schema<Result>;
|
|
@@ -135,7 +139,7 @@ type OutputSpec<Result> = {
|
|
|
135
139
|
description?: string;
|
|
136
140
|
body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
|
|
137
141
|
};
|
|
138
|
-
type Route<ContextTo, Result, Path extends string, AdditionalParams extends
|
|
142
|
+
type Route<ContextTo, Result, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query, Body> = {
|
|
139
143
|
body?: z.Schema<Body>;
|
|
140
144
|
query?: {
|
|
141
145
|
[Key in keyof Query]: z.Schema<Query[Key]>;
|
|
@@ -143,19 +147,33 @@ type Route<ContextTo, Result, Path extends string, AdditionalParams extends Reco
|
|
|
143
147
|
path: Path;
|
|
144
148
|
method: Method;
|
|
145
149
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
146
|
-
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute>;
|
|
147
|
-
run(data: RouteRunData<ExtractRouteParams<Path>
|
|
150
|
+
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute, any>;
|
|
151
|
+
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
148
152
|
};
|
|
149
153
|
type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
150
154
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
155
|
+
/**
|
|
156
|
+
* A helper to check if the environment is Node.js-like and the `NODE_ENV` environment variable is set to `'development'`
|
|
157
|
+
*/
|
|
158
|
+
declare const isNodeLikeDev: boolean;
|
|
159
|
+
type ErroredAPIResponse = {
|
|
160
|
+
success: false;
|
|
161
|
+
data: null;
|
|
162
|
+
message: string;
|
|
158
163
|
};
|
|
164
|
+
type SuccessfulAPIResponse<T> = {
|
|
165
|
+
success: true;
|
|
166
|
+
data: T;
|
|
167
|
+
};
|
|
168
|
+
type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
|
|
169
|
+
type AnyResponse = APIResponse<unknown>;
|
|
170
|
+
type MakeOptional<T, K extends keyof T> = T extends T ? Omit<T, K> & Partial<Pick<T, K>> : never;
|
|
171
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
172
|
+
type KaitoMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
173
|
+
type NotReadonly<T> = {
|
|
174
|
+
-readonly [K in keyof T]: T[K];
|
|
175
|
+
};
|
|
176
|
+
type ExtractRouteParams<T extends string> = string extends T ? string : T extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<Rest> : T extends `${string}:${infer Param}` ? Param : never;
|
|
159
177
|
/**
|
|
160
178
|
* Accepts a router instance, and returns a union of all the routes in the router
|
|
161
179
|
*
|
|
@@ -166,46 +184,21 @@ type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, u
|
|
|
166
184
|
* type Routes = InferRoutes<typeof app>;
|
|
167
185
|
* ```
|
|
168
186
|
*/
|
|
169
|
-
type InferRoutes<R extends Router<
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
params?: never;
|
|
183
|
-
} | {
|
|
184
|
-
route: R_1;
|
|
185
|
-
params: Record<string, string>;
|
|
186
|
-
};
|
|
187
|
-
private static buildQuerySchema;
|
|
188
|
-
serve: () => (request: Request) => Promise<Response>;
|
|
189
|
-
openapi: (highLevelSpec: {
|
|
190
|
-
info: {
|
|
191
|
-
version: string;
|
|
192
|
-
title: string;
|
|
193
|
-
description?: string;
|
|
194
|
-
};
|
|
195
|
-
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
196
|
-
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never>>;
|
|
197
|
-
private readonly method;
|
|
198
|
-
get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>>;
|
|
199
|
-
post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>>;
|
|
200
|
-
put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>>;
|
|
201
|
-
patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>>;
|
|
202
|
-
delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>>;
|
|
203
|
-
head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>>;
|
|
204
|
-
options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>>;
|
|
205
|
-
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R>;
|
|
206
|
-
}
|
|
187
|
+
type InferRoutes<R extends Router<never, never, never, never, never>> = R extends Router<any, any, any, infer R extends AnyRoute, any> ? R : never;
|
|
188
|
+
/**
|
|
189
|
+
* A function that is called to get the context for a request.
|
|
190
|
+
*
|
|
191
|
+
* This is useful for things like authentication, to pass in a database connection, etc.
|
|
192
|
+
*
|
|
193
|
+
* It's fine for this function to throw; if it does, the error is passed to the `onError` function.
|
|
194
|
+
*
|
|
195
|
+
* @param req - The kaito request object, which contains the request method, url, headers, etc
|
|
196
|
+
* @param head - The kaito head object, which contains getters and setters for headers and status
|
|
197
|
+
* @returns The context for your routes
|
|
198
|
+
*/
|
|
199
|
+
type GetContext<Result, Input extends readonly unknown[]> = (req: KaitoRequest, head: KaitoHead, ...args: Input) => MaybePromise<Result>;
|
|
207
200
|
|
|
208
|
-
type KaitoConfig<ContextFrom> = {
|
|
201
|
+
type KaitoConfig<ContextFrom, Input extends readonly unknown[]> = {
|
|
209
202
|
/**
|
|
210
203
|
* A function that is called to get the context for a request.
|
|
211
204
|
*
|
|
@@ -213,7 +206,7 @@ type KaitoConfig<ContextFrom> = {
|
|
|
213
206
|
*
|
|
214
207
|
* It's fine for this function to throw; if it does, the error is passed to the `onError` function.
|
|
215
208
|
*/
|
|
216
|
-
getContext?: GetContext<ContextFrom>;
|
|
209
|
+
getContext?: GetContext<ContextFrom, Input>;
|
|
217
210
|
/**
|
|
218
211
|
* A function that is called when an error occurs inside a route handler.
|
|
219
212
|
*
|
|
@@ -263,14 +256,15 @@ type KaitoConfig<ContextFrom> = {
|
|
|
263
256
|
*/
|
|
264
257
|
transform?: (req: Request, res: Response) => MaybePromise<Response | void | undefined>;
|
|
265
258
|
};
|
|
259
|
+
|
|
266
260
|
/**
|
|
267
|
-
*
|
|
261
|
+
* Helper function for instantiating a Kaito router
|
|
268
262
|
*
|
|
269
263
|
* This is the starting point for any Kaito application
|
|
270
264
|
*
|
|
271
265
|
* @param config - The configuration for the router
|
|
272
266
|
* @returns A new Kaito router
|
|
273
267
|
*/
|
|
274
|
-
declare
|
|
268
|
+
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
275
269
|
|
|
276
270
|
export { type APIResponse, type AnyQuery, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MakeOptional, type MaybePromise, type NotReadonly, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z
|
|
1
|
+
import { z } from 'zod';
|
|
2
2
|
import { KaitoSSEResponse } from './stream/stream.js';
|
|
3
3
|
|
|
4
4
|
declare class WrappedError<T> extends Error {
|
|
@@ -72,55 +72,59 @@ declare class KaitoHead {
|
|
|
72
72
|
get touched(): boolean;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
data: null;
|
|
82
|
-
message: string;
|
|
83
|
-
};
|
|
84
|
-
type SuccessfulAPIResponse<T> = {
|
|
85
|
-
success: true;
|
|
86
|
-
data: T;
|
|
87
|
-
};
|
|
88
|
-
type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
|
|
89
|
-
type AnyResponse = APIResponse<unknown>;
|
|
90
|
-
type MakeOptional<T, K extends keyof T> = T extends T ? Omit<T, K> & Partial<Pick<T, K>> : never;
|
|
91
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
92
|
-
type KaitoMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
93
|
-
type NotReadonly<T> = {
|
|
94
|
-
-readonly [K in keyof T]: T[K];
|
|
75
|
+
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextTo, infer Result, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyOutput> ? Route<ContextTo, Result, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyOutput> : never;
|
|
76
|
+
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
77
|
+
type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
|
|
78
|
+
routes: Set<Routes>;
|
|
79
|
+
through: (context: unknown, params: RequiredParams) => Promise<ContextTo>;
|
|
80
|
+
config: KaitoConfig<ContextFrom, Input>;
|
|
95
81
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
82
|
+
declare class Router<ContextFrom, ContextTo, RequiredParams extends string, R extends AnyRoute, Input extends readonly unknown[]> {
|
|
83
|
+
private readonly state;
|
|
84
|
+
static create: <Context = null, Input_1 extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input_1>) => Router<Context, Context, never, never, Input_1>;
|
|
85
|
+
protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, R, Input>);
|
|
86
|
+
get routes(): Set<R>;
|
|
87
|
+
private readonly add;
|
|
88
|
+
readonly params: [R] extends [never] ? <NextParams extends string>() => Router<ContextFrom, ContextTo, NextParams, R, Input> : 'router.params() can only be called before any routes are attached';
|
|
89
|
+
readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends string, OtherRoutes extends AnyRoute>(pathPrefix: [NextRequiredParams] extends [ExtractRouteParams<PathPrefix> | RequiredParams] ? PathPrefix : `Missing ${Exclude<NextRequiredParams, ExtractRouteParams<PathPrefix> | RequiredParams>}`, other: Router<ContextFrom, unknown, NextRequiredParams, OtherRoutes, Input>) => Router<ContextFrom, ContextTo, RequiredParams, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>, Input>;
|
|
90
|
+
protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
|
|
91
|
+
route?: never;
|
|
92
|
+
params?: never;
|
|
93
|
+
} | {
|
|
94
|
+
route: R_1;
|
|
95
|
+
params: Record<string, string>;
|
|
96
|
+
};
|
|
97
|
+
private static buildQuerySchema;
|
|
98
|
+
serve: () => (request: Request, ...args: Input) => Promise<Response>;
|
|
99
|
+
openapi: (highLevelSpec: {
|
|
100
|
+
info: {
|
|
101
|
+
version: string;
|
|
102
|
+
title: string;
|
|
103
|
+
description?: string;
|
|
104
|
+
};
|
|
105
|
+
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
106
|
+
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
107
|
+
private readonly method;
|
|
108
|
+
get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, Input>;
|
|
109
|
+
post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, Input>;
|
|
110
|
+
put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, Input>;
|
|
111
|
+
patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, Input>;
|
|
112
|
+
delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, Input>;
|
|
113
|
+
head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, Input>;
|
|
114
|
+
options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
|
|
115
|
+
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
|
|
116
|
+
}
|
|
113
117
|
|
|
114
|
-
type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
118
|
+
type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
|
|
119
|
+
params: Record<Params, string>;
|
|
115
120
|
ctx: Context;
|
|
116
|
-
body: BodyOutput;
|
|
117
121
|
query: QueryOutput;
|
|
118
|
-
|
|
122
|
+
body: BodyOutput;
|
|
119
123
|
};
|
|
120
124
|
type AnyQuery = {
|
|
121
125
|
[key in string]: any;
|
|
122
126
|
};
|
|
123
|
-
type Through<From, To, RequiredParams extends
|
|
127
|
+
type Through<From, To, RequiredParams extends string> = (context: From, params: Record<RequiredParams, string>) => Promise<To>;
|
|
124
128
|
type SSEOutputSpec<Result> = {
|
|
125
129
|
type: 'sse';
|
|
126
130
|
schema: z.Schema<Result>;
|
|
@@ -135,7 +139,7 @@ type OutputSpec<Result> = {
|
|
|
135
139
|
description?: string;
|
|
136
140
|
body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
|
|
137
141
|
};
|
|
138
|
-
type Route<ContextTo, Result, Path extends string, AdditionalParams extends
|
|
142
|
+
type Route<ContextTo, Result, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query, Body> = {
|
|
139
143
|
body?: z.Schema<Body>;
|
|
140
144
|
query?: {
|
|
141
145
|
[Key in keyof Query]: z.Schema<Query[Key]>;
|
|
@@ -143,19 +147,33 @@ type Route<ContextTo, Result, Path extends string, AdditionalParams extends Reco
|
|
|
143
147
|
path: Path;
|
|
144
148
|
method: Method;
|
|
145
149
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
146
|
-
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute>;
|
|
147
|
-
run(data: RouteRunData<ExtractRouteParams<Path>
|
|
150
|
+
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute, any>;
|
|
151
|
+
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
148
152
|
};
|
|
149
153
|
type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
150
154
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
155
|
+
/**
|
|
156
|
+
* A helper to check if the environment is Node.js-like and the `NODE_ENV` environment variable is set to `'development'`
|
|
157
|
+
*/
|
|
158
|
+
declare const isNodeLikeDev: boolean;
|
|
159
|
+
type ErroredAPIResponse = {
|
|
160
|
+
success: false;
|
|
161
|
+
data: null;
|
|
162
|
+
message: string;
|
|
158
163
|
};
|
|
164
|
+
type SuccessfulAPIResponse<T> = {
|
|
165
|
+
success: true;
|
|
166
|
+
data: T;
|
|
167
|
+
};
|
|
168
|
+
type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
|
|
169
|
+
type AnyResponse = APIResponse<unknown>;
|
|
170
|
+
type MakeOptional<T, K extends keyof T> = T extends T ? Omit<T, K> & Partial<Pick<T, K>> : never;
|
|
171
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
172
|
+
type KaitoMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
173
|
+
type NotReadonly<T> = {
|
|
174
|
+
-readonly [K in keyof T]: T[K];
|
|
175
|
+
};
|
|
176
|
+
type ExtractRouteParams<T extends string> = string extends T ? string : T extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<Rest> : T extends `${string}:${infer Param}` ? Param : never;
|
|
159
177
|
/**
|
|
160
178
|
* Accepts a router instance, and returns a union of all the routes in the router
|
|
161
179
|
*
|
|
@@ -166,46 +184,21 @@ type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, u
|
|
|
166
184
|
* type Routes = InferRoutes<typeof app>;
|
|
167
185
|
* ```
|
|
168
186
|
*/
|
|
169
|
-
type InferRoutes<R extends Router<
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
params?: never;
|
|
183
|
-
} | {
|
|
184
|
-
route: R_1;
|
|
185
|
-
params: Record<string, string>;
|
|
186
|
-
};
|
|
187
|
-
private static buildQuerySchema;
|
|
188
|
-
serve: () => (request: Request) => Promise<Response>;
|
|
189
|
-
openapi: (highLevelSpec: {
|
|
190
|
-
info: {
|
|
191
|
-
version: string;
|
|
192
|
-
title: string;
|
|
193
|
-
description?: string;
|
|
194
|
-
};
|
|
195
|
-
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
196
|
-
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never>>;
|
|
197
|
-
private readonly method;
|
|
198
|
-
get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>>;
|
|
199
|
-
post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>>;
|
|
200
|
-
put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>>;
|
|
201
|
-
patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>>;
|
|
202
|
-
delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>>;
|
|
203
|
-
head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>>;
|
|
204
|
-
options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<ExtractRouteParams<Path> & RequiredParams, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router">) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>>;
|
|
205
|
-
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R>;
|
|
206
|
-
}
|
|
187
|
+
type InferRoutes<R extends Router<never, never, never, never, never>> = R extends Router<any, any, any, infer R extends AnyRoute, any> ? R : never;
|
|
188
|
+
/**
|
|
189
|
+
* A function that is called to get the context for a request.
|
|
190
|
+
*
|
|
191
|
+
* This is useful for things like authentication, to pass in a database connection, etc.
|
|
192
|
+
*
|
|
193
|
+
* It's fine for this function to throw; if it does, the error is passed to the `onError` function.
|
|
194
|
+
*
|
|
195
|
+
* @param req - The kaito request object, which contains the request method, url, headers, etc
|
|
196
|
+
* @param head - The kaito head object, which contains getters and setters for headers and status
|
|
197
|
+
* @returns The context for your routes
|
|
198
|
+
*/
|
|
199
|
+
type GetContext<Result, Input extends readonly unknown[]> = (req: KaitoRequest, head: KaitoHead, ...args: Input) => MaybePromise<Result>;
|
|
207
200
|
|
|
208
|
-
type KaitoConfig<ContextFrom> = {
|
|
201
|
+
type KaitoConfig<ContextFrom, Input extends readonly unknown[]> = {
|
|
209
202
|
/**
|
|
210
203
|
* A function that is called to get the context for a request.
|
|
211
204
|
*
|
|
@@ -213,7 +206,7 @@ type KaitoConfig<ContextFrom> = {
|
|
|
213
206
|
*
|
|
214
207
|
* It's fine for this function to throw; if it does, the error is passed to the `onError` function.
|
|
215
208
|
*/
|
|
216
|
-
getContext?: GetContext<ContextFrom>;
|
|
209
|
+
getContext?: GetContext<ContextFrom, Input>;
|
|
217
210
|
/**
|
|
218
211
|
* A function that is called when an error occurs inside a route handler.
|
|
219
212
|
*
|
|
@@ -263,14 +256,15 @@ type KaitoConfig<ContextFrom> = {
|
|
|
263
256
|
*/
|
|
264
257
|
transform?: (req: Request, res: Response) => MaybePromise<Response | void | undefined>;
|
|
265
258
|
};
|
|
259
|
+
|
|
266
260
|
/**
|
|
267
|
-
*
|
|
261
|
+
* Helper function for instantiating a Kaito router
|
|
268
262
|
*
|
|
269
263
|
* This is the starting point for any Kaito application
|
|
270
264
|
*
|
|
271
265
|
* @param config - The configuration for the router
|
|
272
266
|
* @returns A new Kaito router
|
|
273
267
|
*/
|
|
274
|
-
declare
|
|
268
|
+
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
275
269
|
|
|
276
270
|
export { type APIResponse, type AnyQuery, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MakeOptional, type MaybePromise, type NotReadonly, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
package/dist/index.js
CHANGED
|
@@ -117,12 +117,13 @@ var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "un
|
|
|
117
117
|
// src/router/router.ts
|
|
118
118
|
var Router = class _Router {
|
|
119
119
|
state;
|
|
120
|
-
static create = (config) =>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
120
|
+
static create = (config = {}) => {
|
|
121
|
+
return new _Router({
|
|
122
|
+
through: async (context) => context,
|
|
123
|
+
routes: /* @__PURE__ */ new Set(),
|
|
124
|
+
config
|
|
125
|
+
});
|
|
126
|
+
};
|
|
126
127
|
constructor(state) {
|
|
127
128
|
this.state = state;
|
|
128
129
|
}
|
|
@@ -141,10 +142,7 @@ var Router = class _Router {
|
|
|
141
142
|
routes: /* @__PURE__ */ new Set([...this.state.routes, merged])
|
|
142
143
|
});
|
|
143
144
|
};
|
|
144
|
-
params = (
|
|
145
|
-
...this.state,
|
|
146
|
-
paramsSchema: z.object(spec)
|
|
147
|
-
});
|
|
145
|
+
params = () => this;
|
|
148
146
|
merge = (pathPrefix, other) => {
|
|
149
147
|
const newRoutes = [...other.state.routes].map((route) => ({
|
|
150
148
|
...route,
|
|
@@ -205,7 +203,7 @@ var Router = class _Router {
|
|
|
205
203
|
});
|
|
206
204
|
}
|
|
207
205
|
const findRoute = _Router.getFindRoute(methodToRoutesMap);
|
|
208
|
-
const handle = async (req) => {
|
|
206
|
+
const handle = async (req, ...args) => {
|
|
209
207
|
const url = new URL(req.url);
|
|
210
208
|
const method = req.method;
|
|
211
209
|
const { route, params: rawParams } = findRoute(method, url.pathname);
|
|
@@ -222,16 +220,15 @@ var Router = class _Router {
|
|
|
222
220
|
try {
|
|
223
221
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
224
222
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
225
|
-
const params = route.router.state.paramsSchema ? route.router.state.paramsSchema.parse(rawParams) : rawParams;
|
|
226
223
|
const ctx = await route.router.state.through(
|
|
227
|
-
await this.state.config.getContext?.(request, head) ?? null,
|
|
228
|
-
|
|
224
|
+
await this.state.config.getContext?.(request, head, ...args) ?? null,
|
|
225
|
+
rawParams
|
|
229
226
|
);
|
|
230
227
|
const result = await route.run({
|
|
231
228
|
ctx,
|
|
232
229
|
body,
|
|
233
230
|
query,
|
|
234
|
-
params
|
|
231
|
+
params: rawParams
|
|
235
232
|
});
|
|
236
233
|
if (result instanceof Response) {
|
|
237
234
|
if (isNodeLikeDev) {
|
|
@@ -285,7 +282,7 @@ var Router = class _Router {
|
|
|
285
282
|
}
|
|
286
283
|
}
|
|
287
284
|
};
|
|
288
|
-
return async (request) => {
|
|
285
|
+
return async (request, ...args) => {
|
|
289
286
|
if (this.state.config.before) {
|
|
290
287
|
const result = await this.state.config.before(request);
|
|
291
288
|
if (result instanceof Response) {
|
|
@@ -298,7 +295,7 @@ var Router = class _Router {
|
|
|
298
295
|
return result;
|
|
299
296
|
}
|
|
300
297
|
}
|
|
301
|
-
const response = await handle(request);
|
|
298
|
+
const response = await handle(request, ...args);
|
|
302
299
|
if (this.state.config.transform) {
|
|
303
300
|
const transformed = await this.state.config.transform(request, response);
|
|
304
301
|
if (transformed instanceof Response) {
|
|
@@ -399,10 +396,8 @@ var Router = class _Router {
|
|
|
399
396
|
};
|
|
400
397
|
};
|
|
401
398
|
|
|
402
|
-
// src/
|
|
403
|
-
|
|
404
|
-
return Router.create(config);
|
|
405
|
-
}
|
|
399
|
+
// src/index.ts
|
|
400
|
+
var create = Router.create;
|
|
406
401
|
export {
|
|
407
402
|
KaitoError,
|
|
408
403
|
KaitoHead,
|