@kaito-http/core 2.8.1 → 2.9.0
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/declarations/src/router.d.ts +18 -13
- package/dist/declarations/src/server.d.ts +1 -1
- package/dist/declarations/src/util.d.ts +1 -1
- package/dist/kaito-http-core.cjs.dev.js +33 -8
- package/dist/kaito-http-core.cjs.prod.js +33 -8
- package/dist/kaito-http-core.esm.js +33 -8
- package/package.json +1 -1
|
@@ -9,11 +9,15 @@ type PrefixRoutesPath<Prefix extends `/${string}`, R extends Routes> = R extends
|
|
|
9
9
|
RemapRoutePrefix<Extract<First, AnyRoute>, Prefix>,
|
|
10
10
|
...PrefixRoutesPath<Prefix, Extract<Rest, readonly AnyRoute[]>>
|
|
11
11
|
] : [];
|
|
12
|
-
export
|
|
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: <
|
|
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,
|
|
25
|
-
readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends Routes>(pathPrefix: PathPrefix, other: Router<
|
|
26
|
-
freeze: (server: ServerConfig<
|
|
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<ContextTo, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method"> : Omit<Route<ContextTo, Result, Path, Method, Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">)) => Router<ContextFrom, ContextTo, [...R, Route<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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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<ContextTo, Result, Path, "GET", Query, BodyOutput, BodyDef, BodyInput>, "path" | "body" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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<ContextTo, Result, Path, "POST", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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<ContextTo, Result, Path, "PUT", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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<ContextTo, Result, Path, "PATCH", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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<ContextTo, Result, Path, "DELETE", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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<ContextTo, Result, Path, "HEAD", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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<ContextTo, Result, Path, "OPTIONS", Query, BodyOutput, BodyDef, BodyInput>, "path" | "method">) => Router<ContextFrom, ContextTo, [...R, Route<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:
|
|
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;
|
|
@@ -187,16 +187,17 @@ class Router {
|
|
|
187
187
|
static handle(
|
|
188
188
|
// Allow for any server to be passed
|
|
189
189
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
190
|
-
server, route, options) {
|
|
190
|
+
server, routerOptions, route, options) {
|
|
191
191
|
return _asyncToGenerator(function* () {
|
|
192
192
|
var send = getSend(options.res);
|
|
193
193
|
try {
|
|
194
194
|
var _yield$route$body$par, _route$body;
|
|
195
|
-
var
|
|
195
|
+
var rootCtx = yield server.getContext(options.req, options.res);
|
|
196
|
+
var through = yield routerOptions.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({
|
|
199
|
-
ctx,
|
|
200
|
+
ctx: through,
|
|
200
201
|
body,
|
|
201
202
|
query,
|
|
202
203
|
params: options.params
|
|
@@ -252,7 +253,8 @@ 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
|
|
@@ -260,13 +262,13 @@ class Router {
|
|
|
260
262
|
method,
|
|
261
263
|
path
|
|
262
264
|
});
|
|
263
|
-
return new Router([...this.routes, merged]);
|
|
265
|
+
return new Router([...this.routes, merged], this.routerOptions);
|
|
264
266
|
});
|
|
265
267
|
_defineProperty(this, "merge", (pathPrefix, other) => {
|
|
266
268
|
var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
|
|
267
269
|
path: "".concat(pathPrefix).concat(route.path)
|
|
268
270
|
}));
|
|
269
|
-
return new Router([...this.routes, ...newRoutes]);
|
|
271
|
+
return new Router([...this.routes, ...newRoutes], this.routerOptions);
|
|
270
272
|
});
|
|
271
273
|
_defineProperty(this, "freeze", server => {
|
|
272
274
|
var instance = fmw__default["default"]({
|
|
@@ -296,7 +298,7 @@ class Router {
|
|
|
296
298
|
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
297
299
|
var req = new KaitoRequest(incomingMessage);
|
|
298
300
|
var res = new KaitoResponse(serverResponse);
|
|
299
|
-
return Router.handle(server, route, {
|
|
301
|
+
return Router.handle(server, _this.routerOptions, route, {
|
|
300
302
|
params,
|
|
301
303
|
req,
|
|
302
304
|
res
|
|
@@ -326,6 +328,19 @@ class Router {
|
|
|
326
328
|
_defineProperty(this, "delete", this.method('DELETE'));
|
|
327
329
|
_defineProperty(this, "head", this.method('HEAD'));
|
|
328
330
|
_defineProperty(this, "options", this.method('OPTIONS'));
|
|
331
|
+
_defineProperty(this, "through", transform => new Router(this.routes, {
|
|
332
|
+
through: function () {
|
|
333
|
+
var _through = _asyncToGenerator(function* (context) {
|
|
334
|
+
var fromCurrentRouter = yield _this.routerOptions.through(context);
|
|
335
|
+
return transform(fromCurrentRouter);
|
|
336
|
+
});
|
|
337
|
+
function through(_x4) {
|
|
338
|
+
return _through.apply(this, arguments);
|
|
339
|
+
}
|
|
340
|
+
return through;
|
|
341
|
+
}()
|
|
342
|
+
}));
|
|
343
|
+
this.routerOptions = options;
|
|
329
344
|
this.routes = routes;
|
|
330
345
|
}
|
|
331
346
|
|
|
@@ -337,7 +352,17 @@ class Router {
|
|
|
337
352
|
* @returns A new router with this route added
|
|
338
353
|
*/
|
|
339
354
|
}
|
|
340
|
-
_defineProperty(Router, "create", () => new Router([]
|
|
355
|
+
_defineProperty(Router, "create", () => new Router([], {
|
|
356
|
+
through: function () {
|
|
357
|
+
var _through2 = _asyncToGenerator(function* (context) {
|
|
358
|
+
return context;
|
|
359
|
+
});
|
|
360
|
+
function through(_x5) {
|
|
361
|
+
return _through2.apply(this, arguments);
|
|
362
|
+
}
|
|
363
|
+
return through;
|
|
364
|
+
}()
|
|
365
|
+
}));
|
|
341
366
|
|
|
342
367
|
/**
|
|
343
368
|
* @deprecated use `createUtilities` instead
|
|
@@ -187,16 +187,17 @@ class Router {
|
|
|
187
187
|
static handle(
|
|
188
188
|
// Allow for any server to be passed
|
|
189
189
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
190
|
-
server, route, options) {
|
|
190
|
+
server, routerOptions, route, options) {
|
|
191
191
|
return _asyncToGenerator(function* () {
|
|
192
192
|
var send = getSend(options.res);
|
|
193
193
|
try {
|
|
194
194
|
var _yield$route$body$par, _route$body;
|
|
195
|
-
var
|
|
195
|
+
var rootCtx = yield server.getContext(options.req, options.res);
|
|
196
|
+
var through = yield routerOptions.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({
|
|
199
|
-
ctx,
|
|
200
|
+
ctx: through,
|
|
200
201
|
body,
|
|
201
202
|
query,
|
|
202
203
|
params: options.params
|
|
@@ -252,7 +253,8 @@ 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
|
|
@@ -260,13 +262,13 @@ class Router {
|
|
|
260
262
|
method,
|
|
261
263
|
path
|
|
262
264
|
});
|
|
263
|
-
return new Router([...this.routes, merged]);
|
|
265
|
+
return new Router([...this.routes, merged], this.routerOptions);
|
|
264
266
|
});
|
|
265
267
|
_defineProperty(this, "merge", (pathPrefix, other) => {
|
|
266
268
|
var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
|
|
267
269
|
path: "".concat(pathPrefix).concat(route.path)
|
|
268
270
|
}));
|
|
269
|
-
return new Router([...this.routes, ...newRoutes]);
|
|
271
|
+
return new Router([...this.routes, ...newRoutes], this.routerOptions);
|
|
270
272
|
});
|
|
271
273
|
_defineProperty(this, "freeze", server => {
|
|
272
274
|
var instance = fmw__default["default"]({
|
|
@@ -296,7 +298,7 @@ class Router {
|
|
|
296
298
|
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
297
299
|
var req = new KaitoRequest(incomingMessage);
|
|
298
300
|
var res = new KaitoResponse(serverResponse);
|
|
299
|
-
return Router.handle(server, route, {
|
|
301
|
+
return Router.handle(server, _this.routerOptions, route, {
|
|
300
302
|
params,
|
|
301
303
|
req,
|
|
302
304
|
res
|
|
@@ -326,6 +328,19 @@ class Router {
|
|
|
326
328
|
_defineProperty(this, "delete", this.method('DELETE'));
|
|
327
329
|
_defineProperty(this, "head", this.method('HEAD'));
|
|
328
330
|
_defineProperty(this, "options", this.method('OPTIONS'));
|
|
331
|
+
_defineProperty(this, "through", transform => new Router(this.routes, {
|
|
332
|
+
through: function () {
|
|
333
|
+
var _through = _asyncToGenerator(function* (context) {
|
|
334
|
+
var fromCurrentRouter = yield _this.routerOptions.through(context);
|
|
335
|
+
return transform(fromCurrentRouter);
|
|
336
|
+
});
|
|
337
|
+
function through(_x4) {
|
|
338
|
+
return _through.apply(this, arguments);
|
|
339
|
+
}
|
|
340
|
+
return through;
|
|
341
|
+
}()
|
|
342
|
+
}));
|
|
343
|
+
this.routerOptions = options;
|
|
329
344
|
this.routes = routes;
|
|
330
345
|
}
|
|
331
346
|
|
|
@@ -337,7 +352,17 @@ class Router {
|
|
|
337
352
|
* @returns A new router with this route added
|
|
338
353
|
*/
|
|
339
354
|
}
|
|
340
|
-
_defineProperty(Router, "create", () => new Router([]
|
|
355
|
+
_defineProperty(Router, "create", () => new Router([], {
|
|
356
|
+
through: function () {
|
|
357
|
+
var _through2 = _asyncToGenerator(function* (context) {
|
|
358
|
+
return context;
|
|
359
|
+
});
|
|
360
|
+
function through(_x5) {
|
|
361
|
+
return _through2.apply(this, arguments);
|
|
362
|
+
}
|
|
363
|
+
return through;
|
|
364
|
+
}()
|
|
365
|
+
}));
|
|
341
366
|
|
|
342
367
|
/**
|
|
343
368
|
* @deprecated use `createUtilities` instead
|
|
@@ -159,16 +159,17 @@ class Router {
|
|
|
159
159
|
static handle(
|
|
160
160
|
// Allow for any server to be passed
|
|
161
161
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
162
|
-
server, route, options) {
|
|
162
|
+
server, routerOptions, route, options) {
|
|
163
163
|
return _asyncToGenerator(function* () {
|
|
164
164
|
var send = getSend(options.res);
|
|
165
165
|
try {
|
|
166
166
|
var _yield$route$body$par, _route$body;
|
|
167
|
-
var
|
|
167
|
+
var rootCtx = yield server.getContext(options.req, options.res);
|
|
168
|
+
var through = yield routerOptions.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({
|
|
171
|
-
ctx,
|
|
172
|
+
ctx: through,
|
|
172
173
|
body,
|
|
173
174
|
query,
|
|
174
175
|
params: options.params
|
|
@@ -224,7 +225,8 @@ 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
|
|
@@ -232,13 +234,13 @@ class Router {
|
|
|
232
234
|
method,
|
|
233
235
|
path
|
|
234
236
|
});
|
|
235
|
-
return new Router([...this.routes, merged]);
|
|
237
|
+
return new Router([...this.routes, merged], this.routerOptions);
|
|
236
238
|
});
|
|
237
239
|
_defineProperty(this, "merge", (pathPrefix, other) => {
|
|
238
240
|
var newRoutes = other.routes.map(route => _objectSpread2(_objectSpread2({}, route), {}, {
|
|
239
241
|
path: "".concat(pathPrefix).concat(route.path)
|
|
240
242
|
}));
|
|
241
|
-
return new Router([...this.routes, ...newRoutes]);
|
|
243
|
+
return new Router([...this.routes, ...newRoutes], this.routerOptions);
|
|
242
244
|
});
|
|
243
245
|
_defineProperty(this, "freeze", server => {
|
|
244
246
|
var instance = fmw({
|
|
@@ -268,7 +270,7 @@ class Router {
|
|
|
268
270
|
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
269
271
|
var req = new KaitoRequest(incomingMessage);
|
|
270
272
|
var res = new KaitoResponse(serverResponse);
|
|
271
|
-
return Router.handle(server, route, {
|
|
273
|
+
return Router.handle(server, _this.routerOptions, route, {
|
|
272
274
|
params,
|
|
273
275
|
req,
|
|
274
276
|
res
|
|
@@ -298,6 +300,19 @@ class Router {
|
|
|
298
300
|
_defineProperty(this, "delete", this.method('DELETE'));
|
|
299
301
|
_defineProperty(this, "head", this.method('HEAD'));
|
|
300
302
|
_defineProperty(this, "options", this.method('OPTIONS'));
|
|
303
|
+
_defineProperty(this, "through", transform => new Router(this.routes, {
|
|
304
|
+
through: function () {
|
|
305
|
+
var _through = _asyncToGenerator(function* (context) {
|
|
306
|
+
var fromCurrentRouter = yield _this.routerOptions.through(context);
|
|
307
|
+
return transform(fromCurrentRouter);
|
|
308
|
+
});
|
|
309
|
+
function through(_x4) {
|
|
310
|
+
return _through.apply(this, arguments);
|
|
311
|
+
}
|
|
312
|
+
return through;
|
|
313
|
+
}()
|
|
314
|
+
}));
|
|
315
|
+
this.routerOptions = options;
|
|
301
316
|
this.routes = routes;
|
|
302
317
|
}
|
|
303
318
|
|
|
@@ -309,7 +324,17 @@ class Router {
|
|
|
309
324
|
* @returns A new router with this route added
|
|
310
325
|
*/
|
|
311
326
|
}
|
|
312
|
-
_defineProperty(Router, "create", () => new Router([]
|
|
327
|
+
_defineProperty(Router, "create", () => new Router([], {
|
|
328
|
+
through: function () {
|
|
329
|
+
var _through2 = _asyncToGenerator(function* (context) {
|
|
330
|
+
return context;
|
|
331
|
+
});
|
|
332
|
+
function through(_x5) {
|
|
333
|
+
return _through2.apply(this, arguments);
|
|
334
|
+
}
|
|
335
|
+
return through;
|
|
336
|
+
}()
|
|
337
|
+
}));
|
|
313
338
|
|
|
314
339
|
/**
|
|
315
340
|
* @deprecated use `createUtilities` instead
|