@kaito-http/core 2.8.1 → 2.9.1

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.
@@ -7,11 +7,12 @@ export type RouteArgument<Path extends string, Context, QueryOutput, BodyOutput>
7
7
  params: ExtractRouteParams<Path>;
8
8
  };
9
9
  export type AnyQueryDefinition = Record<string, z.ZodTypeAny>;
10
- export type Route<Context, Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition, BodyOutput, BodyDef extends z.ZodTypeDef, BodyInput> = {
10
+ export type Route<ContextFrom, ContextTo, Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition, BodyOutput, BodyDef extends z.ZodTypeDef, BodyInput> = {
11
+ through: (context: ContextFrom) => Promise<ContextTo>;
11
12
  body?: z.ZodType<BodyOutput, BodyDef, BodyInput>;
12
13
  query?: Query;
13
14
  path: Path;
14
15
  method: Method;
15
- run(args: RouteArgument<Path, Context, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result>;
16
+ run(args: RouteArgument<Path, ContextTo, z.infer<z.ZodObject<Query>>, BodyOutput>): Promise<Result>;
16
17
  };
17
- export type AnyRoute<Context = any> = Route<Context, any, any, any, AnyQueryDefinition, any, any, any>;
18
+ export type AnyRoute<FromContext = any, ToContext = any> = Route<FromContext, ToContext, any, any, any, AnyQueryDefinition, any, any, any>;
@@ -4,16 +4,20 @@ import type { AnyQueryDefinition, AnyRoute, Route } from './route';
4
4
  import type { ServerConfig } from './server';
5
5
  import type { KaitoMethod } from './util';
6
6
  type Routes = readonly AnyRoute[];
7
- type RemapRoutePrefix<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer Context, infer Result, infer Path, infer Method, infer Query, infer BodyOutput, infer BodyDef, infer BodyInput> ? Route<Context, Result, `${Prefix}${Path}`, Method, Query, BodyOutput, BodyDef, BodyInput> : never;
7
+ type RemapRoutePrefix<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextFrom, infer ContextTo, infer Result, infer Path, infer Method, infer Query, infer BodyOutput, infer BodyDef, infer BodyInput> ? Route<ContextFrom, ContextTo, Result, `${Prefix}${Path}`, Method, Query, BodyOutput, BodyDef, BodyInput> : never;
8
8
  type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends [infer First, ...infer Rest] ? [
9
9
  RemapRoutePrefix<Extract<First, AnyRoute>, Prefix>,
10
10
  ...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute[]>>
11
11
  ] : [];
12
- export declare class Router<Context, R extends Routes> {
12
+ export type RouterOptions<ContextFrom, ContextTo> = {
13
+ through: (context: ContextFrom) => Promise<ContextTo>;
14
+ };
15
+ export declare class Router<ContextFrom, ContextTo, R extends Routes> {
16
+ private readonly routerOptions;
13
17
  readonly routes: R;
14
- static create: <Context_1>() => Router<Context_1, []>;
18
+ static create: <Context>() => Router<Context, Context, []>;
15
19
  private static handle;
16
- constructor(routes: R);
20
+ constructor(routes: R, options: RouterOptions<ContextFrom, ContextTo>);
17
21
  /**
18
22
  * Adds a new route to the router
19
23
  * @param method The HTTP method to add a route for
@@ -21,16 +25,17 @@ export declare class Router<Context, R extends Routes> {
21
25
  * @param route The route specification to add to this router
22
26
  * @returns A new router with this route added
23
27
  */
24
- add: <Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(method: Method, path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | (Method extends "GET" ? Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method"> : Omit<Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">)) => Router<Context, [...R, Route<Context, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>]>;
25
- readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<Context, OtherRoutes>) => Router<Context, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
26
- freeze: (server: ServerConfig<Context, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
28
+ add: <Result, Path extends string, Method extends KaitoMethod, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(method: Method, path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | (Method extends "GET" ? Omit<Route<ContextFrom, ContextTo, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method"> : Omit<Route<ContextFrom, ContextTo, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">)) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>]>;
29
+ readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<ContextTo, ContextTo, OtherRoutes>) => Router<ContextFrom, ContextTo, [...R, ...PrefixRoutesPath<PathPrefix, OtherRoutes>]>;
30
+ freeze: (server: ServerConfig<ContextFrom, any>) => fmw.Instance<fmw.HTTPVersion.V1>;
27
31
  private readonly method;
28
- get: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "GET", Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "GET", Query, BodyOutput, BodyDef, BodyInput>]>;
29
- post: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "POST", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "POST", Query, BodyOutput, BodyDef, BodyInput>]>;
30
- put: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "PUT", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "PUT", Query, BodyOutput, BodyDef, BodyInput>]>;
31
- patch: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "PATCH", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "PATCH", Query, BodyOutput, BodyDef, BodyInput>]>;
32
- delete: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "DELETE", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "DELETE", Query, BodyOutput, BodyDef, BodyInput>]>;
33
- head: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "HEAD", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "HEAD", Query, BodyOutput, BodyDef, BodyInput>]>;
34
- options: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, Context, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<Context, Result, Path, "OPTIONS", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<Context, [...R, Route<Context, Result, Path, "OPTIONS", Query, BodyOutput, BodyDef, BodyInput>]>;
32
+ get: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "GET", Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "GET", Query, BodyOutput, BodyDef, BodyInput>]>;
33
+ post: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "POST", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "POST", Query, BodyOutput, BodyDef, BodyInput>]>;
34
+ put: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "PUT", Query, BodyOutput, BodyDef, BodyInput>]>;
35
+ patch: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "PATCH", Query, BodyOutput, BodyDef, BodyInput>]>;
36
+ delete: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "DELETE", Query, BodyOutput, BodyDef, BodyInput>]>;
37
+ head: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "HEAD", Query, BodyOutput, BodyDef, BodyInput>]>;
38
+ options: <Result, Path extends string, Query extends AnyQueryDefinition = {}, BodyOutput = never, BodyDef extends z.ZodTypeDef = z.ZodTypeDef, BodyInput = BodyOutput>(path: Path, route: ((args: import("./route").RouteArgument<Path, ContextTo, z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }> extends infer T extends object ? { [k in keyof T]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof Query]: Query[k_1]["_output"]; }>[k]; } : never, BodyOutput>) => Promise<Result>) | Omit<Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<ContextFrom, ContextTo, Result, Path, "OPTIONS", Query, BodyOutput, BodyDef, BodyInput>]>;
39
+ through: <NextContext>(transform: (context: ContextTo) => Promise<NextContext>) => Router<ContextFrom, NextContext, R>;
35
40
  }
36
41
  export {};
@@ -41,6 +41,6 @@ export type ServerConfig<Context, BeforeAfterContext> = ServerConfigWithBefore<B
41
41
  };
42
42
  export declare function createFMWServer<Context, BeforeAfterContext = null>(config: ServerConfig<Context, BeforeAfterContext>): {
43
43
  readonly server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
44
- readonly fmw: import("find-my-way").Instance<import("find-my-way").HTTPVersion.V1>;
44
+ readonly fmw: any;
45
45
  };
46
46
  export declare function createServer<Context, BeforeAfterContext = null>(config: ServerConfig<Context, BeforeAfterContext>): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
@@ -33,7 +33,7 @@ export declare function createGetContext<Context>(callback: GetContext<Context>)
33
33
  */
34
34
  export declare function createUtilities<Context>(getContext: GetContext<Context>): {
35
35
  getContext: GetContext<Context>;
36
- router: () => Router<Context, []>;
36
+ router: () => Router<Context, Context, []>;
37
37
  };
38
38
  export type InferContext<T> = T extends (req: KaitoRequest, res: KaitoResponse) => Promise<infer U> ? U : never;
39
39
  export declare function getLastEntryInMultiHeaderValue(headerValue: string | string[]): string;
@@ -192,7 +192,8 @@ class Router {
192
192
  var send = getSend(options.res);
193
193
  try {
194
194
  var _yield$route$body$par, _route$body;
195
- var ctx = yield server.getContext(options.req, options.res);
195
+ var rootCtx = yield server.getContext(options.req, options.res);
196
+ var ctx = yield route.through(rootCtx);
196
197
  var body = (_yield$route$body$par = yield (_route$body = route.body) === null || _route$body === void 0 ? void 0 : _route$body.parse(yield getBody(options.req))) !== null && _yield$route$body$par !== void 0 ? _yield$route$body$par : undefined;
197
198
  var query = route.query ? zod.z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
198
199
  var result = yield route.run({
@@ -252,21 +253,23 @@ class Router {
252
253
  }
253
254
  })();
254
255
  }
255
- constructor(routes) {
256
+ constructor(routes, options) {
257
+ var _this = this;
256
258
  _defineProperty(this, "add", (method, path, route) => {
257
259
  var merged = _objectSpread2(_objectSpread2({}, typeof route === 'object' ? route : {
258
260
  run: route
259
261
  }), {}, {
260
262
  method,
261
- path
263
+ path,
264
+ through: this.routerOptions.through
262
265
  });
263
- return new Router([...this.routes, merged]);
266
+ return new Router([...this.routes, merged], this.routerOptions);
264
267
  });
265
268
  _defineProperty(this, "merge", (pathPrefix, other) => {
266
269
  var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
267
270
  path: "".concat(pathPrefix).concat(route.path)
268
271
  }));
269
- return new Router([...this.routes, ...newRoutes]);
272
+ return new Router([...this.routes, ...newRoutes], this.routerOptions);
270
273
  });
271
274
  _defineProperty(this, "freeze", server => {
272
275
  var instance = fmw__default["default"]({
@@ -326,6 +329,19 @@ class Router {
326
329
  _defineProperty(this, "delete", this.method('DELETE'));
327
330
  _defineProperty(this, "head", this.method('HEAD'));
328
331
  _defineProperty(this, "options", this.method('OPTIONS'));
332
+ _defineProperty(this, "through", transform => new Router(this.routes, {
333
+ through: function () {
334
+ var _through = _asyncToGenerator(function* (context) {
335
+ var fromCurrentRouter = yield _this.routerOptions.through(context);
336
+ return transform(fromCurrentRouter);
337
+ });
338
+ function through(_x4) {
339
+ return _through.apply(this, arguments);
340
+ }
341
+ return through;
342
+ }()
343
+ }));
344
+ this.routerOptions = options;
329
345
  this.routes = routes;
330
346
  }
331
347
 
@@ -337,7 +353,17 @@ class Router {
337
353
  * @returns A new router with this route added
338
354
  */
339
355
  }
340
- _defineProperty(Router, "create", () => new Router([]));
356
+ _defineProperty(Router, "create", () => new Router([], {
357
+ through: function () {
358
+ var _through2 = _asyncToGenerator(function* (context) {
359
+ return context;
360
+ });
361
+ function through(_x5) {
362
+ return _through2.apply(this, arguments);
363
+ }
364
+ return through;
365
+ }()
366
+ }));
341
367
 
342
368
  /**
343
369
  * @deprecated use `createUtilities` instead
@@ -192,7 +192,8 @@ class Router {
192
192
  var send = getSend(options.res);
193
193
  try {
194
194
  var _yield$route$body$par, _route$body;
195
- var ctx = yield server.getContext(options.req, options.res);
195
+ var rootCtx = yield server.getContext(options.req, options.res);
196
+ var ctx = yield route.through(rootCtx);
196
197
  var body = (_yield$route$body$par = yield (_route$body = route.body) === null || _route$body === void 0 ? void 0 : _route$body.parse(yield getBody(options.req))) !== null && _yield$route$body$par !== void 0 ? _yield$route$body$par : undefined;
197
198
  var query = route.query ? zod.z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
198
199
  var result = yield route.run({
@@ -252,21 +253,23 @@ class Router {
252
253
  }
253
254
  })();
254
255
  }
255
- constructor(routes) {
256
+ constructor(routes, options) {
257
+ var _this = this;
256
258
  _defineProperty(this, "add", (method, path, route) => {
257
259
  var merged = _objectSpread2(_objectSpread2({}, typeof route === 'object' ? route : {
258
260
  run: route
259
261
  }), {}, {
260
262
  method,
261
- path
263
+ path,
264
+ through: this.routerOptions.through
262
265
  });
263
- return new Router([...this.routes, merged]);
266
+ return new Router([...this.routes, merged], this.routerOptions);
264
267
  });
265
268
  _defineProperty(this, "merge", (pathPrefix, other) => {
266
269
  var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
267
270
  path: "".concat(pathPrefix).concat(route.path)
268
271
  }));
269
- return new Router([...this.routes, ...newRoutes]);
272
+ return new Router([...this.routes, ...newRoutes], this.routerOptions);
270
273
  });
271
274
  _defineProperty(this, "freeze", server => {
272
275
  var instance = fmw__default["default"]({
@@ -326,6 +329,19 @@ class Router {
326
329
  _defineProperty(this, "delete", this.method('DELETE'));
327
330
  _defineProperty(this, "head", this.method('HEAD'));
328
331
  _defineProperty(this, "options", this.method('OPTIONS'));
332
+ _defineProperty(this, "through", transform => new Router(this.routes, {
333
+ through: function () {
334
+ var _through = _asyncToGenerator(function* (context) {
335
+ var fromCurrentRouter = yield _this.routerOptions.through(context);
336
+ return transform(fromCurrentRouter);
337
+ });
338
+ function through(_x4) {
339
+ return _through.apply(this, arguments);
340
+ }
341
+ return through;
342
+ }()
343
+ }));
344
+ this.routerOptions = options;
329
345
  this.routes = routes;
330
346
  }
331
347
 
@@ -337,7 +353,17 @@ class Router {
337
353
  * @returns A new router with this route added
338
354
  */
339
355
  }
340
- _defineProperty(Router, "create", () => new Router([]));
356
+ _defineProperty(Router, "create", () => new Router([], {
357
+ through: function () {
358
+ var _through2 = _asyncToGenerator(function* (context) {
359
+ return context;
360
+ });
361
+ function through(_x5) {
362
+ return _through2.apply(this, arguments);
363
+ }
364
+ return through;
365
+ }()
366
+ }));
341
367
 
342
368
  /**
343
369
  * @deprecated use `createUtilities` instead
@@ -164,7 +164,8 @@ class Router {
164
164
  var send = getSend(options.res);
165
165
  try {
166
166
  var _yield$route$body$par, _route$body;
167
- var ctx = yield server.getContext(options.req, options.res);
167
+ var rootCtx = yield server.getContext(options.req, options.res);
168
+ var ctx = yield route.through(rootCtx);
168
169
  var body = (_yield$route$body$par = yield (_route$body = route.body) === null || _route$body === void 0 ? void 0 : _route$body.parse(yield getBody(options.req))) !== null && _yield$route$body$par !== void 0 ? _yield$route$body$par : undefined;
169
170
  var query = route.query ? z.object(route.query).parse(Object.fromEntries(options.req.url.searchParams.entries())) : {};
170
171
  var result = yield route.run({
@@ -224,21 +225,23 @@ class Router {
224
225
  }
225
226
  })();
226
227
  }
227
- constructor(routes) {
228
+ constructor(routes, options) {
229
+ var _this = this;
228
230
  _defineProperty(this, "add", (method, path, route) => {
229
231
  var merged = _objectSpread2(_objectSpread2({}, typeof route === 'object' ? route : {
230
232
  run: route
231
233
  }), {}, {
232
234
  method,
233
- path
235
+ path,
236
+ through: this.routerOptions.through
234
237
  });
235
- return new Router([...this.routes, merged]);
238
+ return new Router([...this.routes, merged], this.routerOptions);
236
239
  });
237
240
  _defineProperty(this, "merge", (pathPrefix, other) => {
238
241
  var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
239
242
  path: "".concat(pathPrefix).concat(route.path)
240
243
  }));
241
- return new Router([...this.routes, ...newRoutes]);
244
+ return new Router([...this.routes, ...newRoutes], this.routerOptions);
242
245
  });
243
246
  _defineProperty(this, "freeze", server => {
244
247
  var instance = fmw({
@@ -298,6 +301,19 @@ class Router {
298
301
  _defineProperty(this, "delete", this.method('DELETE'));
299
302
  _defineProperty(this, "head", this.method('HEAD'));
300
303
  _defineProperty(this, "options", this.method('OPTIONS'));
304
+ _defineProperty(this, "through", transform => new Router(this.routes, {
305
+ through: function () {
306
+ var _through = _asyncToGenerator(function* (context) {
307
+ var fromCurrentRouter = yield _this.routerOptions.through(context);
308
+ return transform(fromCurrentRouter);
309
+ });
310
+ function through(_x4) {
311
+ return _through.apply(this, arguments);
312
+ }
313
+ return through;
314
+ }()
315
+ }));
316
+ this.routerOptions = options;
301
317
  this.routes = routes;
302
318
  }
303
319
 
@@ -309,7 +325,17 @@ class Router {
309
325
  * @returns A new router with this route added
310
326
  */
311
327
  }
312
- _defineProperty(Router, "create", () => new Router([]));
328
+ _defineProperty(Router, "create", () => new Router([], {
329
+ through: function () {
330
+ var _through2 = _asyncToGenerator(function* (context) {
331
+ return context;
332
+ });
333
+ function through(_x5) {
334
+ return _through2.apply(this, arguments);
335
+ }
336
+ return through;
337
+ }()
338
+ }));
313
339
 
314
340
  /**
315
341
  * @deprecated use `createUtilities` instead
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaito-http/core",
3
- "version": "2.8.1",
3
+ "version": "2.9.1",
4
4
  "description": "Functional HTTP Framework for TypeScript",
5
5
  "repository": "https://github.com/kaito-http/kaito",
6
6
  "author": "Alistair Smith <hi@alistair.sh>",