@taserjs/router 0.0.5 → 0.1.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/cjs/builder/app.cjs +5 -0
- package/dist/cjs/builder/app.cjs.map +1 -1
- package/dist/cjs/builder/app.d.cts +7 -7
- package/dist/cjs/builder/factories.cjs +4 -4
- package/dist/cjs/builder/factories.cjs.map +1 -1
- package/dist/cjs/builder/factories.d.cts +5 -51
- package/dist/cjs/builder/route.cjs +22 -1
- package/dist/cjs/builder/route.cjs.map +1 -1
- package/dist/cjs/builder/route.d.cts +1 -2
- package/dist/cjs/builder/router.cjs +3 -5
- package/dist/cjs/builder/router.cjs.map +1 -1
- package/dist/cjs/builder/router.d.cts +11 -12
- package/dist/cjs/define/middleware.cjs +2 -2
- package/dist/cjs/define/middleware.cjs.map +1 -1
- package/dist/cjs/define/middleware.d.cts +10 -8
- package/dist/cjs/index.cjs +0 -12
- package/dist/cjs/index.d.cts +4 -4
- package/dist/cjs/reply.cjs +8 -19
- package/dist/cjs/reply.d.cts +1 -2
- package/dist/cjs/stream.cjs +8 -31
- package/dist/cjs/stream.d.cts +1 -1
- package/dist/cjs/types/app.d.cts +3 -21
- package/dist/cjs/types/index.d.cts +29 -7
- package/dist/cjs/types/returns.d.cts +6 -9
- package/dist/cjs/types/type-utils.d.cts +7 -5
- package/dist/cjs/types/units.cjs.map +1 -1
- package/dist/cjs/types/units.d.cts +4 -10
- package/dist/esm/builder/app.d.ts +7 -7
- package/dist/esm/builder/app.js +5 -1
- package/dist/esm/builder/app.js.map +1 -1
- package/dist/esm/builder/factories.d.ts +5 -51
- package/dist/esm/builder/factories.js +4 -4
- package/dist/esm/builder/factories.js.map +1 -1
- package/dist/esm/builder/route.d.ts +1 -2
- package/dist/esm/builder/route.js +22 -1
- package/dist/esm/builder/route.js.map +1 -1
- package/dist/esm/builder/router.d.ts +11 -12
- package/dist/esm/builder/router.js +3 -5
- package/dist/esm/builder/router.js.map +1 -1
- package/dist/esm/define/middleware.d.ts +10 -8
- package/dist/esm/define/middleware.js +2 -2
- package/dist/esm/define/middleware.js.map +1 -1
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +2 -2
- package/dist/esm/reply.d.ts +1 -2
- package/dist/esm/reply.js +1 -2
- package/dist/esm/stream.d.ts +1 -1
- package/dist/esm/stream.js +1 -2
- package/dist/esm/types/app.d.ts +3 -21
- package/dist/esm/types/index.d.ts +29 -7
- package/dist/esm/types/returns.d.ts +6 -9
- package/dist/esm/types/type-utils.d.ts +7 -5
- package/dist/esm/types/units.d.ts +4 -10
- package/dist/esm/types/units.js.map +1 -1
- package/package.json +3 -3
- package/src/builder/app.ts +16 -5
- package/src/builder/factories.ts +28 -155
- package/src/builder/route.ts +25 -1
- package/src/builder/router.ts +24 -157
- package/src/define/middleware.ts +115 -123
- package/src/index.ts +5 -9
- package/src/reply.ts +1 -11
- package/src/stream.ts +1 -1
- package/src/types/app.ts +3 -22
- package/src/types/index.ts +83 -13
- package/src/types/returns.ts +7 -76
- package/src/types/type-utils.ts +9 -4
- package/src/types/units.ts +34 -10
package/dist/cjs/builder/app.cjs
CHANGED
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
var TaserServeView = class {
|
|
3
3
|
runtime;
|
|
4
4
|
fetch;
|
|
5
|
+
request;
|
|
5
6
|
constructor(runtime) {
|
|
6
7
|
this.runtime = runtime;
|
|
7
8
|
this.fetch = (request, env, executionCtx) => {
|
|
8
9
|
return this.runtime.fetch(request, env, executionCtx);
|
|
9
10
|
};
|
|
11
|
+
this.request = (path, init) => {
|
|
12
|
+
return this.runtime.request(path, init);
|
|
13
|
+
};
|
|
10
14
|
}
|
|
11
15
|
};
|
|
12
16
|
var TaserApp = class extends TaserServeView {
|
|
@@ -18,5 +22,6 @@ var TaserApp = class extends TaserServeView {
|
|
|
18
22
|
};
|
|
19
23
|
//#endregion
|
|
20
24
|
exports.TaserApp = TaserApp;
|
|
25
|
+
exports.TaserServeView = TaserServeView;
|
|
21
26
|
|
|
22
27
|
//# sourceMappingURL=app.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.cjs","names":[],"sources":["../../../src/builder/app.ts"],"sourcesContent":["import type { RouteManifestShape, TaserRuntime } from \"@taserjs/router-core\";\n\
|
|
1
|
+
{"version":3,"file":"app.cjs","names":[],"sources":["../../../src/builder/app.ts"],"sourcesContent":["import type { RouteManifestShape, TaserRuntime } from \"@taserjs/router-core\";\n\nexport class TaserServeView<THasNotFound extends boolean = boolean> {\n readonly fetch: (\n request: Request,\n env?: unknown,\n executionCtx?: unknown,\n ) => THasNotFound extends true\n ? Promise<Response> | Response\n : Promise<Response | undefined> | Response | undefined;\n\n readonly request: (\n path: string,\n init?: RequestInit,\n ) => THasNotFound extends true ? Promise<Response> : Promise<Response | undefined>;\n\n constructor(protected readonly runtime: TaserRuntime<THasNotFound>) {\n this.fetch = (request: Request, env?: unknown, executionCtx?: unknown) => {\n return this.runtime.fetch(request, env, executionCtx as never);\n };\n this.request = (path: string, init?: RequestInit) => {\n return this.runtime.request(path, init);\n };\n }\n}\n\nexport class TaserApp<\n TManifest extends RouteManifestShape = RouteManifestShape,\n THasNotFound extends boolean = boolean,\n> extends TaserServeView<THasNotFound> {\n readonly __manifest?: TManifest;\n\n constructor(runtime: TaserRuntime<THasNotFound>, manifest?: TManifest) {\n super(runtime);\n if (manifest !== undefined) {\n this.__manifest = manifest;\n }\n }\n}\n"],"mappings":";AAEA,IAAa,iBAAb,MAAoE;CAcnC;CAb/B;CAQA;CAKA,YAAY,SAAwD;EAArC,KAAA,UAAA;EAC7B,KAAK,SAAS,SAAkB,KAAe,iBAA2B;GACxE,OAAO,KAAK,QAAQ,MAAM,SAAS,KAAK,YAAqB;EAC/D;EACA,KAAK,WAAW,MAAc,SAAuB;GACnD,OAAO,KAAK,QAAQ,QAAQ,MAAM,IAAI;EACxC;CACF;AACF;AAEA,IAAa,WAAb,cAGU,eAA6B;CACrC;CAEA,YAAY,SAAqC,UAAsB;EACrE,MAAM,OAAO;EACb,IAAI,aAAa,KAAA,GACf,KAAK,aAAa;CAEtB;AACF"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { RouteManifestShape, TaserRuntime } from '@taserjs/router-core';
|
|
2
|
-
declare class TaserServeView {
|
|
3
|
-
protected readonly runtime: TaserRuntime
|
|
4
|
-
readonly fetch: (request: Request, env?: unknown, executionCtx?: unknown) => Promise<Response> | Response;
|
|
5
|
-
|
|
2
|
+
export declare class TaserServeView<THasNotFound extends boolean = boolean> {
|
|
3
|
+
protected readonly runtime: TaserRuntime<THasNotFound>;
|
|
4
|
+
readonly fetch: (request: Request, env?: unknown, executionCtx?: unknown) => THasNotFound extends true ? Promise<Response> | Response : Promise<Response | undefined> | Response | undefined;
|
|
5
|
+
readonly request: (path: string, init?: RequestInit) => THasNotFound extends true ? Promise<Response> : Promise<Response | undefined>;
|
|
6
|
+
constructor(runtime: TaserRuntime<THasNotFound>);
|
|
6
7
|
}
|
|
7
|
-
export declare class TaserApp<TManifest extends RouteManifestShape = RouteManifestShape> extends TaserServeView {
|
|
8
|
+
export declare class TaserApp<TManifest extends RouteManifestShape = RouteManifestShape, THasNotFound extends boolean = boolean> extends TaserServeView<THasNotFound> {
|
|
8
9
|
readonly __manifest?: TManifest;
|
|
9
|
-
constructor(runtime: TaserRuntime
|
|
10
|
+
constructor(runtime: TaserRuntime<THasNotFound>, manifest?: TManifest);
|
|
10
11
|
}
|
|
11
|
-
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const require_route = require("./route.cjs");
|
|
2
2
|
//#region src/builder/factories.ts
|
|
3
3
|
function createWithoutBodyRoute(method) {
|
|
4
|
-
return ((path
|
|
4
|
+
return ((path) => require_route.createRouteBuilder(path, method));
|
|
5
5
|
}
|
|
6
6
|
function createWithBodyRoute(method) {
|
|
7
|
-
return ((path
|
|
7
|
+
return ((path) => require_route.createRouteBuilder(path, method));
|
|
8
8
|
}
|
|
9
9
|
var createGetRoute = createWithoutBodyRoute("GET");
|
|
10
10
|
var createDeleteRoute = createWithoutBodyRoute("DELETE");
|
|
@@ -13,8 +13,8 @@ var createHeadRoute = createWithoutBodyRoute("HEAD");
|
|
|
13
13
|
var createPostRoute = createWithBodyRoute("POST");
|
|
14
14
|
var createPutRoute = createWithBodyRoute("PUT");
|
|
15
15
|
var createPatchRoute = createWithBodyRoute("PATCH");
|
|
16
|
-
var createAnyRoute = ((path, methods
|
|
17
|
-
var createAllRoute = ((path
|
|
16
|
+
var createAnyRoute = ((path, methods) => require_route.createRouteBuilder(path, "ANY", methods));
|
|
17
|
+
var createAllRoute = ((path) => require_route.createRouteBuilder(path, "ALL"));
|
|
18
18
|
//#endregion
|
|
19
19
|
exports.createAllRoute = createAllRoute;
|
|
20
20
|
exports.createAnyRoute = createAnyRoute;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factories.cjs","names":[],"sources":["../../../src/builder/factories.ts"],"sourcesContent":["import { createRouteBuilder } from \"./route.js\";\nimport type { RouteBuilder, RoutePath
|
|
1
|
+
{"version":3,"file":"factories.cjs","names":[],"sources":["../../../src/builder/factories.ts"],"sourcesContent":["import { createRouteBuilder } from \"./route.js\";\nimport type { RouteBuilder, RoutePath } from \"../types/index.js\";\nimport type { AppContext, HttpMethod } from \"../types/units.js\";\n\nexport type CreateWithoutBodyRoute<\n M extends \"GET\" | \"DELETE\" | \"OPTIONS\" | \"HEAD\",\n TAppContext extends Record<string, unknown> = AppContext,\n> = <const Path extends RoutePath>(\n path: Path,\n) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;\n\nexport type CreateWithBodyRoute<\n M extends \"POST\" | \"PUT\" | \"PATCH\",\n TAppContext extends Record<string, unknown> = AppContext,\n> = <const Path extends RoutePath>(\n path: Path,\n) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;\n\nexport type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = <\n const Path extends RoutePath,\n const Methods extends readonly HttpMethod[],\n>(\n path: Path,\n methods: Methods,\n) => RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;\n\nexport type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = <\n const Path extends RoutePath,\n>(\n path: Path,\n) => RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;\n\nfunction createWithoutBodyRoute<M extends \"GET\" | \"DELETE\" | \"OPTIONS\" | \"HEAD\">(\n method: M,\n): CreateWithoutBodyRoute<M> {\n return ((path: string) =>\n createRouteBuilder(path, method)) as unknown as CreateWithoutBodyRoute<M>;\n}\n\nfunction createWithBodyRoute<M extends \"POST\" | \"PUT\" | \"PATCH\">(\n method: M,\n): CreateWithBodyRoute<M> {\n return ((path: string) => createRouteBuilder(path, method)) as unknown as CreateWithBodyRoute<M>;\n}\n\nexport const createGetRoute = createWithoutBodyRoute(\"GET\");\nexport const createDeleteRoute = createWithoutBodyRoute(\"DELETE\");\nexport const createOptionsRoute = createWithoutBodyRoute(\"OPTIONS\");\nexport const createHeadRoute = createWithoutBodyRoute(\"HEAD\");\nexport const createPostRoute = createWithBodyRoute(\"POST\");\nexport const createPutRoute = createWithBodyRoute(\"PUT\");\nexport const createPatchRoute = createWithBodyRoute(\"PATCH\");\n\nexport const createAnyRoute: CreateAnyRoute = ((path: string, methods: readonly HttpMethod[]) =>\n createRouteBuilder(path, \"ANY\", methods)) as unknown as CreateAnyRoute;\n\nexport const createAllRoute: CreateAllRoute = ((path: string) =>\n createRouteBuilder(path, \"ALL\")) as unknown as CreateAllRoute;\n"],"mappings":";;AAgCA,SAAS,uBACP,QAC2B;CAC3B,SAAS,SACP,cAAA,mBAAmB,MAAM,MAAM;AACnC;AAEA,SAAS,oBACP,QACwB;CACxB,SAAS,SAAiB,cAAA,mBAAmB,MAAM,MAAM;AAC3D;AAEA,IAAa,iBAAiB,uBAAuB,KAAK;AAC1D,IAAa,oBAAoB,uBAAuB,QAAQ;AAChE,IAAa,qBAAqB,uBAAuB,SAAS;AAClE,IAAa,kBAAkB,uBAAuB,MAAM;AAC5D,IAAa,kBAAkB,oBAAoB,MAAM;AACzD,IAAa,iBAAiB,oBAAoB,KAAK;AACvD,IAAa,mBAAmB,oBAAoB,OAAO;AAE3D,IAAa,mBAAmC,MAAc,YAC5D,cAAA,mBAAmB,MAAM,OAAO,OAAO;AAEzC,IAAa,mBAAmC,SAC9C,cAAA,mBAAmB,MAAM,KAAK"}
|
|
@@ -1,54 +1,9 @@
|
|
|
1
|
-
import { RouteBuilder, RoutePath
|
|
1
|
+
import { RouteBuilder, RoutePath } from '../types/index.js';
|
|
2
2
|
import { AppContext, HttpMethod } from '../types/units.js';
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
type WithBodyOptions<TQuery, TParams, TBody, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown> = WithoutBodyOptions<TQuery, TParams, TQueryIn, TParamsIn> & {
|
|
8
|
-
body?: Schema<TBody, TBodyIn>;
|
|
9
|
-
};
|
|
10
|
-
export type CreateWithoutBodyRoute<M extends "GET" | "DELETE" | "OPTIONS" | "HEAD", TAppContext extends Record<string, unknown> = AppContext> = {
|
|
11
|
-
<const Path extends RoutePath>(path: Path): RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
12
|
-
<const Path extends RoutePath, TQuery = unknown, TParams = unknown, TQueryIn = unknown, TParamsIn = unknown>(path: Path, options: WithoutBodyOptions<TQuery, TParams, TQueryIn, TParamsIn>): RouteBuilder<Path, M, readonly [], {
|
|
13
|
-
query: TQuery;
|
|
14
|
-
params: TParams;
|
|
15
|
-
queryIn: TQueryIn;
|
|
16
|
-
paramsIn: TParamsIn;
|
|
17
|
-
}, {}, TAppContext>;
|
|
18
|
-
};
|
|
19
|
-
export type CreateWithBodyRoute<M extends "POST" | "PUT" | "PATCH", TAppContext extends Record<string, unknown> = AppContext> = {
|
|
20
|
-
<const Path extends RoutePath>(path: Path): RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
21
|
-
<const Path extends RoutePath, TQuery = unknown, TParams = unknown, TBody = unknown, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(path: Path, options: WithBodyOptions<TQuery, TParams, TBody, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, M, readonly [], {
|
|
22
|
-
query: TQuery;
|
|
23
|
-
params: TParams;
|
|
24
|
-
body: TBody;
|
|
25
|
-
queryIn: TQueryIn;
|
|
26
|
-
paramsIn: TParamsIn;
|
|
27
|
-
bodyIn: TBodyIn;
|
|
28
|
-
}, {}, TAppContext>;
|
|
29
|
-
};
|
|
30
|
-
export type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = {
|
|
31
|
-
<const Path extends RoutePath, const Methods extends readonly HttpMethod[]>(path: Path, methods: Methods): RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;
|
|
32
|
-
<const Path extends RoutePath, const Methods extends readonly HttpMethod[], TQuery = unknown, TParams = unknown, TBody = unknown, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(path: Path, methods: Methods, options: WithBodyOptions<TQuery, TParams, TBody, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, Methods[number], readonly [], {
|
|
33
|
-
query: TQuery;
|
|
34
|
-
params: TParams;
|
|
35
|
-
body: TBody;
|
|
36
|
-
queryIn: TQueryIn;
|
|
37
|
-
paramsIn: TParamsIn;
|
|
38
|
-
bodyIn: TBodyIn;
|
|
39
|
-
}, {}, TAppContext>;
|
|
40
|
-
};
|
|
41
|
-
export type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = {
|
|
42
|
-
<const Path extends RoutePath>(path: Path): RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;
|
|
43
|
-
<const Path extends RoutePath, TQuery = unknown, TParams = unknown, TBody = unknown, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(path: Path, options: WithBodyOptions<TQuery, TParams, TBody, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, HttpMethod, readonly [], {
|
|
44
|
-
query: TQuery;
|
|
45
|
-
params: TParams;
|
|
46
|
-
body: TBody;
|
|
47
|
-
queryIn: TQueryIn;
|
|
48
|
-
paramsIn: TParamsIn;
|
|
49
|
-
bodyIn: TBodyIn;
|
|
50
|
-
}, {}, TAppContext>;
|
|
51
|
-
};
|
|
3
|
+
export type CreateWithoutBodyRoute<M extends "GET" | "DELETE" | "OPTIONS" | "HEAD", TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
4
|
+
export type CreateWithBodyRoute<M extends "POST" | "PUT" | "PATCH", TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
5
|
+
export type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath, const Methods extends readonly HttpMethod[]>(path: Path, methods: Methods) => RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;
|
|
6
|
+
export type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;
|
|
52
7
|
export declare const createGetRoute: CreateWithoutBodyRoute<"GET", AppContext>;
|
|
53
8
|
export declare const createDeleteRoute: CreateWithoutBodyRoute<"DELETE", AppContext>;
|
|
54
9
|
export declare const createOptionsRoute: CreateWithoutBodyRoute<"OPTIONS", AppContext>;
|
|
@@ -58,4 +13,3 @@ export declare const createPutRoute: CreateWithBodyRoute<"PUT", AppContext>;
|
|
|
58
13
|
export declare const createPatchRoute: CreateWithBodyRoute<"PATCH", AppContext>;
|
|
59
14
|
export declare const createAnyRoute: CreateAnyRoute;
|
|
60
15
|
export declare const createAllRoute: CreateAllRoute;
|
|
61
|
-
export {};
|
|
@@ -22,12 +22,32 @@ function buildRouteBase(path, method, methods, middlewares) {
|
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
24
|
/** Shared internal builder — not part of the public `@taserjs/router` API. */
|
|
25
|
-
function createRouteBuilder(path, method,
|
|
25
|
+
function createRouteBuilder(path, method, methods) {
|
|
26
26
|
const middlewares = [];
|
|
27
27
|
let routeReturns = {};
|
|
28
|
+
const validators = {};
|
|
29
|
+
let bodyMode;
|
|
28
30
|
const builder = {
|
|
29
31
|
path,
|
|
30
32
|
method,
|
|
33
|
+
query(schema) {
|
|
34
|
+
validators.query = schema;
|
|
35
|
+
return builder;
|
|
36
|
+
},
|
|
37
|
+
params(schema) {
|
|
38
|
+
validators.params = schema;
|
|
39
|
+
return builder;
|
|
40
|
+
},
|
|
41
|
+
body(modeOrSchema, schema) {
|
|
42
|
+
if (typeof modeOrSchema === "string") {
|
|
43
|
+
bodyMode = modeOrSchema;
|
|
44
|
+
if (schema !== void 0) validators.body = schema;
|
|
45
|
+
} else {
|
|
46
|
+
bodyMode = "json";
|
|
47
|
+
validators.body = modeOrSchema;
|
|
48
|
+
}
|
|
49
|
+
return builder;
|
|
50
|
+
},
|
|
31
51
|
returns(map) {
|
|
32
52
|
routeReturns = {
|
|
33
53
|
...routeReturns,
|
|
@@ -68,6 +88,7 @@ function createRouteBuilder(path, method, validators = {}, methods) {
|
|
|
68
88
|
handler: unit.handler,
|
|
69
89
|
...routeSchemas,
|
|
70
90
|
...handlerSchemas,
|
|
91
|
+
...bodyMode ? { bodyMode } : {},
|
|
71
92
|
...returns ? { returns } : {}
|
|
72
93
|
};
|
|
73
94
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.cjs","names":[],"sources":["../../../src/builder/route.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport {\n collectReturnsFromDefinitions,\n hasInputSchemas,\n mergeReturnsMaps,\n withAuto422,\n} from \"@taserjs/router-utils\";\n\nimport type {\n Method,\n RouteBuilder,\n RoutePath,\n ReturnsMap,\n ValidatorParts,\n} from \"../types/index.js\";\nimport type { HttpMethod, MiddlewareDefinition } from \"../types/units.js\";\nimport { isHandlerUnit } from \"../types/units.js\";\nimport { HANDLER_SCHEMA_KEY_MAP } from \"./constants.js\";\nimport { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\n\nfunction toUtilsMap(map: ReturnsMap | undefined): Record<number, StandardSchemaV1> {\n if (!map) {\n return {};\n }\n const out: Record<number, StandardSchemaV1> = {};\n for (const [key, schema] of Object.entries(map)) {\n if (schema !== undefined) {\n out[Number(key)] = schema;\n }\n }\n return out;\n}\n\nfunction buildEffectiveReturns(args: {\n middlewareReturns: ReturnsMap;\n routeReturns: ReturnsMap;\n handlerMiddlewareReturns: ReturnsMap;\n handlerReturns: ReturnsMap | undefined;\n schemas: {\n query?: unknown;\n params?: unknown;\n body?: unknown;\n handlerQuery?: unknown;\n handlerParams?: unknown;\n handlerBody?: unknown;\n middlewares?: readonly MiddlewareDefinition[];\n handlerMiddlewares?: readonly MiddlewareDefinition[];\n };\n}): Record<number, StandardSchemaV1> | undefined {\n const merged = mergeReturnsMaps(\n toUtilsMap(args.middlewareReturns),\n toUtilsMap(args.routeReturns),\n toUtilsMap(args.handlerMiddlewareReturns),\n toUtilsMap(args.handlerReturns),\n );\n const with422 = withAuto422(merged, hasInputSchemas(args.schemas));\n return Object.keys(with422).length > 0 ? with422 : undefined;\n}\n\nfunction buildRouteBase(\n path: string,\n method: HttpMethod | \"ANY\" | \"ALL\",\n methods: readonly HttpMethod[] | undefined,\n middlewares: MiddlewareDefinition[],\n) {\n return {\n path,\n method,\n ...(methods ? { methods: [...methods] } : {}),\n middlewares: [...middlewares],\n };\n}\n\n/** Shared internal builder — not part of the public `@taserjs/router` API. */\nexport function createRouteBuilder(\n path: string,\n method: HttpMethod | \"ANY\" | \"ALL\",\n
|
|
1
|
+
{"version":3,"file":"route.cjs","names":[],"sources":["../../../src/builder/route.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport {\n collectReturnsFromDefinitions,\n hasInputSchemas,\n mergeReturnsMaps,\n withAuto422,\n} from \"@taserjs/router-utils\";\nimport type { BodyMode } from \"@taserjs/router-core\";\n\nimport type {\n Method,\n RouteBuilder,\n RoutePath,\n ReturnsMap,\n ValidatorParts,\n Schema,\n} from \"../types/index.js\";\nimport type { HttpMethod, MiddlewareDefinition } from \"../types/units.js\";\nimport { isHandlerUnit } from \"../types/units.js\";\nimport { HANDLER_SCHEMA_KEY_MAP } from \"./constants.js\";\nimport { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\n\nfunction toUtilsMap(map: ReturnsMap | undefined): Record<number, StandardSchemaV1> {\n if (!map) {\n return {};\n }\n const out: Record<number, StandardSchemaV1> = {};\n for (const [key, schema] of Object.entries(map)) {\n if (schema !== undefined) {\n out[Number(key)] = schema;\n }\n }\n return out;\n}\n\nfunction buildEffectiveReturns(args: {\n middlewareReturns: ReturnsMap;\n routeReturns: ReturnsMap;\n handlerMiddlewareReturns: ReturnsMap;\n handlerReturns: ReturnsMap | undefined;\n schemas: {\n query?: unknown;\n params?: unknown;\n body?: unknown;\n handlerQuery?: unknown;\n handlerParams?: unknown;\n handlerBody?: unknown;\n middlewares?: readonly MiddlewareDefinition[];\n handlerMiddlewares?: readonly MiddlewareDefinition[];\n };\n}): Record<number, StandardSchemaV1> | undefined {\n const merged = mergeReturnsMaps(\n toUtilsMap(args.middlewareReturns),\n toUtilsMap(args.routeReturns),\n toUtilsMap(args.handlerMiddlewareReturns),\n toUtilsMap(args.handlerReturns),\n );\n const with422 = withAuto422(merged, hasInputSchemas(args.schemas));\n return Object.keys(with422).length > 0 ? with422 : undefined;\n}\n\nfunction buildRouteBase(\n path: string,\n method: HttpMethod | \"ANY\" | \"ALL\",\n methods: readonly HttpMethod[] | undefined,\n middlewares: MiddlewareDefinition[],\n) {\n return {\n path,\n method,\n ...(methods ? { methods: [...methods] } : {}),\n middlewares: [...middlewares],\n };\n}\n\n/** Shared internal builder — not part of the public `@taserjs/router` API. */\nexport function createRouteBuilder(\n path: string,\n method: HttpMethod | \"ANY\" | \"ALL\",\n methods?: readonly HttpMethod[],\n): RouteBuilder<RoutePath, Method, readonly [], ValidatorParts> {\n const middlewares: MiddlewareDefinition[] = [];\n let routeReturns: Record<number, StandardSchemaV1> = {};\n const validators: SchemaValidators = {};\n let bodyMode: BodyMode | undefined;\n\n const builder = {\n path,\n method,\n query(schema: Schema<unknown>) {\n validators.query = schema;\n return builder;\n },\n params(schema: Schema<unknown>) {\n validators.params = schema;\n return builder;\n },\n body(modeOrSchema: BodyMode | Schema<unknown>, schema?: Schema<unknown>) {\n if (typeof modeOrSchema === \"string\") {\n bodyMode = modeOrSchema as BodyMode;\n if (schema !== undefined) {\n validators.body = schema;\n }\n } else {\n bodyMode = \"json\";\n validators.body = modeOrSchema;\n }\n return builder;\n },\n returns(map: ReturnsMap) {\n routeReturns = { ...routeReturns, ...toUtilsMap(map) };\n return builder;\n },\n use(definition: MiddlewareDefinition) {\n middlewares.push(definition);\n return builder;\n },\n handler(fnOrUnit: ((ctx: unknown) => unknown) | unknown) {\n const routeSchemas = pickDefinedSchemas(validators);\n const base = buildRouteBase(path, method, methods, middlewares);\n\n const unit = isHandlerUnit(fnOrUnit)\n ? fnOrUnit\n : {\n handler: fnOrUnit as (ctx: unknown) => Response | Promise<Response>,\n middlewares: [] as MiddlewareDefinition[],\n returns: undefined as ReturnsMap | undefined,\n };\n\n const handlerSchemas = isHandlerUnit(fnOrUnit)\n ? pickDefinedSchemas(fnOrUnit, HANDLER_SCHEMA_KEY_MAP)\n : {};\n\n const returns = buildEffectiveReturns({\n middlewareReturns: collectReturnsFromDefinitions(middlewares),\n routeReturns,\n handlerMiddlewareReturns: collectReturnsFromDefinitions(unit.middlewares),\n handlerReturns: unit.returns,\n schemas: {\n ...validators,\n handlerQuery: (unit as any).query,\n handlerParams: (unit as any).params,\n handlerBody: (unit as any).body,\n middlewares,\n handlerMiddlewares: unit.middlewares,\n },\n });\n\n return {\n ...base,\n handlerMiddlewares: [...unit.middlewares],\n handler: unit.handler,\n ...routeSchemas,\n ...handlerSchemas,\n ...(bodyMode ? { bodyMode } : {}),\n ...(returns ? { returns } : {}),\n };\n },\n };\n\n return builder as unknown as RouteBuilder<RoutePath, Method, readonly [], ValidatorParts>;\n}\n"],"mappings":";;;;;AAsBA,SAAS,WAAW,KAA+D;CACjF,IAAI,CAAC,KACH,OAAO,CAAC;CAEV,MAAM,MAAwC,CAAC;CAC/C,KAAK,MAAM,CAAC,KAAK,WAAW,OAAO,QAAQ,GAAG,GAC5C,IAAI,WAAW,KAAA,GACb,IAAI,OAAO,GAAG,KAAK;CAGvB,OAAO;AACT;AAEA,SAAS,sBAAsB,MAekB;CAO/C,MAAM,WAAA,GAAA,sBAAA,YAAA,EAAA,GAAA,sBAAA,iBAAA,CALJ,WAAW,KAAK,iBAAiB,GACjC,WAAW,KAAK,YAAY,GAC5B,WAAW,KAAK,wBAAwB,GACxC,WAAW,KAAK,cAAc,CAEJ,IAAA,GAAA,sBAAA,gBAAA,CAAwB,KAAK,OAAO,CAAC;CACjE,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IAAI,UAAU,KAAA;AACrD;AAEA,SAAS,eACP,MACA,QACA,SACA,aACA;CACA,OAAO;EACL;EACA;EACA,GAAI,UAAU,EAAE,SAAS,CAAC,GAAG,OAAO,EAAE,IAAI,CAAC;EAC3C,aAAa,CAAC,GAAG,WAAW;CAC9B;AACF;;AAGA,SAAgB,mBACd,MACA,QACA,SAC8D;CAC9D,MAAM,cAAsC,CAAC;CAC7C,IAAI,eAAiD,CAAC;CACtD,MAAM,aAA+B,CAAC;CACtC,IAAI;CAEJ,MAAM,UAAU;EACd;EACA;EACA,MAAM,QAAyB;GAC7B,WAAW,QAAQ;GACnB,OAAO;EACT;EACA,OAAO,QAAyB;GAC9B,WAAW,SAAS;GACpB,OAAO;EACT;EACA,KAAK,cAA0C,QAA0B;GACvE,IAAI,OAAO,iBAAiB,UAAU;IACpC,WAAW;IACX,IAAI,WAAW,KAAA,GACb,WAAW,OAAO;GAEtB,OAAO;IACL,WAAW;IACX,WAAW,OAAO;GACpB;GACA,OAAO;EACT;EACA,QAAQ,KAAiB;GACvB,eAAe;IAAE,GAAG;IAAc,GAAG,WAAW,GAAG;GAAE;GACrD,OAAO;EACT;EACA,IAAI,YAAkC;GACpC,YAAY,KAAK,UAAU;GAC3B,OAAO;EACT;EACA,QAAQ,UAAiD;GACvD,MAAM,eAAe,mBAAA,mBAAmB,UAAU;GAClD,MAAM,OAAO,eAAe,MAAM,QAAQ,SAAS,WAAW;GAE9D,MAAM,OAAO,cAAA,cAAc,QAAQ,IAC/B,WACA;IACE,SAAS;IACT,aAAa,CAAC;IACd,SAAS,KAAA;GACX;GAEJ,MAAM,iBAAiB,cAAA,cAAc,QAAQ,IACzC,mBAAA,mBAAmB,UAAU,kBAAA,sBAAsB,IACnD,CAAC;GAEL,MAAM,UAAU,sBAAsB;IACpC,oBAAA,GAAA,sBAAA,8BAAA,CAAiD,WAAW;IAC5D;IACA,2BAAA,GAAA,sBAAA,8BAAA,CAAwD,KAAK,WAAW;IACxE,gBAAgB,KAAK;IACrB,SAAS;KACP,GAAG;KACH,cAAe,KAAa;KAC5B,eAAgB,KAAa;KAC7B,aAAc,KAAa;KAC3B;KACA,oBAAoB,KAAK;IAC3B;GACF,CAAC;GAED,OAAO;IACL,GAAG;IACH,oBAAoB,CAAC,GAAG,KAAK,WAAW;IACxC,SAAS,KAAK;IACd,GAAG;IACH,GAAG;IACH,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;IAC/B,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;GAC/B;EACF;CACF;CAEA,OAAO;AACT"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Method, RouteBuilder, RoutePath, ValidatorParts } from '../types/index.js';
|
|
2
2
|
import { HttpMethod } from '../types/units.js';
|
|
3
|
-
import { SchemaValidators } from '../define/validators.js';
|
|
4
3
|
/** Shared internal builder — not part of the public `@taserjs/router` API. */
|
|
5
|
-
export declare function createRouteBuilder(path: string, method: HttpMethod | "ANY" | "ALL",
|
|
4
|
+
export declare function createRouteBuilder(path: string, method: HttpMethod | "ANY" | "ALL", methods?: readonly HttpMethod[]): RouteBuilder<RoutePath, Method, readonly [], ValidatorParts>;
|
|
@@ -47,14 +47,12 @@ var TaserRouter = class {
|
|
|
47
47
|
defineHandler(options) {
|
|
48
48
|
return options === void 0 ? require_handler.defineHandler() : require_handler.defineHandler(options);
|
|
49
49
|
}
|
|
50
|
-
defineMiddleware
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
create(manifest) {
|
|
50
|
+
defineMiddleware = require_middleware.defineMiddleware;
|
|
51
|
+
create(manifest, runtimeOptions) {
|
|
54
52
|
const definition = this.state.contextDef;
|
|
55
53
|
const validateResponse = this.state.options.response?.validate ?? true;
|
|
56
54
|
const onValidationFailure = this.state.options.response?.onValidationFailure;
|
|
57
|
-
const basePath =
|
|
55
|
+
const basePath = runtimeOptions?.basePath;
|
|
58
56
|
const hasCustomContext = definition.boot !== void 0 || definition.request !== void 0;
|
|
59
57
|
let bootContextResolved;
|
|
60
58
|
let bootPromise;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.cjs","names":[],"sources":["../../../src/builder/router.ts"],"sourcesContent":["import {\n createTaserCompatHandler,\n createTaserRuntime,\n type NotFoundHandler,\n type OnErrorHandler,\n type RouteManifestShape,\n} from \"@taserjs/router-core\";\nimport { isPromise, normalizeOnError } from \"@taserjs/router-utils\";\n\nimport {\n createAllRoute,\n createAnyRoute,\n createDeleteRoute,\n createGetRoute,\n createHeadRoute,\n createOptionsRoute,\n createPatchRoute,\n createPostRoute,\n createPutRoute,\n} from \"./factories.js\";\nimport { defineHandler } from \"../define/handler.js\";\nimport {\n defineMiddleware,\n type DefineMiddlewareOptions,\n type MultiScopedMiddlewareOptions,\n type ScopedMiddlewareOptions,\n} from \"../define/middleware.js\";\nimport { createMiddleware } from \"./middleware.js\";\nimport { TaserApp } from \"./app.js\";\nimport type { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from \"../types/app.js\";\nimport type {\n AppContext,\n ExtractState,\n IsUnknown,\n MiddlewareReturnFromParts,\n MiddlewareUnit,\n} from \"../types/units.js\";\nimport type { HandlerBuilder, LayoutId, MiddlewareBuilder } from \"../types/index.js\";\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type { Schema } from \"../types/schema.js\";\nimport type {\n CreateAllRoute,\n CreateAnyRoute,\n CreateWithBodyRoute,\n CreateWithoutBodyRoute,\n} from \"./factories.js\";\n\nfunction toOnErrorHandler(\n onErrorOptions: OnErrorOptions | OnErrorOptions[\"handle\"],\n): OnErrorHandler {\n return normalizeOnError(onErrorOptions);\n}\n\ntype RouterState = {\n options: CreateTaserAppOptions;\n contextDef: ContextDefinition<Record<string, unknown>, Record<string, unknown>>;\n onError?: OnErrorHandler;\n notFound?: NotFoundHandler;\n};\n\nconst emptyContext: ContextDefinition<Record<string, unknown>, Record<string, unknown>> = {};\n\nexport class TaserRouter<TAppContext extends Record<string, unknown> = AppContext> {\n private readonly state: RouterState;\n\n constructor(options: CreateTaserAppOptions = {}, state?: Partial<Omit<RouterState, \"options\">>) {\n this.state = {\n options,\n contextDef: state?.contextDef ?? emptyContext,\n ...(state?.onError !== undefined ? { onError: state.onError } : {}),\n ...(state?.notFound !== undefined ? { notFound: state.notFound } : {}),\n };\n }\n\n context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(\n definition: ContextDefinition<TBoot, TReq>,\n ): TaserRouter<TBoot & TReq> {\n this.state.contextDef = definition;\n return this as unknown as TaserRouter<TBoot & TReq>;\n }\n\n onError<TResponses extends ReturnsMap = ReturnsMap>(\n options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>[\"handle\"],\n ): this {\n this.state.onError = toOnErrorHandler(options);\n return this;\n }\n\n notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown): this {\n this.state.notFound = (ctx) => handler(ctx);\n return this;\n }\n\n get: CreateWithoutBodyRoute<\"GET\", TAppContext> = createGetRoute as CreateWithoutBodyRoute<\n \"GET\",\n TAppContext\n >;\n post: CreateWithBodyRoute<\"POST\", TAppContext> = createPostRoute as CreateWithBodyRoute<\n \"POST\",\n TAppContext\n >;\n put: CreateWithBodyRoute<\"PUT\", TAppContext> = createPutRoute as CreateWithBodyRoute<\n \"PUT\",\n TAppContext\n >;\n patch: CreateWithBodyRoute<\"PATCH\", TAppContext> = createPatchRoute as CreateWithBodyRoute<\n \"PATCH\",\n TAppContext\n >;\n delete: CreateWithoutBodyRoute<\"DELETE\", TAppContext> =\n createDeleteRoute as CreateWithoutBodyRoute<\"DELETE\", TAppContext>;\n options: CreateWithoutBodyRoute<\"OPTIONS\", TAppContext> =\n createOptionsRoute as CreateWithoutBodyRoute<\"OPTIONS\", TAppContext>;\n head: CreateWithoutBodyRoute<\"HEAD\", TAppContext> = createHeadRoute as CreateWithoutBodyRoute<\n \"HEAD\",\n TAppContext\n >;\n any: CreateAnyRoute<TAppContext> = createAnyRoute as CreateAnyRoute<TAppContext>;\n all: CreateAllRoute<TAppContext> = createAllRoute as CreateAllRoute<TAppContext>;\n\n middleware<const Layout extends LayoutId>(\n layout: Layout,\n ): MiddlewareBuilder<Layout, readonly [], TAppContext> {\n return createMiddleware(layout) as MiddlewareBuilder<Layout, readonly [], TAppContext>;\n }\n\n defineHandler<TQuery = unknown, TParams = unknown, TBody = unknown>(options?: {\n query?: Schema<TQuery>;\n params?: Schema<TParams>;\n body?: Schema<TBody>;\n }): HandlerBuilder<\n readonly [],\n { query: TQuery; params: TParams; body: TBody },\n {},\n TAppContext\n > {\n return options === undefined\n ? defineHandler<TAppContext>()\n : defineHandler<TAppContext>(options);\n }\n\n defineMiddleware(\n middleware: Parameters<typeof createTaserCompatHandler>[0],\n ): MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;\n\n defineMiddleware<\n const Layout extends LayoutId,\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n >(\n layout: Layout,\n options: ScopedMiddlewareOptions<\n Layout,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): MiddlewareUnit<\n MiddlewareReturnFromParts<\n TQuery,\n TParams,\n TBody,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n TQueryIn,\n TParamsIn,\n TBodyIn\n >,\n TReturns,\n Layout,\n TRequires\n >;\n\n defineMiddleware<\n const Layouts extends readonly [LayoutId, ...LayoutId[]],\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n >(\n layouts: Layouts,\n options: MultiScopedMiddlewareOptions<\n Layouts,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): MiddlewareUnit<\n MiddlewareReturnFromParts<\n TQuery,\n TParams,\n TBody,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n TQueryIn,\n TParamsIn,\n TBodyIn\n >,\n TReturns,\n Layouts,\n TRequires\n >;\n\n defineMiddleware<\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n >(\n options: DefineMiddlewareOptions<\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): MiddlewareUnit<\n MiddlewareReturnFromParts<\n TQuery,\n TParams,\n TBody,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n TQueryIn,\n TParamsIn,\n TBodyIn\n >,\n TReturns,\n null,\n TRequires\n >;\n\n defineMiddleware(first: any, second?: any): any {\n return defineMiddleware(first, second);\n }\n\n create<const TManifest extends RouteManifestShape>(manifest: TManifest): TaserApp<TManifest> {\n const definition = this.state.contextDef;\n const validateResponse = this.state.options.response?.validate ?? true;\n const onValidationFailure = this.state.options.response?.onValidationFailure;\n const basePath = this.state.options.basePath;\n\n const hasCustomContext = definition.boot !== undefined || definition.request !== undefined;\n let bootContextResolved: Record<string, unknown> | undefined;\n let bootPromise: Promise<Record<string, unknown>> | undefined;\n\n if (definition.boot !== undefined) {\n const bootResult = definition.boot();\n if (isPromise(bootResult)) {\n bootPromise = bootResult.then((ctx) => {\n bootContextResolved = (ctx ?? {}) as Record<string, unknown>;\n return bootContextResolved;\n });\n } else {\n bootContextResolved = (bootResult ?? {}) as Record<string, unknown>;\n }\n } else if (hasCustomContext) {\n bootContextResolved = {};\n }\n\n const contextFactory = hasCustomContext\n ? (req?: Request) => {\n if (bootPromise !== undefined && bootContextResolved === undefined) {\n return bootPromise.then(async (bootContext) => {\n if (!definition.request) {\n return { ...bootContext };\n }\n const requestResult = definition.request(req!);\n const requestContext = isPromise(requestResult) ? await requestResult : requestResult;\n return { ...bootContext, ...requestContext };\n });\n }\n\n const bootContext = bootContextResolved ?? {};\n if (!definition.request) {\n return { ...bootContext };\n }\n\n const requestResult = definition.request(req!);\n if (isPromise(requestResult)) {\n return requestResult.then((requestContext) => ({\n ...bootContext,\n ...requestContext,\n }));\n }\n\n return { ...bootContext, ...(requestResult as Record<string, unknown>) };\n }\n : () => ({});\n\n const runtime = createTaserRuntime(manifest, contextFactory, {\n ...(basePath !== undefined ? { basePath } : {}),\n response: {\n validate: validateResponse,\n ...(onValidationFailure !== undefined ? { onValidationFailure } : {}),\n },\n ...(this.state.onError !== undefined ? { onError: this.state.onError } : {}),\n ...(this.state.notFound !== undefined ? { notFound: this.state.notFound } : {}),\n ...(this.state.options.cookies !== undefined ? { cookies: this.state.options.cookies } : {}),\n });\n\n return new TaserApp(runtime, manifest);\n }\n}\n\nexport function createTaserApp(options: CreateTaserAppOptions = {}): TaserRouter {\n return new TaserRouter(options);\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"router.cjs","names":[],"sources":["../../../src/builder/router.ts"],"sourcesContent":["import {\n createTaserRuntime,\n type NotFoundHandler,\n type OnErrorHandler,\n type RouteManifestShape,\n} from \"@taserjs/router-core\";\nimport { isPromise, normalizeOnError } from \"@taserjs/router-utils\";\n\nimport {\n createAllRoute,\n createAnyRoute,\n createDeleteRoute,\n createGetRoute,\n createHeadRoute,\n createOptionsRoute,\n createPatchRoute,\n createPostRoute,\n createPutRoute,\n} from \"./factories.js\";\nimport { defineHandler } from \"../define/handler.js\";\nimport { defineMiddleware, type DefineMiddlewareFn } from \"../define/middleware.js\";\nimport { createMiddleware } from \"./middleware.js\";\nimport { TaserApp } from \"./app.js\";\nimport type { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from \"../types/app.js\";\nimport type { AppContext } from \"../types/units.js\";\nimport type { HandlerBuilder, LayoutId, MiddlewareBuilder } from \"../types/index.js\";\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type { Schema } from \"../types/schema.js\";\nimport type {\n CreateAllRoute,\n CreateAnyRoute,\n CreateWithBodyRoute,\n CreateWithoutBodyRoute,\n} from \"./factories.js\";\n\nfunction toOnErrorHandler(\n onErrorOptions: OnErrorOptions | OnErrorOptions[\"handle\"],\n): OnErrorHandler {\n return normalizeOnError(onErrorOptions);\n}\n\ntype RouterState = {\n options: CreateTaserAppOptions;\n contextDef: ContextDefinition<Record<string, unknown>, Record<string, unknown>>;\n onError?: OnErrorHandler;\n notFound?: NotFoundHandler;\n};\n\nconst emptyContext: ContextDefinition<Record<string, unknown>, Record<string, unknown>> = {};\n\nexport class TaserRouter<\n TAppContext extends Record<string, unknown> = AppContext,\n THasNotFound extends boolean = false,\n> {\n private readonly state: RouterState;\n\n constructor(options: CreateTaserAppOptions = {}, state?: Partial<Omit<RouterState, \"options\">>) {\n this.state = {\n options,\n contextDef: state?.contextDef ?? emptyContext,\n ...(state?.onError !== undefined ? { onError: state.onError } : {}),\n ...(state?.notFound !== undefined ? { notFound: state.notFound } : {}),\n };\n }\n\n context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(\n definition: ContextDefinition<TBoot, TReq>,\n ): TaserRouter<TBoot & TReq, THasNotFound> {\n this.state.contextDef = definition;\n return this as unknown as TaserRouter<TBoot & TReq, THasNotFound>;\n }\n\n onError<TResponses extends ReturnsMap = ReturnsMap>(\n options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>[\"handle\"],\n ): this {\n this.state.onError = toOnErrorHandler(options);\n return this;\n }\n\n notFound(\n handler: (ctx: unknown) => Response | Promise<Response> | unknown,\n ): TaserRouter<TAppContext, true> {\n this.state.notFound = (ctx) => handler(ctx);\n return this as unknown as TaserRouter<TAppContext, true>;\n }\n\n get: CreateWithoutBodyRoute<\"GET\", TAppContext> = createGetRoute as CreateWithoutBodyRoute<\n \"GET\",\n TAppContext\n >;\n post: CreateWithBodyRoute<\"POST\", TAppContext> = createPostRoute as CreateWithBodyRoute<\n \"POST\",\n TAppContext\n >;\n put: CreateWithBodyRoute<\"PUT\", TAppContext> = createPutRoute as CreateWithBodyRoute<\n \"PUT\",\n TAppContext\n >;\n patch: CreateWithBodyRoute<\"PATCH\", TAppContext> = createPatchRoute as CreateWithBodyRoute<\n \"PATCH\",\n TAppContext\n >;\n delete: CreateWithoutBodyRoute<\"DELETE\", TAppContext> =\n createDeleteRoute as CreateWithoutBodyRoute<\"DELETE\", TAppContext>;\n options: CreateWithoutBodyRoute<\"OPTIONS\", TAppContext> =\n createOptionsRoute as CreateWithoutBodyRoute<\"OPTIONS\", TAppContext>;\n head: CreateWithoutBodyRoute<\"HEAD\", TAppContext> = createHeadRoute as CreateWithoutBodyRoute<\n \"HEAD\",\n TAppContext\n >;\n any: CreateAnyRoute<TAppContext> = createAnyRoute as CreateAnyRoute<TAppContext>;\n all: CreateAllRoute<TAppContext> = createAllRoute as CreateAllRoute<TAppContext>;\n\n middleware<const Layout extends LayoutId>(\n layout: Layout,\n ): MiddlewareBuilder<Layout, readonly [], TAppContext> {\n return createMiddleware(layout) as MiddlewareBuilder<Layout, readonly [], TAppContext>;\n }\n\n defineHandler<TQuery = unknown, TParams = unknown, TBody = unknown>(options?: {\n query?: Schema<TQuery>;\n params?: Schema<TParams>;\n body?: Schema<TBody>;\n }): HandlerBuilder<\n readonly [],\n { query: TQuery; params: TParams; body: TBody },\n {},\n TAppContext\n > {\n return options === undefined\n ? defineHandler<TAppContext>()\n : defineHandler<TAppContext>(options);\n }\n\n defineMiddleware: DefineMiddlewareFn<TAppContext> =\n defineMiddleware as DefineMiddlewareFn<TAppContext>;\n\n create<const TManifest extends RouteManifestShape>(\n manifest: TManifest,\n runtimeOptions?: { basePath?: string },\n ): TaserApp<TManifest, THasNotFound> {\n const definition = this.state.contextDef;\n const validateResponse = this.state.options.response?.validate ?? true;\n const onValidationFailure = this.state.options.response?.onValidationFailure;\n const basePath = runtimeOptions?.basePath;\n\n const hasCustomContext = definition.boot !== undefined || definition.request !== undefined;\n let bootContextResolved: Record<string, unknown> | undefined;\n let bootPromise: Promise<Record<string, unknown>> | undefined;\n\n if (definition.boot !== undefined) {\n const bootResult = definition.boot();\n if (isPromise(bootResult)) {\n bootPromise = bootResult.then((ctx) => {\n bootContextResolved = (ctx ?? {}) as Record<string, unknown>;\n return bootContextResolved;\n });\n } else {\n bootContextResolved = (bootResult ?? {}) as Record<string, unknown>;\n }\n } else if (hasCustomContext) {\n bootContextResolved = {};\n }\n\n const contextFactory = hasCustomContext\n ? (req?: Request) => {\n if (bootPromise !== undefined && bootContextResolved === undefined) {\n return bootPromise.then(async (bootContext) => {\n if (!definition.request) {\n return { ...bootContext };\n }\n const requestResult = definition.request(req!);\n const requestContext = isPromise(requestResult) ? await requestResult : requestResult;\n return { ...bootContext, ...requestContext };\n });\n }\n\n const bootContext = bootContextResolved ?? {};\n if (!definition.request) {\n return { ...bootContext };\n }\n\n const requestResult = definition.request(req!);\n if (isPromise(requestResult)) {\n return requestResult.then((requestContext) => ({\n ...bootContext,\n ...requestContext,\n }));\n }\n\n return { ...bootContext, ...(requestResult as Record<string, unknown>) };\n }\n : () => ({});\n\n const runtime = createTaserRuntime(manifest, contextFactory, {\n ...(basePath !== undefined ? { basePath } : {}),\n response: {\n validate: validateResponse,\n ...(onValidationFailure !== undefined ? { onValidationFailure } : {}),\n },\n ...(this.state.onError !== undefined ? { onError: this.state.onError } : {}),\n ...(this.state.notFound !== undefined ? { notFound: this.state.notFound } : {}),\n ...(this.state.options.cookies !== undefined ? { cookies: this.state.options.cookies } : {}),\n });\n\n return new TaserApp(runtime, manifest) as unknown as TaserApp<TManifest, THasNotFound>;\n }\n}\n\nexport function createTaserApp(\n options: CreateTaserAppOptions = {},\n): TaserRouter<AppContext, false> {\n return new TaserRouter<AppContext, false>(options);\n}\n"],"mappings":";;;;;;;;AAmCA,SAAS,iBACP,gBACgB;CAChB,QAAA,GAAA,sBAAA,iBAAA,CAAwB,cAAc;AACxC;AASA,IAAM,eAAoF,CAAC;AAE3F,IAAa,cAAb,MAGE;CACA;CAEA,YAAY,UAAiC,CAAC,GAAG,OAA+C;EAC9F,KAAK,QAAQ;GACX;GACA,YAAY,OAAO,cAAc;GACjC,GAAI,OAAO,YAAY,KAAA,IAAY,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;GACjE,GAAI,OAAO,aAAa,KAAA,IAAY,EAAE,UAAU,MAAM,SAAS,IAAI,CAAC;EACtE;CACF;CAEA,QACE,YACyC;EACzC,KAAK,MAAM,aAAa;EACxB,OAAO;CACT;CAEA,QACE,SACM;EACN,KAAK,MAAM,UAAU,iBAAiB,OAAO;EAC7C,OAAO;CACT;CAEA,SACE,SACgC;EAChC,KAAK,MAAM,YAAY,QAAQ,QAAQ,GAAG;EAC1C,OAAO;CACT;CAEA,MAAkD,kBAAA;CAIlD,OAAiD,kBAAA;CAIjD,MAA+C,kBAAA;CAI/C,QAAmD,kBAAA;CAInD,SACE,kBAAA;CACF,UACE,kBAAA;CACF,OAAoD,kBAAA;CAIpD,MAAmC,kBAAA;CACnC,MAAmC,kBAAA;CAEnC,WACE,QACqD;EACrD,OAAO,qBAAA,iBAAiB,MAAM;CAChC;CAEA,cAAoE,SASlE;EACA,OAAO,YAAY,KAAA,IACf,gBAAA,cAA2B,IAC3B,gBAAA,cAA2B,OAAO;CACxC;CAEA,mBACE,mBAAA;CAEF,OACE,UACA,gBACmC;EACnC,MAAM,aAAa,KAAK,MAAM;EAC9B,MAAM,mBAAmB,KAAK,MAAM,QAAQ,UAAU,YAAY;EAClE,MAAM,sBAAsB,KAAK,MAAM,QAAQ,UAAU;EACzD,MAAM,WAAW,gBAAgB;EAEjC,MAAM,mBAAmB,WAAW,SAAS,KAAA,KAAa,WAAW,YAAY,KAAA;EACjF,IAAI;EACJ,IAAI;EAEJ,IAAI,WAAW,SAAS,KAAA,GAAW;GACjC,MAAM,aAAa,WAAW,KAAK;GACnC,KAAA,GAAA,sBAAA,UAAA,CAAc,UAAU,GACtB,cAAc,WAAW,MAAM,QAAQ;IACrC,sBAAuB,OAAO,CAAC;IAC/B,OAAO;GACT,CAAC;QAED,sBAAuB,cAAc,CAAC;EAE1C,OAAO,IAAI,kBACT,sBAAsB,CAAC;EA4CzB,OAAO,IAAI,YAAA,UAAA,GAAA,qBAAA,mBAAA,CAXwB,UA9BZ,oBAClB,QAAkB;GACjB,IAAI,gBAAgB,KAAA,KAAa,wBAAwB,KAAA,GACvD,OAAO,YAAY,KAAK,OAAO,gBAAgB;IAC7C,IAAI,CAAC,WAAW,SACd,OAAO,EAAE,GAAG,YAAY;IAE1B,MAAM,gBAAgB,WAAW,QAAQ,GAAI;IAC7C,MAAM,kBAAA,GAAA,sBAAA,UAAA,CAA2B,aAAa,IAAI,MAAM,gBAAgB;IACxE,OAAO;KAAE,GAAG;KAAa,GAAG;IAAe;GAC7C,CAAC;GAGH,MAAM,cAAc,uBAAuB,CAAC;GAC5C,IAAI,CAAC,WAAW,SACd,OAAO,EAAE,GAAG,YAAY;GAG1B,MAAM,gBAAgB,WAAW,QAAQ,GAAI;GAC7C,KAAA,GAAA,sBAAA,UAAA,CAAc,aAAa,GACzB,OAAO,cAAc,MAAM,oBAAoB;IAC7C,GAAG;IACH,GAAG;GACL,EAAE;GAGJ,OAAO;IAAE,GAAG;IAAa,GAAI;GAA0C;EACzE,WACO,CAAC,IAEiD;GAC3D,GAAI,aAAa,KAAA,IAAY,EAAE,SAAS,IAAI,CAAC;GAC7C,UAAU;IACR,UAAU;IACV,GAAI,wBAAwB,KAAA,IAAY,EAAE,oBAAoB,IAAI,CAAC;GACrE;GACA,GAAI,KAAK,MAAM,YAAY,KAAA,IAAY,EAAE,SAAS,KAAK,MAAM,QAAQ,IAAI,CAAC;GAC1E,GAAI,KAAK,MAAM,aAAa,KAAA,IAAY,EAAE,UAAU,KAAK,MAAM,SAAS,IAAI,CAAC;GAC7E,GAAI,KAAK,MAAM,QAAQ,YAAY,KAAA,IAAY,EAAE,SAAS,KAAK,MAAM,QAAQ,QAAQ,IAAI,CAAC;EAC5F,CAEoB,GAAS,QAAQ;CACvC;AACF;AAEA,SAAgB,eACd,UAAiC,CAAC,GACF;CAChC,OAAO,IAAI,YAA+B,OAAO;AACnD"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { NotFoundHandler, OnErrorHandler, RouteManifestShape } from '@taserjs/router-core';
|
|
2
|
+
import { DefineMiddlewareFn } from '../define/middleware.js';
|
|
3
3
|
import { TaserApp } from './app.js';
|
|
4
4
|
import { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from '../types/app.js';
|
|
5
|
-
import { AppContext
|
|
5
|
+
import { AppContext } from '../types/units.js';
|
|
6
6
|
import { HandlerBuilder, LayoutId, MiddlewareBuilder } from '../types/index.js';
|
|
7
7
|
import { ReturnsMap } from '../types/returns.js';
|
|
8
8
|
import { Schema } from '../types/schema.js';
|
|
@@ -13,12 +13,12 @@ type RouterState = {
|
|
|
13
13
|
onError?: OnErrorHandler;
|
|
14
14
|
notFound?: NotFoundHandler;
|
|
15
15
|
};
|
|
16
|
-
export declare class TaserRouter<TAppContext extends Record<string, unknown> = AppContext> {
|
|
16
|
+
export declare class TaserRouter<TAppContext extends Record<string, unknown> = AppContext, THasNotFound extends boolean = false> {
|
|
17
17
|
private readonly state;
|
|
18
18
|
constructor(options?: CreateTaserAppOptions, state?: Partial<Omit<RouterState, "options">>);
|
|
19
|
-
context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(definition: ContextDefinition<TBoot, TReq>): TaserRouter<TBoot & TReq>;
|
|
19
|
+
context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(definition: ContextDefinition<TBoot, TReq>): TaserRouter<TBoot & TReq, THasNotFound>;
|
|
20
20
|
onError<TResponses extends ReturnsMap = ReturnsMap>(options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>["handle"]): this;
|
|
21
|
-
notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown):
|
|
21
|
+
notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown): TaserRouter<TAppContext, true>;
|
|
22
22
|
get: CreateWithoutBodyRoute<"GET", TAppContext>;
|
|
23
23
|
post: CreateWithBodyRoute<"POST", TAppContext>;
|
|
24
24
|
put: CreateWithBodyRoute<"PUT", TAppContext>;
|
|
@@ -38,11 +38,10 @@ export declare class TaserRouter<TAppContext extends Record<string, unknown> = A
|
|
|
38
38
|
params: TParams;
|
|
39
39
|
body: TBody;
|
|
40
40
|
}, {}, TAppContext>;
|
|
41
|
-
defineMiddleware
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
create<const TManifest extends RouteManifestShape>(manifest: TManifest): TaserApp<TManifest>;
|
|
41
|
+
defineMiddleware: DefineMiddlewareFn<TAppContext>;
|
|
42
|
+
create<const TManifest extends RouteManifestShape>(manifest: TManifest, runtimeOptions?: {
|
|
43
|
+
basePath?: string;
|
|
44
|
+
}): TaserApp<TManifest, THasNotFound>;
|
|
46
45
|
}
|
|
47
|
-
export declare function createTaserApp(options?: CreateTaserAppOptions): TaserRouter
|
|
46
|
+
export declare function createTaserApp(options?: CreateTaserAppOptions): TaserRouter<AppContext, false>;
|
|
48
47
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
let _taserjs_router_core = require("@taserjs/router-core");
|
|
2
2
|
//#region src/define/middleware.ts
|
|
3
|
-
function defineMiddleware(first, second) {
|
|
3
|
+
var defineMiddleware = function defineMiddleware(first, second) {
|
|
4
4
|
if (typeof first === "string" || Array.isArray(first)) {
|
|
5
5
|
const options = second;
|
|
6
6
|
return {
|
|
@@ -20,7 +20,7 @@ function defineMiddleware(first, second) {
|
|
|
20
20
|
__middlewareAcc: void 0,
|
|
21
21
|
...options.returns ? { __returns: options.returns } : {}
|
|
22
22
|
};
|
|
23
|
-
}
|
|
23
|
+
};
|
|
24
24
|
//#endregion
|
|
25
25
|
exports.defineMiddleware = defineMiddleware;
|
|
26
26
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":[],"sources":["../../../src/define/middleware.ts"],"sourcesContent":["import { createTaserCompatHandler, type Awaitable } from \"@taserjs/router-core\";\n\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type {\n AppContext,\n
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":[],"sources":["../../../src/define/middleware.ts"],"sourcesContent":["import { createTaserCompatHandler, type Awaitable } from \"@taserjs/router-core\";\n\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type {\n AppContext,\n DefineMiddlewareResult,\n MiddlewareReturnFromParts,\n MiddlewareUnit,\n NextFn,\n StandaloneMiddlewareContext,\n} from \"../types/units.js\";\nimport type {\n LayoutId,\n ResolveLayoutMiddlewaresState,\n ResolveLayoutsState,\n} from \"../types/index.js\";\nimport type { Schema } from \"../types/schema.js\";\n\nexport type HonoMiddlewareHandler = Parameters<typeof createTaserCompatHandler>[0];\n\nexport type HonoMiddlewareUnit = MiddlewareUnit<\n MiddlewareReturnFromParts<unknown, unknown, unknown, {}>,\n {},\n null,\n {}\n>;\n\nexport type DefineMiddlewareOptions<\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (\n ctx: StandaloneMiddlewareContext<TQuery, TParams, TBody, TAppContext, TRequires>,\n next: NextFn<NoInfer<TState>>,\n ) => Awaitable<R>;\n};\n\nexport type ScopedMiddlewareOptions<\n Layout extends LayoutId,\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (\n ctx: StandaloneMiddlewareContext<\n TQuery,\n TParams,\n TBody,\n TAppContext,\n ResolveLayoutMiddlewaresState<Layout> & TRequires\n >,\n next: NextFn<NoInfer<TState>>,\n ) => Awaitable<R>;\n};\n\nexport type MultiScopedMiddlewareOptions<\n Layouts extends readonly LayoutId[],\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (\n ctx: StandaloneMiddlewareContext<\n TQuery,\n TParams,\n TBody,\n TAppContext,\n ResolveLayoutsState<Layouts> & TRequires\n >,\n next: NextFn<NoInfer<TState>>,\n ) => Awaitable<R>;\n};\n\nexport interface DefineMiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {\n (middleware: HonoMiddlewareHandler): HonoMiddlewareUnit;\n\n <\n const Layout extends LayoutId,\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n >(\n layout: Layout,\n options: ScopedMiddlewareOptions<\n Layout,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TReturns,\n Layout,\n TRequires\n >;\n\n <\n const Layouts extends readonly [LayoutId, ...LayoutId[]],\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n >(\n layouts: Layouts,\n options: MultiScopedMiddlewareOptions<\n Layouts,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TReturns,\n Layouts,\n TRequires\n >;\n\n <\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n >(\n options: DefineMiddlewareOptions<\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TReturns,\n null,\n TRequires\n >;\n}\n\nexport const defineMiddleware: DefineMiddlewareFn<AppContext> = function defineMiddleware(\n first: any,\n second?: any,\n): any {\n if (typeof first === \"string\" || Array.isArray(first)) {\n const options = second!;\n const unit = {\n ...options,\n __middlewareAcc: undefined as unknown,\n ...(options?.returns ? { __returns: options.returns } : {}),\n __requiredLayouts: first,\n };\n return unit;\n }\n\n if (typeof first === \"function\") {\n const honoMiddleware = first as HonoMiddlewareHandler;\n return defineMiddleware({\n handler: (ctx: unknown, next: (state?: Record<string, unknown>) => unknown) =>\n createTaserCompatHandler(honoMiddleware)(ctx, next),\n });\n }\n\n const options = first as DefineMiddlewareOptions<\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any\n >;\n const unit = {\n ...options,\n __middlewareAcc: undefined as unknown,\n ...(options.returns ? { __returns: options.returns } : {}),\n };\n\n return unit;\n};\n"],"mappings":";;AA6OA,IAAa,mBAAmD,SAAS,iBACvE,OACA,QACK;CACL,IAAI,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;EACrD,MAAM,UAAU;EAOhB,OAAO;GALL,GAAG;GACH,iBAAiB,KAAA;GACjB,GAAI,SAAS,UAAU,EAAE,WAAW,QAAQ,QAAQ,IAAI,CAAC;GACzD,mBAAmB;EAEd;CACT;CAEA,IAAI,OAAO,UAAU,YAAY;EAC/B,MAAM,iBAAiB;EACvB,OAAO,iBAAiB,EACtB,UAAU,KAAc,UAAA,GAAA,qBAAA,yBAAA,CACG,cAAc,CAAC,CAAC,KAAK,IAAI,EACtD,CAAC;CACH;CAEA,MAAM,UAAU;CAmBhB,OAAO;EALL,GAAG;EACH,iBAAiB,KAAA;EACjB,GAAI,QAAQ,UAAU,EAAE,WAAW,QAAQ,QAAQ,IAAI,CAAC;CAGnD;AACT"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createTaserCompatHandler, Awaitable } from '@taserjs/router-core';
|
|
2
2
|
import { ReturnsMap } from '../types/returns.js';
|
|
3
|
-
import { AppContext,
|
|
3
|
+
import { AppContext, DefineMiddlewareResult, MiddlewareReturnFromParts, MiddlewareUnit, NextFn, StandaloneMiddlewareContext } from '../types/units.js';
|
|
4
4
|
import { LayoutId, ResolveLayoutMiddlewaresState, ResolveLayoutsState } from '../types/index.js';
|
|
5
5
|
import { Schema } from '../types/schema.js';
|
|
6
|
-
type HonoMiddlewareHandler = Parameters<typeof createTaserCompatHandler>[0];
|
|
7
|
-
type HonoMiddlewareUnit = MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;
|
|
6
|
+
export type HonoMiddlewareHandler = Parameters<typeof createTaserCompatHandler>[0];
|
|
7
|
+
export type HonoMiddlewareUnit = MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;
|
|
8
8
|
export type DefineMiddlewareOptions<TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, TAppContext extends Record<string, unknown> = AppContext, R = unknown> = {
|
|
9
9
|
query?: Schema<TQuery, TQueryIn>;
|
|
10
10
|
params?: Schema<TParams, TParamsIn>;
|
|
@@ -26,8 +26,10 @@ export type MultiScopedMiddlewareOptions<Layouts extends readonly LayoutId[], TS
|
|
|
26
26
|
returns?: TReturns;
|
|
27
27
|
handler: (ctx: StandaloneMiddlewareContext<TQuery, TParams, TBody, TAppContext, ResolveLayoutsState<Layouts> & TRequires>, next: NextFn<NoInfer<TState>>) => Awaitable<R>;
|
|
28
28
|
};
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
export interface DefineMiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {
|
|
30
|
+
(middleware: HonoMiddlewareHandler): HonoMiddlewareUnit;
|
|
31
|
+
<const Layout extends LayoutId, TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, R = unknown>(layout: Layout, options: ScopedMiddlewareOptions<Layout, TState, TRequires, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn, TAppContext, R>): DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn, TParamsIn, TBodyIn, TReturns, Layout, TRequires>;
|
|
32
|
+
<const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, R = unknown>(layouts: Layouts, options: MultiScopedMiddlewareOptions<Layouts, TState, TRequires, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn, TAppContext, R>): DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn, TParamsIn, TBodyIn, TReturns, Layouts, TRequires>;
|
|
33
|
+
<TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, R = unknown>(options: DefineMiddlewareOptions<TState, TRequires, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn, TAppContext, R>): DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn, TParamsIn, TBodyIn, TReturns, null, TRequires>;
|
|
34
|
+
}
|
|
35
|
+
export declare const defineMiddleware: DefineMiddlewareFn<AppContext>;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -17,18 +17,6 @@ exports.createContext = require_create_context.createContext;
|
|
|
17
17
|
exports.createTaserApp = require_router.createTaserApp;
|
|
18
18
|
exports.defineHandler = require_handler.defineHandler;
|
|
19
19
|
exports.defineMiddleware = require_middleware.defineMiddleware;
|
|
20
|
-
Object.defineProperty(exports, "reply", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function() {
|
|
23
|
-
return _taserjs_router_utils.reply;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
Object.defineProperty(exports, "stream", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
get: function() {
|
|
29
|
-
return _taserjs_router_utils.stream;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
20
|
Object.defineProperty(exports, "validationErrorSchema", {
|
|
33
21
|
enumerable: true,
|
|
34
22
|
get: function() {
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type { RouterRegister } from './register.js';
|
|
2
|
-
export type { InferInput, InferOutput, InferRouteContext, InferRouteInput, InferRouteOutput, Method, PathParams, ReturnsMap, RouteDefinition, RouteExport, RoutePath, Schema, Simplify, } from './types/index.js';
|
|
3
|
-
export type { ContextDefinition, CreateTaserAppOptions, InferAppContext, InferAppManifest, OnErrorOptions, RouteManifestShape,
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
2
|
+
export type { HttpMethod, InferInput, InferOutput, InferRouteContext, InferRouteInput, InferRouteOutput, Method, MiddlewareDefinition, PathParams, ReturnsMap, RouteDefinition, RouteExport, RoutePath, Schema, Simplify, StatusCode, } from './types/index.js';
|
|
3
|
+
export type { ContextDefinition, CreateTaserAppOptions, InferAppContext, InferAppManifest, OnErrorOptions, RouteManifestShape, } from './types/app.js';
|
|
4
|
+
export { ValidationError, validationErrorSchema } from '@taserjs/router-utils';
|
|
5
|
+
export type { ResponseValidationFailureHandler, SuccessStatusCode } from '@taserjs/router-utils';
|
|
6
6
|
export { createTaserApp, TaserRouter } from './builder/router.js';
|
|
7
7
|
export { TaserApp } from './builder/app.js';
|
|
8
8
|
export { createContext } from './context/create-context.js';
|
package/dist/cjs/reply.cjs
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
});
|
|
9
|
-
Object.defineProperty(exports, "reply", {
|
|
10
|
-
enumerable: true,
|
|
11
|
-
get: function() {
|
|
12
|
-
return _taserjs_router_utils_reply.reply;
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
Object.defineProperty(exports, "successReply", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function() {
|
|
18
|
-
return _taserjs_router_utils_reply.successReply;
|
|
19
|
-
}
|
|
1
|
+
var _taserjs_router_utils_reply = require("@taserjs/router-utils/reply");
|
|
2
|
+
Object.keys(_taserjs_router_utils_reply).forEach(function(k) {
|
|
3
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
4
|
+
enumerable: true,
|
|
5
|
+
get: function() {
|
|
6
|
+
return _taserjs_router_utils_reply[k];
|
|
7
|
+
}
|
|
8
|
+
});
|
|
20
9
|
});
|
package/dist/cjs/reply.d.cts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type { BinaryBody, BodyKind, FileReplyInit, RedirectInit, ReplyBodyKind, ReplyInit, ReplyOf, SuccessReplyData, } from '@taserjs/router-utils/reply';
|
|
1
|
+
export * from '@taserjs/router-utils/reply';
|
package/dist/cjs/stream.cjs
CHANGED
|
@@ -1,32 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
});
|
|
9
|
-
Object.defineProperty(exports, "buffer", {
|
|
10
|
-
enumerable: true,
|
|
11
|
-
get: function() {
|
|
12
|
-
return _taserjs_router_utils.buffer;
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
Object.defineProperty(exports, "file", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function() {
|
|
18
|
-
return _taserjs_router_utils.file;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
Object.defineProperty(exports, "pipe", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function() {
|
|
24
|
-
return _taserjs_router_utils.pipe;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(exports, "stream", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function() {
|
|
30
|
-
return _taserjs_router_utils.stream;
|
|
31
|
-
}
|
|
1
|
+
var _taserjs_router_utils_stream = require("@taserjs/router-utils/stream");
|
|
2
|
+
Object.keys(_taserjs_router_utils_stream).forEach(function(k) {
|
|
3
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
4
|
+
enumerable: true,
|
|
5
|
+
get: function() {
|
|
6
|
+
return _taserjs_router_utils_stream[k];
|
|
7
|
+
}
|
|
8
|
+
});
|
|
32
9
|
});
|
package/dist/cjs/stream.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from '@taserjs/router-utils/stream';
|