@kaito-http/core 4.0.0-beta.3 → 4.0.0-beta.5
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 +6 -3
- package/dist/index.d.cts +28 -27
- package/dist/index.d.ts +28 -27
- package/dist/index.js +5 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
KaitoError: () => KaitoError,
|
|
24
|
+
KaitoHead: () => KaitoHead,
|
|
24
25
|
KaitoRequest: () => KaitoRequest,
|
|
25
26
|
Router: () => Router,
|
|
26
27
|
WrappedError: () => WrappedError,
|
|
@@ -168,6 +169,7 @@ var Router = class _Router {
|
|
|
168
169
|
routes: /* @__PURE__ */ new Set([...this.state.routes, merged])
|
|
169
170
|
});
|
|
170
171
|
};
|
|
172
|
+
params = () => new _Router(this.state);
|
|
171
173
|
merge = (pathPrefix, other) => {
|
|
172
174
|
const newRoutes = [...other.state.routes].map((route) => ({
|
|
173
175
|
...route,
|
|
@@ -242,7 +244,7 @@ var Router = class _Router {
|
|
|
242
244
|
try {
|
|
243
245
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
244
246
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
245
|
-
const ctx = await route.through(await this.state.config.getContext?.(request, head) ?? null);
|
|
247
|
+
const ctx = await route.through(await this.state.config.getContext?.(request, head) ?? null, params);
|
|
246
248
|
const result = await route.run({
|
|
247
249
|
ctx,
|
|
248
250
|
body,
|
|
@@ -398,18 +400,19 @@ var Router = class _Router {
|
|
|
398
400
|
through = (through) => {
|
|
399
401
|
return new _Router({
|
|
400
402
|
...this.state,
|
|
401
|
-
through: async (context) => await through(await this.state.through(context))
|
|
403
|
+
through: async (context, params) => await through(await this.state.through(context, params), params)
|
|
402
404
|
});
|
|
403
405
|
};
|
|
404
406
|
};
|
|
405
407
|
|
|
406
408
|
// src/create.ts
|
|
407
409
|
function create(config = {}) {
|
|
408
|
-
return
|
|
410
|
+
return Router.create(config);
|
|
409
411
|
}
|
|
410
412
|
// Annotate the CommonJS export names for ESM import in node:
|
|
411
413
|
0 && (module.exports = {
|
|
412
414
|
KaitoError,
|
|
415
|
+
KaitoHead,
|
|
413
416
|
KaitoRequest,
|
|
414
417
|
Router,
|
|
415
418
|
WrappedError,
|
package/dist/index.d.cts
CHANGED
|
@@ -109,16 +109,16 @@ type ExtractRouteParams<T extends string> = string extends T ? Record<string, st
|
|
|
109
109
|
*/
|
|
110
110
|
type GetContext<Result> = (req: KaitoRequest, head: KaitoHead) => MaybePromise<Result>;
|
|
111
111
|
|
|
112
|
-
type RouteRunData<
|
|
112
|
+
type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
113
113
|
ctx: Context;
|
|
114
114
|
body: BodyOutput;
|
|
115
115
|
query: QueryOutput;
|
|
116
|
-
params:
|
|
116
|
+
params: Params;
|
|
117
117
|
};
|
|
118
118
|
type AnyQuery = {
|
|
119
119
|
[key in string]: any;
|
|
120
120
|
};
|
|
121
|
-
type Through<From, To
|
|
121
|
+
type Through<From, To, RequiredParams extends Record<string, string>> = (context: From, params: RequiredParams) => Promise<To>;
|
|
122
122
|
type SSEOutputSpec<Result> = {
|
|
123
123
|
type: 'sse';
|
|
124
124
|
schema: z.Schema<Result>;
|
|
@@ -133,8 +133,8 @@ type OutputSpec<Result> = {
|
|
|
133
133
|
description?: string;
|
|
134
134
|
body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
|
|
135
135
|
};
|
|
136
|
-
type Route<ContextTo, Result, Path extends string, Method extends KaitoMethod, Query, Body> = {
|
|
137
|
-
through: Through<unknown, ContextTo>;
|
|
136
|
+
type Route<ContextTo, Result, Path extends string, AdditionalParams extends Record<string, string>, Method extends KaitoMethod, Query, Body> = {
|
|
137
|
+
through: Through<unknown, ContextTo, AdditionalParams>;
|
|
138
138
|
body?: z.Schema<Body>;
|
|
139
139
|
query?: {
|
|
140
140
|
[Key in keyof Query]: z.Schema<Query[Key]>;
|
|
@@ -142,15 +142,15 @@ type Route<ContextTo, Result, Path extends string, Method extends KaitoMethod, Q
|
|
|
142
142
|
path: Path;
|
|
143
143
|
method: Method;
|
|
144
144
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
145
|
-
run(data: RouteRunData<Path, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
145
|
+
run(data: RouteRunData<ExtractRouteParams<Path> & AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
146
146
|
};
|
|
147
|
-
type AnyRoute = Route<any, any, any, any, any, any>;
|
|
147
|
+
type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
148
148
|
|
|
149
|
-
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextTo, infer Result, infer Path, infer Method, infer Query, infer BodyOutput> ? Route<ContextTo, Result, `${Prefix}${Path}`, Method, Query, BodyOutput> : never;
|
|
149
|
+
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}`, AdditionalParams, Method, Query, BodyOutput> : never;
|
|
150
150
|
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
151
|
-
type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute
|
|
151
|
+
type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute, RequiredParams extends Record<string, string>> = {
|
|
152
152
|
routes: Set<Routes>;
|
|
153
|
-
through: (context: unknown) => Promise<ContextTo>;
|
|
153
|
+
through: (context: unknown, params: RequiredParams) => Promise<ContextTo>;
|
|
154
154
|
config: KaitoConfig<ContextFrom>;
|
|
155
155
|
};
|
|
156
156
|
/**
|
|
@@ -158,19 +158,20 @@ type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute> = {
|
|
|
158
158
|
*
|
|
159
159
|
* @example
|
|
160
160
|
* ```ts
|
|
161
|
-
* const app = router
|
|
161
|
+
* const app = router.get('/', () => 'Hello, world!');
|
|
162
162
|
*
|
|
163
163
|
* type Routes = InferRoutes<typeof app>;
|
|
164
164
|
* ```
|
|
165
165
|
*/
|
|
166
|
-
type InferRoutes<R extends Router<any, any, any>> = R extends Router<any, any, infer R extends AnyRoute> ? R : never;
|
|
167
|
-
declare class Router<ContextFrom, ContextTo, R extends AnyRoute
|
|
166
|
+
type InferRoutes<R extends Router<any, any, any, any>> = R extends Router<any, any, infer R extends AnyRoute, any> ? R : never;
|
|
167
|
+
declare class Router<ContextFrom, ContextTo, R extends AnyRoute, RequiredParams extends Record<string, string>> {
|
|
168
168
|
private readonly state;
|
|
169
|
-
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never>;
|
|
170
|
-
constructor(state: RouterState<ContextFrom, ContextTo, R>);
|
|
169
|
+
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never, {}>;
|
|
170
|
+
protected constructor(state: RouterState<ContextFrom, ContextTo, R, RequiredParams>);
|
|
171
171
|
get routes(): Set<R>;
|
|
172
172
|
private add;
|
|
173
|
-
|
|
173
|
+
params: this extends Router<infer ContextFrom, infer ContextTo, infer R extends AnyRoute, infer Params extends Record<string, string>> ? [keyof Params] extends [never] ? <NextParams extends Record<string, string> = {}>() => Router<ContextFrom, ContextTo, R, NextParams> : 'You cannot define params() on a router that has already had params defined, as routes that already consume params can break.' : never;
|
|
174
|
+
readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends AnyRoute, NextRequiredParams extends Record<string, string>>(pathPrefix: keyof NextRequiredParams extends keyof ExtractRouteParams<PathPrefix> | keyof RequiredParams ? PathPrefix : `${string}:/${Exclude<Extract<keyof NextRequiredParams, string>, keyof RequiredParams>}${string}`, other: Router<ContextFrom, unknown, OtherRoutes, NextRequiredParams>) => Router<ContextFrom, ContextTo, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>, RequiredParams>;
|
|
174
175
|
protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
|
|
175
176
|
route?: never;
|
|
176
177
|
params?: never;
|
|
@@ -187,16 +188,16 @@ declare class Router<ContextFrom, ContextTo, R extends AnyRoute> {
|
|
|
187
188
|
description?: string;
|
|
188
189
|
};
|
|
189
190
|
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
190
|
-
}) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Response, "/openapi.json", "GET", AnyQuery, unknown
|
|
191
|
+
}) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", AnyQuery, unknown>, RequiredParams>;
|
|
191
192
|
private readonly method;
|
|
192
|
-
get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "GET", Query, Body
|
|
193
|
-
post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "POST", Query, Body
|
|
194
|
-
put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "PUT", Query, Body
|
|
195
|
-
patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "PATCH", Query, Body
|
|
196
|
-
delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "DELETE", Query, Body
|
|
197
|
-
head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "HEAD", Query, Body
|
|
198
|
-
options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "OPTIONS", Query, Body
|
|
199
|
-
through: <NextContext>(through: (context: ContextTo) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R>;
|
|
193
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, RequiredParams>;
|
|
194
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, RequiredParams>;
|
|
195
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, RequiredParams>;
|
|
196
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, RequiredParams>;
|
|
197
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, RequiredParams>;
|
|
198
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, RequiredParams>;
|
|
199
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, RequiredParams>;
|
|
200
|
+
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R, RequiredParams>;
|
|
200
201
|
}
|
|
201
202
|
|
|
202
203
|
type KaitoConfig<ContextFrom> = {
|
|
@@ -265,6 +266,6 @@ type KaitoConfig<ContextFrom> = {
|
|
|
265
266
|
* @param config - The configuration for the router
|
|
266
267
|
* @returns A new Kaito router
|
|
267
268
|
*/
|
|
268
|
-
declare function create<Context = null>(config?: KaitoConfig<Context>):
|
|
269
|
+
declare function create<Context = null>(config?: KaitoConfig<Context>): Router<Context, Context, never, {}>;
|
|
269
270
|
|
|
270
|
-
export { type APIResponse, type AnyQuery, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, type KaitoConfig, KaitoError, type KaitoMethod, KaitoRequest, type MakeOptional, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
|
271
|
+
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 OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
package/dist/index.d.ts
CHANGED
|
@@ -109,16 +109,16 @@ type ExtractRouteParams<T extends string> = string extends T ? Record<string, st
|
|
|
109
109
|
*/
|
|
110
110
|
type GetContext<Result> = (req: KaitoRequest, head: KaitoHead) => MaybePromise<Result>;
|
|
111
111
|
|
|
112
|
-
type RouteRunData<
|
|
112
|
+
type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
113
113
|
ctx: Context;
|
|
114
114
|
body: BodyOutput;
|
|
115
115
|
query: QueryOutput;
|
|
116
|
-
params:
|
|
116
|
+
params: Params;
|
|
117
117
|
};
|
|
118
118
|
type AnyQuery = {
|
|
119
119
|
[key in string]: any;
|
|
120
120
|
};
|
|
121
|
-
type Through<From, To
|
|
121
|
+
type Through<From, To, RequiredParams extends Record<string, string>> = (context: From, params: RequiredParams) => Promise<To>;
|
|
122
122
|
type SSEOutputSpec<Result> = {
|
|
123
123
|
type: 'sse';
|
|
124
124
|
schema: z.Schema<Result>;
|
|
@@ -133,8 +133,8 @@ type OutputSpec<Result> = {
|
|
|
133
133
|
description?: string;
|
|
134
134
|
body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
|
|
135
135
|
};
|
|
136
|
-
type Route<ContextTo, Result, Path extends string, Method extends KaitoMethod, Query, Body> = {
|
|
137
|
-
through: Through<unknown, ContextTo>;
|
|
136
|
+
type Route<ContextTo, Result, Path extends string, AdditionalParams extends Record<string, string>, Method extends KaitoMethod, Query, Body> = {
|
|
137
|
+
through: Through<unknown, ContextTo, AdditionalParams>;
|
|
138
138
|
body?: z.Schema<Body>;
|
|
139
139
|
query?: {
|
|
140
140
|
[Key in keyof Query]: z.Schema<Query[Key]>;
|
|
@@ -142,15 +142,15 @@ type Route<ContextTo, Result, Path extends string, Method extends KaitoMethod, Q
|
|
|
142
142
|
path: Path;
|
|
143
143
|
method: Method;
|
|
144
144
|
openapi?: OutputSpec<NoInfer<Result>>;
|
|
145
|
-
run(data: RouteRunData<Path, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
145
|
+
run(data: RouteRunData<ExtractRouteParams<Path> & AdditionalParams, ContextTo, Query, Body>): Promise<Result> | Result;
|
|
146
146
|
};
|
|
147
|
-
type AnyRoute = Route<any, any, any, any, any, any>;
|
|
147
|
+
type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
148
148
|
|
|
149
|
-
type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextTo, infer Result, infer Path, infer Method, infer Query, infer BodyOutput> ? Route<ContextTo, Result, `${Prefix}${Path}`, Method, Query, BodyOutput> : never;
|
|
149
|
+
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}`, AdditionalParams, Method, Query, BodyOutput> : never;
|
|
150
150
|
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
151
|
-
type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute
|
|
151
|
+
type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute, RequiredParams extends Record<string, string>> = {
|
|
152
152
|
routes: Set<Routes>;
|
|
153
|
-
through: (context: unknown) => Promise<ContextTo>;
|
|
153
|
+
through: (context: unknown, params: RequiredParams) => Promise<ContextTo>;
|
|
154
154
|
config: KaitoConfig<ContextFrom>;
|
|
155
155
|
};
|
|
156
156
|
/**
|
|
@@ -158,19 +158,20 @@ type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute> = {
|
|
|
158
158
|
*
|
|
159
159
|
* @example
|
|
160
160
|
* ```ts
|
|
161
|
-
* const app = router
|
|
161
|
+
* const app = router.get('/', () => 'Hello, world!');
|
|
162
162
|
*
|
|
163
163
|
* type Routes = InferRoutes<typeof app>;
|
|
164
164
|
* ```
|
|
165
165
|
*/
|
|
166
|
-
type InferRoutes<R extends Router<any, any, any>> = R extends Router<any, any, infer R extends AnyRoute> ? R : never;
|
|
167
|
-
declare class Router<ContextFrom, ContextTo, R extends AnyRoute
|
|
166
|
+
type InferRoutes<R extends Router<any, any, any, any>> = R extends Router<any, any, infer R extends AnyRoute, any> ? R : never;
|
|
167
|
+
declare class Router<ContextFrom, ContextTo, R extends AnyRoute, RequiredParams extends Record<string, string>> {
|
|
168
168
|
private readonly state;
|
|
169
|
-
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never>;
|
|
170
|
-
constructor(state: RouterState<ContextFrom, ContextTo, R>);
|
|
169
|
+
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never, {}>;
|
|
170
|
+
protected constructor(state: RouterState<ContextFrom, ContextTo, R, RequiredParams>);
|
|
171
171
|
get routes(): Set<R>;
|
|
172
172
|
private add;
|
|
173
|
-
|
|
173
|
+
params: this extends Router<infer ContextFrom, infer ContextTo, infer R extends AnyRoute, infer Params extends Record<string, string>> ? [keyof Params] extends [never] ? <NextParams extends Record<string, string> = {}>() => Router<ContextFrom, ContextTo, R, NextParams> : 'You cannot define params() on a router that has already had params defined, as routes that already consume params can break.' : never;
|
|
174
|
+
readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends AnyRoute, NextRequiredParams extends Record<string, string>>(pathPrefix: keyof NextRequiredParams extends keyof ExtractRouteParams<PathPrefix> | keyof RequiredParams ? PathPrefix : `${string}:/${Exclude<Extract<keyof NextRequiredParams, string>, keyof RequiredParams>}${string}`, other: Router<ContextFrom, unknown, OtherRoutes, NextRequiredParams>) => Router<ContextFrom, ContextTo, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>, RequiredParams>;
|
|
174
175
|
protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
|
|
175
176
|
route?: never;
|
|
176
177
|
params?: never;
|
|
@@ -187,16 +188,16 @@ declare class Router<ContextFrom, ContextTo, R extends AnyRoute> {
|
|
|
187
188
|
description?: string;
|
|
188
189
|
};
|
|
189
190
|
servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
|
|
190
|
-
}) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Response, "/openapi.json", "GET", AnyQuery, unknown
|
|
191
|
+
}) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Response, "/openapi.json", RequiredParams, "GET", AnyQuery, unknown>, RequiredParams>;
|
|
191
192
|
private readonly method;
|
|
192
|
-
get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "GET", Query, Body
|
|
193
|
-
post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "POST", Query, Body
|
|
194
|
-
put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "PUT", Query, Body
|
|
195
|
-
patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "PATCH", Query, Body
|
|
196
|
-
delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "DELETE", Query, Body
|
|
197
|
-
head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "HEAD", Query, Body
|
|
198
|
-
options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "OPTIONS", Query, Body
|
|
199
|
-
through: <NextContext>(through: (context: ContextTo) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R>;
|
|
193
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "GET", Query, Body>, RequiredParams>;
|
|
194
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "POST", Query, Body>, RequiredParams>;
|
|
195
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "PUT", Query, Body>, RequiredParams>;
|
|
196
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "PATCH", Query, Body>, RequiredParams>;
|
|
197
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "DELETE", Query, Body>, RequiredParams>;
|
|
198
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "HEAD", Query, Body>, RequiredParams>;
|
|
199
|
+
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" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, RequiredParams, "OPTIONS", Query, Body>, RequiredParams>;
|
|
200
|
+
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R, RequiredParams>;
|
|
200
201
|
}
|
|
201
202
|
|
|
202
203
|
type KaitoConfig<ContextFrom> = {
|
|
@@ -265,6 +266,6 @@ type KaitoConfig<ContextFrom> = {
|
|
|
265
266
|
* @param config - The configuration for the router
|
|
266
267
|
* @returns A new Kaito router
|
|
267
268
|
*/
|
|
268
|
-
declare function create<Context = null>(config?: KaitoConfig<Context>):
|
|
269
|
+
declare function create<Context = null>(config?: KaitoConfig<Context>): Router<Context, Context, never, {}>;
|
|
269
270
|
|
|
270
|
-
export { type APIResponse, type AnyQuery, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, type KaitoConfig, KaitoError, type KaitoMethod, KaitoRequest, type MakeOptional, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
|
271
|
+
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 OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
|
package/dist/index.js
CHANGED
|
@@ -137,6 +137,7 @@ var Router = class _Router {
|
|
|
137
137
|
routes: /* @__PURE__ */ new Set([...this.state.routes, merged])
|
|
138
138
|
});
|
|
139
139
|
};
|
|
140
|
+
params = () => new _Router(this.state);
|
|
140
141
|
merge = (pathPrefix, other) => {
|
|
141
142
|
const newRoutes = [...other.state.routes].map((route) => ({
|
|
142
143
|
...route,
|
|
@@ -211,7 +212,7 @@ var Router = class _Router {
|
|
|
211
212
|
try {
|
|
212
213
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
213
214
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
214
|
-
const ctx = await route.through(await this.state.config.getContext?.(request, head) ?? null);
|
|
215
|
+
const ctx = await route.through(await this.state.config.getContext?.(request, head) ?? null, params);
|
|
215
216
|
const result = await route.run({
|
|
216
217
|
ctx,
|
|
217
218
|
body,
|
|
@@ -367,17 +368,18 @@ var Router = class _Router {
|
|
|
367
368
|
through = (through) => {
|
|
368
369
|
return new _Router({
|
|
369
370
|
...this.state,
|
|
370
|
-
through: async (context) => await through(await this.state.through(context))
|
|
371
|
+
through: async (context, params) => await through(await this.state.through(context, params), params)
|
|
371
372
|
});
|
|
372
373
|
};
|
|
373
374
|
};
|
|
374
375
|
|
|
375
376
|
// src/create.ts
|
|
376
377
|
function create(config = {}) {
|
|
377
|
-
return
|
|
378
|
+
return Router.create(config);
|
|
378
379
|
}
|
|
379
380
|
export {
|
|
380
381
|
KaitoError,
|
|
382
|
+
KaitoHead,
|
|
381
383
|
KaitoRequest,
|
|
382
384
|
Router,
|
|
383
385
|
WrappedError,
|