clear-router 2.5.6 → 2.5.8

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 (41) hide show
  1. package/dist/{bindings-DIanvIVd.mjs → bindings-DJDdQv1q.mjs} +1 -1
  2. package/dist/{bindings-DvV2DXWi.cjs → bindings-T0FKpWyi.cjs} +6 -0
  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 +94 -32
  28. package/dist/index.d.cts +23 -4
  29. package/dist/index.d.mts +23 -4
  30. package/dist/index.mjs +94 -32
  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-_II3dOJ5.mjs → responses-B184V5nj.mjs} +1 -1
  36. package/dist/{responses-JzXstGU5.cjs → responses-CfCmLLo9.cjs} +1 -1
  37. package/dist/{router-DCMtQ_Xi.d.mts → router-B8l8jerS.d.mts} +23 -4
  38. package/dist/{router-BYZmNzrZ.d.cts → router-CiGcLUZD.d.cts} +23 -4
  39. package/dist/{router-CU4V1kX0.cjs → router-DbPFGTYG.cjs} +95 -33
  40. package/dist/{router-B3QjblRX.mjs → router-we2Hw9nA.mjs} +95 -33
  41. package/package.json +5 -5
@@ -1,4 +1,4 @@
1
- import { a as getStandardMetadata, i as getDesignParamTypes, n as Container, o as Response, r as getBindingMetadataFromTargets, s as Request } from "./bindings-DIanvIVd.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-DJDdQv1q.mjs";
2
2
  import { AsyncLocalStorage } from "node:async_hooks";
3
3
 
4
4
  //#region src/Route.ts
@@ -42,6 +42,7 @@ var CoreRouter = class {
42
42
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
43
43
  static defaultConfigKey = Symbol.for("clear-router:default-config");
44
44
  static pluginStoreKey = Symbol.for("clear-router:plugins");
45
+ static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
45
46
  static createBaseConfig() {
46
47
  return {
47
48
  methodOverride: {
@@ -88,6 +89,11 @@ var CoreRouter = class {
88
89
  if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
89
90
  return g[this.pluginStoreKey];
90
91
  }
92
+ static getPluginPendingStore() {
93
+ const g = globalThis;
94
+ if (!g[this.pluginPendingKey]) g[this.pluginPendingKey] = /* @__PURE__ */ new Set();
95
+ return g[this.pluginPendingKey];
96
+ }
91
97
  static createDefaultState() {
92
98
  return {
93
99
  config: this.getDefaultConfig(),
@@ -193,28 +199,81 @@ var CoreRouter = class {
193
199
  * @param options
194
200
  * @returns
195
201
  */
196
- static use(plugin, options) {
202
+ static async use(plugin, options) {
197
203
  const name = typeof plugin === "function" ? plugin.name : plugin.name;
198
204
  const store = this.getPluginStore();
199
205
  if (name && store.has(name)) return;
200
- const ctx = {
201
- container: Container,
202
- bind: Container.bind.bind(Container),
203
- configure: this.configure.bind(this),
204
- configureDefaults: this.configureDefaults.bind(this),
205
- options
206
- };
207
- if (typeof plugin === "function") plugin(ctx);
208
- else plugin.setup(ctx);
209
206
  if (name) store.add(name);
207
+ const setup = async () => {
208
+ const ctx = {
209
+ container: Container,
210
+ bind: this.createPluginBind(),
211
+ configure: this.configure.bind(this),
212
+ configureDefaults: this.configureDefaults.bind(this),
213
+ get request() {
214
+ return this.getRequest();
215
+ },
216
+ get response() {
217
+ return this.getResponse();
218
+ },
219
+ getRequest: () => this.getCurrentPluginRequestContext()?.request,
220
+ getResponse: () => this.getCurrentPluginRequestContext()?.response,
221
+ options
222
+ };
223
+ if (typeof plugin === "function") await plugin(ctx);
224
+ else await plugin.setup(ctx);
225
+ };
226
+ const pending = this.getPluginPendingStore();
227
+ const promise = setup();
228
+ pending.add(promise);
229
+ try {
230
+ await promise;
231
+ } catch (error) {
232
+ if (name) store.delete(name);
233
+ throw error;
234
+ } finally {
235
+ pending.delete(promise);
236
+ }
237
+ }
238
+ static async pluginsReady() {
239
+ const pending = Array.from(this.getPluginPendingStore());
240
+ if (!pending.length) return;
241
+ await Promise.all(pending);
210
242
  }
211
243
  static groupContext = new AsyncLocalStorage();
244
+ static pluginRequestContext = new AsyncLocalStorage();
212
245
  static routes = [];
213
246
  static routesByPathMethod = {};
214
247
  static routesByMethod = {};
215
248
  static prefix = "";
216
249
  static groupMiddlewares = [];
217
250
  static globalMiddlewares = [];
251
+ static getCurrentPluginRequestContext() {
252
+ return this.pluginRequestContext.getStore();
253
+ }
254
+ static createPluginRequestContext(ctx) {
255
+ const request = ctx.clearRequest;
256
+ const response = ctx.clearResponse;
257
+ return {
258
+ ...ctx,
259
+ ctx,
260
+ request,
261
+ response,
262
+ clearRequest: request,
263
+ clearResponse: response
264
+ };
265
+ }
266
+ static createPluginBind() {
267
+ const bind = (token, value) => {
268
+ if (typeof value === "function" && !isClass(value)) {
269
+ const factory = value;
270
+ Container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)));
271
+ return;
272
+ }
273
+ Container.bind(token, value);
274
+ };
275
+ return bind;
276
+ }
218
277
  static ensureState() {
219
278
  this.bindStateAccessors();
220
279
  if (!this.config) this.config = { methodOverride: {
@@ -529,29 +588,32 @@ var CoreRouter = class {
529
588
  };
530
589
  }
531
590
  static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
532
- if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
533
- const metadata = getBindingMetadataFromTargets([
534
- {
535
- target: bindingTarget,
536
- propertyKey: bindingMethod
537
- },
538
- { target: bindingHandler },
539
- {
540
- target: bindingTarget,
541
- propertyKey: "__class__"
591
+ return this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
592
+ await this.pluginsReady();
593
+ if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
594
+ const metadata = getBindingMetadataFromTargets([
595
+ {
596
+ target: bindingTarget,
597
+ propertyKey: bindingMethod
598
+ },
599
+ { target: bindingHandler },
600
+ {
601
+ target: bindingTarget,
602
+ propertyKey: "__class__"
603
+ }
604
+ ]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
605
+ if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
606
+ const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
607
+ const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
608
+ if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
609
+ const args = [];
610
+ for (const token of tokens) {
611
+ const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
612
+ if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
613
+ args.push(resolved);
542
614
  }
543
- ]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
544
- if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
545
- const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
546
- const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
547
- if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
548
- const args = [];
549
- for (const token of tokens) {
550
- const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
551
- if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
552
- args.push(resolved);
553
- }
554
- return handlerFunction(...args);
615
+ return handlerFunction(...args);
616
+ });
555
617
  }
556
618
  static bindRequestToInstance(ctx, instance, route, payload) {
557
619
  const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : new Request({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-router",
3
- "version": "2.5.6",
3
+ "version": "2.5.8",
4
4
  "description": "Laravel-style routing for Node.js with support for Express, H3, Fastify, Hono, and Koa, including CommonJS, ESM, and TypeScript support.",
5
5
  "keywords": [
6
6
  "h3",
@@ -121,7 +121,7 @@
121
121
  "@types/koa": "^3.0.2",
122
122
  "@types/koa__router": "^12.0.5",
123
123
  "@types/node": "^20.10.6",
124
- "@vitest/coverage-v8": "4.0.18",
124
+ "@vitest/coverage-v8": "4.1.5",
125
125
  "eslint": "^10.0.2",
126
126
  "fastify": "^5.8.2",
127
127
  "hono": "^4.12.8",
@@ -133,9 +133,9 @@
133
133
  "typescript": "^5.3.3",
134
134
  "typescript-eslint": "^8.56.1",
135
135
  "unrun": "0.2.27",
136
- "vite-tsconfig-paths": "^6.1.1",
137
- "vitepress": "^1.6.4",
138
- "vitest": "^4.0.18"
136
+ "vite": "^8.0.11",
137
+ "vitepress": "^2.0.0-alpha.17",
138
+ "vitest": "^4.1.5"
139
139
  },
140
140
  "engines": {
141
141
  "node": ">=20.0.0"