clear-router 2.5.11 → 2.5.13

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.
Files changed (42) hide show
  1. package/dist/{bindings-CDGLuAq4.cjs → bindings-CLsZjOEy.cjs} +8 -1
  2. package/dist/{bindings-B6x2HfzP.mjs → bindings-XLDXFpHZ.mjs} +8 -1
  3. package/dist/core/index.cjs +2 -2
  4. package/dist/core/index.d.cts +2 -2
  5. package/dist/core/index.d.mts +2 -2
  6. package/dist/core/index.mjs +2 -2
  7. package/dist/decorators/index.cjs +1 -1
  8. package/dist/decorators/index.mjs +1 -1
  9. package/dist/decorators/setup.cjs +2 -2
  10. package/dist/decorators/setup.mjs +2 -2
  11. package/dist/express/index.cjs +3 -3
  12. package/dist/express/index.d.cts +1 -1
  13. package/dist/express/index.d.mts +1 -1
  14. package/dist/express/index.mjs +3 -3
  15. package/dist/fastify/index.cjs +3 -3
  16. package/dist/fastify/index.d.cts +1 -1
  17. package/dist/fastify/index.d.mts +1 -1
  18. package/dist/fastify/index.mjs +3 -3
  19. package/dist/h3/index.cjs +3 -3
  20. package/dist/h3/index.d.cts +1 -1
  21. package/dist/h3/index.d.mts +1 -1
  22. package/dist/h3/index.mjs +3 -3
  23. package/dist/hono/index.cjs +3 -3
  24. package/dist/hono/index.d.cts +1 -1
  25. package/dist/hono/index.d.mts +1 -1
  26. package/dist/hono/index.mjs +3 -3
  27. package/dist/index.cjs +46 -6
  28. package/dist/index.d.cts +40 -24
  29. package/dist/index.d.mts +40 -24
  30. package/dist/index.mjs +46 -6
  31. package/dist/koa/index.cjs +3 -3
  32. package/dist/koa/index.d.cts +1 -1
  33. package/dist/koa/index.d.mts +1 -1
  34. package/dist/koa/index.mjs +3 -3
  35. package/dist/{responses-DuZeRyGE.mjs → responses-BvETUeDL.mjs} +1 -1
  36. package/dist/{responses-CBP3RYjJ.cjs → responses-Bvnk0uvc.cjs} +1 -1
  37. package/dist/{router-CArh9OHb.mjs → router-CPmZcbU0.mjs} +39 -6
  38. package/dist/{router-DtqXUlJq.cjs → router-D9H9-T27.cjs} +39 -6
  39. package/dist/{router-DCGZNRNj.d.cts → router-DTI0BowV.d.cts} +40 -24
  40. package/dist/{router-BkYtqjYr.d.mts → router-DiKqUyvk.d.mts} +40 -24
  41. package/dist/types/core/Response.d.mts +3 -1
  42. package/package.json +1 -1
package/dist/index.d.mts CHANGED
@@ -41,8 +41,10 @@ declare class Response$1 {
41
41
  headers: Headers;
42
42
  sent: boolean;
43
43
  statusCode: number;
44
- constructor(init?: Partial<Response$1>);
44
+ statusText: string;
45
+ constructor(init?: Partial<Response$1 | globalThis.Response>);
45
46
  status(code: number): this;
47
+ setStatusText(text: string): this;
46
48
  code(code: number): this;
47
49
  setHeader(name: string, value: string): this;
48
50
  header(name: string, value: string): this;
@@ -153,14 +155,25 @@ interface ClearRouterPluginRequestContext {
153
155
  ctx: any;
154
156
  request: Request;
155
157
  response: Response$1;
158
+ getBindings: () => Record<string, BindValue>;
156
159
  [key: string]: any;
157
160
  }
161
+ interface ClearRouterPluginArgumentsContext extends ClearRouterPluginRequestContext {
162
+ target?: object;
163
+ method?: PropertyKey;
164
+ handler?: object;
165
+ metadata?: object;
166
+ tokens: BindToken[];
167
+ designTokens: BindToken[];
168
+ }
158
169
  type PluginBindFactory<T = any> = (ctx: ClearRouterPluginRequestContext) => T | Promise<T>;
159
170
  type PluginBindValue<T = any> = BindValue<T> | PluginBindFactory<T>;
160
171
  type PluginBind = <T>(token: BindToken<T>, value: PluginBindValue<T>) => void;
172
+ type PluginArgumentsResolver = (ctx: ClearRouterPluginArgumentsContext) => any[] | undefined | Promise<any[] | undefined>;
161
173
  interface ClearRouterPluginContext<Options = any> {
162
174
  container: typeof Container;
163
175
  bind: PluginBind;
176
+ resolveArguments: (resolver: PluginArgumentsResolver) => void;
164
177
  bindings: Record<string, BindValue>;
165
178
  configure: (options: RouterConfig) => void;
166
179
  configureDefaults: (options: RouterConfig) => void;
@@ -191,13 +204,15 @@ declare abstract class CoreRouter {
191
204
  private static readonly defaultConfigKey;
192
205
  private static readonly pluginStoreKey;
193
206
  private static readonly pluginPendingKey;
207
+ private static readonly pluginArgumentResolversKey;
194
208
  protected static createBaseConfig(): RouterConfig;
195
209
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
196
210
  protected static getDefaultConfig(): RouterConfig;
197
- protected static resolveStateNamespace(this: any): string;
211
+ protected static resolveStateNamespace(): string;
198
212
  protected static getStateStore(): Record<string, any>;
199
213
  protected static getPluginStore(): Set<string>;
200
214
  protected static getPluginPendingStore(): Set<Promise<void>>;
215
+ protected static getPluginArgumentResolvers(): Set<PluginArgumentsResolver>;
201
216
  protected static createDefaultState(): {
202
217
  config: RouterConfig;
203
218
  groupContext: AsyncLocalStorage<{
@@ -211,10 +226,10 @@ declare abstract class CoreRouter {
211
226
  groupMiddlewares: any[];
212
227
  globalMiddlewares: any[];
213
228
  };
214
- protected static bindStateAccessors(this: any): void;
229
+ protected static bindStateAccessors(): void;
215
230
  protected static createDefaultOptionsHandler(): any;
216
231
  static config: RouterConfig;
217
- static configureDefaults(this: any, options?: RouterConfig): void;
232
+ static configureDefaults(options?: RouterConfig): void;
218
233
  /**
219
234
  * Use a registered plugin
220
235
  *
@@ -223,8 +238,8 @@ declare abstract class CoreRouter {
223
238
  * @param options
224
239
  * @returns
225
240
  */
226
- static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
227
- protected static pluginsReady(this: any): Promise<void>;
241
+ static use<Options = any>(plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
242
+ protected static pluginsReady(): Promise<void>;
228
243
  protected static groupContext: AsyncLocalStorage<{
229
244
  prefix: string;
230
245
  groupMiddlewares: any[];
@@ -239,7 +254,8 @@ declare abstract class CoreRouter {
239
254
  protected static getCurrentPluginRequestContext(): ClearRouterPluginRequestContext | undefined;
240
255
  protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
241
256
  protected static createPluginBind(): PluginBind;
242
- protected static ensureState(this: any): void;
257
+ protected static resolvePluginArguments(this: any, ctx: any, routeContext: Omit<ClearRouterPluginArgumentsContext, keyof ClearRouterPluginRequestContext>): Promise<any[] | undefined>;
258
+ protected static ensureState(): void;
243
259
  /**
244
260
  * Normalizes a path by ensuring it starts with a single slash and does not have trailing
245
261
  * slashes, while preserving dynamic segments and parameters.
@@ -256,7 +272,7 @@ declare abstract class CoreRouter {
256
272
  * @returns
257
273
  */
258
274
  static configure(this: any, options?: RouterConfig): void;
259
- protected static resolveMethodOverride(this: any, method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
275
+ protected static resolveMethodOverride(method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
260
276
  /**
261
277
  * Adds a new route to the router.
262
278
  *
@@ -266,7 +282,7 @@ declare abstract class CoreRouter {
266
282
  * @param handler
267
283
  * @param middlewares
268
284
  */
269
- static add(this: any, methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
285
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
270
286
  /**
271
287
  * Define a resourceful API controller with standard CRUD routes.
272
288
  *
@@ -275,7 +291,7 @@ declare abstract class CoreRouter {
275
291
  * @param controller
276
292
  * @param options
277
293
  */
278
- static apiResource(this: any, basePath: string, controller: any, options?: {
294
+ static apiResource(basePath: string, controller: any, options?: {
279
295
  only?: ControllerAction[];
280
296
  except?: ControllerAction[];
281
297
  middlewares?: ApiResourceMiddleware<any>;
@@ -288,7 +304,7 @@ declare abstract class CoreRouter {
288
304
  * @param handler The handler function for the GET route.
289
305
  * @param middlewares Optional middlewares to apply to the GET route.
290
306
  */
291
- static get(this: any, path: string, handler: any, middlewares?: any[] | any): void;
307
+ static get(path: string, handler: any, middlewares?: any[] | any): void;
292
308
  /**
293
309
  * Adds a new POST route to the router.
294
310
  *
@@ -297,7 +313,7 @@ declare abstract class CoreRouter {
297
313
  * @param handler
298
314
  * @param middlewares
299
315
  */
300
- static post(this: any, path: string, handler: any, middlewares?: any[] | any): void;
316
+ static post(path: string, handler: any, middlewares?: any[] | any): void;
301
317
  /**
302
318
  * Adds a new PUT route to the router.
303
319
  *
@@ -306,7 +322,7 @@ declare abstract class CoreRouter {
306
322
  * @param handler
307
323
  * @param middlewares
308
324
  */
309
- static put(this: any, path: string, handler: any, middlewares?: any[] | any): void;
325
+ static put(path: string, handler: any, middlewares?: any[] | any): void;
310
326
  /**
311
327
  * Adds a new DELETE route to the router.
312
328
  *
@@ -315,7 +331,7 @@ declare abstract class CoreRouter {
315
331
  * @param handler
316
332
  * @param middlewares
317
333
  */
318
- static delete(this: any, path: string, handler: any, middlewares?: any[] | any): void;
334
+ static delete(path: string, handler: any, middlewares?: any[] | any): void;
319
335
  /**
320
336
  * Adds a new PATCH route to the router.
321
337
  *
@@ -324,7 +340,7 @@ declare abstract class CoreRouter {
324
340
  * @param handler
325
341
  * @param middlewares
326
342
  */
327
- static patch(this: any, path: string, handler: any, middlewares?: any[] | any): void;
343
+ static patch(path: string, handler: any, middlewares?: any[] | any): void;
328
344
  /**
329
345
  * Adds a new OPTIONS route to the router.
330
346
  *
@@ -333,7 +349,7 @@ declare abstract class CoreRouter {
333
349
  * @param handler
334
350
  * @param middlewares
335
351
  */
336
- static options(this: any, path: string, handler: any, middlewares?: any[] | any): void;
352
+ static options(path: string, handler: any, middlewares?: any[] | any): void;
337
353
  /**
338
354
  * Adds a new HEAD route to the router.
339
355
  *
@@ -342,7 +358,7 @@ declare abstract class CoreRouter {
342
358
  * @param handler
343
359
  * @param middlewares
344
360
  */
345
- static head(this: any, path: string, handler: any, middlewares?: any[] | any): void;
361
+ static head(path: string, handler: any, middlewares?: any[] | any): void;
346
362
  /**
347
363
  * Defines a group of routes with a common prefix.
348
364
  *
@@ -351,7 +367,7 @@ declare abstract class CoreRouter {
351
367
  * @param callback
352
368
  * @param middlewares
353
369
  */
354
- static group(this: any, prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
370
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
355
371
  /**
356
372
  * Adds global middlewares to the router, which will be applied to all routes.
357
373
  *
@@ -359,24 +375,24 @@ declare abstract class CoreRouter {
359
375
  * @param middlewares
360
376
  * @param callback
361
377
  */
362
- static middleware(this: any, middlewares: any[], callback: () => void): void;
378
+ static middleware(middlewares: any[], callback: () => void): void;
363
379
  /**
364
380
  * Retrieves all registered routes in the router, optionally organized by path or method
365
381
  * for easier access and management.
366
382
  *
367
383
  * @param this
368
384
  */
369
- static allRoutes(this: any): Array<Route<any, any, any>>;
385
+ static allRoutes(): Array<Route<any, any, any>>;
370
386
  /**
371
387
  * @param this
372
388
  * @param type - 'path' to get routes organized by path
373
389
  */
374
- static allRoutes(this: any, type: 'path'): Record<string, Route<any, any, any>>;
390
+ static allRoutes(type: 'path'): Record<string, Route<any, any, any>>;
375
391
  /**
376
392
  * @param this
377
393
  * @param type - 'method' to get routes organized by method
378
394
  */
379
- static allRoutes(this: any, type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
395
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
380
396
  protected static resolveHandler(route: Route<any, any, any>): {
381
397
  handlerFunction: ((ctx: any, req: Request) => any | Promise<any>) | null;
382
398
  instance: Controller<any> | null;
@@ -385,7 +401,7 @@ declare abstract class CoreRouter {
385
401
  bindingHandler?: object;
386
402
  bindingMetadata?: object;
387
403
  };
388
- 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>;
404
+ protected static callHandler(handlerFunction: (ctx: any, req: Request) => any | Promise<any>, ctx: any, bindingTarget?: object, bindingMethod?: PropertyKey, bindingHandler?: object, bindingMetadata?: object): Promise<any>;
389
405
  protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
390
406
  body: Record<string, any>;
391
407
  query: Record<string, any>;
@@ -394,4 +410,4 @@ declare abstract class CoreRouter {
394
410
  }): void;
395
411
  }
396
412
  //#endregion
397
- export { ClearRequest, ClearRouterPlugin, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, Controller, CoreRouter, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response$1 as Response, Route, definePlugin };
413
+ export { ClearRequest, ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, Controller, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response$1 as Response, Route, definePlugin };
package/dist/index.mjs CHANGED
@@ -97,14 +97,21 @@ var Response = class {
97
97
  headers = new Headers();
98
98
  sent = false;
99
99
  statusCode = 200;
100
+ statusText = "OK";
100
101
  constructor(init) {
101
- Object.assign(this, init);
102
+ const { status: _, ...rest } = init ?? {};
103
+ Object.assign(this, rest);
102
104
  if (init?.headers && !(init.headers instanceof Headers)) this.headers = new Headers(init.headers);
105
+ if (init?.status && typeof init.status === "number") this.statusCode = init?.status;
103
106
  }
104
107
  status(code) {
105
108
  this.statusCode = code;
106
109
  return this;
107
110
  }
111
+ setStatusText(text) {
112
+ this.statusText = text;
113
+ return this;
114
+ }
108
115
  code(code) {
109
116
  return this.status(code);
110
117
  }
@@ -262,6 +269,7 @@ var CoreRouter = class {
262
269
  static defaultConfigKey = Symbol.for("clear-router:default-config");
263
270
  static pluginStoreKey = Symbol.for("clear-router:plugins");
264
271
  static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
272
+ static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
265
273
  static createBaseConfig() {
266
274
  return {
267
275
  methodOverride: {
@@ -313,6 +321,11 @@ var CoreRouter = class {
313
321
  if (!g[this.pluginPendingKey]) g[this.pluginPendingKey] = /* @__PURE__ */ new Set();
314
322
  return g[this.pluginPendingKey];
315
323
  }
324
+ static getPluginArgumentResolvers() {
325
+ const g = globalThis;
326
+ if (!g[this.pluginArgumentResolversKey]) g[this.pluginArgumentResolversKey] = /* @__PURE__ */ new Set();
327
+ return g[this.pluginArgumentResolversKey];
328
+ }
316
329
  static createDefaultState() {
317
330
  return {
318
331
  config: this.getDefaultConfig(),
@@ -427,6 +440,9 @@ var CoreRouter = class {
427
440
  const ctx = {
428
441
  container: Container,
429
442
  bind: this.createPluginBind(),
443
+ resolveArguments: (resolver) => {
444
+ this.getPluginArgumentResolvers().add(resolver);
445
+ },
430
446
  bindings: Container.bindings(),
431
447
  configure: this.configure.bind(this),
432
448
  configureDefaults: this.configureDefaults.bind(this),
@@ -479,8 +495,7 @@ var CoreRouter = class {
479
495
  ctx,
480
496
  request,
481
497
  response,
482
- clearRequest: request,
483
- clearResponse: response
498
+ getBindings: () => Container.bindings()
484
499
  };
485
500
  }
486
501
  static createPluginBind() {
@@ -494,6 +509,18 @@ var CoreRouter = class {
494
509
  };
495
510
  return bind;
496
511
  }
512
+ static async resolvePluginArguments(ctx, routeContext) {
513
+ const resolvers = Array.from(this.getPluginArgumentResolvers());
514
+ if (!resolvers.length) return void 0;
515
+ const pluginContext = {
516
+ ...this.createPluginRequestContext(ctx),
517
+ ...routeContext
518
+ };
519
+ for (const resolver of resolvers) {
520
+ const args = await resolver(pluginContext);
521
+ if (Array.isArray(args)) return args;
522
+ }
523
+ }
497
524
  static ensureState() {
498
525
  this.bindStateAccessors();
499
526
  if (!this.config) this.config = { methodOverride: {
@@ -811,6 +838,7 @@ var CoreRouter = class {
811
838
  return this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
812
839
  await this.pluginsReady();
813
840
  if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
841
+ const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
814
842
  const metadata = getBindingMetadataFromTargets([
815
843
  {
816
844
  target: bindingTarget,
@@ -822,9 +850,17 @@ var CoreRouter = class {
822
850
  propertyKey: "__class__"
823
851
  }
824
852
  ]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
853
+ const tokens = metadata?.tokens?.length ? metadata.tokens : designTokens;
854
+ const pluginArgs = await this.resolvePluginArguments(ctx, {
855
+ target: bindingTarget,
856
+ method: bindingMethod,
857
+ handler: bindingHandler,
858
+ metadata: bindingMetadata,
859
+ tokens,
860
+ designTokens
861
+ });
862
+ if (pluginArgs) return handlerFunction(...pluginArgs);
825
863
  if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
826
- const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
827
- const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
828
864
  if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
829
865
  const args = [];
830
866
  for (const token of tokens) {
@@ -854,7 +890,11 @@ var CoreRouter = class {
854
890
  clearRequest.query = payload.query;
855
891
  clearRequest.params = payload.params;
856
892
  ctx.clearRequest = clearRequest;
857
- if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = new Response();
893
+ Container.bind(Request, ctx.clearRequest);
894
+ if (!(ctx.clearResponse instanceof Response)) {
895
+ ctx.clearResponse = new Response(ctx.response ?? ctx.reply ?? ctx.res);
896
+ Container.bind(Response, ctx.clearResponse);
897
+ }
858
898
  if (!instance) return;
859
899
  instance.ctx = ctx;
860
900
  instance.body = payload.body;
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- require('../bindings-CDGLuAq4.cjs');
3
- const require_router = require('../router-DtqXUlJq.cjs');
4
- const require_responses = require('../responses-CBP3RYjJ.cjs');
2
+ require('../bindings-CLsZjOEy.cjs');
3
+ const require_router = require('../router-D9H9-T27.cjs');
4
+ const require_responses = require('../responses-Bvnk0uvc.cjs');
5
5
 
6
6
  //#region src/koa/router.ts
7
7
  /**
@@ -1,4 +1,4 @@
1
- import { C as HttpMethod, S as ControllerHandler, b as ApiResourceMiddleware, d as Request, f as Route, t as CoreRouter, w as Response, x as ControllerAction } from "../router-DCGZNRNj.cjs";
1
+ import { C as ControllerAction, E as Response, S as ApiResourceMiddleware, T as HttpMethod, m as Route, p as Request, t as CoreRouter, w as ControllerHandler } from "../router-DTI0BowV.cjs";
2
2
  import Koa from "koa";
3
3
  import Router$1 from "@koa/router";
4
4
 
@@ -1,4 +1,4 @@
1
- import { C as HttpMethod, S as ControllerHandler, b as ApiResourceMiddleware, d as Request, f as Route, t as CoreRouter, w as Response, x as ControllerAction } from "../router-BkYtqjYr.mjs";
1
+ import { C as ControllerAction, E as Response, S as ApiResourceMiddleware, T as HttpMethod, m as Route, p as Request, t as CoreRouter, w as ControllerHandler } from "../router-DiKqUyvk.mjs";
2
2
  import Koa from "koa";
3
3
  import Router$1 from "@koa/router";
4
4
 
@@ -1,6 +1,6 @@
1
- import "../bindings-B6x2HfzP.mjs";
2
- import { t as CoreRouter } from "../router-CArh9OHb.mjs";
3
- import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-DuZeRyGE.mjs";
1
+ import "../bindings-XLDXFpHZ.mjs";
2
+ import { t as CoreRouter } from "../router-CPmZcbU0.mjs";
3
+ import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-BvETUeDL.mjs";
4
4
 
5
5
  //#region src/koa/router.ts
6
6
  /**
@@ -1,4 +1,4 @@
1
- import { s as Response } from "./bindings-B6x2HfzP.mjs";
1
+ import { s as Response } from "./bindings-XLDXFpHZ.mjs";
2
2
 
3
3
  //#region src/core/responses.ts
4
4
  function isFetchResponse(value) {
@@ -1,4 +1,4 @@
1
- const require_bindings = require('./bindings-CDGLuAq4.cjs');
1
+ const require_bindings = require('./bindings-CLsZjOEy.cjs');
2
2
 
3
3
  //#region src/core/responses.ts
4
4
  function isFetchResponse(value) {
@@ -1,4 +1,4 @@
1
- import { a as getStandardMetadata, c as Request, i as getDesignParamTypes, n as Container, o as isClass, r as getBindingMetadataFromTargets, s as Response } from "./bindings-B6x2HfzP.mjs";
1
+ import { a as getStandardMetadata, c as Request, i as getDesignParamTypes, n as Container, o as isClass, r as getBindingMetadataFromTargets, s as Response } from "./bindings-XLDXFpHZ.mjs";
2
2
  import { AsyncLocalStorage } from "node:async_hooks";
3
3
 
4
4
  //#region src/Route.ts
@@ -43,6 +43,7 @@ var CoreRouter = class {
43
43
  static defaultConfigKey = Symbol.for("clear-router:default-config");
44
44
  static pluginStoreKey = Symbol.for("clear-router:plugins");
45
45
  static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
46
+ static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
46
47
  static createBaseConfig() {
47
48
  return {
48
49
  methodOverride: {
@@ -94,6 +95,11 @@ var CoreRouter = class {
94
95
  if (!g[this.pluginPendingKey]) g[this.pluginPendingKey] = /* @__PURE__ */ new Set();
95
96
  return g[this.pluginPendingKey];
96
97
  }
98
+ static getPluginArgumentResolvers() {
99
+ const g = globalThis;
100
+ if (!g[this.pluginArgumentResolversKey]) g[this.pluginArgumentResolversKey] = /* @__PURE__ */ new Set();
101
+ return g[this.pluginArgumentResolversKey];
102
+ }
97
103
  static createDefaultState() {
98
104
  return {
99
105
  config: this.getDefaultConfig(),
@@ -208,6 +214,9 @@ var CoreRouter = class {
208
214
  const ctx = {
209
215
  container: Container,
210
216
  bind: this.createPluginBind(),
217
+ resolveArguments: (resolver) => {
218
+ this.getPluginArgumentResolvers().add(resolver);
219
+ },
211
220
  bindings: Container.bindings(),
212
221
  configure: this.configure.bind(this),
213
222
  configureDefaults: this.configureDefaults.bind(this),
@@ -260,8 +269,7 @@ var CoreRouter = class {
260
269
  ctx,
261
270
  request,
262
271
  response,
263
- clearRequest: request,
264
- clearResponse: response
272
+ getBindings: () => Container.bindings()
265
273
  };
266
274
  }
267
275
  static createPluginBind() {
@@ -275,6 +283,18 @@ var CoreRouter = class {
275
283
  };
276
284
  return bind;
277
285
  }
286
+ static async resolvePluginArguments(ctx, routeContext) {
287
+ const resolvers = Array.from(this.getPluginArgumentResolvers());
288
+ if (!resolvers.length) return void 0;
289
+ const pluginContext = {
290
+ ...this.createPluginRequestContext(ctx),
291
+ ...routeContext
292
+ };
293
+ for (const resolver of resolvers) {
294
+ const args = await resolver(pluginContext);
295
+ if (Array.isArray(args)) return args;
296
+ }
297
+ }
278
298
  static ensureState() {
279
299
  this.bindStateAccessors();
280
300
  if (!this.config) this.config = { methodOverride: {
@@ -592,6 +612,7 @@ var CoreRouter = class {
592
612
  return this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
593
613
  await this.pluginsReady();
594
614
  if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
615
+ const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
595
616
  const metadata = getBindingMetadataFromTargets([
596
617
  {
597
618
  target: bindingTarget,
@@ -603,9 +624,17 @@ var CoreRouter = class {
603
624
  propertyKey: "__class__"
604
625
  }
605
626
  ]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
627
+ const tokens = metadata?.tokens?.length ? metadata.tokens : designTokens;
628
+ const pluginArgs = await this.resolvePluginArguments(ctx, {
629
+ target: bindingTarget,
630
+ method: bindingMethod,
631
+ handler: bindingHandler,
632
+ metadata: bindingMetadata,
633
+ tokens,
634
+ designTokens
635
+ });
636
+ if (pluginArgs) return handlerFunction(...pluginArgs);
606
637
  if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
607
- const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
608
- const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
609
638
  if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
610
639
  const args = [];
611
640
  for (const token of tokens) {
@@ -635,7 +664,11 @@ var CoreRouter = class {
635
664
  clearRequest.query = payload.query;
636
665
  clearRequest.params = payload.params;
637
666
  ctx.clearRequest = clearRequest;
638
- if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = new Response();
667
+ Container.bind(Request, ctx.clearRequest);
668
+ if (!(ctx.clearResponse instanceof Response)) {
669
+ ctx.clearResponse = new Response(ctx.response ?? ctx.reply ?? ctx.res);
670
+ Container.bind(Response, ctx.clearResponse);
671
+ }
639
672
  if (!instance) return;
640
673
  instance.ctx = ctx;
641
674
  instance.body = payload.body;
@@ -1,4 +1,4 @@
1
- const require_bindings = require('./bindings-CDGLuAq4.cjs');
1
+ const require_bindings = require('./bindings-CLsZjOEy.cjs');
2
2
  let node_async_hooks = require("node:async_hooks");
3
3
 
4
4
  //#region src/Route.ts
@@ -43,6 +43,7 @@ var CoreRouter = class {
43
43
  static defaultConfigKey = Symbol.for("clear-router:default-config");
44
44
  static pluginStoreKey = Symbol.for("clear-router:plugins");
45
45
  static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
46
+ static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
46
47
  static createBaseConfig() {
47
48
  return {
48
49
  methodOverride: {
@@ -94,6 +95,11 @@ var CoreRouter = class {
94
95
  if (!g[this.pluginPendingKey]) g[this.pluginPendingKey] = /* @__PURE__ */ new Set();
95
96
  return g[this.pluginPendingKey];
96
97
  }
98
+ static getPluginArgumentResolvers() {
99
+ const g = globalThis;
100
+ if (!g[this.pluginArgumentResolversKey]) g[this.pluginArgumentResolversKey] = /* @__PURE__ */ new Set();
101
+ return g[this.pluginArgumentResolversKey];
102
+ }
97
103
  static createDefaultState() {
98
104
  return {
99
105
  config: this.getDefaultConfig(),
@@ -208,6 +214,9 @@ var CoreRouter = class {
208
214
  const ctx = {
209
215
  container: require_bindings.Container,
210
216
  bind: this.createPluginBind(),
217
+ resolveArguments: (resolver) => {
218
+ this.getPluginArgumentResolvers().add(resolver);
219
+ },
211
220
  bindings: require_bindings.Container.bindings(),
212
221
  configure: this.configure.bind(this),
213
222
  configureDefaults: this.configureDefaults.bind(this),
@@ -260,8 +269,7 @@ var CoreRouter = class {
260
269
  ctx,
261
270
  request,
262
271
  response,
263
- clearRequest: request,
264
- clearResponse: response
272
+ getBindings: () => require_bindings.Container.bindings()
265
273
  };
266
274
  }
267
275
  static createPluginBind() {
@@ -275,6 +283,18 @@ var CoreRouter = class {
275
283
  };
276
284
  return bind;
277
285
  }
286
+ static async resolvePluginArguments(ctx, routeContext) {
287
+ const resolvers = Array.from(this.getPluginArgumentResolvers());
288
+ if (!resolvers.length) return void 0;
289
+ const pluginContext = {
290
+ ...this.createPluginRequestContext(ctx),
291
+ ...routeContext
292
+ };
293
+ for (const resolver of resolvers) {
294
+ const args = await resolver(pluginContext);
295
+ if (Array.isArray(args)) return args;
296
+ }
297
+ }
278
298
  static ensureState() {
279
299
  this.bindStateAccessors();
280
300
  if (!this.config) this.config = { methodOverride: {
@@ -592,6 +612,7 @@ var CoreRouter = class {
592
612
  return this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
593
613
  await this.pluginsReady();
594
614
  if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
615
+ const designTokens = [...bindingTarget ? require_bindings.getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? require_bindings.getDesignParamTypes(bindingHandler) : []];
595
616
  const metadata = require_bindings.getBindingMetadataFromTargets([
596
617
  {
597
618
  target: bindingTarget,
@@ -603,9 +624,17 @@ var CoreRouter = class {
603
624
  propertyKey: "__class__"
604
625
  }
605
626
  ]) ?? require_bindings.getStandardMetadata(bindingMetadata, bindingMethod) ?? require_bindings.getStandardMetadata(bindingMetadata, "__class__");
627
+ const tokens = metadata?.tokens?.length ? metadata.tokens : designTokens;
628
+ const pluginArgs = await this.resolvePluginArguments(ctx, {
629
+ target: bindingTarget,
630
+ method: bindingMethod,
631
+ handler: bindingHandler,
632
+ metadata: bindingMetadata,
633
+ tokens,
634
+ designTokens
635
+ });
636
+ if (pluginArgs) return handlerFunction(...pluginArgs);
606
637
  if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
607
- const designTokens = [...bindingTarget ? require_bindings.getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? require_bindings.getDesignParamTypes(bindingHandler) : []];
608
- const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
609
638
  if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
610
639
  const args = [];
611
640
  for (const token of tokens) {
@@ -635,7 +664,11 @@ var CoreRouter = class {
635
664
  clearRequest.query = payload.query;
636
665
  clearRequest.params = payload.params;
637
666
  ctx.clearRequest = clearRequest;
638
- if (!(ctx.clearResponse instanceof require_bindings.Response)) ctx.clearResponse = new require_bindings.Response();
667
+ require_bindings.Container.bind(require_bindings.Request, ctx.clearRequest);
668
+ if (!(ctx.clearResponse instanceof require_bindings.Response)) {
669
+ ctx.clearResponse = new require_bindings.Response(ctx.response ?? ctx.reply ?? ctx.res);
670
+ require_bindings.Container.bind(require_bindings.Response, ctx.clearResponse);
671
+ }
639
672
  if (!instance) return;
640
673
  instance.ctx = ctx;
641
674
  instance.body = payload.body;