clear-router 2.5.4 → 2.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/bindings-BL1d0dFY.d.cts +20 -0
- package/dist/bindings-DIanvIVd.mjs +211 -0
- package/dist/bindings-DqvZ9-YW.d.mts +20 -0
- package/dist/bindings-DvV2DXWi.cjs +253 -0
- package/dist/core/index.cjs +5 -2
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.mts +2 -2
- package/dist/core/index.mjs +3 -2
- package/dist/decorators/index.cjs +5 -0
- package/dist/decorators/index.d.cts +2 -0
- package/dist/decorators/index.d.mts +2 -0
- package/dist/decorators/index.mjs +3 -0
- package/dist/decorators/setup.cjs +15 -0
- package/dist/decorators/setup.d.cts +2 -0
- package/dist/decorators/setup.d.mts +2 -0
- package/dist/decorators/setup.mjs +13 -0
- package/dist/express/index.cjs +26 -9
- package/dist/express/index.d.cts +1 -1
- package/dist/express/index.d.mts +1 -1
- package/dist/express/index.mjs +26 -9
- package/dist/fastify/index.cjs +24 -8
- package/dist/fastify/index.d.cts +4 -2
- package/dist/fastify/index.d.mts +4 -2
- package/dist/fastify/index.mjs +24 -8
- package/dist/h3/index.cjs +26 -9
- package/dist/h3/index.d.cts +1 -1
- package/dist/h3/index.d.mts +1 -1
- package/dist/h3/index.mjs +26 -9
- package/dist/hono/index.cjs +28 -11
- package/dist/hono/index.d.cts +4 -2
- package/dist/hono/index.d.mts +4 -2
- package/dist/hono/index.mjs +28 -11
- package/dist/index.cjs +271 -27
- package/dist/index.d.cts +70 -18
- package/dist/index.d.mts +70 -18
- package/dist/index.mjs +270 -28
- package/dist/koa/index.cjs +24 -8
- package/dist/koa/index.d.cts +4 -2
- package/dist/koa/index.d.mts +4 -2
- package/dist/koa/index.mjs +24 -8
- package/dist/{responses-B-UirWFf.cjs → responses-JzXstGU5.cjs} +18 -1
- package/dist/{responses-CJaD0ugv.mjs → responses-_II3dOJ5.mjs} +19 -1
- package/dist/{router-CS_2XQ7I.d.cts → router-78fDk87B.d.cts} +68 -12
- package/dist/{router-BReOXz-F.mjs → router-BAWdklDQ.mjs} +130 -48
- package/dist/{router-_w2VzMQF.d.mts → router-BdfAh1me.d.mts} +67 -11
- package/dist/{router-CHg0pZUO.cjs → router-CCUF5FE7.cjs} +130 -48
- package/dist/types/basic.d.mts +7 -0
- package/dist/types/core/Request.d.mts +25 -0
- package/dist/types/core/Response.d.mts +21 -0
- package/dist/types/express.d.mts +5 -2
- package/dist/types/fastify.d.mts +5 -2
- package/dist/types/h3.d.mts +5 -2
- package/dist/types/hono.d.mts +5 -2
- package/dist/types/koa.d.mts +5 -2
- package/package.json +16 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
import { NextFunction, Request, Response as Response$
|
|
3
|
-
import { Middleware } from "h3";
|
|
2
|
+
import { NextFunction, Request as Request$1, Response as Response$2 } from "express";
|
|
3
|
+
import { Middleware as Middleware$1 } from "h3";
|
|
4
4
|
|
|
5
5
|
//#region types/basic.d.ts
|
|
6
6
|
/**
|
|
@@ -26,16 +26,37 @@ interface RouterConfig {
|
|
|
26
26
|
bodyKeys?: string[] | string; /** Keys in the request headers to check for method override */
|
|
27
27
|
headerKeys?: string[] | string;
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Optional method binding / container resolution support. Disabled by default.
|
|
31
|
+
*/
|
|
32
|
+
container?: {
|
|
33
|
+
/** Whether decorated handler parameter binding is enabled */enabled?: boolean; /** Whether unknown constructor tokens should be instantiated automatically */
|
|
34
|
+
autoDiscover?: boolean;
|
|
35
|
+
};
|
|
29
36
|
}
|
|
30
37
|
//#endregion
|
|
31
|
-
//#region
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
//#region src/core/Response.d.ts
|
|
39
|
+
declare class Response$1 {
|
|
40
|
+
body: any;
|
|
41
|
+
headers: Headers;
|
|
42
|
+
sent: boolean;
|
|
43
|
+
statusCode: number;
|
|
44
|
+
constructor(init?: Partial<Response$1>);
|
|
45
|
+
status(code: number): this;
|
|
46
|
+
code(code: number): this;
|
|
47
|
+
setHeader(name: string, value: string): this;
|
|
48
|
+
header(name: string, value: string): this;
|
|
49
|
+
set(name: string, value: string): this;
|
|
50
|
+
type(contentType: string): this;
|
|
51
|
+
send(body?: any): this;
|
|
52
|
+
json(body: any): this;
|
|
53
|
+
html(body: string): this;
|
|
54
|
+
text(body: string): this;
|
|
55
|
+
noContent(): this;
|
|
56
|
+
}
|
|
36
57
|
//#endregion
|
|
37
58
|
//#region src/Route.d.ts
|
|
38
|
-
declare class Route<X = any, M = Middleware | Middleware
|
|
59
|
+
declare class Route<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
39
60
|
ctx: X;
|
|
40
61
|
body: RequestData;
|
|
41
62
|
query: RequestData;
|
|
@@ -52,8 +73,34 @@ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
|
|
|
52
73
|
constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]);
|
|
53
74
|
}
|
|
54
75
|
//#endregion
|
|
76
|
+
//#region src/core/Request.d.ts
|
|
77
|
+
declare class Request<X = any, M = any> extends ClearRequest<X, M> {
|
|
78
|
+
original?: any;
|
|
79
|
+
method: string;
|
|
80
|
+
path: string;
|
|
81
|
+
url: string;
|
|
82
|
+
headers: Headers | Record<string, any>;
|
|
83
|
+
constructor(init?: Partial<Request<X, M>> & {
|
|
84
|
+
body?: RequestData;
|
|
85
|
+
query?: RequestData;
|
|
86
|
+
params?: RequestData;
|
|
87
|
+
route?: Route<X, M>;
|
|
88
|
+
});
|
|
89
|
+
getBody(): RequestData;
|
|
90
|
+
header(name: string): string;
|
|
91
|
+
param(name: string): any;
|
|
92
|
+
input(name: string): any;
|
|
93
|
+
is(method: HttpMethod | string): boolean;
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region types/express.d.ts
|
|
97
|
+
/**
|
|
98
|
+
* Middleware function type
|
|
99
|
+
*/
|
|
100
|
+
type Middleware = (req: Request$1, res: Response$2, next: NextFunction) => any | Promise<any>;
|
|
101
|
+
//#endregion
|
|
55
102
|
//#region src/ClearRequest.d.ts
|
|
56
|
-
declare class ClearRequest<X = any, M = Middleware | Middleware
|
|
103
|
+
declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
|
|
57
104
|
[key: string]: any;
|
|
58
105
|
/**
|
|
59
106
|
* @param body - Parsed request body
|
|
@@ -92,16 +139,14 @@ declare abstract class CoreRouter {
|
|
|
92
139
|
protected static routerStateNamespace: string;
|
|
93
140
|
private static readonly stateStoreKey;
|
|
94
141
|
private static readonly stateBoundKey;
|
|
142
|
+
private static readonly defaultConfigKey;
|
|
143
|
+
protected static createBaseConfig(): RouterConfig;
|
|
144
|
+
protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
|
|
145
|
+
protected static getDefaultConfig(): RouterConfig;
|
|
95
146
|
protected static resolveStateNamespace(this: any): string;
|
|
96
147
|
protected static getStateStore(): Record<string, any>;
|
|
97
148
|
protected static createDefaultState(): {
|
|
98
|
-
config:
|
|
99
|
-
methodOverride: {
|
|
100
|
-
enabled: boolean;
|
|
101
|
-
bodyKeys: string[];
|
|
102
|
-
headerKeys: string[];
|
|
103
|
-
};
|
|
104
|
-
};
|
|
149
|
+
config: RouterConfig;
|
|
105
150
|
groupContext: AsyncLocalStorage<{
|
|
106
151
|
prefix: string;
|
|
107
152
|
groupMiddlewares: any[];
|
|
@@ -116,6 +161,7 @@ declare abstract class CoreRouter {
|
|
|
116
161
|
protected static bindStateAccessors(this: any): void;
|
|
117
162
|
protected static createDefaultOptionsHandler(): any;
|
|
118
163
|
static config: RouterConfig;
|
|
164
|
+
static configureDefaults(this: any, options?: RouterConfig): void;
|
|
119
165
|
protected static groupContext: AsyncLocalStorage<{
|
|
120
166
|
prefix: string;
|
|
121
167
|
groupMiddlewares: any[];
|
|
@@ -265,14 +311,20 @@ declare abstract class CoreRouter {
|
|
|
265
311
|
*/
|
|
266
312
|
static allRoutes(this: any, type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
267
313
|
protected static resolveHandler(route: Route<any, any, any>): {
|
|
268
|
-
handlerFunction: ((ctx: any, req:
|
|
314
|
+
handlerFunction: ((ctx: any, req: Request) => any | Promise<any>) | null;
|
|
269
315
|
instance: Controller<any> | null;
|
|
316
|
+
bindingTarget?: object;
|
|
317
|
+
bindingMethod?: PropertyKey;
|
|
318
|
+
bindingHandler?: object;
|
|
319
|
+
bindingMetadata?: object;
|
|
270
320
|
};
|
|
321
|
+
protected static callHandler(this: any, handlerFunction: (ctx: any, req: Request) => any | Promise<any>, ctx: any, bindingTarget?: object, bindingMethod?: PropertyKey, bindingHandler?: object, bindingMetadata?: object): Promise<any>;
|
|
271
322
|
protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
|
|
272
323
|
body: Record<string, any>;
|
|
273
324
|
query: Record<string, any>;
|
|
274
325
|
params: Record<string, any>;
|
|
326
|
+
method?: HttpMethod | string;
|
|
275
327
|
}): void;
|
|
276
328
|
}
|
|
277
329
|
//#endregion
|
|
278
|
-
export { ClearRequest, Controller, CoreRouter, Route };
|
|
330
|
+
export { ClearRequest, Controller, CoreRouter, Request, Response$1 as Response, Route };
|
package/dist/index.mjs
CHANGED
|
@@ -58,6 +58,146 @@ var Route = class {
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/core/Request.ts
|
|
63
|
+
var Request = class extends ClearRequest {
|
|
64
|
+
original;
|
|
65
|
+
method = "GET";
|
|
66
|
+
path = "/";
|
|
67
|
+
url = "/";
|
|
68
|
+
headers = {};
|
|
69
|
+
constructor(init) {
|
|
70
|
+
super(init);
|
|
71
|
+
Object.assign(this, init);
|
|
72
|
+
}
|
|
73
|
+
getBody() {
|
|
74
|
+
return this.body ?? {};
|
|
75
|
+
}
|
|
76
|
+
header(name) {
|
|
77
|
+
if (typeof this.headers.get === "function") return this.headers.get(name) || "";
|
|
78
|
+
const headers = this.headers;
|
|
79
|
+
const value = headers[name] ?? headers[name.toLowerCase()];
|
|
80
|
+
return Array.isArray(value) ? String(value[0] ?? "") : String(value ?? "");
|
|
81
|
+
}
|
|
82
|
+
param(name) {
|
|
83
|
+
return this.params?.[name];
|
|
84
|
+
}
|
|
85
|
+
input(name) {
|
|
86
|
+
return this.body?.[name] ?? this.query?.[name] ?? this.params?.[name];
|
|
87
|
+
}
|
|
88
|
+
is(method) {
|
|
89
|
+
return this.method.toLowerCase() === String(method).toLowerCase();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/core/Response.ts
|
|
95
|
+
var Response = class {
|
|
96
|
+
body;
|
|
97
|
+
headers = new Headers();
|
|
98
|
+
sent = false;
|
|
99
|
+
statusCode = 200;
|
|
100
|
+
constructor(init) {
|
|
101
|
+
Object.assign(this, init);
|
|
102
|
+
if (init?.headers && !(init.headers instanceof Headers)) this.headers = new Headers(init.headers);
|
|
103
|
+
}
|
|
104
|
+
status(code) {
|
|
105
|
+
this.statusCode = code;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
code(code) {
|
|
109
|
+
return this.status(code);
|
|
110
|
+
}
|
|
111
|
+
setHeader(name, value) {
|
|
112
|
+
this.headers.set(name, value);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
header(name, value) {
|
|
116
|
+
return this.setHeader(name, value);
|
|
117
|
+
}
|
|
118
|
+
set(name, value) {
|
|
119
|
+
return this.setHeader(name, value);
|
|
120
|
+
}
|
|
121
|
+
type(contentType) {
|
|
122
|
+
return this.setHeader("Content-Type", contentType);
|
|
123
|
+
}
|
|
124
|
+
send(body) {
|
|
125
|
+
this.body = body;
|
|
126
|
+
this.sent = true;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
json(body) {
|
|
130
|
+
return this.type("application/json; charset=utf-8").send(body);
|
|
131
|
+
}
|
|
132
|
+
html(body) {
|
|
133
|
+
return this.type("text/html; charset=utf-8").send(body);
|
|
134
|
+
}
|
|
135
|
+
text(body) {
|
|
136
|
+
return this.type("text/plain; charset=utf-8").send(body);
|
|
137
|
+
}
|
|
138
|
+
noContent() {
|
|
139
|
+
return this.status(204).send(null);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/core/bindings.ts
|
|
145
|
+
const metadataKey = Symbol.for("clear-router:binding-metadata");
|
|
146
|
+
const bindings = /* @__PURE__ */ new WeakMap();
|
|
147
|
+
var Container = class {
|
|
148
|
+
static registry = /* @__PURE__ */ new Map();
|
|
149
|
+
static bind(token, value) {
|
|
150
|
+
this.registry.set(token, value);
|
|
151
|
+
}
|
|
152
|
+
static unbind(token) {
|
|
153
|
+
this.registry.delete(token);
|
|
154
|
+
}
|
|
155
|
+
static clear() {
|
|
156
|
+
this.registry.clear();
|
|
157
|
+
}
|
|
158
|
+
static has(token) {
|
|
159
|
+
return this.registry.has(token);
|
|
160
|
+
}
|
|
161
|
+
static async resolve(token, ctx, autoDiscover = false) {
|
|
162
|
+
if (token === Request) return ctx.clearRequest;
|
|
163
|
+
if (token === Response) return ctx.clearResponse;
|
|
164
|
+
if (this.registry.has(token)) return this.resolveBinding(this.registry.get(token), ctx, autoDiscover);
|
|
165
|
+
if (autoDiscover && typeof token === "function") return new token();
|
|
166
|
+
}
|
|
167
|
+
static async resolveBinding(binding, ctx, autoDiscover) {
|
|
168
|
+
if (!binding) return void 0;
|
|
169
|
+
if (typeof binding !== "function") return binding;
|
|
170
|
+
if (isClass(binding)) return new binding();
|
|
171
|
+
const resolved = await binding(ctx);
|
|
172
|
+
if (typeof resolved === "function" && autoDiscover && isClass(resolved)) return new resolved();
|
|
173
|
+
return resolved;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
function getStandardMetadata(metadata, propertyKey) {
|
|
177
|
+
const store = metadata && metadata[metadataKey];
|
|
178
|
+
if (!store) return void 0;
|
|
179
|
+
return propertyKey ? store[propertyKey] : void 0;
|
|
180
|
+
}
|
|
181
|
+
function getBindingMetadataFromTargets(targets) {
|
|
182
|
+
for (const { target, propertyKey } of targets) {
|
|
183
|
+
if (!target) continue;
|
|
184
|
+
const metadata = getBindingMetadata(target, propertyKey);
|
|
185
|
+
if (metadata) return metadata;
|
|
186
|
+
const standardMetadata = getStandardMetadata(target[Symbol.metadata], propertyKey);
|
|
187
|
+
if (standardMetadata) return standardMetadata;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function getBindingMetadata(target, propertyKey) {
|
|
191
|
+
if (propertyKey) return bindings.get(target)?.get(propertyKey);
|
|
192
|
+
return bindings.get(target)?.get("__route_handler__");
|
|
193
|
+
}
|
|
194
|
+
function getDesignParamTypes(target, propertyKey) {
|
|
195
|
+
return Reflect.getMetadata?.("design:paramtypes", target, propertyKey) ?? [];
|
|
196
|
+
}
|
|
197
|
+
function isClass(value) {
|
|
198
|
+
return typeof value === "function" && /^class\s/.test(Function.prototype.toString.call(value));
|
|
199
|
+
}
|
|
200
|
+
|
|
61
201
|
//#endregion
|
|
62
202
|
//#region src/core/router.ts
|
|
63
203
|
/**
|
|
@@ -70,6 +210,40 @@ var CoreRouter = class {
|
|
|
70
210
|
static routerStateNamespace = "clear-router:core";
|
|
71
211
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
72
212
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
213
|
+
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
214
|
+
static createBaseConfig() {
|
|
215
|
+
return {
|
|
216
|
+
methodOverride: {
|
|
217
|
+
enabled: true,
|
|
218
|
+
bodyKeys: ["_method"],
|
|
219
|
+
headerKeys: ["x-http-method"]
|
|
220
|
+
},
|
|
221
|
+
container: {
|
|
222
|
+
enabled: false,
|
|
223
|
+
autoDiscover: false
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
static mergeConfig(target, source) {
|
|
228
|
+
if (!source) return target;
|
|
229
|
+
if (source.methodOverride) target.methodOverride = {
|
|
230
|
+
...target.methodOverride || {},
|
|
231
|
+
...source.methodOverride
|
|
232
|
+
};
|
|
233
|
+
if (source.container) target.container = {
|
|
234
|
+
...target.container || {},
|
|
235
|
+
...source.container
|
|
236
|
+
};
|
|
237
|
+
return target;
|
|
238
|
+
}
|
|
239
|
+
static getDefaultConfig() {
|
|
240
|
+
const g = globalThis;
|
|
241
|
+
if (!g[this.defaultConfigKey]) g[this.defaultConfigKey] = this.createBaseConfig();
|
|
242
|
+
return {
|
|
243
|
+
methodOverride: { ...g[this.defaultConfigKey].methodOverride },
|
|
244
|
+
container: { ...g[this.defaultConfigKey].container }
|
|
245
|
+
};
|
|
246
|
+
}
|
|
73
247
|
static resolveStateNamespace() {
|
|
74
248
|
return String(this.routerStateNamespace || this.name || "clear-router:core");
|
|
75
249
|
}
|
|
@@ -80,11 +254,7 @@ var CoreRouter = class {
|
|
|
80
254
|
}
|
|
81
255
|
static createDefaultState() {
|
|
82
256
|
return {
|
|
83
|
-
config:
|
|
84
|
-
enabled: true,
|
|
85
|
-
bodyKeys: ["_method"],
|
|
86
|
-
headerKeys: ["x-http-method"]
|
|
87
|
-
} },
|
|
257
|
+
config: this.getDefaultConfig(),
|
|
88
258
|
groupContext: new AsyncLocalStorage(),
|
|
89
259
|
routes: [],
|
|
90
260
|
routesByPathMethod: {},
|
|
@@ -161,11 +331,24 @@ var CoreRouter = class {
|
|
|
161
331
|
}
|
|
162
332
|
};
|
|
163
333
|
}
|
|
164
|
-
static config = {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
334
|
+
static config = {
|
|
335
|
+
methodOverride: {
|
|
336
|
+
enabled: true,
|
|
337
|
+
bodyKeys: ["_method"],
|
|
338
|
+
headerKeys: ["x-http-method"]
|
|
339
|
+
},
|
|
340
|
+
container: {
|
|
341
|
+
enabled: false,
|
|
342
|
+
autoDiscover: false
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
static configureDefaults(options) {
|
|
346
|
+
const g = globalThis;
|
|
347
|
+
const defaults = this.mergeConfig(g[this.defaultConfigKey] || this.createBaseConfig(), options);
|
|
348
|
+
g[this.defaultConfigKey] = defaults;
|
|
349
|
+
const store = this.getStateStore();
|
|
350
|
+
for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
|
|
351
|
+
}
|
|
169
352
|
static groupContext = new AsyncLocalStorage();
|
|
170
353
|
static routes = [];
|
|
171
354
|
static routesByPathMethod = {};
|
|
@@ -207,11 +390,12 @@ var CoreRouter = class {
|
|
|
207
390
|
*/
|
|
208
391
|
static configure(options) {
|
|
209
392
|
this.ensureState();
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
393
|
+
this.config = this.mergeConfig(this.getDefaultConfig(), this.config);
|
|
394
|
+
const container = options?.container;
|
|
395
|
+
if (container) {
|
|
396
|
+
if (typeof container.enabled === "boolean") this.config.container.enabled = container.enabled;
|
|
397
|
+
if (typeof container.autoDiscover === "boolean") this.config.container.autoDiscover = container.autoDiscover;
|
|
398
|
+
}
|
|
215
399
|
const override = options?.methodOverride;
|
|
216
400
|
if (!override) return;
|
|
217
401
|
if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
|
|
@@ -448,38 +632,96 @@ var CoreRouter = class {
|
|
|
448
632
|
static resolveHandler(route) {
|
|
449
633
|
let handlerFunction;
|
|
450
634
|
let instance = null;
|
|
451
|
-
|
|
452
|
-
|
|
635
|
+
let bindingTarget;
|
|
636
|
+
let bindingMethod;
|
|
637
|
+
let bindingHandler;
|
|
638
|
+
let bindingMetadata;
|
|
639
|
+
if (typeof route.handler === "function") {
|
|
640
|
+
handlerFunction = route.handler.bind(route);
|
|
641
|
+
bindingTarget = route.handler;
|
|
642
|
+
bindingHandler = route.handler;
|
|
643
|
+
} else if (Array.isArray(route.handler) && route.handler.length === 2) {
|
|
453
644
|
const [ControllerType, method] = route.handler;
|
|
454
645
|
if (["function", "object"].includes(typeof ControllerType) && typeof ControllerType[method] === "function") {
|
|
455
646
|
instance = ControllerType;
|
|
456
647
|
handlerFunction = ControllerType[method].bind(ControllerType);
|
|
648
|
+
bindingTarget = ControllerType;
|
|
649
|
+
bindingMethod = method;
|
|
650
|
+
bindingHandler = ControllerType[method];
|
|
651
|
+
bindingMetadata = ControllerType[Symbol.metadata];
|
|
457
652
|
} else if (typeof ControllerType === "function") {
|
|
458
653
|
instance = new ControllerType();
|
|
459
|
-
if (typeof instance[method] === "function")
|
|
460
|
-
|
|
654
|
+
if (typeof instance[method] === "function") {
|
|
655
|
+
handlerFunction = instance[method].bind(instance);
|
|
656
|
+
bindingTarget = ControllerType.prototype;
|
|
657
|
+
bindingMethod = method;
|
|
658
|
+
bindingHandler = instance[method];
|
|
659
|
+
bindingMetadata = ControllerType[Symbol.metadata];
|
|
660
|
+
} else throw new Error(`Method "${method}" not found in controller instance "${ControllerType.name}"`);
|
|
461
661
|
} else throw new Error(`Invalid controller type for route: ${route.path}`);
|
|
462
662
|
} else throw new Error(`Invalid handler format for route: ${route.path}`);
|
|
463
663
|
return {
|
|
464
664
|
handlerFunction,
|
|
465
|
-
instance
|
|
665
|
+
instance,
|
|
666
|
+
bindingTarget,
|
|
667
|
+
bindingMethod,
|
|
668
|
+
bindingHandler,
|
|
669
|
+
bindingMetadata
|
|
466
670
|
};
|
|
467
671
|
}
|
|
672
|
+
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
673
|
+
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
674
|
+
const metadata = getBindingMetadataFromTargets([
|
|
675
|
+
{
|
|
676
|
+
target: bindingTarget,
|
|
677
|
+
propertyKey: bindingMethod
|
|
678
|
+
},
|
|
679
|
+
{ target: bindingHandler },
|
|
680
|
+
{
|
|
681
|
+
target: bindingTarget,
|
|
682
|
+
propertyKey: "__class__"
|
|
683
|
+
}
|
|
684
|
+
]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
|
|
685
|
+
if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
|
|
686
|
+
const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
|
|
687
|
+
const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
|
|
688
|
+
if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
689
|
+
const args = [];
|
|
690
|
+
for (const token of tokens) {
|
|
691
|
+
const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
|
|
692
|
+
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
693
|
+
args.push(resolved);
|
|
694
|
+
}
|
|
695
|
+
return handlerFunction(...args);
|
|
696
|
+
}
|
|
468
697
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
698
|
+
const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : new Request({
|
|
699
|
+
ctx,
|
|
700
|
+
route,
|
|
701
|
+
body: payload.body,
|
|
702
|
+
query: payload.query,
|
|
703
|
+
params: payload.params,
|
|
704
|
+
method: String(payload.method || ctx.req?.method || ctx.method || "GET").toUpperCase(),
|
|
705
|
+
path: String(ctx.path || ctx.req?.path || ctx.req?.url || route.path),
|
|
706
|
+
url: String(ctx.url || ctx.req?.url || ctx.req?.originalUrl || route.path),
|
|
707
|
+
headers: ctx.req?.headers || ctx.headers || {},
|
|
708
|
+
original: ctx.req || ctx.request || ctx
|
|
709
|
+
});
|
|
710
|
+
clearRequest.ctx = ctx;
|
|
711
|
+
clearRequest.route = route;
|
|
712
|
+
clearRequest.body = payload.body;
|
|
713
|
+
clearRequest.query = payload.query;
|
|
714
|
+
clearRequest.params = payload.params;
|
|
715
|
+
ctx.clearRequest = clearRequest;
|
|
716
|
+
if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = new Response();
|
|
469
717
|
if (!instance) return;
|
|
470
718
|
instance.ctx = ctx;
|
|
471
719
|
instance.body = payload.body;
|
|
472
720
|
instance.query = payload.query;
|
|
473
721
|
instance.params = payload.params;
|
|
474
|
-
instance.clearRequest =
|
|
475
|
-
ctx,
|
|
476
|
-
route,
|
|
477
|
-
body: instance.body,
|
|
478
|
-
query: instance.query,
|
|
479
|
-
params: instance.params
|
|
480
|
-
});
|
|
722
|
+
instance.clearRequest = clearRequest;
|
|
481
723
|
}
|
|
482
724
|
};
|
|
483
725
|
|
|
484
726
|
//#endregion
|
|
485
|
-
export { ClearRequest, Controller, CoreRouter, Route };
|
|
727
|
+
export { ClearRequest, Controller, CoreRouter, Request, Response, Route };
|
package/dist/koa/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
|
-
const
|
|
2
|
+
require('../bindings-DvV2DXWi.cjs');
|
|
3
|
+
const require_router = require('../router-CCUF5FE7.cjs');
|
|
4
|
+
const require_responses = require('../responses-JzXstGU5.cjs');
|
|
4
5
|
|
|
5
6
|
//#region src/koa/router.ts
|
|
6
7
|
/**
|
|
@@ -44,6 +45,9 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
44
45
|
});
|
|
45
46
|
if (!meta) return void 0;
|
|
46
47
|
ctx.status = meta.status;
|
|
48
|
+
meta.headers?.forEach((headerValue, key) => {
|
|
49
|
+
ctx.set(key, headerValue);
|
|
50
|
+
});
|
|
47
51
|
if (require_responses.isFetchResponse(meta.body)) {
|
|
48
52
|
meta.body.headers.forEach((headerValue, key) => {
|
|
49
53
|
ctx.set(key, headerValue);
|
|
@@ -180,10 +184,18 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
180
184
|
for (const route of this.routes) {
|
|
181
185
|
let handlerFunction = null;
|
|
182
186
|
let instance = null;
|
|
187
|
+
let bindingTarget;
|
|
188
|
+
let bindingMethod;
|
|
189
|
+
let bindingHandler;
|
|
190
|
+
let bindingMetadata;
|
|
183
191
|
try {
|
|
184
192
|
const resolved = this.resolveHandler(route);
|
|
185
193
|
handlerFunction = resolved.handlerFunction;
|
|
186
194
|
instance = resolved.instance;
|
|
195
|
+
bindingTarget = resolved.bindingTarget;
|
|
196
|
+
bindingMethod = resolved.bindingMethod;
|
|
197
|
+
bindingHandler = resolved.bindingHandler;
|
|
198
|
+
bindingMetadata = resolved.bindingMetadata;
|
|
187
199
|
} catch (error) {
|
|
188
200
|
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
189
201
|
throw error;
|
|
@@ -210,11 +222,13 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
210
222
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
211
223
|
body: reqBody,
|
|
212
224
|
query: ctx.query,
|
|
213
|
-
params: ctx.params ?? {}
|
|
225
|
+
params: ctx.params ?? {},
|
|
226
|
+
method
|
|
214
227
|
});
|
|
215
|
-
const result = handlerFunction
|
|
228
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
216
229
|
const resolved = await Promise.resolve(result);
|
|
217
|
-
|
|
230
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
231
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
218
232
|
});
|
|
219
233
|
if ([
|
|
220
234
|
"put",
|
|
@@ -228,11 +242,13 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
228
242
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
229
243
|
body: reqBody,
|
|
230
244
|
query: ctx.query,
|
|
231
|
-
params: ctx.params ?? {}
|
|
245
|
+
params: ctx.params ?? {},
|
|
246
|
+
method
|
|
232
247
|
});
|
|
233
|
-
const result = handlerFunction
|
|
248
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
234
249
|
const resolved = await Promise.resolve(result);
|
|
235
|
-
|
|
250
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
251
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
236
252
|
});
|
|
237
253
|
}
|
|
238
254
|
}
|
package/dist/koa/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-78fDk87B.cjs";
|
|
2
2
|
import Koa from "koa";
|
|
3
3
|
import Router$1 from "@koa/router";
|
|
4
4
|
|
|
@@ -9,10 +9,12 @@ interface RequestWithGetBody extends Koa.Request {
|
|
|
9
9
|
}
|
|
10
10
|
interface HttpContext extends Koa.Context {
|
|
11
11
|
request: RequestWithGetBody;
|
|
12
|
+
clearRequest: Request;
|
|
13
|
+
clearResponse: Response;
|
|
12
14
|
params: Record<string, any>;
|
|
13
15
|
query: Record<string, any>;
|
|
14
16
|
}
|
|
15
|
-
type RouteHandler = (ctx: HttpContext, req:
|
|
17
|
+
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
16
18
|
type Handler = RouteHandler | ControllerHandler;
|
|
17
19
|
type Middleware = Koa.Middleware<any, any>;
|
|
18
20
|
type KoaRouterApp = Router$1<any, any>;
|
package/dist/koa/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as
|
|
1
|
+
import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-BdfAh1me.mjs";
|
|
2
2
|
import Koa from "koa";
|
|
3
3
|
import Router$1 from "@koa/router";
|
|
4
4
|
|
|
@@ -9,10 +9,12 @@ interface RequestWithGetBody extends Koa.Request {
|
|
|
9
9
|
}
|
|
10
10
|
interface HttpContext extends Koa.Context {
|
|
11
11
|
request: RequestWithGetBody;
|
|
12
|
+
clearRequest: Request;
|
|
13
|
+
clearResponse: Response;
|
|
12
14
|
params: Record<string, any>;
|
|
13
15
|
query: Record<string, any>;
|
|
14
16
|
}
|
|
15
|
-
type RouteHandler = (ctx: HttpContext, req:
|
|
17
|
+
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
16
18
|
type Handler = RouteHandler | ControllerHandler;
|
|
17
19
|
type Middleware = Koa.Middleware<any, any>;
|
|
18
20
|
type KoaRouterApp = Router$1<any, any>;
|
package/dist/koa/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import "../bindings-DIanvIVd.mjs";
|
|
2
|
+
import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
|
|
3
|
+
import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-_II3dOJ5.mjs";
|
|
3
4
|
|
|
4
5
|
//#region src/koa/router.ts
|
|
5
6
|
/**
|
|
@@ -43,6 +44,9 @@ var Router = class Router extends CoreRouter {
|
|
|
43
44
|
});
|
|
44
45
|
if (!meta) return void 0;
|
|
45
46
|
ctx.status = meta.status;
|
|
47
|
+
meta.headers?.forEach((headerValue, key) => {
|
|
48
|
+
ctx.set(key, headerValue);
|
|
49
|
+
});
|
|
46
50
|
if (isFetchResponse(meta.body)) {
|
|
47
51
|
meta.body.headers.forEach((headerValue, key) => {
|
|
48
52
|
ctx.set(key, headerValue);
|
|
@@ -179,10 +183,18 @@ var Router = class Router extends CoreRouter {
|
|
|
179
183
|
for (const route of this.routes) {
|
|
180
184
|
let handlerFunction = null;
|
|
181
185
|
let instance = null;
|
|
186
|
+
let bindingTarget;
|
|
187
|
+
let bindingMethod;
|
|
188
|
+
let bindingHandler;
|
|
189
|
+
let bindingMetadata;
|
|
182
190
|
try {
|
|
183
191
|
const resolved = this.resolveHandler(route);
|
|
184
192
|
handlerFunction = resolved.handlerFunction;
|
|
185
193
|
instance = resolved.instance;
|
|
194
|
+
bindingTarget = resolved.bindingTarget;
|
|
195
|
+
bindingMethod = resolved.bindingMethod;
|
|
196
|
+
bindingHandler = resolved.bindingHandler;
|
|
197
|
+
bindingMetadata = resolved.bindingMetadata;
|
|
186
198
|
} catch (error) {
|
|
187
199
|
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
188
200
|
throw error;
|
|
@@ -209,11 +221,13 @@ var Router = class Router extends CoreRouter {
|
|
|
209
221
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
210
222
|
body: reqBody,
|
|
211
223
|
query: ctx.query,
|
|
212
|
-
params: ctx.params ?? {}
|
|
224
|
+
params: ctx.params ?? {},
|
|
225
|
+
method
|
|
213
226
|
});
|
|
214
|
-
const result = handlerFunction
|
|
227
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
215
228
|
const resolved = await Promise.resolve(result);
|
|
216
|
-
|
|
229
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
230
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
217
231
|
});
|
|
218
232
|
if ([
|
|
219
233
|
"put",
|
|
@@ -227,11 +241,13 @@ var Router = class Router extends CoreRouter {
|
|
|
227
241
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
228
242
|
body: reqBody,
|
|
229
243
|
query: ctx.query,
|
|
230
|
-
params: ctx.params ?? {}
|
|
244
|
+
params: ctx.params ?? {},
|
|
245
|
+
method
|
|
231
246
|
});
|
|
232
|
-
const result = handlerFunction
|
|
247
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
233
248
|
const resolved = await Promise.resolve(result);
|
|
234
|
-
|
|
249
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
250
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
235
251
|
});
|
|
236
252
|
}
|
|
237
253
|
}
|