@kaito-http/core 4.0.0-beta.10 → 4.0.0-beta.11
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 +17 -10
- package/dist/index.d.cts +30 -25
- package/dist/index.d.ts +30 -25
- package/dist/index.js +17 -10
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -147,12 +147,14 @@ 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
|
+
paramsSchema: null
|
|
156
|
+
});
|
|
157
|
+
};
|
|
156
158
|
constructor(state) {
|
|
157
159
|
this.state = state;
|
|
158
160
|
}
|
|
@@ -235,7 +237,7 @@ var Router = class _Router {
|
|
|
235
237
|
});
|
|
236
238
|
}
|
|
237
239
|
const findRoute = _Router.getFindRoute(methodToRoutesMap);
|
|
238
|
-
const handle = async (req) => {
|
|
240
|
+
const handle = async (req, ...args) => {
|
|
239
241
|
const url = new URL(req.url);
|
|
240
242
|
const method = req.method;
|
|
241
243
|
const { route, params: rawParams } = findRoute(method, url.pathname);
|
|
@@ -254,7 +256,7 @@ var Router = class _Router {
|
|
|
254
256
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
255
257
|
const params = route.router.state.paramsSchema ? route.router.state.paramsSchema.parse(rawParams) : rawParams;
|
|
256
258
|
const ctx = await route.router.state.through(
|
|
257
|
-
await this.state.config.getContext?.(request, head) ?? null,
|
|
259
|
+
await this.state.config.getContext?.(request, head, ...args) ?? null,
|
|
258
260
|
params
|
|
259
261
|
);
|
|
260
262
|
const result = await route.run({
|
|
@@ -315,7 +317,7 @@ var Router = class _Router {
|
|
|
315
317
|
}
|
|
316
318
|
}
|
|
317
319
|
};
|
|
318
|
-
return async (request) => {
|
|
320
|
+
return async (request, ...args) => {
|
|
319
321
|
if (this.state.config.before) {
|
|
320
322
|
const result = await this.state.config.before(request);
|
|
321
323
|
if (result instanceof Response) {
|
|
@@ -328,7 +330,7 @@ var Router = class _Router {
|
|
|
328
330
|
return result;
|
|
329
331
|
}
|
|
330
332
|
}
|
|
331
|
-
const response = await handle(request);
|
|
333
|
+
const response = await handle(request, ...args);
|
|
332
334
|
if (this.state.config.transform) {
|
|
333
335
|
const transformed = await this.state.config.transform(request, response);
|
|
334
336
|
if (transformed instanceof Response) {
|
|
@@ -433,6 +435,11 @@ var Router = class _Router {
|
|
|
433
435
|
function create(config = {}) {
|
|
434
436
|
return Router.create(config);
|
|
435
437
|
}
|
|
438
|
+
create.withInput = () => {
|
|
439
|
+
return {
|
|
440
|
+
create: (config = {}) => Router.create(config)
|
|
441
|
+
};
|
|
442
|
+
};
|
|
436
443
|
// Annotate the CommonJS export names for ESM import in node:
|
|
437
444
|
0 && (module.exports = {
|
|
438
445
|
KaitoError,
|
package/dist/index.d.cts
CHANGED
|
@@ -109,7 +109,7 @@ type ExtractRouteParams<T extends string> = string extends T ? Record<string, st
|
|
|
109
109
|
* @param head - The kaito head object, which contains getters and setters for headers and status
|
|
110
110
|
* @returns The context for your routes
|
|
111
111
|
*/
|
|
112
|
-
type GetContext<Result> = (req: KaitoRequest, head: KaitoHead) => MaybePromise<Result>;
|
|
112
|
+
type GetContext<Result, WithArgument> = (req: KaitoRequest, head: KaitoHead, ...args: [WithArgument] extends [never] ? [] : [input: WithArgument]) => MaybePromise<Result>;
|
|
113
113
|
|
|
114
114
|
type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
115
115
|
ctx: Context;
|
|
@@ -143,17 +143,17 @@ type Route<ContextTo, Result, Path extends string, AdditionalParams extends Reco
|
|
|
143
143
|
path: Path;
|
|
144
144
|
method: Method;
|
|
145
145
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
146
|
-
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute>;
|
|
146
|
+
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute, any>;
|
|
147
147
|
run(data: RouteRunData<ExtractRouteParams<Path> & AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
148
148
|
};
|
|
149
149
|
type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
150
150
|
|
|
151
151
|
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;
|
|
152
152
|
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
153
|
-
type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, Routes extends AnyRoute> = {
|
|
153
|
+
type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, Routes extends AnyRoute, Input> = {
|
|
154
154
|
routes: Set<Routes>;
|
|
155
155
|
through: (context: unknown, params: RequiredParams) => Promise<ContextTo>;
|
|
156
|
-
config: KaitoConfig<ContextFrom>;
|
|
156
|
+
config: KaitoConfig<ContextFrom, Input>;
|
|
157
157
|
paramsSchema: z.Schema<RequiredParams> | null;
|
|
158
158
|
};
|
|
159
159
|
/**
|
|
@@ -166,17 +166,17 @@ type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, u
|
|
|
166
166
|
* type Routes = InferRoutes<typeof app>;
|
|
167
167
|
* ```
|
|
168
168
|
*/
|
|
169
|
-
type InferRoutes<R extends Router<any, any, any, any>> = R extends Router<any, any, any, infer R extends AnyRoute> ? R : never;
|
|
170
|
-
declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, R extends AnyRoute> {
|
|
169
|
+
type InferRoutes<R extends Router<any, any, any, any, any>> = R extends Router<any, any, any, infer R extends AnyRoute, any> ? R : never;
|
|
170
|
+
declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, R extends AnyRoute, Input> {
|
|
171
171
|
private readonly state;
|
|
172
|
-
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, {}, never>;
|
|
173
|
-
protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, R>);
|
|
172
|
+
static create: <Context, Input_1 = never>(config: KaitoConfig<Context, Input_1>) => Router<Context, Context, {}, never, Input_1>;
|
|
173
|
+
protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, R, Input>);
|
|
174
174
|
get routes(): Set<R>;
|
|
175
175
|
private add;
|
|
176
|
-
params: this extends Router<infer ContextFrom, infer ContextTo, infer Params extends Record<string, unknown>, infer R extends AnyRoute> ? [keyof Params] extends [never] ? <NextParams extends Record<string, unknown> = {}>(spec: {
|
|
176
|
+
params: this extends Router<infer ContextFrom, infer ContextTo, infer Params extends Record<string, unknown>, infer R extends AnyRoute, infer Input> ? [keyof Params] extends [never] ? <NextParams extends Record<string, unknown> = {}>(spec: {
|
|
177
177
|
[Key in keyof NextParams]: z.ZodType<NextParams[Key], ZodTypeDef, string>;
|
|
178
|
-
}) => Router<ContextFrom, ContextTo, NextParams, R> : 'You cannot define params() on a router that has already had params defined, as routes that already consume params can break.' : never;
|
|
179
|
-
readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends Record<string, unknown>, OtherRoutes extends AnyRoute>(pathPrefix: keyof NextRequiredParams extends keyof ExtractRouteParams<PathPrefix> | keyof RequiredParams ? PathPrefix : `Missing ${Exclude<Extract<keyof NextRequiredParams, string>, keyof RequiredParams>}${string}`, other: Router<ContextFrom, unknown, NextRequiredParams, OtherRoutes>) => Router<ContextFrom, ContextTo, RequiredParams, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute
|
|
178
|
+
}) => Router<ContextFrom, ContextTo, NextParams, R, Input> : 'You cannot define params() on a router that has already had params defined, as routes that already consume params can break.' : never;
|
|
179
|
+
readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends Record<string, unknown>, OtherRoutes extends AnyRoute>(pathPrefix: keyof NextRequiredParams extends keyof ExtractRouteParams<PathPrefix> | keyof RequiredParams ? PathPrefix : `Missing ${Exclude<Extract<keyof NextRequiredParams, string>, keyof RequiredParams>}${string}`, other: Router<ContextFrom, unknown, NextRequiredParams, OtherRoutes, Input>) => Router<ContextFrom, ContextTo, RequiredParams, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>, Input>;
|
|
180
180
|
protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
|
|
181
181
|
route?: never;
|
|
182
182
|
params?: never;
|
|
@@ -185,7 +185,7 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<strin
|
|
|
185
185
|
params: Record<string, string>;
|
|
186
186
|
};
|
|
187
187
|
private static buildQuerySchema;
|
|
188
|
-
serve: () => (request: Request) => Promise<Response>;
|
|
188
|
+
serve: () => (request: Request, ...args: [Input] extends [never] ? [] : [input: Input]) => Promise<Response>;
|
|
189
189
|
openapi: (highLevelSpec: {
|
|
190
190
|
info: {
|
|
191
191
|
version: string;
|
|
@@ -193,19 +193,19 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<strin
|
|
|
193
193
|
description?: string;
|
|
194
194
|
};
|
|
195
195
|
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
196
|
-
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never
|
|
196
|
+
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
197
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>;
|
|
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
206
|
}
|
|
207
207
|
|
|
208
|
-
type KaitoConfig<ContextFrom> = {
|
|
208
|
+
type KaitoConfig<ContextFrom, WithArgument> = {
|
|
209
209
|
/**
|
|
210
210
|
* A function that is called to get the context for a request.
|
|
211
211
|
*
|
|
@@ -213,7 +213,7 @@ type KaitoConfig<ContextFrom> = {
|
|
|
213
213
|
*
|
|
214
214
|
* It's fine for this function to throw; if it does, the error is passed to the `onError` function.
|
|
215
215
|
*/
|
|
216
|
-
getContext?: GetContext<ContextFrom>;
|
|
216
|
+
getContext?: GetContext<ContextFrom, WithArgument>;
|
|
217
217
|
/**
|
|
218
218
|
* A function that is called when an error occurs inside a route handler.
|
|
219
219
|
*
|
|
@@ -264,13 +264,18 @@ type KaitoConfig<ContextFrom> = {
|
|
|
264
264
|
transform?: (req: Request, res: Response) => MaybePromise<Response | void | undefined>;
|
|
265
265
|
};
|
|
266
266
|
/**
|
|
267
|
-
*
|
|
267
|
+
* Helper function for instantiating a Kaito router
|
|
268
268
|
*
|
|
269
269
|
* This is the starting point for any Kaito application
|
|
270
270
|
*
|
|
271
271
|
* @param config - The configuration for the router
|
|
272
272
|
* @returns A new Kaito router
|
|
273
273
|
*/
|
|
274
|
-
declare function create<Context = null>(config?: KaitoConfig<Context>): Router<Context, Context, {}, never>;
|
|
274
|
+
declare function create<Context = null>(config?: KaitoConfig<Context, never>): Router<Context, Context, {}, never, never>;
|
|
275
|
+
declare namespace create {
|
|
276
|
+
var withInput: <Input = never>() => {
|
|
277
|
+
create: <Context>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, {}, never, Input>;
|
|
278
|
+
};
|
|
279
|
+
}
|
|
275
280
|
|
|
276
281
|
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
|
@@ -109,7 +109,7 @@ type ExtractRouteParams<T extends string> = string extends T ? Record<string, st
|
|
|
109
109
|
* @param head - The kaito head object, which contains getters and setters for headers and status
|
|
110
110
|
* @returns The context for your routes
|
|
111
111
|
*/
|
|
112
|
-
type GetContext<Result> = (req: KaitoRequest, head: KaitoHead) => MaybePromise<Result>;
|
|
112
|
+
type GetContext<Result, WithArgument> = (req: KaitoRequest, head: KaitoHead, ...args: [WithArgument] extends [never] ? [] : [input: WithArgument]) => MaybePromise<Result>;
|
|
113
113
|
|
|
114
114
|
type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
115
115
|
ctx: Context;
|
|
@@ -143,17 +143,17 @@ type Route<ContextTo, Result, Path extends string, AdditionalParams extends Reco
|
|
|
143
143
|
path: Path;
|
|
144
144
|
method: Method;
|
|
145
145
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
146
|
-
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute>;
|
|
146
|
+
router: Router<unknown, ContextTo, AdditionalParams, AnyRoute, any>;
|
|
147
147
|
run(data: RouteRunData<ExtractRouteParams<Path> & AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
148
148
|
};
|
|
149
149
|
type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
150
150
|
|
|
151
151
|
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;
|
|
152
152
|
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
153
|
-
type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, Routes extends AnyRoute> = {
|
|
153
|
+
type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, Routes extends AnyRoute, Input> = {
|
|
154
154
|
routes: Set<Routes>;
|
|
155
155
|
through: (context: unknown, params: RequiredParams) => Promise<ContextTo>;
|
|
156
|
-
config: KaitoConfig<ContextFrom>;
|
|
156
|
+
config: KaitoConfig<ContextFrom, Input>;
|
|
157
157
|
paramsSchema: z.Schema<RequiredParams> | null;
|
|
158
158
|
};
|
|
159
159
|
/**
|
|
@@ -166,17 +166,17 @@ type RouterState<ContextFrom, ContextTo, RequiredParams extends Record<string, u
|
|
|
166
166
|
* type Routes = InferRoutes<typeof app>;
|
|
167
167
|
* ```
|
|
168
168
|
*/
|
|
169
|
-
type InferRoutes<R extends Router<any, any, any, any>> = R extends Router<any, any, any, infer R extends AnyRoute> ? R : never;
|
|
170
|
-
declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, R extends AnyRoute> {
|
|
169
|
+
type InferRoutes<R extends Router<any, any, any, any, any>> = R extends Router<any, any, any, infer R extends AnyRoute, any> ? R : never;
|
|
170
|
+
declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<string, unknown>, R extends AnyRoute, Input> {
|
|
171
171
|
private readonly state;
|
|
172
|
-
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, {}, never>;
|
|
173
|
-
protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, R>);
|
|
172
|
+
static create: <Context, Input_1 = never>(config: KaitoConfig<Context, Input_1>) => Router<Context, Context, {}, never, Input_1>;
|
|
173
|
+
protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, R, Input>);
|
|
174
174
|
get routes(): Set<R>;
|
|
175
175
|
private add;
|
|
176
|
-
params: this extends Router<infer ContextFrom, infer ContextTo, infer Params extends Record<string, unknown>, infer R extends AnyRoute> ? [keyof Params] extends [never] ? <NextParams extends Record<string, unknown> = {}>(spec: {
|
|
176
|
+
params: this extends Router<infer ContextFrom, infer ContextTo, infer Params extends Record<string, unknown>, infer R extends AnyRoute, infer Input> ? [keyof Params] extends [never] ? <NextParams extends Record<string, unknown> = {}>(spec: {
|
|
177
177
|
[Key in keyof NextParams]: z.ZodType<NextParams[Key], ZodTypeDef, string>;
|
|
178
|
-
}) => Router<ContextFrom, ContextTo, NextParams, R> : 'You cannot define params() on a router that has already had params defined, as routes that already consume params can break.' : never;
|
|
179
|
-
readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends Record<string, unknown>, OtherRoutes extends AnyRoute>(pathPrefix: keyof NextRequiredParams extends keyof ExtractRouteParams<PathPrefix> | keyof RequiredParams ? PathPrefix : `Missing ${Exclude<Extract<keyof NextRequiredParams, string>, keyof RequiredParams>}${string}`, other: Router<ContextFrom, unknown, NextRequiredParams, OtherRoutes>) => Router<ContextFrom, ContextTo, RequiredParams, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute
|
|
178
|
+
}) => Router<ContextFrom, ContextTo, NextParams, R, Input> : 'You cannot define params() on a router that has already had params defined, as routes that already consume params can break.' : never;
|
|
179
|
+
readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends Record<string, unknown>, OtherRoutes extends AnyRoute>(pathPrefix: keyof NextRequiredParams extends keyof ExtractRouteParams<PathPrefix> | keyof RequiredParams ? PathPrefix : `Missing ${Exclude<Extract<keyof NextRequiredParams, string>, keyof RequiredParams>}${string}`, other: Router<ContextFrom, unknown, NextRequiredParams, OtherRoutes, Input>) => Router<ContextFrom, ContextTo, RequiredParams, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>, Input>;
|
|
180
180
|
protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
|
|
181
181
|
route?: never;
|
|
182
182
|
params?: never;
|
|
@@ -185,7 +185,7 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<strin
|
|
|
185
185
|
params: Record<string, string>;
|
|
186
186
|
};
|
|
187
187
|
private static buildQuerySchema;
|
|
188
|
-
serve: () => (request: Request) => Promise<Response>;
|
|
188
|
+
serve: () => (request: Request, ...args: [Input] extends [never] ? [] : [input: Input]) => Promise<Response>;
|
|
189
189
|
openapi: (highLevelSpec: {
|
|
190
190
|
info: {
|
|
191
191
|
version: string;
|
|
@@ -193,19 +193,19 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends Record<strin
|
|
|
193
193
|
description?: string;
|
|
194
194
|
};
|
|
195
195
|
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
196
|
-
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never
|
|
196
|
+
}) => Router<ContextFrom, ContextTo, RequiredParams, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
|
|
197
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>;
|
|
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
206
|
}
|
|
207
207
|
|
|
208
|
-
type KaitoConfig<ContextFrom> = {
|
|
208
|
+
type KaitoConfig<ContextFrom, WithArgument> = {
|
|
209
209
|
/**
|
|
210
210
|
* A function that is called to get the context for a request.
|
|
211
211
|
*
|
|
@@ -213,7 +213,7 @@ type KaitoConfig<ContextFrom> = {
|
|
|
213
213
|
*
|
|
214
214
|
* It's fine for this function to throw; if it does, the error is passed to the `onError` function.
|
|
215
215
|
*/
|
|
216
|
-
getContext?: GetContext<ContextFrom>;
|
|
216
|
+
getContext?: GetContext<ContextFrom, WithArgument>;
|
|
217
217
|
/**
|
|
218
218
|
* A function that is called when an error occurs inside a route handler.
|
|
219
219
|
*
|
|
@@ -264,13 +264,18 @@ type KaitoConfig<ContextFrom> = {
|
|
|
264
264
|
transform?: (req: Request, res: Response) => MaybePromise<Response | void | undefined>;
|
|
265
265
|
};
|
|
266
266
|
/**
|
|
267
|
-
*
|
|
267
|
+
* Helper function for instantiating a Kaito router
|
|
268
268
|
*
|
|
269
269
|
* This is the starting point for any Kaito application
|
|
270
270
|
*
|
|
271
271
|
* @param config - The configuration for the router
|
|
272
272
|
* @returns A new Kaito router
|
|
273
273
|
*/
|
|
274
|
-
declare function create<Context = null>(config?: KaitoConfig<Context>): Router<Context, Context, {}, never>;
|
|
274
|
+
declare function create<Context = null>(config?: KaitoConfig<Context, never>): Router<Context, Context, {}, never, never>;
|
|
275
|
+
declare namespace create {
|
|
276
|
+
var withInput: <Input = never>() => {
|
|
277
|
+
create: <Context>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, {}, never, Input>;
|
|
278
|
+
};
|
|
279
|
+
}
|
|
275
280
|
|
|
276
281
|
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,14 @@ 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
|
+
paramsSchema: null
|
|
126
|
+
});
|
|
127
|
+
};
|
|
126
128
|
constructor(state) {
|
|
127
129
|
this.state = state;
|
|
128
130
|
}
|
|
@@ -205,7 +207,7 @@ var Router = class _Router {
|
|
|
205
207
|
});
|
|
206
208
|
}
|
|
207
209
|
const findRoute = _Router.getFindRoute(methodToRoutesMap);
|
|
208
|
-
const handle = async (req) => {
|
|
210
|
+
const handle = async (req, ...args) => {
|
|
209
211
|
const url = new URL(req.url);
|
|
210
212
|
const method = req.method;
|
|
211
213
|
const { route, params: rawParams } = findRoute(method, url.pathname);
|
|
@@ -224,7 +226,7 @@ var Router = class _Router {
|
|
|
224
226
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
225
227
|
const params = route.router.state.paramsSchema ? route.router.state.paramsSchema.parse(rawParams) : rawParams;
|
|
226
228
|
const ctx = await route.router.state.through(
|
|
227
|
-
await this.state.config.getContext?.(request, head) ?? null,
|
|
229
|
+
await this.state.config.getContext?.(request, head, ...args) ?? null,
|
|
228
230
|
params
|
|
229
231
|
);
|
|
230
232
|
const result = await route.run({
|
|
@@ -285,7 +287,7 @@ var Router = class _Router {
|
|
|
285
287
|
}
|
|
286
288
|
}
|
|
287
289
|
};
|
|
288
|
-
return async (request) => {
|
|
290
|
+
return async (request, ...args) => {
|
|
289
291
|
if (this.state.config.before) {
|
|
290
292
|
const result = await this.state.config.before(request);
|
|
291
293
|
if (result instanceof Response) {
|
|
@@ -298,7 +300,7 @@ var Router = class _Router {
|
|
|
298
300
|
return result;
|
|
299
301
|
}
|
|
300
302
|
}
|
|
301
|
-
const response = await handle(request);
|
|
303
|
+
const response = await handle(request, ...args);
|
|
302
304
|
if (this.state.config.transform) {
|
|
303
305
|
const transformed = await this.state.config.transform(request, response);
|
|
304
306
|
if (transformed instanceof Response) {
|
|
@@ -403,6 +405,11 @@ var Router = class _Router {
|
|
|
403
405
|
function create(config = {}) {
|
|
404
406
|
return Router.create(config);
|
|
405
407
|
}
|
|
408
|
+
create.withInput = () => {
|
|
409
|
+
return {
|
|
410
|
+
create: (config = {}) => Router.create(config)
|
|
411
|
+
};
|
|
412
|
+
};
|
|
406
413
|
export {
|
|
407
414
|
KaitoError,
|
|
408
415
|
KaitoHead,
|