clear-router 2.6.2 → 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--8gWGXv-.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-Bu4kNHUo.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-awXi28kb.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-DgZmT-17.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--8gWGXv-.cjs');
3
+ const require_router = require('../router-DJvP-A5M.cjs');
4
4
  require('./index.cjs');
5
5
  require("reflect-metadata");
6
6
 
@@ -1,3 +1,2 @@
1
1
  import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-CNL7bpz5.mjs";
2
- import "reflect-metadata";
3
2
  export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
@@ -1,5 +1,5 @@
1
1
  import { n as Container, t as Bind } from "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-DgZmT-17.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--8gWGXv-.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-Bu4kNHUo.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-awXi28kb.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-DgZmT-17.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--8gWGXv-.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-Bu4kNHUo.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-awXi28kb.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-DgZmT-17.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--8gWGXv-.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-Bu4kNHUo.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-awXi28kb.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-DgZmT-17.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--8gWGXv-.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-Bu4kNHUo.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-awXi28kb.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-DgZmT-17.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,21 +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 = [];
304
326
  /**
305
327
  * Resets the router to it's default state
306
328
  */
307
329
  static reset() {
308
- this.routes = [];
330
+ this.routes.clear();
309
331
  this.prefix = "";
310
332
  this.groupMiddlewares = [];
311
333
  this.globalMiddlewares = [];
312
- this.routesByPathMethod = {};
313
- this.routesByMethod = {};
314
- this.routesByName = {};
334
+ this.routesByPathMethod.clear();
335
+ this.routesByMethod.clear();
336
+ this.routesByName.clear();
315
337
  return this;
316
338
  }
317
339
  static createBaseConfig() {
318
340
  return {
341
+ inferParamName: false,
319
342
  methodOverride: {
320
343
  enabled: true,
321
344
  bodyKeys: ["_method"],
@@ -343,6 +366,7 @@ var CoreRouter = class {
343
366
  const g = globalThis;
344
367
  if (!g[this.defaultConfigKey]) g[this.defaultConfigKey] = this.createBaseConfig();
345
368
  return {
369
+ inferParamName: g[this.defaultConfigKey].inferParamName,
346
370
  methodOverride: { ...g[this.defaultConfigKey].methodOverride },
347
371
  container: { ...g[this.defaultConfigKey].container }
348
372
  };
@@ -374,10 +398,10 @@ var CoreRouter = class {
374
398
  return {
375
399
  config: this.getDefaultConfig(),
376
400
  groupContext: new node_async_hooks.AsyncLocalStorage(),
377
- routes: [],
378
- routesByPathMethod: {},
379
- routesByMethod: {},
380
- routesByName: {},
401
+ routes: /* @__PURE__ */ new Set([]),
402
+ routesByPathMethod: /* @__PURE__ */ new Map(),
403
+ routesByMethod: /* @__PURE__ */ new Map(),
404
+ routesByName: /* @__PURE__ */ new Map(),
381
405
  prefix: "",
382
406
  groupMiddlewares: [],
383
407
  globalMiddlewares: []
@@ -451,17 +475,6 @@ var CoreRouter = class {
451
475
  }
452
476
  };
453
477
  }
454
- static config = {
455
- methodOverride: {
456
- enabled: true,
457
- bodyKeys: ["_method"],
458
- headerKeys: ["x-http-method"]
459
- },
460
- container: {
461
- enabled: false,
462
- autoDiscover: false
463
- }
464
- };
465
478
  static configureDefaults(options) {
466
479
  const g = globalThis;
467
480
  const defaults = this.mergeConfig(g[this.defaultConfigKey] || this.createBaseConfig(), options);
@@ -522,15 +535,6 @@ var CoreRouter = class {
522
535
  if (!pending.length) return;
523
536
  await Promise.all(pending);
524
537
  }
525
- static groupContext = new node_async_hooks.AsyncLocalStorage();
526
- static pluginRequestContext = new node_async_hooks.AsyncLocalStorage();
527
- static routes = [];
528
- static routesByPathMethod = {};
529
- static routesByMethod = {};
530
- static routesByName = {};
531
- static prefix = "";
532
- static groupMiddlewares = [];
533
- static globalMiddlewares = [];
534
538
  static getCurrentPluginRequestContext() {
535
539
  return this.pluginRequestContext.getStore();
536
540
  }
@@ -576,10 +580,10 @@ var CoreRouter = class {
576
580
  headerKeys: ["x-http-method"]
577
581
  } };
578
582
  if (!this.groupContext) this.groupContext = new node_async_hooks.AsyncLocalStorage();
579
- if (!Array.isArray(this.routes)) this.routes = [];
580
- if (!this.routesByPathMethod || typeof this.routesByPathMethod !== "object") this.routesByPathMethod = {};
581
- if (!this.routesByMethod || typeof this.routesByMethod !== "object") this.routesByMethod = {};
582
- 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();
583
587
  if (typeof this.prefix !== "string") this.prefix = "";
584
588
  if (!Array.isArray(this.groupMiddlewares)) this.groupMiddlewares = [];
585
589
  if (!Array.isArray(this.globalMiddlewares)) this.globalMiddlewares = [];
@@ -650,13 +654,15 @@ var CoreRouter = class {
650
654
  if (typeof container.enabled === "boolean") this.config.container.enabled = container.enabled;
651
655
  if (typeof container.autoDiscover === "boolean") this.config.container.autoDiscover = container.autoDiscover;
652
656
  }
657
+ if (options?.inferParamName) this.config.inferParamName = options?.inferParamName;
653
658
  const override = options?.methodOverride;
654
- if (!override) return;
655
- if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
656
- const bodyKeys = override.bodyKeys;
657
- if (typeof bodyKeys !== "undefined") this.config.methodOverride.bodyKeys = (Array.isArray(bodyKeys) ? bodyKeys : [bodyKeys]).map((e) => String(e).trim()).filter(Boolean);
658
- const headerKeys = override.headerKeys;
659
- 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
+ }
660
666
  }
661
667
  static resolveMethodOverride(method, headers, body) {
662
668
  this.ensureState();
@@ -718,16 +724,16 @@ var CoreRouter = class {
718
724
  registrationPaths,
719
725
  parameters,
720
726
  onName: (name, route, previousName) => {
721
- if (previousName && this.routesByName[previousName] === route) delete this.routesByName[previousName];
722
- this.routesByName[name] = route;
727
+ if (previousName && this.routesByName.get(previousName) === route) this.routesByName.delete(previousName);
728
+ this.routesByName.set(name, route);
723
729
  }
724
730
  });
725
- if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, this.createDefaultOptionsHandler());
726
- this.routes.push(route);
731
+ if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
732
+ this.routes.add(route);
727
733
  for (const method of methods.map((m) => m.toUpperCase())) {
728
- this.routesByPathMethod[`${method} ${fullPath}`] = route;
729
- if (!this.routesByMethod[method]) this.routesByMethod[method] = [];
730
- 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);
731
737
  }
732
738
  return route;
733
739
  }
@@ -740,6 +746,11 @@ var CoreRouter = class {
740
746
  * @param options
741
747
  */
742
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
+ }
743
754
  const actions = {
744
755
  index: {
745
756
  method: "get",
@@ -747,7 +758,7 @@ var CoreRouter = class {
747
758
  },
748
759
  show: {
749
760
  method: "get",
750
- path: "/:id"
761
+ path: `/:${paramName}`
751
762
  },
752
763
  create: {
753
764
  method: "post",
@@ -755,11 +766,11 @@ var CoreRouter = class {
755
766
  },
756
767
  update: {
757
768
  method: "put",
758
- path: "/:id"
769
+ path: `/:${paramName}`
759
770
  },
760
771
  destroy: {
761
772
  method: "delete",
762
- path: "/:id"
773
+ path: `/:${paramName}`
763
774
  }
764
775
  };
765
776
  const only = options?.only || Object.keys(actions);
@@ -770,7 +781,8 @@ var CoreRouter = class {
770
781
  if (typeof preController[action] === "function") {
771
782
  const { method, path } = actions[action];
772
783
  const actionMiddlewares = typeof options?.middlewares === "object" && !Array.isArray(options.middlewares) ? options.middlewares[action] : options?.middlewares;
773
- 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());
774
786
  }
775
787
  }
776
788
  }
@@ -889,14 +901,14 @@ var CoreRouter = class {
889
901
  }
890
902
  static allRoutes(type) {
891
903
  this.ensureState();
892
- if (type === "method") return this.routesByMethod;
893
- if (type === "path") return this.routesByPathMethod;
894
- if (type === "name") return this.routesByName;
895
- 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");
896
908
  }
897
909
  static route(name) {
898
910
  this.ensureState();
899
- return this.routesByName[name];
911
+ return this.routesByName.get(name);
900
912
  }
901
913
  static url(name, params) {
902
914
  return this.route(name)?.toPath(params);
@@ -917,6 +929,14 @@ var CoreRouter = class {
917
929
  static setResponseProvider(provider) {
918
930
  this.responseProvider = provider;
919
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
+ }
920
940
  static initializeInstance(provider, args) {
921
941
  const isRequest = [
922
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,19 @@ 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[];
242
260
  /**
243
261
  * Resets the router to it's default state
244
262
  */
@@ -257,17 +275,16 @@ declare abstract class CoreRouter {
257
275
  prefix: string;
258
276
  groupMiddlewares: any[];
259
277
  }>;
260
- routes: Array<Route<any, any, any>>;
261
- routesByPathMethod: Record<string, Route<any, any, any>>;
262
- routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
263
- 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>;
264
282
  prefix: string;
265
283
  groupMiddlewares: any[];
266
284
  globalMiddlewares: any[];
267
285
  };
268
286
  protected static bindStateAccessors(): void;
269
287
  protected static createDefaultOptionsHandler(): any;
270
- static config: RouterConfig;
271
288
  static configureDefaults(options?: RouterConfig): void;
272
289
  /**
273
290
  * Use a registered plugin
@@ -279,18 +296,6 @@ declare abstract class CoreRouter {
279
296
  */
280
297
  static use<Options = any>(plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
281
298
  protected static pluginsReady(): Promise<void>;
282
- protected static groupContext: AsyncLocalStorage<{
283
- prefix: string;
284
- groupMiddlewares: any[];
285
- }>;
286
- protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext>;
287
- static routes: Array<Route<any, any, any>>;
288
- static routesByPathMethod: Record<string, Route<any, any, any>>;
289
- static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
290
- static routesByName: Record<string, Route<any, any, any>>;
291
- static prefix: string;
292
- static groupMiddlewares: any[];
293
- static globalMiddlewares: any[];
294
299
  protected static getCurrentPluginRequestContext(): ClearRouterPluginRequestContext | undefined;
295
300
  protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
296
301
  protected static createPluginBind(): PluginBind;
@@ -455,6 +460,7 @@ declare abstract class CoreRouter {
455
460
  * @param provider
456
461
  */
457
462
  static setResponseProvider(provider: typeof Response$1): void;
463
+ private static hasPackageInstalled;
458
464
  /**
459
465
  * Provide a class that will overide the base Response instance
460
466
  *