@kaito-http/core 4.0.0-beta.4 → 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 +2 -2
- package/dist/index.d.cts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -244,7 +244,7 @@ var Router = class _Router {
|
|
|
244
244
|
try {
|
|
245
245
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
246
246
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
247
|
-
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);
|
|
248
248
|
const result = await route.run({
|
|
249
249
|
ctx,
|
|
250
250
|
body,
|
|
@@ -400,7 +400,7 @@ var Router = class _Router {
|
|
|
400
400
|
through = (through) => {
|
|
401
401
|
return new _Router({
|
|
402
402
|
...this.state,
|
|
403
|
-
through: async (context) => await through(await this.state.through(context))
|
|
403
|
+
through: async (context, params) => await through(await this.state.through(context, params), params)
|
|
404
404
|
});
|
|
405
405
|
};
|
|
406
406
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -118,7 +118,7 @@ type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
|
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>;
|
|
@@ -134,7 +134,7 @@ type OutputSpec<Result> = {
|
|
|
134
134
|
body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
|
|
135
135
|
};
|
|
136
136
|
type Route<ContextTo, Result, Path extends string, AdditionalParams extends Record<string, string>, Method extends KaitoMethod, Query, Body> = {
|
|
137
|
-
through: Through<unknown, ContextTo>;
|
|
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]>;
|
|
@@ -148,9 +148,9 @@ type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
|
148
148
|
|
|
149
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,7 +158,7 @@ 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
|
* ```
|
|
@@ -167,7 +167,7 @@ type InferRoutes<R extends Router<any, any, any, any>> = R extends Router<any, a
|
|
|
167
167
|
declare class Router<ContextFrom, ContextTo, R extends AnyRoute, RequiredParams extends Record<string, string>> {
|
|
168
168
|
private readonly state;
|
|
169
169
|
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never, {}>;
|
|
170
|
-
|
|
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;
|
|
@@ -197,7 +197,7 @@ declare class Router<ContextFrom, ContextTo, R extends AnyRoute, RequiredParams
|
|
|
197
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
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
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) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R, RequiredParams>;
|
|
200
|
+
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R, RequiredParams>;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
type KaitoConfig<ContextFrom> = {
|
package/dist/index.d.ts
CHANGED
|
@@ -118,7 +118,7 @@ type RouteRunData<Params, Context, QueryOutput, BodyOutput> = {
|
|
|
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>;
|
|
@@ -134,7 +134,7 @@ type OutputSpec<Result> = {
|
|
|
134
134
|
body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
|
|
135
135
|
};
|
|
136
136
|
type Route<ContextTo, Result, Path extends string, AdditionalParams extends Record<string, string>, Method extends KaitoMethod, Query, Body> = {
|
|
137
|
-
through: Through<unknown, ContextTo>;
|
|
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]>;
|
|
@@ -148,9 +148,9 @@ type AnyRoute = Route<any, any, any, any, any, any, any>;
|
|
|
148
148
|
|
|
149
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,7 +158,7 @@ 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
|
* ```
|
|
@@ -167,7 +167,7 @@ type InferRoutes<R extends Router<any, any, any, any>> = R extends Router<any, a
|
|
|
167
167
|
declare class Router<ContextFrom, ContextTo, R extends AnyRoute, RequiredParams extends Record<string, string>> {
|
|
168
168
|
private readonly state;
|
|
169
169
|
static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never, {}>;
|
|
170
|
-
|
|
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;
|
|
@@ -197,7 +197,7 @@ declare class Router<ContextFrom, ContextTo, R extends AnyRoute, RequiredParams
|
|
|
197
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
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
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) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R, RequiredParams>;
|
|
200
|
+
through: <NextContext>(through: (context: ContextTo, params: RequiredParams) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R, RequiredParams>;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
type KaitoConfig<ContextFrom> = {
|
package/dist/index.js
CHANGED
|
@@ -212,7 +212,7 @@ var Router = class _Router {
|
|
|
212
212
|
try {
|
|
213
213
|
const body = route.body ? await route.body.parseAsync(await req.json()) : void 0;
|
|
214
214
|
const query = route.fastQuerySchema ? await route.fastQuerySchema.parseAsync(url.searchParams) : {};
|
|
215
|
-
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);
|
|
216
216
|
const result = await route.run({
|
|
217
217
|
ctx,
|
|
218
218
|
body,
|
|
@@ -368,7 +368,7 @@ var Router = class _Router {
|
|
|
368
368
|
through = (through) => {
|
|
369
369
|
return new _Router({
|
|
370
370
|
...this.state,
|
|
371
|
-
through: async (context) => await through(await this.state.through(context))
|
|
371
|
+
through: async (context, params) => await through(await this.state.through(context, params), params)
|
|
372
372
|
});
|
|
373
373
|
};
|
|
374
374
|
};
|