@kaito-http/core 4.0.0-beta.11 → 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 +7 -19
- package/dist/index.d.cts +86 -97
- package/dist/index.d.ts +86 -97
- package/dist/index.js +7 -19
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -147,12 +147,11 @@ 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) => {
|
|
150
|
+
static create = (config = {}) => {
|
|
151
151
|
return new _Router({
|
|
152
152
|
through: async (context) => context,
|
|
153
153
|
routes: /* @__PURE__ */ new Set(),
|
|
154
|
-
config
|
|
155
|
-
paramsSchema: null
|
|
154
|
+
config
|
|
156
155
|
});
|
|
157
156
|
};
|
|
158
157
|
constructor(state) {
|
|
@@ -173,10 +172,7 @@ var Router = class _Router {
|
|
|
173
172
|
routes: /* @__PURE__ */ new Set([...this.state.routes, merged])
|
|
174
173
|
});
|
|
175
174
|
};
|
|
176
|
-
params = (
|
|
177
|
-
...this.state,
|
|
178
|
-
paramsSchema: import_zod.z.object(spec)
|
|
179
|
-
});
|
|
175
|
+
params = () => this;
|
|
180
176
|
merge = (pathPrefix, other) => {
|
|
181
177
|
const newRoutes = [...other.state.routes].map((route) => ({
|
|
182
178
|
...route,
|
|
@@ -254,16 +250,15 @@ var Router = class _Router {
|
|
|
254
250
|
try {
|
|
255
251
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
256
252
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
257
|
-
const params = route.router.state.paramsSchema ? route.router.state.paramsSchema.parse(rawParams) : rawParams;
|
|
258
253
|
const ctx = await route.router.state.through(
|
|
259
254
|
await this.state.config.getContext?.(request, head, ...args) ?? null,
|
|
260
|
-
|
|
255
|
+
rawParams
|
|
261
256
|
);
|
|
262
257
|
const result = await route.run({
|
|
263
258
|
ctx,
|
|
264
259
|
body,
|
|
265
260
|
query,
|
|
266
|
-
params
|
|
261
|
+
params: rawParams
|
|
267
262
|
});
|
|
268
263
|
if (result instanceof Response) {
|
|
269
264
|
if (isNodeLikeDev) {
|
|
@@ -431,15 +426,8 @@ var Router = class _Router {
|
|
|
431
426
|
};
|
|
432
427
|
};
|
|
433
428
|
|
|
434
|
-
// src/
|
|
435
|
-
|
|
436
|
-
return Router.create(config);
|
|
437
|
-
}
|
|
438
|
-
create.withInput = () => {
|
|
439
|
-
return {
|
|
440
|
-
create: (config = {}) => Router.create(config)
|
|
441
|
-
};
|
|
442
|
-
};
|
|
429
|
+
// src/index.ts
|
|
430
|
+
var create = Router.create;
|
|
443
431
|
// Annotate the CommonJS export names for ESM import in node:
|
|
444
432
|
0 && (module.exports = {
|
|
445
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]>;
|
|
@@ -144,18 +148,32 @@ type Route<ContextTo, Result, Path extends string, AdditionalParams extends Reco
|
|
|
144
148
|
method: Method;
|
|
145
149
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
146
150
|
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute, any>;
|
|
147
|
-
run(data: RouteRunData<ExtractRouteParams<Path>
|
|
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;
|
|
163
|
+
};
|
|
164
|
+
type SuccessfulAPIResponse<T> = {
|
|
165
|
+
success: true;
|
|
166
|
+
data: T;
|
|
158
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, ...args: [Input] extends [never] ? [] : [input: Input]) => 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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
205
|
-
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
|
|
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, WithArgument> = {
|
|
|
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,6 +256,7 @@ type KaitoConfig<ContextFrom, WithArgument> = {
|
|
|
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
|
*
|
|
@@ -271,11 +265,6 @@ type KaitoConfig<ContextFrom, WithArgument> = {
|
|
|
271
265
|
* @param config - The configuration for the router
|
|
272
266
|
* @returns A new Kaito router
|
|
273
267
|
*/
|
|
274
|
-
declare
|
|
275
|
-
declare namespace create {
|
|
276
|
-
var withInput: <Input = never>() => {
|
|
277
|
-
create: <Context>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, {}, never, Input>;
|
|
278
|
-
};
|
|
279
|
-
}
|
|
268
|
+
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
280
269
|
|
|
281
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]>;
|
|
@@ -144,18 +148,32 @@ type Route<ContextTo, Result, Path extends string, AdditionalParams extends Reco
|
|
|
144
148
|
method: Method;
|
|
145
149
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
146
150
|
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute, any>;
|
|
147
|
-
run(data: RouteRunData<ExtractRouteParams<Path>
|
|
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;
|
|
163
|
+
};
|
|
164
|
+
type SuccessfulAPIResponse<T> = {
|
|
165
|
+
success: true;
|
|
166
|
+
data: T;
|
|
158
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, ...args: [Input] extends [never] ? [] : [input: Input]) => 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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
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>, Input>;
|
|
205
|
-
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, R, Input>;
|
|
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, WithArgument> = {
|
|
|
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,6 +256,7 @@ type KaitoConfig<ContextFrom, WithArgument> = {
|
|
|
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
|
*
|
|
@@ -271,11 +265,6 @@ type KaitoConfig<ContextFrom, WithArgument> = {
|
|
|
271
265
|
* @param config - The configuration for the router
|
|
272
266
|
* @returns A new Kaito router
|
|
273
267
|
*/
|
|
274
|
-
declare
|
|
275
|
-
declare namespace create {
|
|
276
|
-
var withInput: <Input = never>() => {
|
|
277
|
-
create: <Context>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, {}, never, Input>;
|
|
278
|
-
};
|
|
279
|
-
}
|
|
268
|
+
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
280
269
|
|
|
281
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,11 @@ 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) => {
|
|
120
|
+
static create = (config = {}) => {
|
|
121
121
|
return new _Router({
|
|
122
122
|
through: async (context) => context,
|
|
123
123
|
routes: /* @__PURE__ */ new Set(),
|
|
124
|
-
config
|
|
125
|
-
paramsSchema: null
|
|
124
|
+
config
|
|
126
125
|
});
|
|
127
126
|
};
|
|
128
127
|
constructor(state) {
|
|
@@ -143,10 +142,7 @@ var Router = class _Router {
|
|
|
143
142
|
routes: /* @__PURE__ */ new Set([...this.state.routes, merged])
|
|
144
143
|
});
|
|
145
144
|
};
|
|
146
|
-
params = (
|
|
147
|
-
...this.state,
|
|
148
|
-
paramsSchema: z.object(spec)
|
|
149
|
-
});
|
|
145
|
+
params = () => this;
|
|
150
146
|
merge = (pathPrefix, other) => {
|
|
151
147
|
const newRoutes = [...other.state.routes].map((route) => ({
|
|
152
148
|
...route,
|
|
@@ -224,16 +220,15 @@ var Router = class _Router {
|
|
|
224
220
|
try {
|
|
225
221
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
226
222
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
227
|
-
const params = route.router.state.paramsSchema ? route.router.state.paramsSchema.parse(rawParams) : rawParams;
|
|
228
223
|
const ctx = await route.router.state.through(
|
|
229
224
|
await this.state.config.getContext?.(request, head, ...args) ?? null,
|
|
230
|
-
|
|
225
|
+
rawParams
|
|
231
226
|
);
|
|
232
227
|
const result = await route.run({
|
|
233
228
|
ctx,
|
|
234
229
|
body,
|
|
235
230
|
query,
|
|
236
|
-
params
|
|
231
|
+
params: rawParams
|
|
237
232
|
});
|
|
238
233
|
if (result instanceof Response) {
|
|
239
234
|
if (isNodeLikeDev) {
|
|
@@ -401,15 +396,8 @@ var Router = class _Router {
|
|
|
401
396
|
};
|
|
402
397
|
};
|
|
403
398
|
|
|
404
|
-
// src/
|
|
405
|
-
|
|
406
|
-
return Router.create(config);
|
|
407
|
-
}
|
|
408
|
-
create.withInput = () => {
|
|
409
|
-
return {
|
|
410
|
-
create: (config = {}) => Router.create(config)
|
|
411
|
-
};
|
|
412
|
-
};
|
|
399
|
+
// src/index.ts
|
|
400
|
+
var create = Router.create;
|
|
413
401
|
export {
|
|
414
402
|
KaitoError,
|
|
415
403
|
KaitoHead,
|