clear-router 2.6.1 → 2.6.3

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.
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_bindings = require('../bindings-CLsZjOEy.cjs');
3
- const require_router = require('../router-BJYHBKMY.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
 
5
5
  //#region src/core/plugins.ts
6
6
  function definePlugin(plugin) {
@@ -1,2 +1,2 @@
1
- import { L as Response, a as ClearRouterPluginInput, c as PluginBind, d as PluginSetupResult, f as definePlugin, i as ClearRouterPluginContext, l as PluginBindFactory, n as ClearRouterPlugin, o as ClearRouterPluginRequestContext, p as Request, r as ClearRouterPluginArgumentsContext, s as PluginArgumentsResolver, t as CoreRouter, u as PluginBindValue } from "../router-B90hsOKM.cjs";
1
+ import { L as Response, a as ClearRouterPluginInput, c as PluginBind, d as PluginSetupResult, f as definePlugin, i as ClearRouterPluginContext, l as PluginBindFactory, n as ClearRouterPlugin, o as ClearRouterPluginRequestContext, p as Request, r as ClearRouterPluginArgumentsContext, s as PluginArgumentsResolver, t as CoreRouter, u as PluginBindValue } from "../router-BITqScD_.cjs";
2
2
  export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin };
@@ -1,2 +1,2 @@
1
- import { L as Response, a as ClearRouterPluginInput, c as PluginBind, d as PluginSetupResult, f as definePlugin, i as ClearRouterPluginContext, l as PluginBindFactory, n as ClearRouterPlugin, o as ClearRouterPluginRequestContext, p as Request, r as ClearRouterPluginArgumentsContext, s as PluginArgumentsResolver, t as CoreRouter, u as PluginBindValue } from "../router-BfYAFCqD.mjs";
1
+ import { L as Response, a as ClearRouterPluginInput, c as PluginBind, d as PluginSetupResult, f as definePlugin, i as ClearRouterPluginContext, l as PluginBindFactory, n as ClearRouterPlugin, o as ClearRouterPluginRequestContext, p as Request, r as ClearRouterPluginArgumentsContext, s as PluginArgumentsResolver, t as CoreRouter, u as PluginBindValue } from "../router-Cs8cC5zd.mjs";
2
2
  export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin };
@@ -1,5 +1,5 @@
1
1
  import { c as Request, s as Response } from "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CbrVYD5L.mjs";
2
+ import { t as CoreRouter } from "../router-9HWz4Tlp.mjs";
3
3
 
4
4
  //#region src/core/plugins.ts
5
5
  function definePlugin(plugin) {
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_bindings = require('../bindings-CLsZjOEy.cjs');
3
- const require_router = require('../router-BJYHBKMY.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
  require('./index.cjs');
5
5
  require("reflect-metadata");
6
6
 
@@ -1,5 +1,5 @@
1
1
  import { n as Container, t as Bind } from "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CbrVYD5L.mjs";
2
+ import { t as CoreRouter } from "../router-9HWz4Tlp.mjs";
3
3
  import "./index.mjs";
4
4
  import "reflect-metadata";
5
5
 
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-CLsZjOEy.cjs');
3
- const require_router = require('../router-BJYHBKMY.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
  const require_responses = require('../responses-Bvnk0uvc.cjs');
5
5
 
6
6
  //#region src/express/router.ts
@@ -162,7 +162,7 @@ var Router = class Router extends require_router.CoreRouter {
162
162
  return super.route(name);
163
163
  }
164
164
  static async apply(router) {
165
- for (const route of this.routes) {
165
+ for (const route of Array.from(this.routes)) {
166
166
  let handlerFunction = null;
167
167
  let instance = null;
168
168
  let bindingTarget;
@@ -1,4 +1,4 @@
1
- import { F as ControllerAction, I as HttpMethod, M as HttpContext, N as Middleware, P as ApiResourceMiddleware, j as Handler, m as Route, t as CoreRouter } from "../router-B90hsOKM.cjs";
1
+ import { F as ControllerAction, I as HttpMethod, M as HttpContext, N as Middleware, P as ApiResourceMiddleware, j as Handler, m as Route, t as CoreRouter } from "../router-BITqScD_.cjs";
2
2
  import { Router as Router$1 } from "express";
3
3
 
4
4
  //#region src/express/router.d.ts
@@ -1,4 +1,4 @@
1
- import { F as ControllerAction, I as HttpMethod, M as HttpContext, N as Middleware, P as ApiResourceMiddleware, j as Handler, m as Route, t as CoreRouter } from "../router-BfYAFCqD.mjs";
1
+ import { F as ControllerAction, I as HttpMethod, M as HttpContext, N as Middleware, P as ApiResourceMiddleware, j as Handler, m as Route, t as CoreRouter } from "../router-Cs8cC5zd.mjs";
2
2
  import { Router as Router$1 } from "express";
3
3
 
4
4
  //#region src/express/router.d.ts
@@ -1,5 +1,5 @@
1
1
  import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CbrVYD5L.mjs";
2
+ import { t as CoreRouter } from "../router-9HWz4Tlp.mjs";
3
3
  import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-BvETUeDL.mjs";
4
4
 
5
5
  //#region src/express/router.ts
@@ -161,7 +161,7 @@ var Router = class Router extends CoreRouter {
161
161
  return super.route(name);
162
162
  }
163
163
  static async apply(router) {
164
- for (const route of this.routes) {
164
+ for (const route of Array.from(this.routes)) {
165
165
  let handlerFunction = null;
166
166
  let instance = null;
167
167
  let bindingTarget;
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-CLsZjOEy.cjs');
3
- const require_router = require('../router-BJYHBKMY.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
  const require_responses = require('../responses-Bvnk0uvc.cjs');
5
5
 
6
6
  //#region src/fastify/router.ts
@@ -161,7 +161,7 @@ var Router = class Router extends require_router.CoreRouter {
161
161
  * @returns The Fastify application instance with the applied routes
162
162
  */
163
163
  static apply(app) {
164
- for (const route of this.routes) {
164
+ for (const route of Array.from(this.routes)) {
165
165
  let handlerFunction = null;
166
166
  let instance = null;
167
167
  let bindingTarget;
@@ -1,4 +1,4 @@
1
- import { A as Middleware, D as FastifyApp, F as ControllerAction, I as HttpMethod, O as Handler, P as ApiResourceMiddleware, k as HttpContext, m as Route, t as CoreRouter } from "../router-B90hsOKM.cjs";
1
+ import { A as Middleware, D as FastifyApp, F as ControllerAction, I as HttpMethod, O as Handler, P as ApiResourceMiddleware, k as HttpContext, m as Route, t as CoreRouter } from "../router-BITqScD_.cjs";
2
2
 
3
3
  //#region src/fastify/router.d.ts
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { A as Middleware, D as FastifyApp, F as ControllerAction, I as HttpMethod, O as Handler, P as ApiResourceMiddleware, k as HttpContext, m as Route, t as CoreRouter } from "../router-BfYAFCqD.mjs";
1
+ import { A as Middleware, D as FastifyApp, F as ControllerAction, I as HttpMethod, O as Handler, P as ApiResourceMiddleware, k as HttpContext, m as Route, t as CoreRouter } from "../router-Cs8cC5zd.mjs";
2
2
 
3
3
  //#region src/fastify/router.d.ts
4
4
  /**
@@ -1,5 +1,5 @@
1
1
  import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CbrVYD5L.mjs";
2
+ import { t as CoreRouter } from "../router-9HWz4Tlp.mjs";
3
3
  import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-BvETUeDL.mjs";
4
4
 
5
5
  //#region src/fastify/router.ts
@@ -160,7 +160,7 @@ var Router = class Router extends CoreRouter {
160
160
  * @returns The Fastify application instance with the applied routes
161
161
  */
162
162
  static apply(app) {
163
- for (const route of this.routes) {
163
+ for (const route of Array.from(this.routes)) {
164
164
  let handlerFunction = null;
165
165
  let instance = null;
166
166
  let bindingTarget;
package/dist/h3/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-CLsZjOEy.cjs');
3
- const require_router = require('../router-BJYHBKMY.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
  const require_responses = require('../responses-Bvnk0uvc.cjs');
5
5
  let h3 = require("h3");
6
6
 
@@ -171,7 +171,7 @@ var Router = class Router extends require_router.CoreRouter {
171
171
  * @returns
172
172
  */
173
173
  static apply(app) {
174
- for (const route of this.routes) {
174
+ for (const route of Array.from(this.routes)) {
175
175
  let handlerFunction = null;
176
176
  let instance = null;
177
177
  let bindingTarget;
@@ -1,4 +1,4 @@
1
- import { C as H3App, E as Middleware, F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, T as HttpContext, m as Route, t as CoreRouter, w as Handler } from "../router-B90hsOKM.cjs";
1
+ import { C as H3App, E as Middleware, F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, T as HttpContext, m as Route, t as CoreRouter, w as Handler } from "../router-BITqScD_.cjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
@@ -1,4 +1,4 @@
1
- import { C as H3App, E as Middleware, F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, T as HttpContext, m as Route, t as CoreRouter, w as Handler } from "../router-BfYAFCqD.mjs";
1
+ import { C as H3App, E as Middleware, F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, T as HttpContext, m as Route, t as CoreRouter, w as Handler } from "../router-Cs8cC5zd.mjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
package/dist/h3/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CbrVYD5L.mjs";
2
+ import { t as CoreRouter } from "../router-9HWz4Tlp.mjs";
3
3
  import { n as resolveResponseMeta } from "../responses-BvETUeDL.mjs";
4
4
  import { HTTPResponse, getQuery, getRouterParams, readBody } from "h3";
5
5
 
@@ -170,7 +170,7 @@ var Router = class Router extends CoreRouter {
170
170
  * @returns
171
171
  */
172
172
  static apply(app) {
173
- for (const route of this.routes) {
173
+ for (const route of Array.from(this.routes)) {
174
174
  let handlerFunction = null;
175
175
  let instance = null;
176
176
  let bindingTarget;
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-CLsZjOEy.cjs');
3
- const require_router = require('../router-BJYHBKMY.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
  const require_responses = require('../responses-Bvnk0uvc.cjs');
5
5
 
6
6
  //#region src/hono/router.ts
@@ -174,7 +174,7 @@ var Router = class Router extends require_router.CoreRouter {
174
174
  * @returns The Hono application instance with the applied routes
175
175
  */
176
176
  static apply(app) {
177
- for (const route of this.routes) {
177
+ for (const route of Array.from(this.routes)) {
178
178
  let handlerFunction = null;
179
179
  let instance = null;
180
180
  let bindingTarget;
@@ -1,4 +1,4 @@
1
- import { F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, S as Middleware, b as HonoApp, m as Route, t as CoreRouter, x as HttpContext, y as Handler } from "../router-B90hsOKM.cjs";
1
+ import { F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, S as Middleware, b as HonoApp, m as Route, t as CoreRouter, x as HttpContext, y as Handler } from "../router-BITqScD_.cjs";
2
2
 
3
3
  //#region src/hono/router.d.ts
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, S as Middleware, b as HonoApp, m as Route, t as CoreRouter, x as HttpContext, y as Handler } from "../router-BfYAFCqD.mjs";
1
+ import { F as ControllerAction, I as HttpMethod, P as ApiResourceMiddleware, S as Middleware, b as HonoApp, m as Route, t as CoreRouter, x as HttpContext, y as Handler } from "../router-Cs8cC5zd.mjs";
2
2
 
3
3
  //#region src/hono/router.d.ts
4
4
  /**
@@ -1,5 +1,5 @@
1
1
  import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-CbrVYD5L.mjs";
2
+ import { t as CoreRouter } from "../router-9HWz4Tlp.mjs";
3
3
  import { n as resolveResponseMeta } from "../responses-BvETUeDL.mjs";
4
4
 
5
5
  //#region src/hono/router.ts
@@ -173,7 +173,7 @@ var Router = class Router extends CoreRouter {
173
173
  * @returns The Hono application instance with the applied routes
174
174
  */
175
175
  static apply(app) {
176
- for (const route of this.routes) {
176
+ for (const route of Array.from(this.routes)) {
177
177
  let handlerFunction = null;
178
178
  let instance = null;
179
179
  let bindingTarget;
package/dist/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  let node_async_hooks = require("node:async_hooks");
3
+ let node_module = require("node:module");
3
4
 
4
5
  //#region src/ClearRequest.ts
5
6
  var ClearRequest = class {
@@ -301,8 +302,43 @@ var CoreRouter = class {
301
302
  static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
302
303
  static requestProvider;
303
304
  static responseProvider;
305
+ static config = {
306
+ inferParamName: false,
307
+ methodOverride: {
308
+ enabled: true,
309
+ bodyKeys: ["_method"],
310
+ headerKeys: ["x-http-method"]
311
+ },
312
+ container: {
313
+ enabled: false,
314
+ autoDiscover: false
315
+ }
316
+ };
317
+ static groupContext = new node_async_hooks.AsyncLocalStorage();
318
+ static pluginRequestContext = new node_async_hooks.AsyncLocalStorage();
319
+ static routes = /* @__PURE__ */ new Set([]);
320
+ static routesByPathMethod = /* @__PURE__ */ new Map();
321
+ static routesByMethod = /* @__PURE__ */ new Map();
322
+ static routesByName = /* @__PURE__ */ new Map();
323
+ static prefix = "";
324
+ static groupMiddlewares = [];
325
+ static globalMiddlewares = [];
326
+ /**
327
+ * Resets the router to it's default state
328
+ */
329
+ static reset() {
330
+ this.routes.clear();
331
+ this.prefix = "";
332
+ this.groupMiddlewares = [];
333
+ this.globalMiddlewares = [];
334
+ this.routesByPathMethod.clear();
335
+ this.routesByMethod.clear();
336
+ this.routesByName.clear();
337
+ return this;
338
+ }
304
339
  static createBaseConfig() {
305
340
  return {
341
+ inferParamName: false,
306
342
  methodOverride: {
307
343
  enabled: true,
308
344
  bodyKeys: ["_method"],
@@ -330,6 +366,7 @@ var CoreRouter = class {
330
366
  const g = globalThis;
331
367
  if (!g[this.defaultConfigKey]) g[this.defaultConfigKey] = this.createBaseConfig();
332
368
  return {
369
+ inferParamName: g[this.defaultConfigKey].inferParamName,
333
370
  methodOverride: { ...g[this.defaultConfigKey].methodOverride },
334
371
  container: { ...g[this.defaultConfigKey].container }
335
372
  };
@@ -361,10 +398,10 @@ var CoreRouter = class {
361
398
  return {
362
399
  config: this.getDefaultConfig(),
363
400
  groupContext: new node_async_hooks.AsyncLocalStorage(),
364
- routes: [],
365
- routesByPathMethod: {},
366
- routesByMethod: {},
367
- routesByName: {},
401
+ routes: /* @__PURE__ */ new Set([]),
402
+ routesByPathMethod: /* @__PURE__ */ new Map(),
403
+ routesByMethod: /* @__PURE__ */ new Map(),
404
+ routesByName: /* @__PURE__ */ new Map(),
368
405
  prefix: "",
369
406
  groupMiddlewares: [],
370
407
  globalMiddlewares: []
@@ -438,17 +475,6 @@ var CoreRouter = class {
438
475
  }
439
476
  };
440
477
  }
441
- static config = {
442
- methodOverride: {
443
- enabled: true,
444
- bodyKeys: ["_method"],
445
- headerKeys: ["x-http-method"]
446
- },
447
- container: {
448
- enabled: false,
449
- autoDiscover: false
450
- }
451
- };
452
478
  static configureDefaults(options) {
453
479
  const g = globalThis;
454
480
  const defaults = this.mergeConfig(g[this.defaultConfigKey] || this.createBaseConfig(), options);
@@ -509,15 +535,6 @@ var CoreRouter = class {
509
535
  if (!pending.length) return;
510
536
  await Promise.all(pending);
511
537
  }
512
- static groupContext = new node_async_hooks.AsyncLocalStorage();
513
- static pluginRequestContext = new node_async_hooks.AsyncLocalStorage();
514
- static routes = [];
515
- static routesByPathMethod = {};
516
- static routesByMethod = {};
517
- static routesByName = {};
518
- static prefix = "";
519
- static groupMiddlewares = [];
520
- static globalMiddlewares = [];
521
538
  static getCurrentPluginRequestContext() {
522
539
  return this.pluginRequestContext.getStore();
523
540
  }
@@ -563,10 +580,10 @@ var CoreRouter = class {
563
580
  headerKeys: ["x-http-method"]
564
581
  } };
565
582
  if (!this.groupContext) this.groupContext = new node_async_hooks.AsyncLocalStorage();
566
- if (!Array.isArray(this.routes)) this.routes = [];
567
- if (!this.routesByPathMethod || typeof this.routesByPathMethod !== "object") this.routesByPathMethod = {};
568
- if (!this.routesByMethod || typeof this.routesByMethod !== "object") this.routesByMethod = {};
569
- if (!this.routesByName || typeof this.routesByName !== "object") this.routesByName = {};
583
+ if (!this.routes || Array.isArray(this.routes)) this.routes = new Set(this.routes ?? []);
584
+ if (!this.routesByPathMethod) this.routesByPathMethod = /* @__PURE__ */ new Map();
585
+ if (!this.routesByMethod) this.routesByMethod = /* @__PURE__ */ new Map();
586
+ if (!this.routesByName) this.routesByName = /* @__PURE__ */ new Map();
570
587
  if (typeof this.prefix !== "string") this.prefix = "";
571
588
  if (!Array.isArray(this.groupMiddlewares)) this.groupMiddlewares = [];
572
589
  if (!Array.isArray(this.globalMiddlewares)) this.globalMiddlewares = [];
@@ -637,13 +654,15 @@ var CoreRouter = class {
637
654
  if (typeof container.enabled === "boolean") this.config.container.enabled = container.enabled;
638
655
  if (typeof container.autoDiscover === "boolean") this.config.container.autoDiscover = container.autoDiscover;
639
656
  }
657
+ if (options?.inferParamName) this.config.inferParamName = options?.inferParamName;
640
658
  const override = options?.methodOverride;
641
- if (!override) return;
642
- if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
643
- const bodyKeys = override.bodyKeys;
644
- if (typeof bodyKeys !== "undefined") this.config.methodOverride.bodyKeys = (Array.isArray(bodyKeys) ? bodyKeys : [bodyKeys]).map((e) => String(e).trim()).filter(Boolean);
645
- const headerKeys = override.headerKeys;
646
- if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
659
+ if (override) {
660
+ if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
661
+ const bodyKeys = override.bodyKeys;
662
+ if (typeof bodyKeys !== "undefined") this.config.methodOverride.bodyKeys = (Array.isArray(bodyKeys) ? bodyKeys : [bodyKeys]).map((e) => String(e).trim()).filter(Boolean);
663
+ const headerKeys = override.headerKeys;
664
+ if (typeof headerKeys !== "undefined") this.config.methodOverride.headerKeys = (Array.isArray(headerKeys) ? headerKeys : [headerKeys]).map((e) => String(e).trim().toLowerCase()).filter(Boolean);
665
+ }
647
666
  }
648
667
  static resolveMethodOverride(method, headers, body) {
649
668
  this.ensureState();
@@ -705,16 +724,16 @@ var CoreRouter = class {
705
724
  registrationPaths,
706
725
  parameters,
707
726
  onName: (name, route, previousName) => {
708
- if (previousName && this.routesByName[previousName] === route) delete this.routesByName[previousName];
709
- this.routesByName[name] = route;
727
+ if (previousName && this.routesByName.get(previousName) === route) this.routesByName.delete(previousName);
728
+ this.routesByName.set(name, route);
710
729
  }
711
730
  });
712
- if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, this.createDefaultOptionsHandler());
713
- this.routes.push(route);
731
+ if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
732
+ this.routes.add(route);
714
733
  for (const method of methods.map((m) => m.toUpperCase())) {
715
- this.routesByPathMethod[`${method} ${fullPath}`] = route;
716
- if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
717
- this.routesByMethod[method].push(route);
734
+ this.routesByPathMethod.set(`${method} ${fullPath}`, route);
735
+ if (!this.routesByMethod.has(method)) this.routesByMethod.set(method, []);
736
+ this.routesByMethod.get(method)?.push(route);
718
737
  }
719
738
  return route;
720
739
  }
@@ -727,6 +746,11 @@ var CoreRouter = class {
727
746
  * @param options
728
747
  */
729
748
  static apiResource(basePath, controller, options) {
749
+ let paramName = "id";
750
+ if (!!this.config.inferParamName && this.hasPackageInstalled("@h3ravel/support")) {
751
+ const { str } = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href)("@h3ravel/support");
752
+ paramName = str(basePath).singular().afterLast("/").toString();
753
+ }
730
754
  const actions = {
731
755
  index: {
732
756
  method: "get",
@@ -734,7 +758,7 @@ var CoreRouter = class {
734
758
  },
735
759
  show: {
736
760
  method: "get",
737
- path: "/:id"
761
+ path: `/:${paramName}`
738
762
  },
739
763
  create: {
740
764
  method: "post",
@@ -742,11 +766,11 @@ var CoreRouter = class {
742
766
  },
743
767
  update: {
744
768
  method: "put",
745
- path: "/:id"
769
+ path: `/:${paramName}`
746
770
  },
747
771
  destroy: {
748
772
  method: "delete",
749
- path: "/:id"
773
+ path: `/:${paramName}`
750
774
  }
751
775
  };
752
776
  const only = options?.only || Object.keys(actions);
@@ -757,7 +781,8 @@ var CoreRouter = class {
757
781
  if (typeof preController[action] === "function") {
758
782
  const { method, path } = actions[action];
759
783
  const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
760
- this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0);
784
+ const name = `${basePath}${path}`.replace(/\{(\w+):[^}]+\}/g, "$1").replace(/\/|:|[{}]/g, ".").replace(/\.{2,}/g, ".").replace(/^\.|\.$/g, "");
785
+ this.add(method, `${basePath}${path}`, [controller, action], Array.isArray(actionMiddlewares) ? actionMiddlewares : actionMiddlewares ? [actionMiddlewares] : void 0).name(name + "." + action.toLowerCase());
761
786
  }
762
787
  }
763
788
  }
@@ -876,14 +901,14 @@ var CoreRouter = class {
876
901
  }
877
902
  static allRoutes(type) {
878
903
  this.ensureState();
879
- if (type === "method") return this.routesByMethod;
880
- if (type === "path") return this.routesByPathMethod;
881
- if (type === "name") return this.routesByName;
882
- return this.routes.filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
904
+ if (type === "method") return Object.fromEntries(this.routesByMethod.entries());
905
+ if (type === "path") return Object.fromEntries(this.routesByPathMethod.entries());
906
+ if (type === "name") return Object.fromEntries(this.routesByName.entries());
907
+ return Array.from(this.routes).filter((e) => e.methods.length > 1 || e.methods[0] !== "options");
883
908
  }
884
909
  static route(name) {
885
910
  this.ensureState();
886
- return this.routesByName[name];
911
+ return this.routesByName.get(name);
887
912
  }
888
913
  static url(name, params) {
889
914
  return this.route(name)?.toPath(params);
@@ -904,6 +929,14 @@ var CoreRouter = class {
904
929
  static setResponseProvider(provider) {
905
930
  this.responseProvider = provider;
906
931
  }
932
+ static hasPackageInstalled(name) {
933
+ try {
934
+ (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href).resolve(name, { paths: [process.cwd()] });
935
+ return true;
936
+ } catch {
937
+ return false;
938
+ }
939
+ }
907
940
  static initializeInstance(provider, args) {
908
941
  const isRequest = [
909
942
  "CoreRequest",
package/dist/index.d.cts CHANGED
@@ -20,6 +20,11 @@ type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
20
20
  type RequestData = Record<string, any>;
21
21
  type ApiResourceMiddleware<M = any> = M | M[] | { [K in ControllerAction]?: M | M[] };
22
22
  interface RouterConfig {
23
+ /**
24
+ * When enabled, API param name will be infered from the route path.
25
+ * So instead of getting /api/users/:id, we will now get /api/users/:user
26
+ */
27
+ inferParamName?: boolean;
23
28
  /**
24
29
  * Configuration for method override functionality, allowing clients to use a
25
30
  * specific header or body parameter to override the HTTP method.
@@ -239,6 +244,23 @@ declare abstract class CoreRouter {
239
244
  private static readonly pluginArgumentResolversKey;
240
245
  private static requestProvider?;
241
246
  private static responseProvider?;
247
+ static config: RouterConfig;
248
+ protected static groupContext: AsyncLocalStorage<{
249
+ prefix: string;
250
+ groupMiddlewares: any[];
251
+ }>;
252
+ protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext>;
253
+ static routes: Set<Route<any, any, any>>;
254
+ static routesByPathMethod: Map<string, Route<any, any, any>>;
255
+ static routesByMethod: Map<"GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD", Route<any, any, any>[]>;
256
+ static routesByName: Map<string, Route<any, any, any>>;
257
+ static prefix: string;
258
+ static groupMiddlewares: any[];
259
+ static globalMiddlewares: any[];
260
+ /**
261
+ * Resets the router to it's default state
262
+ */
263
+ static reset(): typeof CoreRouter;
242
264
  protected static createBaseConfig(): RouterConfig;
243
265
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
244
266
  protected static getDefaultConfig(): RouterConfig;
@@ -253,17 +275,16 @@ declare abstract class CoreRouter {
253
275
  prefix: string;
254
276
  groupMiddlewares: any[];
255
277
  }>;
256
- routes: Array<Route<any, any, any>>;
257
- routesByPathMethod: Record<string, Route<any, any, any>>;
258
- routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
259
- routesByName: Record<string, Route<any, any, any>>;
278
+ routes: Set<never>;
279
+ routesByPathMethod: Map<any, any>;
280
+ routesByMethod: Map<any, any>;
281
+ routesByName: Map<any, any>;
260
282
  prefix: string;
261
283
  groupMiddlewares: any[];
262
284
  globalMiddlewares: any[];
263
285
  };
264
286
  protected static bindStateAccessors(): void;
265
287
  protected static createDefaultOptionsHandler(): any;
266
- static config: RouterConfig;
267
288
  static configureDefaults(options?: RouterConfig): void;
268
289
  /**
269
290
  * Use a registered plugin
@@ -275,18 +296,6 @@ declare abstract class CoreRouter {
275
296
  */
276
297
  static use<Options = any>(plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
277
298
  protected static pluginsReady(): Promise<void>;
278
- protected static groupContext: AsyncLocalStorage<{
279
- prefix: string;
280
- groupMiddlewares: any[];
281
- }>;
282
- protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext>;
283
- static routes: Array<Route<any, any, any>>;
284
- static routesByPathMethod: Record<string, Route<any, any, any>>;
285
- static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
286
- static routesByName: Record<string, Route<any, any, any>>;
287
- static prefix: string;
288
- static groupMiddlewares: any[];
289
- static globalMiddlewares: any[];
290
299
  protected static getCurrentPluginRequestContext(): ClearRouterPluginRequestContext | undefined;
291
300
  protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
292
301
  protected static createPluginBind(): PluginBind;
@@ -451,6 +460,7 @@ declare abstract class CoreRouter {
451
460
  * @param provider
452
461
  */
453
462
  static setResponseProvider(provider: typeof Response$1): void;
463
+ private static hasPackageInstalled;
454
464
  /**
455
465
  * Provide a class that will overide the base Response instance
456
466
  *