clear-router 2.5.5 → 2.5.6

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/README.md +1 -0
  2. package/dist/core/index.cjs +9 -2
  3. package/dist/core/index.d.cts +2 -2
  4. package/dist/core/index.d.mts +2 -2
  5. package/dist/core/index.mjs +8 -2
  6. package/dist/decorators/index.d.cts +1 -1
  7. package/dist/decorators/index.d.mts +1 -1
  8. package/dist/decorators/setup.cjs +1 -1
  9. package/dist/decorators/setup.d.cts +1 -1
  10. package/dist/decorators/setup.d.mts +1 -1
  11. package/dist/decorators/setup.mjs +1 -1
  12. package/dist/express/index.cjs +1 -1
  13. package/dist/express/index.d.cts +1 -1
  14. package/dist/express/index.d.mts +1 -1
  15. package/dist/express/index.mjs +1 -1
  16. package/dist/fastify/index.cjs +1 -1
  17. package/dist/fastify/index.d.cts +1 -1
  18. package/dist/fastify/index.d.mts +1 -1
  19. package/dist/fastify/index.mjs +1 -1
  20. package/dist/h3/index.cjs +1 -1
  21. package/dist/h3/index.d.cts +1 -1
  22. package/dist/h3/index.d.mts +1 -1
  23. package/dist/h3/index.mjs +1 -1
  24. package/dist/hono/index.cjs +1 -1
  25. package/dist/hono/index.d.cts +1 -1
  26. package/dist/hono/index.d.mts +1 -1
  27. package/dist/hono/index.mjs +1 -1
  28. package/dist/index.cjs +37 -1
  29. package/dist/index.d.cts +43 -1
  30. package/dist/index.d.mts +43 -1
  31. package/dist/index.mjs +36 -1
  32. package/dist/koa/index.cjs +1 -1
  33. package/dist/koa/index.d.cts +1 -1
  34. package/dist/koa/index.d.mts +1 -1
  35. package/dist/koa/index.mjs +1 -1
  36. package/dist/{router-BAWdklDQ.mjs → router-B3QjblRX.mjs} +29 -0
  37. package/dist/{router-BdfAh1me.d.mts → router-BYZmNzrZ.d.cts} +32 -3
  38. package/dist/{router-CCUF5FE7.cjs → router-CU4V1kX0.cjs} +29 -0
  39. package/dist/{router-78fDk87B.d.cts → router-DCMtQ_Xi.d.mts} +32 -3
  40. package/package.json +1 -1
  41. /package/dist/{bindings-BL1d0dFY.d.cts → bindings-CV1e5jho.d.mts} +0 -0
  42. /package/dist/{bindings-DqvZ9-YW.d.mts → bindings-NV0CdqGl.d.cts} +0 -0
package/README.md CHANGED
@@ -51,6 +51,7 @@ yarn add clear-router express
51
51
  - `clearRequest` is passed as second handler argument for controller handlers
52
52
  - Route handlers can return response values directly across Express, Fastify, Hono, H3, and Koa
53
53
  - Optional decorated container binding for controller method arguments
54
+ - Plugin API for registering container bindings from external packages
54
55
  - Supports TS 5.2+ standard decorators with explicit `@Bind(...)` tokens
55
56
  - Optional `clear-router/decorators/setup` entry imports `reflect-metadata` and enables container binding defaults
56
57
  - Auto-binds controller methods
@@ -1,7 +1,14 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_bindings = require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
 
5
+ //#region src/core/plugins.ts
6
+ function definePlugin(plugin) {
7
+ return plugin;
8
+ }
9
+
10
+ //#endregion
5
11
  exports.CoreRouter = require_router.CoreRouter;
6
12
  exports.Request = require_bindings.Request;
7
- exports.Response = require_bindings.Response;
13
+ exports.Response = require_bindings.Response;
14
+ exports.definePlugin = definePlugin;
@@ -1,2 +1,2 @@
1
- import { h as Response, n as Request, t as CoreRouter } from "../router-78fDk87B.cjs";
2
- export { CoreRouter, Request, Response };
1
+ import { a as PluginBind, c as Request, i as ClearRouterPluginInput, n as ClearRouterPlugin, o as PluginSetupResult, r as ClearRouterPluginContext, s as definePlugin, t as CoreRouter, x as Response } from "../router-BYZmNzrZ.cjs";
2
+ export { ClearRouterPlugin, ClearRouterPluginContext, ClearRouterPluginInput, CoreRouter, PluginBind, PluginSetupResult, Request, Response, definePlugin };
@@ -1,2 +1,2 @@
1
- import { h as Response, n as Request, t as CoreRouter } from "../router-BdfAh1me.mjs";
2
- export { CoreRouter, Request, Response };
1
+ import { a as PluginBind, c as Request, i as ClearRouterPluginInput, n as ClearRouterPlugin, o as PluginSetupResult, r as ClearRouterPluginContext, s as definePlugin, t as CoreRouter, x as Response } from "../router-DCMtQ_Xi.mjs";
2
+ export { ClearRouterPlugin, ClearRouterPluginContext, ClearRouterPluginInput, CoreRouter, PluginBind, PluginSetupResult, Request, Response, definePlugin };
@@ -1,4 +1,10 @@
1
1
  import { o as Response, s as Request } from "../bindings-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
3
 
4
- export { CoreRouter, Request, Response };
4
+ //#region src/core/plugins.ts
5
+ function definePlugin(plugin) {
6
+ return plugin;
7
+ }
8
+
9
+ //#endregion
10
+ export { CoreRouter, Request, Response, definePlugin };
@@ -1,2 +1,2 @@
1
- import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-BL1d0dFY.cjs";
1
+ import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-NV0CdqGl.cjs";
2
2
  export { Bind, type BindDecorator, type BindFactory, type BindToken, type BindValue, Container };
@@ -1,2 +1,2 @@
1
- import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-DqvZ9-YW.mjs";
1
+ import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-CV1e5jho.mjs";
2
2
  export { Bind, type BindDecorator, type BindFactory, type BindToken, type BindValue, Container };
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_bindings = require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
  require('./index.cjs');
5
5
  require("reflect-metadata");
6
6
 
@@ -1,2 +1,2 @@
1
- import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-BL1d0dFY.cjs";
1
+ import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-NV0CdqGl.cjs";
2
2
  export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
@@ -1,2 +1,2 @@
1
- import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-DqvZ9-YW.mjs";
1
+ import { a as BindValue, i as BindToken, n as BindDecorator, o as Container, r as BindFactory, t as Bind } from "../bindings-CV1e5jho.mjs";
2
2
  export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
@@ -1,5 +1,5 @@
1
1
  import { n as Container, t as Bind } from "../bindings-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.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-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
  const require_responses = require('../responses-JzXstGU5.cjs');
5
5
 
6
6
  //#region src/express/router.ts
@@ -1,4 +1,4 @@
1
- import { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-78fDk87B.cjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, g as Middleware, h as HttpContext, l as Route, m as Handler, t as CoreRouter, v as ControllerAction } from "../router-BYZmNzrZ.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 { c as Handler, d as ApiResourceMiddleware, f as ControllerAction, l as HttpContext, m as HttpMethod, r as Route, t as CoreRouter, u as Middleware } from "../router-BdfAh1me.mjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, g as Middleware, h as HttpContext, l as Route, m as Handler, t as CoreRouter, v as ControllerAction } from "../router-DCMtQ_Xi.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-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
3
  import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-_II3dOJ5.mjs";
4
4
 
5
5
  //#region src/express/router.ts
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
  const require_responses = require('../responses-JzXstGU5.cjs');
5
5
 
6
6
  //#region src/fastify/router.ts
@@ -1,4 +1,4 @@
1
- import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-78fDk87B.cjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-BYZmNzrZ.cjs";
2
2
  import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3
3
 
4
4
  //#region types/fastify.d.ts
@@ -1,4 +1,4 @@
1
- import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-BdfAh1me.mjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-DCMtQ_Xi.mjs";
2
2
  import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3
3
 
4
4
  //#region types/fastify.d.ts
@@ -1,5 +1,5 @@
1
1
  import "../bindings-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
3
  import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-_II3dOJ5.mjs";
4
4
 
5
5
  //#region src/fastify/router.ts
package/dist/h3/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
  const require_responses = require('../responses-JzXstGU5.cjs');
5
5
  let h3 = require("h3");
6
6
 
@@ -1,4 +1,4 @@
1
- import { a as Handler, d as ApiResourceMiddleware, f as ControllerAction, i as H3App, m as HttpMethod, o as HttpContext, r as Route, s as Middleware, t as CoreRouter } from "../router-78fDk87B.cjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, d as Handler, f as HttpContext, l as Route, p as Middleware, t as CoreRouter, u as H3App, v as ControllerAction } from "../router-BYZmNzrZ.cjs";
2
2
  import { H3 } from "h3";
3
3
 
4
4
  //#region src/h3/router.d.ts
@@ -1,4 +1,4 @@
1
- import { a as Handler, d as ApiResourceMiddleware, f as ControllerAction, i as H3App, m as HttpMethod, o as HttpContext, r as Route, s as Middleware, t as CoreRouter } from "../router-BdfAh1me.mjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, d as Handler, f as HttpContext, l as Route, p as Middleware, t as CoreRouter, u as H3App, v as ControllerAction } from "../router-DCMtQ_Xi.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-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
3
  import { n as resolveResponseMeta } from "../responses-_II3dOJ5.mjs";
4
4
  import { HTTPResponse, getQuery, getRouterParams, readBody } from "h3";
5
5
 
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
  const require_responses = require('../responses-JzXstGU5.cjs');
5
5
 
6
6
  //#region src/hono/router.ts
@@ -1,4 +1,4 @@
1
- import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-78fDk87B.cjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-BYZmNzrZ.cjs";
2
2
  import { Context, HonoRequest, MiddlewareHandler } from "hono";
3
3
 
4
4
  //#region types/hono.d.ts
@@ -1,4 +1,4 @@
1
- import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-BdfAh1me.mjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-DCMtQ_Xi.mjs";
2
2
  import { Context, HonoRequest, MiddlewareHandler } from "hono";
3
3
 
4
4
  //#region types/hono.d.ts
@@ -1,5 +1,5 @@
1
1
  import "../bindings-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
3
  import { n as resolveResponseMeta } from "../responses-_II3dOJ5.mjs";
4
4
 
5
5
  //#region src/hono/router.ts
package/dist/index.cjs CHANGED
@@ -141,6 +141,12 @@ var Response = class {
141
141
  }
142
142
  };
143
143
 
144
+ //#endregion
145
+ //#region src/core/plugins.ts
146
+ function definePlugin(plugin) {
147
+ return plugin;
148
+ }
149
+
144
150
  //#endregion
145
151
  //#region src/core/bindings.ts
146
152
  const metadataKey = Symbol.for("clear-router:binding-metadata");
@@ -212,6 +218,7 @@ var CoreRouter = class {
212
218
  static stateStoreKey = Symbol.for("clear-router:router-state");
213
219
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
214
220
  static defaultConfigKey = Symbol.for("clear-router:default-config");
221
+ static pluginStoreKey = Symbol.for("clear-router:plugins");
215
222
  static createBaseConfig() {
216
223
  return {
217
224
  methodOverride: {
@@ -253,6 +260,11 @@ var CoreRouter = class {
253
260
  if (!g[this.stateStoreKey]) g[this.stateStoreKey] = Object.create(null);
254
261
  return g[this.stateStoreKey];
255
262
  }
263
+ static getPluginStore() {
264
+ const g = globalThis;
265
+ if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
266
+ return g[this.pluginStoreKey];
267
+ }
256
268
  static createDefaultState() {
257
269
  return {
258
270
  config: this.getDefaultConfig(),
@@ -350,6 +362,29 @@ var CoreRouter = class {
350
362
  const store = this.getStateStore();
351
363
  for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
352
364
  }
365
+ /**
366
+ * Use a registered plugin
367
+ *
368
+ * @param this
369
+ * @param plugin
370
+ * @param options
371
+ * @returns
372
+ */
373
+ static use(plugin, options) {
374
+ const name = typeof plugin === "function" ? plugin.name : plugin.name;
375
+ const store = this.getPluginStore();
376
+ if (name && store.has(name)) return;
377
+ const ctx = {
378
+ container: Container,
379
+ bind: Container.bind.bind(Container),
380
+ configure: this.configure.bind(this),
381
+ configureDefaults: this.configureDefaults.bind(this),
382
+ options
383
+ };
384
+ if (typeof plugin === "function") plugin(ctx);
385
+ else plugin.setup(ctx);
386
+ if (name) store.add(name);
387
+ }
353
388
  static groupContext = new node_async_hooks.AsyncLocalStorage();
354
389
  static routes = [];
355
390
  static routesByPathMethod = {};
@@ -730,4 +765,5 @@ exports.Controller = Controller;
730
765
  exports.CoreRouter = CoreRouter;
731
766
  exports.Request = Request;
732
767
  exports.Response = Response;
733
- exports.Route = Route;
768
+ exports.Route = Route;
769
+ exports.definePlugin = definePlugin;
package/dist/index.d.cts CHANGED
@@ -128,6 +128,37 @@ declare abstract class Controller<X = any> {
128
128
  clearRequest: ClearRequest;
129
129
  }
130
130
  //#endregion
131
+ //#region src/core/bindings.d.ts
132
+ type BindToken<T = any> = abstract new (...args: any[]) => T;
133
+ type BindFactory<T = any> = (ctx: any) => T | Promise<T>;
134
+ type BindValue<T = any> = T | BindFactory<T> | BindToken<T>;
135
+ declare class Container {
136
+ private static readonly registry;
137
+ static bind<T>(token: BindToken<T>, value: BindValue<T>): void;
138
+ static unbind<T>(token: BindToken<T>): void;
139
+ static clear(): void;
140
+ static has<T>(token: BindToken<T>): boolean;
141
+ static resolve<T>(token: BindToken<T>, ctx: any, autoDiscover?: boolean): Promise<T | undefined>;
142
+ private static resolveBinding;
143
+ }
144
+ //#endregion
145
+ //#region src/core/plugins.d.ts
146
+ type PluginSetupResult = void;
147
+ type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
148
+ interface ClearRouterPluginContext<Options = any> {
149
+ container: typeof Container;
150
+ bind: PluginBind;
151
+ configure: (options: RouterConfig) => void;
152
+ configureDefaults: (options: RouterConfig) => void;
153
+ options: Options;
154
+ }
155
+ interface ClearRouterPlugin<Options = any> {
156
+ name?: string;
157
+ setup: (ctx: ClearRouterPluginContext<Options>) => PluginSetupResult;
158
+ }
159
+ type ClearRouterPluginInput<Options = any> = ClearRouterPlugin<Options> | ((ctx: ClearRouterPluginContext<Options>) => PluginSetupResult);
160
+ declare function definePlugin<Options = any>(plugin: ClearRouterPlugin<Options>): ClearRouterPlugin<Options>;
161
+ //#endregion
131
162
  //#region src/core/router.d.ts
132
163
  /**
133
164
  * @class clear-router CoreRouter
@@ -140,11 +171,13 @@ declare abstract class CoreRouter {
140
171
  private static readonly stateStoreKey;
141
172
  private static readonly stateBoundKey;
142
173
  private static readonly defaultConfigKey;
174
+ private static readonly pluginStoreKey;
143
175
  protected static createBaseConfig(): RouterConfig;
144
176
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
145
177
  protected static getDefaultConfig(): RouterConfig;
146
178
  protected static resolveStateNamespace(this: any): string;
147
179
  protected static getStateStore(): Record<string, any>;
180
+ protected static getPluginStore(): Set<string>;
148
181
  protected static createDefaultState(): {
149
182
  config: RouterConfig;
150
183
  groupContext: AsyncLocalStorage<{
@@ -162,6 +195,15 @@ declare abstract class CoreRouter {
162
195
  protected static createDefaultOptionsHandler(): any;
163
196
  static config: RouterConfig;
164
197
  static configureDefaults(this: any, options?: RouterConfig): void;
198
+ /**
199
+ * Use a registered plugin
200
+ *
201
+ * @param this
202
+ * @param plugin
203
+ * @param options
204
+ * @returns
205
+ */
206
+ static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
165
207
  protected static groupContext: AsyncLocalStorage<{
166
208
  prefix: string;
167
209
  groupMiddlewares: any[];
@@ -327,4 +369,4 @@ declare abstract class CoreRouter {
327
369
  }): void;
328
370
  }
329
371
  //#endregion
330
- export { ClearRequest, Controller, CoreRouter, Request, Response$1 as Response, Route };
372
+ export { ClearRequest, ClearRouterPlugin, ClearRouterPluginContext, ClearRouterPluginInput, Controller, CoreRouter, PluginBind, PluginSetupResult, Request, Response$1 as Response, Route, definePlugin };
package/dist/index.d.mts CHANGED
@@ -128,6 +128,37 @@ declare abstract class Controller<X = any> {
128
128
  clearRequest: ClearRequest;
129
129
  }
130
130
  //#endregion
131
+ //#region src/core/bindings.d.ts
132
+ type BindToken<T = any> = abstract new (...args: any[]) => T;
133
+ type BindFactory<T = any> = (ctx: any) => T | Promise<T>;
134
+ type BindValue<T = any> = T | BindFactory<T> | BindToken<T>;
135
+ declare class Container {
136
+ private static readonly registry;
137
+ static bind<T>(token: BindToken<T>, value: BindValue<T>): void;
138
+ static unbind<T>(token: BindToken<T>): void;
139
+ static clear(): void;
140
+ static has<T>(token: BindToken<T>): boolean;
141
+ static resolve<T>(token: BindToken<T>, ctx: any, autoDiscover?: boolean): Promise<T | undefined>;
142
+ private static resolveBinding;
143
+ }
144
+ //#endregion
145
+ //#region src/core/plugins.d.ts
146
+ type PluginSetupResult = void;
147
+ type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
148
+ interface ClearRouterPluginContext<Options = any> {
149
+ container: typeof Container;
150
+ bind: PluginBind;
151
+ configure: (options: RouterConfig) => void;
152
+ configureDefaults: (options: RouterConfig) => void;
153
+ options: Options;
154
+ }
155
+ interface ClearRouterPlugin<Options = any> {
156
+ name?: string;
157
+ setup: (ctx: ClearRouterPluginContext<Options>) => PluginSetupResult;
158
+ }
159
+ type ClearRouterPluginInput<Options = any> = ClearRouterPlugin<Options> | ((ctx: ClearRouterPluginContext<Options>) => PluginSetupResult);
160
+ declare function definePlugin<Options = any>(plugin: ClearRouterPlugin<Options>): ClearRouterPlugin<Options>;
161
+ //#endregion
131
162
  //#region src/core/router.d.ts
132
163
  /**
133
164
  * @class clear-router CoreRouter
@@ -140,11 +171,13 @@ declare abstract class CoreRouter {
140
171
  private static readonly stateStoreKey;
141
172
  private static readonly stateBoundKey;
142
173
  private static readonly defaultConfigKey;
174
+ private static readonly pluginStoreKey;
143
175
  protected static createBaseConfig(): RouterConfig;
144
176
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
145
177
  protected static getDefaultConfig(): RouterConfig;
146
178
  protected static resolveStateNamespace(this: any): string;
147
179
  protected static getStateStore(): Record<string, any>;
180
+ protected static getPluginStore(): Set<string>;
148
181
  protected static createDefaultState(): {
149
182
  config: RouterConfig;
150
183
  groupContext: AsyncLocalStorage<{
@@ -162,6 +195,15 @@ declare abstract class CoreRouter {
162
195
  protected static createDefaultOptionsHandler(): any;
163
196
  static config: RouterConfig;
164
197
  static configureDefaults(this: any, options?: RouterConfig): void;
198
+ /**
199
+ * Use a registered plugin
200
+ *
201
+ * @param this
202
+ * @param plugin
203
+ * @param options
204
+ * @returns
205
+ */
206
+ static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
165
207
  protected static groupContext: AsyncLocalStorage<{
166
208
  prefix: string;
167
209
  groupMiddlewares: any[];
@@ -327,4 +369,4 @@ declare abstract class CoreRouter {
327
369
  }): void;
328
370
  }
329
371
  //#endregion
330
- export { ClearRequest, Controller, CoreRouter, Request, Response$1 as Response, Route };
372
+ export { ClearRequest, ClearRouterPlugin, ClearRouterPluginContext, ClearRouterPluginInput, Controller, CoreRouter, PluginBind, PluginSetupResult, Request, Response$1 as Response, Route, definePlugin };
package/dist/index.mjs CHANGED
@@ -140,6 +140,12 @@ var Response = class {
140
140
  }
141
141
  };
142
142
 
143
+ //#endregion
144
+ //#region src/core/plugins.ts
145
+ function definePlugin(plugin) {
146
+ return plugin;
147
+ }
148
+
143
149
  //#endregion
144
150
  //#region src/core/bindings.ts
145
151
  const metadataKey = Symbol.for("clear-router:binding-metadata");
@@ -211,6 +217,7 @@ var CoreRouter = class {
211
217
  static stateStoreKey = Symbol.for("clear-router:router-state");
212
218
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
213
219
  static defaultConfigKey = Symbol.for("clear-router:default-config");
220
+ static pluginStoreKey = Symbol.for("clear-router:plugins");
214
221
  static createBaseConfig() {
215
222
  return {
216
223
  methodOverride: {
@@ -252,6 +259,11 @@ var CoreRouter = class {
252
259
  if (!g[this.stateStoreKey]) g[this.stateStoreKey] = Object.create(null);
253
260
  return g[this.stateStoreKey];
254
261
  }
262
+ static getPluginStore() {
263
+ const g = globalThis;
264
+ if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
265
+ return g[this.pluginStoreKey];
266
+ }
255
267
  static createDefaultState() {
256
268
  return {
257
269
  config: this.getDefaultConfig(),
@@ -349,6 +361,29 @@ var CoreRouter = class {
349
361
  const store = this.getStateStore();
350
362
  for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
351
363
  }
364
+ /**
365
+ * Use a registered plugin
366
+ *
367
+ * @param this
368
+ * @param plugin
369
+ * @param options
370
+ * @returns
371
+ */
372
+ static use(plugin, options) {
373
+ const name = typeof plugin === "function" ? plugin.name : plugin.name;
374
+ const store = this.getPluginStore();
375
+ if (name && store.has(name)) return;
376
+ const ctx = {
377
+ container: Container,
378
+ bind: Container.bind.bind(Container),
379
+ configure: this.configure.bind(this),
380
+ configureDefaults: this.configureDefaults.bind(this),
381
+ options
382
+ };
383
+ if (typeof plugin === "function") plugin(ctx);
384
+ else plugin.setup(ctx);
385
+ if (name) store.add(name);
386
+ }
352
387
  static groupContext = new AsyncLocalStorage();
353
388
  static routes = [];
354
389
  static routesByPathMethod = {};
@@ -724,4 +759,4 @@ var CoreRouter = class {
724
759
  };
725
760
 
726
761
  //#endregion
727
- export { ClearRequest, Controller, CoreRouter, Request, Response, Route };
762
+ export { ClearRequest, Controller, CoreRouter, Request, Response, Route, definePlugin };
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CCUF5FE7.cjs');
3
+ const require_router = require('../router-CU4V1kX0.cjs');
4
4
  const require_responses = require('../responses-JzXstGU5.cjs');
5
5
 
6
6
  //#region src/koa/router.ts
@@ -1,4 +1,4 @@
1
- import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-78fDk87B.cjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-BYZmNzrZ.cjs";
2
2
  import Koa from "koa";
3
3
  import Router$1 from "@koa/router";
4
4
 
@@ -1,4 +1,4 @@
1
- import { d as ApiResourceMiddleware, f as ControllerAction, h as Response, m as HttpMethod, n as Request, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-BdfAh1me.mjs";
1
+ import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-DCMtQ_Xi.mjs";
2
2
  import Koa from "koa";
3
3
  import Router$1 from "@koa/router";
4
4
 
@@ -1,5 +1,5 @@
1
1
  import "../bindings-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
2
+ import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
3
  import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-_II3dOJ5.mjs";
4
4
 
5
5
  //#region src/koa/router.ts
@@ -41,6 +41,7 @@ var CoreRouter = class {
41
41
  static stateStoreKey = Symbol.for("clear-router:router-state");
42
42
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
43
43
  static defaultConfigKey = Symbol.for("clear-router:default-config");
44
+ static pluginStoreKey = Symbol.for("clear-router:plugins");
44
45
  static createBaseConfig() {
45
46
  return {
46
47
  methodOverride: {
@@ -82,6 +83,11 @@ var CoreRouter = class {
82
83
  if (!g[this.stateStoreKey]) g[this.stateStoreKey] = Object.create(null);
83
84
  return g[this.stateStoreKey];
84
85
  }
86
+ static getPluginStore() {
87
+ const g = globalThis;
88
+ if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
89
+ return g[this.pluginStoreKey];
90
+ }
85
91
  static createDefaultState() {
86
92
  return {
87
93
  config: this.getDefaultConfig(),
@@ -179,6 +185,29 @@ var CoreRouter = class {
179
185
  const store = this.getStateStore();
180
186
  for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
181
187
  }
188
+ /**
189
+ * Use a registered plugin
190
+ *
191
+ * @param this
192
+ * @param plugin
193
+ * @param options
194
+ * @returns
195
+ */
196
+ static use(plugin, options) {
197
+ const name = typeof plugin === "function" ? plugin.name : plugin.name;
198
+ const store = this.getPluginStore();
199
+ 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
+ if (name) store.add(name);
210
+ }
182
211
  static groupContext = new AsyncLocalStorage();
183
212
  static routes = [];
184
213
  static routesByPathMethod = {};
@@ -1,6 +1,7 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
- import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
1
+ import { a as BindValue, i as BindToken, o as Container } from "./bindings-NV0CdqGl.cjs";
3
2
  import { NextFunction, Request, Response as Response$1 } from "express";
3
+ import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
4
+ import { AsyncLocalStorage } from "node:async_hooks";
4
5
 
5
6
  //#region src/core/Response.d.ts
6
7
  declare class Response$2 {
@@ -188,6 +189,23 @@ declare class Request$1<X = any, M = any> extends ClearRequest<X, M> {
188
189
  is(method: HttpMethod | string): boolean;
189
190
  }
190
191
  //#endregion
192
+ //#region src/core/plugins.d.ts
193
+ type PluginSetupResult = void;
194
+ type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
195
+ interface ClearRouterPluginContext<Options = any> {
196
+ container: typeof Container;
197
+ bind: PluginBind;
198
+ configure: (options: RouterConfig) => void;
199
+ configureDefaults: (options: RouterConfig) => void;
200
+ options: Options;
201
+ }
202
+ interface ClearRouterPlugin<Options = any> {
203
+ name?: string;
204
+ setup: (ctx: ClearRouterPluginContext<Options>) => PluginSetupResult;
205
+ }
206
+ type ClearRouterPluginInput<Options = any> = ClearRouterPlugin<Options> | ((ctx: ClearRouterPluginContext<Options>) => PluginSetupResult);
207
+ declare function definePlugin<Options = any>(plugin: ClearRouterPlugin<Options>): ClearRouterPlugin<Options>;
208
+ //#endregion
191
209
  //#region src/Controller.d.ts
192
210
  declare abstract class Controller<X = any> {
193
211
  [x: string]: any;
@@ -210,11 +228,13 @@ declare abstract class CoreRouter {
210
228
  private static readonly stateStoreKey;
211
229
  private static readonly stateBoundKey;
212
230
  private static readonly defaultConfigKey;
231
+ private static readonly pluginStoreKey;
213
232
  protected static createBaseConfig(): RouterConfig;
214
233
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
215
234
  protected static getDefaultConfig(): RouterConfig;
216
235
  protected static resolveStateNamespace(this: any): string;
217
236
  protected static getStateStore(): Record<string, any>;
237
+ protected static getPluginStore(): Set<string>;
218
238
  protected static createDefaultState(): {
219
239
  config: RouterConfig;
220
240
  groupContext: AsyncLocalStorage<{
@@ -232,6 +252,15 @@ declare abstract class CoreRouter {
232
252
  protected static createDefaultOptionsHandler(): any;
233
253
  static config: RouterConfig;
234
254
  static configureDefaults(this: any, options?: RouterConfig): void;
255
+ /**
256
+ * Use a registered plugin
257
+ *
258
+ * @param this
259
+ * @param plugin
260
+ * @param options
261
+ * @returns
262
+ */
263
+ static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
235
264
  protected static groupContext: AsyncLocalStorage<{
236
265
  prefix: string;
237
266
  groupMiddlewares: any[];
@@ -397,4 +426,4 @@ declare abstract class CoreRouter {
397
426
  }): void;
398
427
  }
399
428
  //#endregion
400
- export { Handler as a, Handler$1 as c, ApiResourceMiddleware as d, ControllerAction as f, Response$2 as h, H3App as i, HttpContext$1 as l, HttpMethod as m, Request$1 as n, HttpContext as o, ControllerHandler as p, Route as r, Middleware as s, CoreRouter as t, Middleware$1 as u };
429
+ export { ApiResourceMiddleware as _, PluginBind as a, HttpMethod as b, Request$1 as c, Handler as d, HttpContext as f, Middleware$1 as g, HttpContext$1 as h, ClearRouterPluginInput as i, Route as l, Handler$1 as m, ClearRouterPlugin as n, PluginSetupResult as o, Middleware as p, ClearRouterPluginContext as r, definePlugin as s, CoreRouter as t, H3App as u, ControllerAction as v, Response$2 as x, ControllerHandler as y };
@@ -41,6 +41,7 @@ var CoreRouter = class {
41
41
  static stateStoreKey = Symbol.for("clear-router:router-state");
42
42
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
43
43
  static defaultConfigKey = Symbol.for("clear-router:default-config");
44
+ static pluginStoreKey = Symbol.for("clear-router:plugins");
44
45
  static createBaseConfig() {
45
46
  return {
46
47
  methodOverride: {
@@ -82,6 +83,11 @@ var CoreRouter = class {
82
83
  if (!g[this.stateStoreKey]) g[this.stateStoreKey] = Object.create(null);
83
84
  return g[this.stateStoreKey];
84
85
  }
86
+ static getPluginStore() {
87
+ const g = globalThis;
88
+ if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
89
+ return g[this.pluginStoreKey];
90
+ }
85
91
  static createDefaultState() {
86
92
  return {
87
93
  config: this.getDefaultConfig(),
@@ -179,6 +185,29 @@ var CoreRouter = class {
179
185
  const store = this.getStateStore();
180
186
  for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
181
187
  }
188
+ /**
189
+ * Use a registered plugin
190
+ *
191
+ * @param this
192
+ * @param plugin
193
+ * @param options
194
+ * @returns
195
+ */
196
+ static use(plugin, options) {
197
+ const name = typeof plugin === "function" ? plugin.name : plugin.name;
198
+ const store = this.getPluginStore();
199
+ if (name && store.has(name)) return;
200
+ const ctx = {
201
+ container: require_bindings.Container,
202
+ bind: require_bindings.Container.bind.bind(require_bindings.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
+ if (name) store.add(name);
210
+ }
182
211
  static groupContext = new node_async_hooks.AsyncLocalStorage();
183
212
  static routes = [];
184
213
  static routesByPathMethod = {};
@@ -1,6 +1,7 @@
1
- import { NextFunction, Request, Response as Response$1 } from "express";
2
- import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
1
+ import { a as BindValue, i as BindToken, o as Container } from "./bindings-CV1e5jho.mjs";
3
2
  import { AsyncLocalStorage } from "node:async_hooks";
3
+ import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
4
+ import { NextFunction, Request, Response as Response$1 } from "express";
4
5
 
5
6
  //#region src/core/Response.d.ts
6
7
  declare class Response$2 {
@@ -188,6 +189,23 @@ declare class Request$1<X = any, M = any> extends ClearRequest<X, M> {
188
189
  is(method: HttpMethod | string): boolean;
189
190
  }
190
191
  //#endregion
192
+ //#region src/core/plugins.d.ts
193
+ type PluginSetupResult = void;
194
+ type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
195
+ interface ClearRouterPluginContext<Options = any> {
196
+ container: typeof Container;
197
+ bind: PluginBind;
198
+ configure: (options: RouterConfig) => void;
199
+ configureDefaults: (options: RouterConfig) => void;
200
+ options: Options;
201
+ }
202
+ interface ClearRouterPlugin<Options = any> {
203
+ name?: string;
204
+ setup: (ctx: ClearRouterPluginContext<Options>) => PluginSetupResult;
205
+ }
206
+ type ClearRouterPluginInput<Options = any> = ClearRouterPlugin<Options> | ((ctx: ClearRouterPluginContext<Options>) => PluginSetupResult);
207
+ declare function definePlugin<Options = any>(plugin: ClearRouterPlugin<Options>): ClearRouterPlugin<Options>;
208
+ //#endregion
191
209
  //#region src/Controller.d.ts
192
210
  declare abstract class Controller<X = any> {
193
211
  [x: string]: any;
@@ -210,11 +228,13 @@ declare abstract class CoreRouter {
210
228
  private static readonly stateStoreKey;
211
229
  private static readonly stateBoundKey;
212
230
  private static readonly defaultConfigKey;
231
+ private static readonly pluginStoreKey;
213
232
  protected static createBaseConfig(): RouterConfig;
214
233
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
215
234
  protected static getDefaultConfig(): RouterConfig;
216
235
  protected static resolveStateNamespace(this: any): string;
217
236
  protected static getStateStore(): Record<string, any>;
237
+ protected static getPluginStore(): Set<string>;
218
238
  protected static createDefaultState(): {
219
239
  config: RouterConfig;
220
240
  groupContext: AsyncLocalStorage<{
@@ -232,6 +252,15 @@ declare abstract class CoreRouter {
232
252
  protected static createDefaultOptionsHandler(): any;
233
253
  static config: RouterConfig;
234
254
  static configureDefaults(this: any, options?: RouterConfig): void;
255
+ /**
256
+ * Use a registered plugin
257
+ *
258
+ * @param this
259
+ * @param plugin
260
+ * @param options
261
+ * @returns
262
+ */
263
+ static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
235
264
  protected static groupContext: AsyncLocalStorage<{
236
265
  prefix: string;
237
266
  groupMiddlewares: any[];
@@ -397,4 +426,4 @@ declare abstract class CoreRouter {
397
426
  }): void;
398
427
  }
399
428
  //#endregion
400
- export { Handler as a, Handler$1 as c, ApiResourceMiddleware as d, ControllerAction as f, Response$2 as h, H3App as i, HttpContext$1 as l, HttpMethod as m, Request$1 as n, HttpContext as o, ControllerHandler as p, Route as r, Middleware as s, CoreRouter as t, Middleware$1 as u };
429
+ export { ApiResourceMiddleware as _, PluginBind as a, HttpMethod as b, Request$1 as c, Handler as d, HttpContext as f, Middleware$1 as g, HttpContext$1 as h, ClearRouterPluginInput as i, Route as l, Handler$1 as m, ClearRouterPlugin as n, PluginSetupResult as o, Middleware as p, ClearRouterPluginContext as r, definePlugin as s, CoreRouter as t, H3App as u, ControllerAction as v, Response$2 as x, ControllerHandler as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-router",
3
- "version": "2.5.5",
3
+ "version": "2.5.6",
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",