@taserjs/router 0.1.8 → 0.1.10
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/middleware-unit.cjs +50 -50
- package/dist/cjs/builder/middleware-unit.cjs.map +1 -1
- package/dist/cjs/builder/middleware-unit.d.cts +34 -1
- package/dist/cjs/builder/middleware.cjs +13 -12
- package/dist/cjs/builder/middleware.cjs.map +1 -1
- package/dist/cjs/builder/normalize.cjs +10 -0
- package/dist/cjs/builder/normalize.cjs.map +1 -0
- package/dist/cjs/builder/normalize.d.cts +2 -0
- package/dist/cjs/builder/route.cjs +63 -67
- package/dist/cjs/builder/route.cjs.map +1 -1
- package/dist/cjs/builder/standalone.cjs +18 -34
- package/dist/cjs/builder/standalone.cjs.map +1 -1
- package/dist/cjs/builder/standalone.d.cts +7 -14
- package/dist/cjs/define/middleware.cjs.map +1 -1
- package/dist/cjs/define/middleware.d.cts +5 -5
- package/dist/cjs/index.cjs +0 -11
- package/dist/cjs/index.d.cts +3 -3
- package/dist/cjs/middleware/hono-mw.cjs.map +1 -1
- package/dist/cjs/types/index.d.cts +4 -3
- package/dist/esm/builder/middleware-unit.d.ts +34 -1
- package/dist/esm/builder/middleware-unit.js +50 -50
- package/dist/esm/builder/middleware-unit.js.map +1 -1
- package/dist/esm/builder/middleware.js +13 -12
- package/dist/esm/builder/middleware.js.map +1 -1
- package/dist/esm/builder/normalize.d.ts +2 -0
- package/dist/esm/builder/normalize.js +10 -0
- package/dist/esm/builder/normalize.js.map +1 -0
- package/dist/esm/builder/route.js +63 -67
- package/dist/esm/builder/route.js.map +1 -1
- package/dist/esm/builder/standalone.d.ts +7 -14
- package/dist/esm/builder/standalone.js +19 -24
- package/dist/esm/builder/standalone.js.map +1 -1
- package/dist/esm/define/middleware.d.ts +5 -5
- package/dist/esm/define/middleware.js.map +1 -1
- package/dist/esm/index.d.ts +3 -3
- package/dist/esm/index.js +2 -2
- package/dist/esm/middleware/hono-mw.js.map +1 -1
- package/dist/esm/types/index.d.ts +4 -3
- package/package.json +3 -3
- package/src/builder/middleware-unit.ts +56 -51
- package/src/builder/middleware.ts +16 -18
- package/src/builder/normalize.ts +13 -0
- package/src/builder/route.ts +81 -82
- package/src/builder/standalone.ts +56 -52
- package/src/define/middleware.ts +5 -4
- package/src/index.ts +3 -17
- package/src/middleware/hono-mw.ts +4 -1
- package/src/types/index.ts +16 -8
- package/dist/cjs/builder/factories.cjs +0 -31
- package/dist/cjs/builder/factories.cjs.map +0 -1
- package/dist/cjs/builder/factories.d.cts +0 -16
- package/dist/esm/builder/factories.d.ts +0 -16
- package/dist/esm/builder/factories.js +0 -22
- package/dist/esm/builder/factories.js.map +0 -1
- package/src/builder/factories.ts +0 -59
|
@@ -1,62 +1,62 @@
|
|
|
1
1
|
const require_validators = require("../define/validators.cjs");
|
|
2
2
|
//#region src/builder/middleware-unit.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
var MiddlewareUnitBuilderImpl = class {
|
|
4
|
+
validators = {};
|
|
5
|
+
bodyMode;
|
|
6
|
+
returnsMap;
|
|
7
|
+
__requiredLayouts;
|
|
8
|
+
__middlewareAcc = void 0;
|
|
9
|
+
constructor(requiredLayouts) {
|
|
10
|
+
this.__requiredLayouts = requiredLayouts;
|
|
11
|
+
}
|
|
12
|
+
query(schema) {
|
|
13
|
+
this.validators.query = schema;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
params(schema) {
|
|
17
|
+
this.validators.params = schema;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
body(modeOrSchema, schema) {
|
|
21
|
+
if (typeof modeOrSchema === "string") {
|
|
22
|
+
this.bodyMode = modeOrSchema;
|
|
23
|
+
if (schema !== void 0) this.validators.body = schema;
|
|
24
|
+
} else {
|
|
25
|
+
this.bodyMode = "json";
|
|
26
|
+
this.validators.body = modeOrSchema;
|
|
27
|
+
}
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
returns(map) {
|
|
31
|
+
this.returnsMap = this.returnsMap ? {
|
|
32
|
+
...this.returnsMap,
|
|
33
|
+
...map
|
|
34
|
+
} : { ...map };
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
requires() {
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
handler(fn) {
|
|
41
|
+
return this.toUnit(fn);
|
|
42
|
+
}
|
|
43
|
+
toUnit(fn) {
|
|
44
|
+
const schemas = require_validators.pickDefinedSchemas(this.validators);
|
|
9
45
|
return {
|
|
10
46
|
...fn ? { handler: fn } : {},
|
|
11
47
|
...schemas,
|
|
12
|
-
...bodyMode ? { bodyMode } : {},
|
|
13
|
-
...returnsMap ? {
|
|
14
|
-
returns: returnsMap,
|
|
15
|
-
__returns: returnsMap
|
|
48
|
+
...this.bodyMode ? { bodyMode: this.bodyMode } : {},
|
|
49
|
+
...this.returnsMap ? {
|
|
50
|
+
returns: this.returnsMap,
|
|
51
|
+
__returns: this.returnsMap
|
|
16
52
|
} : {},
|
|
17
|
-
...
|
|
53
|
+
...this.__requiredLayouts !== void 0 ? { __requiredLayouts: this.__requiredLayouts } : {},
|
|
18
54
|
__middlewareAcc: void 0
|
|
19
55
|
};
|
|
20
56
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return builder;
|
|
25
|
-
},
|
|
26
|
-
params(schema) {
|
|
27
|
-
validators.params = schema;
|
|
28
|
-
return builder;
|
|
29
|
-
},
|
|
30
|
-
body(modeOrSchema, schema) {
|
|
31
|
-
if (typeof modeOrSchema === "string") {
|
|
32
|
-
bodyMode = modeOrSchema;
|
|
33
|
-
if (schema !== void 0) validators.body = schema;
|
|
34
|
-
} else {
|
|
35
|
-
bodyMode = "json";
|
|
36
|
-
validators.body = modeOrSchema;
|
|
37
|
-
}
|
|
38
|
-
return builder;
|
|
39
|
-
},
|
|
40
|
-
returns(map) {
|
|
41
|
-
returnsMap = {
|
|
42
|
-
...returnsMap,
|
|
43
|
-
...map
|
|
44
|
-
};
|
|
45
|
-
return builder;
|
|
46
|
-
},
|
|
47
|
-
requires() {
|
|
48
|
-
return builder;
|
|
49
|
-
},
|
|
50
|
-
handler(fn) {
|
|
51
|
-
return toUnit(fn);
|
|
52
|
-
},
|
|
53
|
-
toUnit() {
|
|
54
|
-
return toUnit();
|
|
55
|
-
},
|
|
56
|
-
__middlewareAcc: void 0
|
|
57
|
-
};
|
|
58
|
-
if (requiredLayouts !== void 0) builder.__requiredLayouts = requiredLayouts;
|
|
59
|
-
return builder;
|
|
57
|
+
};
|
|
58
|
+
function createMiddlewareUnitBuilder(requiredLayouts) {
|
|
59
|
+
return new MiddlewareUnitBuilderImpl(requiredLayouts);
|
|
60
60
|
}
|
|
61
61
|
//#endregion
|
|
62
62
|
exports.createMiddlewareUnitBuilder = createMiddlewareUnitBuilder;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware-unit.cjs","names":[],"sources":["../../../src/builder/middleware-unit.ts"],"sourcesContent":["import type { BodyMode } from \"@taserjs/router-core\";\n\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type { Schema } from \"../types/schema.js\";\nimport { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\n\
|
|
1
|
+
{"version":3,"file":"middleware-unit.cjs","names":[],"sources":["../../../src/builder/middleware-unit.ts"],"sourcesContent":["import type { BodyMode } from \"@taserjs/router-core\";\n\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type { Schema } from \"../types/schema.js\";\nimport { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\n\nclass MiddlewareUnitBuilderImpl {\n private readonly validators: SchemaValidators = {};\n private bodyMode: BodyMode | undefined;\n private returnsMap: ReturnsMap | undefined;\n readonly __requiredLayouts: string | readonly string[] | undefined;\n readonly __middlewareAcc: unknown = undefined;\n\n constructor(requiredLayouts?: string | readonly string[]) {\n this.__requiredLayouts = requiredLayouts;\n }\n\n query(schema: Schema<unknown>): this {\n this.validators.query = schema;\n return this;\n }\n\n params(schema: Schema<unknown>): this {\n this.validators.params = schema;\n return this;\n }\n\n body(modeOrSchema: BodyMode | Schema<unknown>, schema?: Schema<unknown>): this {\n if (typeof modeOrSchema === \"string\") {\n this.bodyMode = modeOrSchema as BodyMode;\n if (schema !== undefined) {\n this.validators.body = schema;\n }\n } else {\n this.bodyMode = \"json\";\n this.validators.body = modeOrSchema;\n }\n return this;\n }\n\n returns(map: ReturnsMap): this {\n this.returnsMap = this.returnsMap ? { ...this.returnsMap, ...map } : { ...map };\n return this;\n }\n\n requires(): this {\n return this;\n }\n\n handler(fn: (ctx: any, next: any) => any) {\n return this.toUnit(fn);\n }\n\n toUnit(fn?: (ctx: any, next: any) => any) {\n const schemas = pickDefinedSchemas(this.validators);\n return {\n ...(fn ? { handler: fn } : {}),\n ...schemas,\n ...(this.bodyMode ? { bodyMode: this.bodyMode } : {}),\n ...(this.returnsMap ? { returns: this.returnsMap, __returns: this.returnsMap } : {}),\n ...(this.__requiredLayouts !== undefined\n ? { __requiredLayouts: this.__requiredLayouts }\n : {}),\n __middlewareAcc: undefined as unknown,\n };\n }\n}\n\nexport function createMiddlewareUnitBuilder(requiredLayouts?: string | readonly string[]) {\n return new MiddlewareUnitBuilderImpl(requiredLayouts);\n}\n"],"mappings":";;AAMA,IAAM,4BAAN,MAAgC;CAC9B,aAAgD,CAAC;CACjD;CACA;CACA;CACA,kBAAoC,KAAA;CAEpC,YAAY,iBAA8C;EACxD,KAAK,oBAAoB;CAC3B;CAEA,MAAM,QAA+B;EACnC,KAAK,WAAW,QAAQ;EACxB,OAAO;CACT;CAEA,OAAO,QAA+B;EACpC,KAAK,WAAW,SAAS;EACzB,OAAO;CACT;CAEA,KAAK,cAA0C,QAAgC;EAC7E,IAAI,OAAO,iBAAiB,UAAU;GACpC,KAAK,WAAW;GAChB,IAAI,WAAW,KAAA,GACb,KAAK,WAAW,OAAO;EAE3B,OAAO;GACL,KAAK,WAAW;GAChB,KAAK,WAAW,OAAO;EACzB;EACA,OAAO;CACT;CAEA,QAAQ,KAAuB;EAC7B,KAAK,aAAa,KAAK,aAAa;GAAE,GAAG,KAAK;GAAY,GAAG;EAAI,IAAI,EAAE,GAAG,IAAI;EAC9E,OAAO;CACT;CAEA,WAAiB;EACf,OAAO;CACT;CAEA,QAAQ,IAAkC;EACxC,OAAO,KAAK,OAAO,EAAE;CACvB;CAEA,OAAO,IAAmC;EACxC,MAAM,UAAU,mBAAA,mBAAmB,KAAK,UAAU;EAClD,OAAO;GACL,GAAI,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;GAC5B,GAAG;GACH,GAAI,KAAK,WAAW,EAAE,UAAU,KAAK,SAAS,IAAI,CAAC;GACnD,GAAI,KAAK,aAAa;IAAE,SAAS,KAAK;IAAY,WAAW,KAAK;GAAW,IAAI,CAAC;GAClF,GAAI,KAAK,sBAAsB,KAAA,IAC3B,EAAE,mBAAmB,KAAK,kBAAkB,IAC5C,CAAC;GACL,iBAAiB,KAAA;EACnB;CACF;AACF;AAEA,SAAgB,4BAA4B,iBAA8C;CACxF,OAAO,IAAI,0BAA0B,eAAe;AACtD"}
|
|
@@ -1 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
import { BodyMode } from '@taserjs/router-core';
|
|
2
|
+
import { ReturnsMap } from '../types/returns.js';
|
|
3
|
+
import { Schema } from '../types/schema.js';
|
|
4
|
+
declare class MiddlewareUnitBuilderImpl {
|
|
5
|
+
private readonly validators;
|
|
6
|
+
private bodyMode;
|
|
7
|
+
private returnsMap;
|
|
8
|
+
readonly __requiredLayouts: string | readonly string[] | undefined;
|
|
9
|
+
readonly __middlewareAcc: unknown;
|
|
10
|
+
constructor(requiredLayouts?: string | readonly string[]);
|
|
11
|
+
query(schema: Schema<unknown>): this;
|
|
12
|
+
params(schema: Schema<unknown>): this;
|
|
13
|
+
body(modeOrSchema: BodyMode | Schema<unknown>, schema?: Schema<unknown>): this;
|
|
14
|
+
returns(map: ReturnsMap): this;
|
|
15
|
+
requires(): this;
|
|
16
|
+
handler(fn: (ctx: any, next: any) => any): {
|
|
17
|
+
__middlewareAcc: unknown;
|
|
18
|
+
__requiredLayouts?: string | readonly string[];
|
|
19
|
+
returns?: ReturnsMap;
|
|
20
|
+
__returns?: ReturnsMap;
|
|
21
|
+
bodyMode?: BodyMode;
|
|
22
|
+
handler?: (ctx: any, next: any) => any;
|
|
23
|
+
};
|
|
24
|
+
toUnit(fn?: (ctx: any, next: any) => any): {
|
|
25
|
+
__middlewareAcc: unknown;
|
|
26
|
+
__requiredLayouts?: string | readonly string[];
|
|
27
|
+
returns?: ReturnsMap;
|
|
28
|
+
__returns?: ReturnsMap;
|
|
29
|
+
bodyMode?: BodyMode;
|
|
30
|
+
handler?: (ctx: any, next: any) => any;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export declare function createMiddlewareUnitBuilder(requiredLayouts?: string | readonly string[]): MiddlewareUnitBuilderImpl;
|
|
34
|
+
export {};
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
+
const require_normalize = require("./normalize.cjs");
|
|
1
2
|
//#region src/builder/middleware.ts
|
|
3
|
+
var MiddlewareBuilderImpl = class {
|
|
4
|
+
layout;
|
|
5
|
+
middlewares = [];
|
|
6
|
+
constructor(layout) {
|
|
7
|
+
this.layout = layout;
|
|
8
|
+
}
|
|
9
|
+
use(definition) {
|
|
10
|
+
this.middlewares.push(require_normalize.normalizeMiddlewareDefinition(definition));
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
2
14
|
function createMiddleware(layout) {
|
|
3
|
-
|
|
4
|
-
const chain = {
|
|
5
|
-
layout,
|
|
6
|
-
middlewares: entries,
|
|
7
|
-
use(definition) {
|
|
8
|
-
if (typeof definition === "function") entries.push({ handler: definition });
|
|
9
|
-
else if (typeof definition?.toUnit === "function") entries.push(definition.toUnit());
|
|
10
|
-
else entries.push(definition);
|
|
11
|
-
return chain;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
return chain;
|
|
15
|
+
return new MiddlewareBuilderImpl(layout);
|
|
15
16
|
}
|
|
16
17
|
//#endregion
|
|
17
18
|
exports.createMiddleware = createMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":[],"sources":["../../../src/builder/middleware.ts"],"sourcesContent":["import type { LayoutId, MiddlewareBuilder } from \"../types/index.js\";\nimport type { MiddlewareDefinition } from \"../types/units.js\";\n\
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":[],"sources":["../../../src/builder/middleware.ts"],"sourcesContent":["import type { LayoutId, MiddlewareBuilder } from \"../types/index.js\";\nimport type { MiddlewareDefinition } from \"../types/units.js\";\nimport { normalizeMiddlewareDefinition } from \"./normalize.js\";\n\nclass MiddlewareBuilderImpl<Layout extends LayoutId> {\n readonly layout: Layout;\n readonly middlewares: MiddlewareDefinition[] = [];\n\n constructor(layout: Layout) {\n this.layout = layout;\n }\n\n use(definition: MiddlewareDefinition | ((ctx: any, next: any) => any)): this {\n this.middlewares.push(normalizeMiddlewareDefinition(definition));\n return this;\n }\n}\n\nexport function createMiddleware<const Layout extends LayoutId>(\n layout: Layout,\n): MiddlewareBuilder<Layout, readonly []> {\n return new MiddlewareBuilderImpl(layout) as unknown as MiddlewareBuilder<Layout, readonly []>;\n}\n"],"mappings":";;AAIA,IAAM,wBAAN,MAAqD;CACnD;CACA,cAA+C,CAAC;CAEhD,YAAY,QAAgB;EAC1B,KAAK,SAAS;CAChB;CAEA,IAAI,YAAyE;EAC3E,KAAK,YAAY,KAAK,kBAAA,8BAA8B,UAAU,CAAC;EAC/D,OAAO;CACT;AACF;AAEA,SAAgB,iBACd,QACwC;CACxC,OAAO,IAAI,sBAAsB,MAAM;AACzC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/builder/normalize.ts
|
|
2
|
+
function normalizeMiddlewareDefinition(definition) {
|
|
3
|
+
if (typeof definition === "function") return { handler: definition };
|
|
4
|
+
if (typeof definition?.toUnit === "function") return definition.toUnit();
|
|
5
|
+
return definition;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
exports.normalizeMiddlewareDefinition = normalizeMiddlewareDefinition;
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=normalize.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.cjs","names":[],"sources":["../../../src/builder/normalize.ts"],"sourcesContent":["import type { MiddlewareDefinition } from \"../types/units.js\";\n\nexport function normalizeMiddlewareDefinition(\n definition: MiddlewareDefinition | ((ctx: any, next: any) => any),\n): MiddlewareDefinition {\n if (typeof definition === \"function\") {\n return { handler: definition as any };\n }\n if (typeof (definition as any)?.toUnit === \"function\") {\n return (definition as any).toUnit();\n }\n return definition;\n}\n"],"mappings":";AAEA,SAAgB,8BACd,YACsB;CACtB,IAAI,OAAO,eAAe,YACxB,OAAO,EAAE,SAAS,WAAkB;CAEtC,IAAI,OAAQ,YAAoB,WAAW,YACzC,OAAQ,WAAmB,OAAO;CAEpC,OAAO;AACT"}
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
const require_validators = require("../define/validators.cjs");
|
|
2
|
+
const require_normalize = require("./normalize.cjs");
|
|
2
3
|
let _taserjs_router_utils = require("@taserjs/router-utils");
|
|
3
4
|
//#region src/builder/route.ts
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
-
const out = {};
|
|
7
|
-
for (const [key, schema] of Object.entries(map)) if (schema !== void 0) out[Number(key)] = schema;
|
|
8
|
-
return out;
|
|
9
|
-
}
|
|
10
|
-
function buildEffectiveReturns(args) {
|
|
11
|
-
const with422 = (0, _taserjs_router_utils.withAuto422)((0, _taserjs_router_utils.mergeReturnsMaps)(toUtilsMap(args.middlewareReturns), toUtilsMap(args.routeReturns)), (0, _taserjs_router_utils.hasInputSchemas)(args.schemas));
|
|
5
|
+
function buildRouteReturnsWith422(args) {
|
|
6
|
+
const with422 = (0, _taserjs_router_utils.withAuto422)((0, _taserjs_router_utils.mergeReturnsMaps)(args.middlewareReturns, args.routeReturns), (0, _taserjs_router_utils.hasInputSchemas)(args.schemas));
|
|
12
7
|
return Object.keys(with422).length > 0 ? with422 : void 0;
|
|
13
8
|
}
|
|
14
9
|
function buildRouteBase(path, method, methods, middlewares) {
|
|
@@ -19,67 +14,68 @@ function buildRouteBase(path, method, methods, middlewares) {
|
|
|
19
14
|
middlewares: [...middlewares]
|
|
20
15
|
};
|
|
21
16
|
}
|
|
17
|
+
var RouteBuilderImpl = class {
|
|
18
|
+
path;
|
|
19
|
+
method;
|
|
20
|
+
methods;
|
|
21
|
+
middlewares = [];
|
|
22
|
+
routeReturns = {};
|
|
23
|
+
validators = {};
|
|
24
|
+
bodyMode;
|
|
25
|
+
constructor(path, method, methods) {
|
|
26
|
+
this.path = path;
|
|
27
|
+
this.method = method;
|
|
28
|
+
this.methods = methods;
|
|
29
|
+
}
|
|
30
|
+
query(schema) {
|
|
31
|
+
this.validators.query = schema;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
params(schema) {
|
|
35
|
+
this.validators.params = schema;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
body(modeOrSchema, schema) {
|
|
39
|
+
if (typeof modeOrSchema === "string") {
|
|
40
|
+
this.bodyMode = modeOrSchema;
|
|
41
|
+
if (schema !== void 0) this.validators.body = schema;
|
|
42
|
+
} else {
|
|
43
|
+
this.bodyMode = "json";
|
|
44
|
+
this.validators.body = modeOrSchema;
|
|
45
|
+
}
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
returns(map) {
|
|
49
|
+
Object.assign(this.routeReturns, map);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
use(definition) {
|
|
53
|
+
this.middlewares.push(require_normalize.normalizeMiddlewareDefinition(definition));
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
handler(fn) {
|
|
57
|
+
const routeSchemas = require_validators.pickDefinedSchemas(this.validators);
|
|
58
|
+
const base = buildRouteBase(this.path, this.method, this.methods, this.middlewares);
|
|
59
|
+
const returns = buildRouteReturnsWith422({
|
|
60
|
+
middlewareReturns: (0, _taserjs_router_utils.collectReturnsFromDefinitions)(this.middlewares),
|
|
61
|
+
routeReturns: this.routeReturns,
|
|
62
|
+
schemas: {
|
|
63
|
+
...this.validators,
|
|
64
|
+
middlewares: this.middlewares
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
...base,
|
|
69
|
+
handler: fn,
|
|
70
|
+
...routeSchemas,
|
|
71
|
+
...this.bodyMode ? { bodyMode: this.bodyMode } : {},
|
|
72
|
+
...returns ? { returns } : {}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
22
76
|
/** Shared internal builder — not part of the public `@taserjs/router` API. */
|
|
23
77
|
function createRouteBuilder(path, method, methods) {
|
|
24
|
-
|
|
25
|
-
let routeReturns = {};
|
|
26
|
-
const validators = {};
|
|
27
|
-
let bodyMode;
|
|
28
|
-
const builder = {
|
|
29
|
-
path,
|
|
30
|
-
method,
|
|
31
|
-
query(schema) {
|
|
32
|
-
validators.query = schema;
|
|
33
|
-
return builder;
|
|
34
|
-
},
|
|
35
|
-
params(schema) {
|
|
36
|
-
validators.params = schema;
|
|
37
|
-
return builder;
|
|
38
|
-
},
|
|
39
|
-
body(modeOrSchema, schema) {
|
|
40
|
-
if (typeof modeOrSchema === "string") {
|
|
41
|
-
bodyMode = modeOrSchema;
|
|
42
|
-
if (schema !== void 0) validators.body = schema;
|
|
43
|
-
} else {
|
|
44
|
-
bodyMode = "json";
|
|
45
|
-
validators.body = modeOrSchema;
|
|
46
|
-
}
|
|
47
|
-
return builder;
|
|
48
|
-
},
|
|
49
|
-
returns(map) {
|
|
50
|
-
routeReturns = {
|
|
51
|
-
...routeReturns,
|
|
52
|
-
...toUtilsMap(map)
|
|
53
|
-
};
|
|
54
|
-
return builder;
|
|
55
|
-
},
|
|
56
|
-
use(definition) {
|
|
57
|
-
if (typeof definition === "function") middlewares.push({ handler: definition });
|
|
58
|
-
else if (typeof definition?.toUnit === "function") middlewares.push(definition.toUnit());
|
|
59
|
-
else middlewares.push(definition);
|
|
60
|
-
return builder;
|
|
61
|
-
},
|
|
62
|
-
handler(fn) {
|
|
63
|
-
const routeSchemas = require_validators.pickDefinedSchemas(validators);
|
|
64
|
-
const base = buildRouteBase(path, method, methods, middlewares);
|
|
65
|
-
const returns = buildEffectiveReturns({
|
|
66
|
-
middlewareReturns: (0, _taserjs_router_utils.collectReturnsFromDefinitions)(middlewares),
|
|
67
|
-
routeReturns,
|
|
68
|
-
schemas: {
|
|
69
|
-
...validators,
|
|
70
|
-
middlewares
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
return {
|
|
74
|
-
...base,
|
|
75
|
-
handler: fn,
|
|
76
|
-
...routeSchemas,
|
|
77
|
-
...bodyMode ? { bodyMode } : {},
|
|
78
|
-
...returns ? { returns } : {}
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
return builder;
|
|
78
|
+
return new RouteBuilderImpl(path, method, methods);
|
|
83
79
|
}
|
|
84
80
|
//#endregion
|
|
85
81
|
exports.createRouteBuilder = createRouteBuilder;
|
|
@@ -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\";\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 { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\
|
|
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 { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\nimport { normalizeMiddlewareDefinition } from \"./normalize.js\";\n\nfunction buildRouteReturnsWith422(args: {\n middlewareReturns: ReturnsMap;\n routeReturns: ReturnsMap;\n schemas: {\n query?: unknown;\n params?: unknown;\n body?: unknown;\n middlewares?: readonly MiddlewareDefinition[];\n };\n}): Record<number, StandardSchemaV1> | undefined {\n const merged = mergeReturnsMaps(\n args.middlewareReturns as Record<number, StandardSchemaV1>,\n args.routeReturns as Record<number, StandardSchemaV1>,\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\nclass RouteBuilderImpl {\n readonly path: string;\n readonly method: HttpMethod | \"ANY\" | \"ALL\";\n readonly methods: readonly HttpMethod[] | undefined;\n private readonly middlewares: MiddlewareDefinition[] = [];\n private readonly routeReturns: Record<number, StandardSchemaV1> = {};\n private readonly validators: SchemaValidators = {};\n private bodyMode: BodyMode | undefined;\n\n constructor(path: string, method: HttpMethod | \"ANY\" | \"ALL\", methods?: readonly HttpMethod[]) {\n this.path = path;\n this.method = method;\n this.methods = methods;\n }\n\n query(schema: Schema<unknown>): this {\n this.validators.query = schema;\n return this;\n }\n\n params(schema: Schema<unknown>): this {\n this.validators.params = schema;\n return this;\n }\n\n body(modeOrSchema: BodyMode | Schema<unknown>, schema?: Schema<unknown>): this {\n if (typeof modeOrSchema === \"string\") {\n this.bodyMode = modeOrSchema as BodyMode;\n if (schema !== undefined) {\n this.validators.body = schema;\n }\n } else {\n this.bodyMode = \"json\";\n this.validators.body = modeOrSchema;\n }\n return this;\n }\n\n returns(map: ReturnsMap): this {\n Object.assign(this.routeReturns, map);\n return this;\n }\n\n use(definition: MiddlewareDefinition | ((ctx: any, next: any) => any)): this {\n this.middlewares.push(normalizeMiddlewareDefinition(definition));\n return this;\n }\n\n handler(fn: (ctx: unknown) => Response | Promise<Response>) {\n const routeSchemas = pickDefinedSchemas(this.validators);\n const base = buildRouteBase(this.path, this.method, this.methods, this.middlewares);\n\n const returns = buildRouteReturnsWith422({\n middlewareReturns: collectReturnsFromDefinitions(this.middlewares),\n routeReturns: this.routeReturns,\n schemas: {\n ...this.validators,\n middlewares: this.middlewares,\n },\n });\n\n return {\n ...base,\n handler: fn,\n ...routeSchemas,\n ...(this.bodyMode ? { bodyMode: this.bodyMode } : {}),\n ...(returns ? { returns } : {}),\n };\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 return new RouteBuilderImpl(path, method, methods) as unknown as RouteBuilder<\n RoutePath,\n Method,\n readonly [],\n ValidatorParts\n >;\n}\n"],"mappings":";;;;AAqBA,SAAS,yBAAyB,MASe;CAK/C,MAAM,WAAA,GAAA,sBAAA,YAAA,EAAA,GAAA,sBAAA,iBAAA,CAHJ,KAAK,mBACL,KAAK,YAEqB,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;AAEA,IAAM,mBAAN,MAAuB;CACrB;CACA;CACA;CACA,cAAuD,CAAC;CACxD,eAAkE,CAAC;CACnE,aAAgD,CAAC;CACjD;CAEA,YAAY,MAAc,QAAoC,SAAiC;EAC7F,KAAK,OAAO;EACZ,KAAK,SAAS;EACd,KAAK,UAAU;CACjB;CAEA,MAAM,QAA+B;EACnC,KAAK,WAAW,QAAQ;EACxB,OAAO;CACT;CAEA,OAAO,QAA+B;EACpC,KAAK,WAAW,SAAS;EACzB,OAAO;CACT;CAEA,KAAK,cAA0C,QAAgC;EAC7E,IAAI,OAAO,iBAAiB,UAAU;GACpC,KAAK,WAAW;GAChB,IAAI,WAAW,KAAA,GACb,KAAK,WAAW,OAAO;EAE3B,OAAO;GACL,KAAK,WAAW;GAChB,KAAK,WAAW,OAAO;EACzB;EACA,OAAO;CACT;CAEA,QAAQ,KAAuB;EAC7B,OAAO,OAAO,KAAK,cAAc,GAAG;EACpC,OAAO;CACT;CAEA,IAAI,YAAyE;EAC3E,KAAK,YAAY,KAAK,kBAAA,8BAA8B,UAAU,CAAC;EAC/D,OAAO;CACT;CAEA,QAAQ,IAAoD;EAC1D,MAAM,eAAe,mBAAA,mBAAmB,KAAK,UAAU;EACvD,MAAM,OAAO,eAAe,KAAK,MAAM,KAAK,QAAQ,KAAK,SAAS,KAAK,WAAW;EAElF,MAAM,UAAU,yBAAyB;GACvC,oBAAA,GAAA,sBAAA,8BAAA,CAAiD,KAAK,WAAW;GACjE,cAAc,KAAK;GACnB,SAAS;IACP,GAAG,KAAK;IACR,aAAa,KAAK;GACpB;EACF,CAAC;EAED,OAAO;GACL,GAAG;GACH,SAAS;GACT,GAAG;GACH,GAAI,KAAK,WAAW,EAAE,UAAU,KAAK,SAAS,IAAI,CAAC;GACnD,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;EAC/B;CACF;AACF;;AAGA,SAAgB,mBACd,MACA,QACA,SAC8D;CAC9D,OAAO,IAAI,iBAAiB,MAAM,QAAQ,OAAO;AAMnD"}
|
|
@@ -1,46 +1,30 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_route = require("./route.cjs");
|
|
2
2
|
const require_middleware = require("./middleware.cjs");
|
|
3
3
|
const require_middleware$1 = require("../define/middleware.cjs");
|
|
4
4
|
//#region src/builder/standalone.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var head = require_factories.createHeadRoute;
|
|
12
|
-
var query = require_factories.createQueryRoute;
|
|
13
|
-
var any = require_factories.createAnyRoute;
|
|
14
|
-
var all = require_factories.createAllRoute;
|
|
15
|
-
var layout = require_middleware.createMiddleware;
|
|
5
|
+
function createWithoutBodyRoute(method) {
|
|
6
|
+
return ((path) => require_route.createRouteBuilder(path, method));
|
|
7
|
+
}
|
|
8
|
+
function createWithBodyRoute(method) {
|
|
9
|
+
return ((path) => require_route.createRouteBuilder(path, method));
|
|
10
|
+
}
|
|
16
11
|
var middleware = require_middleware$1.middleware;
|
|
17
12
|
var t = {
|
|
18
|
-
get,
|
|
19
|
-
post,
|
|
20
|
-
put,
|
|
21
|
-
patch,
|
|
22
|
-
delete:
|
|
23
|
-
options,
|
|
24
|
-
head,
|
|
25
|
-
query,
|
|
26
|
-
any,
|
|
27
|
-
all,
|
|
28
|
-
layout,
|
|
13
|
+
get: createWithoutBodyRoute("GET"),
|
|
14
|
+
post: createWithBodyRoute("POST"),
|
|
15
|
+
put: createWithBodyRoute("PUT"),
|
|
16
|
+
patch: createWithBodyRoute("PATCH"),
|
|
17
|
+
delete: createWithoutBodyRoute("DELETE"),
|
|
18
|
+
options: createWithoutBodyRoute("OPTIONS"),
|
|
19
|
+
head: createWithoutBodyRoute("HEAD"),
|
|
20
|
+
query: createWithBodyRoute("QUERY"),
|
|
21
|
+
any: ((path, methods) => require_route.createRouteBuilder(path, "ANY", methods)),
|
|
22
|
+
all: ((path) => require_route.createRouteBuilder(path, "ALL")),
|
|
23
|
+
layout: require_middleware.createMiddleware,
|
|
29
24
|
middleware
|
|
30
25
|
};
|
|
31
26
|
//#endregion
|
|
32
|
-
exports.all = all;
|
|
33
|
-
exports.any = any;
|
|
34
|
-
exports.del = del;
|
|
35
|
-
exports.get = get;
|
|
36
|
-
exports.head = head;
|
|
37
|
-
exports.layout = layout;
|
|
38
27
|
exports.middleware = middleware;
|
|
39
|
-
exports.options = options;
|
|
40
|
-
exports.patch = patch;
|
|
41
|
-
exports.post = post;
|
|
42
|
-
exports.put = put;
|
|
43
|
-
exports.query = query;
|
|
44
28
|
exports.t = t;
|
|
45
29
|
|
|
46
30
|
//# sourceMappingURL=standalone.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standalone.cjs","names":[],"sources":["../../../src/builder/standalone.ts"],"sourcesContent":["import type { AppContext, LayoutBuilder } from \"../types/index.js\";\nimport {\
|
|
1
|
+
{"version":3,"file":"standalone.cjs","names":[],"sources":["../../../src/builder/standalone.ts"],"sourcesContent":["import type { AppContext, LayoutBuilder, RouteBuilder, RoutePath } from \"../types/index.js\";\nimport type { HttpMethod } from \"../types/units.js\";\nimport { createRouteBuilder } from \"./route.js\";\nimport { createMiddleware } from \"./middleware.js\";\nimport { middleware as createMiddlewareFn, type MiddlewareFn } from \"../define/middleware.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\" | \"QUERY\",\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\" | \"QUERY\">(\n method: M,\n): CreateWithBodyRoute<M> {\n return ((path: string) => createRouteBuilder(path, method)) as unknown as CreateWithBodyRoute<M>;\n}\n\nexport type TaserNamespace<TAppContext extends Record<string, unknown> = AppContext> = {\n readonly get: CreateWithoutBodyRoute<\"GET\", TAppContext>;\n readonly post: CreateWithBodyRoute<\"POST\", TAppContext>;\n readonly put: CreateWithBodyRoute<\"PUT\", TAppContext>;\n readonly patch: CreateWithBodyRoute<\"PATCH\", TAppContext>;\n readonly delete: CreateWithoutBodyRoute<\"DELETE\", TAppContext>;\n readonly options: CreateWithoutBodyRoute<\"OPTIONS\", TAppContext>;\n readonly head: CreateWithoutBodyRoute<\"HEAD\", TAppContext>;\n readonly query: CreateWithBodyRoute<\"QUERY\", TAppContext>;\n readonly any: CreateAnyRoute<TAppContext>;\n readonly all: CreateAllRoute<TAppContext>;\n readonly layout: LayoutBuilder<TAppContext>;\n readonly middleware: MiddlewareFn<TAppContext>;\n};\n\nexport const middleware: MiddlewareFn<AppContext> = createMiddlewareFn as MiddlewareFn<AppContext>;\n\nexport const t: TaserNamespace<AppContext> = {\n get: createWithoutBodyRoute(\"GET\"),\n post: createWithBodyRoute(\"POST\"),\n put: createWithBodyRoute(\"PUT\"),\n patch: createWithBodyRoute(\"PATCH\"),\n delete: createWithoutBodyRoute(\"DELETE\"),\n options: createWithoutBodyRoute(\"OPTIONS\"),\n head: createWithoutBodyRoute(\"HEAD\"),\n query: createWithBodyRoute(\"QUERY\"),\n any: ((path: string, methods: readonly HttpMethod[]) =>\n createRouteBuilder(path, \"ANY\", methods)) as unknown as CreateAnyRoute<AppContext>,\n all: ((path: string) => createRouteBuilder(path, \"ALL\")) as unknown as CreateAllRoute<AppContext>,\n layout: createMiddleware as LayoutBuilder<AppContext>,\n middleware,\n};\n"],"mappings":";;;;AAkCA,SAAS,uBACP,QAC2B;CAC3B,SAAS,SACP,cAAA,mBAAmB,MAAM,MAAM;AACnC;AAEA,SAAS,oBACP,QACwB;CACxB,SAAS,SAAiB,cAAA,mBAAmB,MAAM,MAAM;AAC3D;AAiBA,IAAa,aAAuC,qBAAA;AAEpD,IAAa,IAAgC;CAC3C,KAAK,uBAAuB,KAAK;CACjC,MAAM,oBAAoB,MAAM;CAChC,KAAK,oBAAoB,KAAK;CAC9B,OAAO,oBAAoB,OAAO;CAClC,QAAQ,uBAAuB,QAAQ;CACvC,SAAS,uBAAuB,SAAS;CACzC,MAAM,uBAAuB,MAAM;CACnC,OAAO,oBAAoB,OAAO;CAClC,OAAO,MAAc,YACnB,cAAA,mBAAmB,MAAM,OAAO,OAAO;CACzC,OAAO,SAAiB,cAAA,mBAAmB,MAAM,KAAK;CACtD,QAAQ,mBAAA;CACR;AACF"}
|
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
import { AppContext, LayoutBuilder } from '../types/index.js';
|
|
2
|
-
import {
|
|
1
|
+
import { AppContext, LayoutBuilder, RouteBuilder, RoutePath } from '../types/index.js';
|
|
2
|
+
import { HttpMethod } from '../types/units.js';
|
|
3
3
|
import { MiddlewareFn } from '../define/middleware.js';
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export declare const del: CreateWithoutBodyRoute<"DELETE", AppContext>;
|
|
9
|
-
export declare const options: CreateWithoutBodyRoute<"OPTIONS", AppContext>;
|
|
10
|
-
export declare const head: CreateWithoutBodyRoute<"HEAD", AppContext>;
|
|
11
|
-
export declare const query: CreateWithBodyRoute<"QUERY", AppContext>;
|
|
12
|
-
export declare const any: CreateAnyRoute<AppContext>;
|
|
13
|
-
export declare const all: CreateAllRoute<AppContext>;
|
|
14
|
-
export declare const layout: LayoutBuilder<AppContext>;
|
|
15
|
-
export declare const middleware: MiddlewareFn<AppContext>;
|
|
4
|
+
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>;
|
|
5
|
+
export type CreateWithBodyRoute<M extends "POST" | "PUT" | "PATCH" | "QUERY", TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
6
|
+
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>;
|
|
7
|
+
export type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;
|
|
16
8
|
export type TaserNamespace<TAppContext extends Record<string, unknown> = AppContext> = {
|
|
17
9
|
readonly get: CreateWithoutBodyRoute<"GET", TAppContext>;
|
|
18
10
|
readonly post: CreateWithBodyRoute<"POST", TAppContext>;
|
|
@@ -27,4 +19,5 @@ export type TaserNamespace<TAppContext extends Record<string, unknown> = AppCont
|
|
|
27
19
|
readonly layout: LayoutBuilder<TAppContext>;
|
|
28
20
|
readonly middleware: MiddlewareFn<TAppContext>;
|
|
29
21
|
};
|
|
22
|
+
export declare const middleware: MiddlewareFn<AppContext>;
|
|
30
23
|
export declare const t: TaserNamespace<AppContext>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":[],"sources":["../../../src/define/middleware.ts"],"sourcesContent":["import { createMiddlewareUnitBuilder } from \"../builder/middleware-unit.js\";\nimport type {\n DefineMiddlewareResult,\n ExtractState,\n IsUnknown,\n MiddlewareRequirements,\n MiddlewareUnitBuilder,\n NextFn,\n StandaloneMiddlewareContext,\n} from \"../types/units.js\";\nimport type {\n AppContext,\n LayoutId,\n ResolveLayoutMiddlewaresState,\n ResolveLayoutsState,\n} from \"../types/index.js\";\n\nexport interface MiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {\n /**\n * Defines a standalone, unscoped middleware using a short function signature.\n *\n * @example\n * ```ts\n * const auth = middleware((ctx, next) => {\n * return next({ user: \"alice\" });\n * });\n * ```\n */\n <TState = unknown, TRequires extends MiddlewareRequirements = {}, R = unknown>(\n fn: (\n ctx: StandaloneMiddlewareContext<unknown, unknown, unknown, TAppContext, {}, TRequires>,\n next: NextFn<NoInfer<TState>>,\n ) => R,\n ): DefineMiddlewareResult<\n unknown,\n unknown,\n unknown,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n R,\n unknown,\n unknown,\n unknown,\n {},\n null,\n TRequires\n >;\n\n /**\n * Constructs a fluent unscoped middleware builder, with optional explicit State generic.\n *\n * @example\n * ```ts\n * const auth = middleware<{ user: string }>()\n * .query(z.object({ token: z.string() }))\n * .body(\"form\", z.object({ file: z.instanceof(File) }))\n * .returns({ 401: z.string() })\n * .handler(async (ctx, next) => next({ user: \"alice\" }));\n * ```\n */\n <TState = unknown>(): MiddlewareUnitBuilder<\n unknown,\n unknown,\n unknown,\n {},\n null,\n {},\n TAppContext,\n {},\n unknown,\n unknown,\n unknown,\n TState\n >;\n\n /**\n * Defines a standalone middleware scoped to a single layout branch using a short function signature.\n */\n <\n const Layout extends LayoutId,\n TState = unknown,\n TRequires extends MiddlewareRequirements = {},\n R = unknown,\n >(\n layout: Layout,\n fn: (\n ctx: StandaloneMiddlewareContext<\n unknown,\n
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":[],"sources":["../../../src/define/middleware.ts"],"sourcesContent":["import { createMiddlewareUnitBuilder } from \"../builder/middleware-unit.js\";\nimport type {\n DefineMiddlewareResult,\n ExtractState,\n IsUnknown,\n MiddlewareRequirements,\n MiddlewareUnitBuilder,\n NextFn,\n StandaloneMiddlewareContext,\n} from \"../types/units.js\";\nimport type {\n AppContext,\n LayoutId,\n LayoutParams,\n ResolveLayoutMiddlewaresState,\n ResolveLayoutsState,\n} from \"../types/index.js\";\n\nexport interface MiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {\n /**\n * Defines a standalone, unscoped middleware using a short function signature.\n *\n * @example\n * ```ts\n * const auth = middleware((ctx, next) => {\n * return next({ user: \"alice\" });\n * });\n * ```\n */\n <TState = unknown, TRequires extends MiddlewareRequirements = {}, R = unknown>(\n fn: (\n ctx: StandaloneMiddlewareContext<unknown, unknown, unknown, TAppContext, {}, TRequires>,\n next: NextFn<NoInfer<TState>>,\n ) => R,\n ): DefineMiddlewareResult<\n unknown,\n unknown,\n unknown,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n R,\n unknown,\n unknown,\n unknown,\n {},\n null,\n TRequires\n >;\n\n /**\n * Constructs a fluent unscoped middleware builder, with optional explicit State generic.\n *\n * @example\n * ```ts\n * const auth = middleware<{ user: string }>()\n * .query(z.object({ token: z.string() }))\n * .body(\"form\", z.object({ file: z.instanceof(File) }))\n * .returns({ 401: z.string() })\n * .handler(async (ctx, next) => next({ user: \"alice\" }));\n * ```\n */\n <TState = unknown>(): MiddlewareUnitBuilder<\n unknown,\n unknown,\n unknown,\n {},\n null,\n {},\n TAppContext,\n {},\n unknown,\n unknown,\n unknown,\n TState\n >;\n\n /**\n * Defines a standalone middleware scoped to a single layout branch using a short function signature.\n */\n <\n const Layout extends LayoutId,\n TState = unknown,\n TRequires extends MiddlewareRequirements = {},\n R = unknown,\n >(\n layout: Layout,\n fn: (\n ctx: StandaloneMiddlewareContext<\n unknown,\n LayoutParams<Layout>,\n unknown,\n TAppContext,\n ResolveLayoutMiddlewaresState<Layout>,\n TRequires\n >,\n next: NextFn<NoInfer<TState>>,\n ) => R,\n ): DefineMiddlewareResult<\n unknown,\n unknown,\n unknown,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n R,\n unknown,\n unknown,\n unknown,\n {},\n Layout,\n TRequires\n >;\n\n /**\n * Defines a standalone middleware scoped to multiple layout branches using a short function signature.\n */\n <\n const Layouts extends readonly [LayoutId, ...LayoutId[]],\n TState = unknown,\n TRequires extends MiddlewareRequirements = {},\n R = unknown,\n >(\n layouts: Layouts,\n fn: (\n ctx: StandaloneMiddlewareContext<\n unknown,\n LayoutParams<Layouts[number]>,\n unknown,\n TAppContext,\n ResolveLayoutsState<Layouts>,\n TRequires\n >,\n next: NextFn<NoInfer<TState>>,\n ) => R,\n ): DefineMiddlewareResult<\n unknown,\n unknown,\n unknown,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n R,\n unknown,\n unknown,\n unknown,\n {},\n Layouts,\n TRequires\n >;\n\n /**\n * Constructs a fluent middleware builder scoped to a single layout branch, with optional explicit State generic.\n */\n <const Layout extends LayoutId, TState = unknown>(\n layout: Layout,\n ): MiddlewareUnitBuilder<\n unknown,\n LayoutParams<Layout>,\n unknown,\n {},\n Layout,\n {},\n TAppContext,\n ResolveLayoutMiddlewaresState<Layout>,\n unknown,\n unknown,\n unknown,\n TState\n >;\n\n /**\n * Constructs a fluent middleware builder scoped to multiple layout branches (branch union), with optional explicit State generic.\n */\n <const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown>(\n layouts: Layouts,\n ): MiddlewareUnitBuilder<\n unknown,\n LayoutParams<Layouts[number]>,\n unknown,\n {},\n Layouts,\n {},\n TAppContext,\n ResolveLayoutsState<Layouts>,\n unknown,\n unknown,\n unknown,\n TState\n >;\n}\n\nexport const middleware: MiddlewareFn<AppContext> = function middleware(\n first?: any,\n second?: any,\n): any {\n if (first === undefined) {\n return createMiddlewareUnitBuilder();\n }\n\n if (typeof first === \"string\" || Array.isArray(first)) {\n if (typeof second === \"function\") {\n return {\n handler: second,\n __middlewareAcc: undefined as unknown,\n __requiredLayouts: first,\n };\n }\n return createMiddlewareUnitBuilder(first);\n }\n\n if (typeof first === \"function\") {\n return {\n handler: first,\n __middlewareAcc: undefined as unknown,\n };\n }\n\n return createMiddlewareUnitBuilder();\n};\n"],"mappings":";;AA0LA,IAAa,aAAuC,SAAS,WAC3D,OACA,QACK;CACL,IAAI,UAAU,KAAA,GACZ,OAAO,wBAAA,4BAA4B;CAGrC,IAAI,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;EACrD,IAAI,OAAO,WAAW,YACpB,OAAO;GACL,SAAS;GACT,iBAAiB,KAAA;GACjB,mBAAmB;EACrB;EAEF,OAAO,wBAAA,4BAA4B,KAAK;CAC1C;CAEA,IAAI,OAAO,UAAU,YACnB,OAAO;EACL,SAAS;EACT,iBAAiB,KAAA;CACnB;CAGF,OAAO,wBAAA,4BAA4B;AACrC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DefineMiddlewareResult, ExtractState, IsUnknown, MiddlewareRequirements, MiddlewareUnitBuilder, NextFn, StandaloneMiddlewareContext } from '../types/units.js';
|
|
2
|
-
import { AppContext, LayoutId, ResolveLayoutMiddlewaresState, ResolveLayoutsState } from '../types/index.js';
|
|
2
|
+
import { AppContext, LayoutId, LayoutParams, ResolveLayoutMiddlewaresState, ResolveLayoutsState } from '../types/index.js';
|
|
3
3
|
export interface MiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {
|
|
4
4
|
/**
|
|
5
5
|
* Defines a standalone, unscoped middleware using a short function signature.
|
|
@@ -28,18 +28,18 @@ export interface MiddlewareFn<TAppContext extends Record<string, unknown> = AppC
|
|
|
28
28
|
/**
|
|
29
29
|
* Defines a standalone middleware scoped to a single layout branch using a short function signature.
|
|
30
30
|
*/
|
|
31
|
-
<const Layout extends LayoutId, TState = unknown, TRequires extends MiddlewareRequirements = {}, R = unknown>(layout: Layout, fn: (ctx: StandaloneMiddlewareContext<unknown,
|
|
31
|
+
<const Layout extends LayoutId, TState = unknown, TRequires extends MiddlewareRequirements = {}, R = unknown>(layout: Layout, fn: (ctx: StandaloneMiddlewareContext<unknown, LayoutParams<Layout>, unknown, TAppContext, ResolveLayoutMiddlewaresState<Layout>, TRequires>, next: NextFn<NoInfer<TState>>) => R): DefineMiddlewareResult<unknown, unknown, unknown, IsUnknown<TState> extends true ? ExtractState<R> : TState, R, unknown, unknown, unknown, {}, Layout, TRequires>;
|
|
32
32
|
/**
|
|
33
33
|
* Defines a standalone middleware scoped to multiple layout branches using a short function signature.
|
|
34
34
|
*/
|
|
35
|
-
<const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown, TRequires extends MiddlewareRequirements = {}, R = unknown>(layouts: Layouts, fn: (ctx: StandaloneMiddlewareContext<unknown,
|
|
35
|
+
<const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown, TRequires extends MiddlewareRequirements = {}, R = unknown>(layouts: Layouts, fn: (ctx: StandaloneMiddlewareContext<unknown, LayoutParams<Layouts[number]>, unknown, TAppContext, ResolveLayoutsState<Layouts>, TRequires>, next: NextFn<NoInfer<TState>>) => R): DefineMiddlewareResult<unknown, unknown, unknown, IsUnknown<TState> extends true ? ExtractState<R> : TState, R, unknown, unknown, unknown, {}, Layouts, TRequires>;
|
|
36
36
|
/**
|
|
37
37
|
* Constructs a fluent middleware builder scoped to a single layout branch, with optional explicit State generic.
|
|
38
38
|
*/
|
|
39
|
-
<const Layout extends LayoutId, TState = unknown>(layout: Layout): MiddlewareUnitBuilder<unknown,
|
|
39
|
+
<const Layout extends LayoutId, TState = unknown>(layout: Layout): MiddlewareUnitBuilder<unknown, LayoutParams<Layout>, unknown, {}, Layout, {}, TAppContext, ResolveLayoutMiddlewaresState<Layout>, unknown, unknown, unknown, TState>;
|
|
40
40
|
/**
|
|
41
41
|
* Constructs a fluent middleware builder scoped to multiple layout branches (branch union), with optional explicit State generic.
|
|
42
42
|
*/
|
|
43
|
-
<const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown>(layouts: Layouts): MiddlewareUnitBuilder<unknown,
|
|
43
|
+
<const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown>(layouts: Layouts): MiddlewareUnitBuilder<unknown, LayoutParams<Layouts[number]>, unknown, {}, Layouts, {}, TAppContext, ResolveLayoutsState<Layouts>, unknown, unknown, unknown, TState>;
|
|
44
44
|
}
|
|
45
45
|
export declare const middleware: MiddlewareFn<AppContext>;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -13,21 +13,10 @@ Object.defineProperty(exports, "ValidationError", {
|
|
|
13
13
|
return _taserjs_router_utils.ValidationError;
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
|
-
exports.all = require_standalone.all;
|
|
17
|
-
exports.any = require_standalone.any;
|
|
18
16
|
exports.createContext = require_create_context.createContext;
|
|
19
17
|
exports.createTaserApp = require_router.createTaserApp;
|
|
20
|
-
exports.del = require_standalone.del;
|
|
21
|
-
exports.get = require_standalone.get;
|
|
22
|
-
exports.head = require_standalone.head;
|
|
23
18
|
exports.honoMw = require_hono_mw.honoMw;
|
|
24
|
-
exports.layout = require_standalone.layout;
|
|
25
19
|
exports.middleware = require_standalone.middleware;
|
|
26
|
-
exports.options = require_standalone.options;
|
|
27
|
-
exports.patch = require_standalone.patch;
|
|
28
|
-
exports.post = require_standalone.post;
|
|
29
|
-
exports.put = require_standalone.put;
|
|
30
|
-
exports.query = require_standalone.query;
|
|
31
20
|
exports.t = require_standalone.t;
|
|
32
21
|
Object.defineProperty(exports, "validationErrorSchema", {
|
|
33
22
|
enumerable: true,
|