@koa/router 15.1.0 → 15.1.2

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.
package/dist/index.d.mts CHANGED
@@ -172,6 +172,7 @@ declare class Layer<StateT = DefaultState, ContextT = DefaultContext, BodyT = un
172
172
  private _reconfigurePathMatching;
173
173
  }
174
174
 
175
+ type RouterInstance<StateT = koa.DefaultState, ContextT = koa.DefaultContext> = RouterImplementation<StateT, ContextT>;
175
176
  /**
176
177
  * Middleware with router property
177
178
  */
@@ -181,7 +182,7 @@ type RouterComposedMiddleware<StateT = koa.DefaultState, ContextT = koa.DefaultC
181
182
  /**
182
183
  * @module koa-router
183
184
  */
184
- declare class Router<StateT = koa.DefaultState, ContextT = koa.DefaultContext> {
185
+ declare class RouterImplementation<StateT = koa.DefaultState, ContextT = koa.DefaultContext> {
185
186
  opts: RouterOptions;
186
187
  methods: string[];
187
188
  exclusive: boolean;
@@ -642,7 +643,8 @@ interface RouterConstructor {
642
643
  url(path: string, ...parameters: unknown[]): string;
643
644
  readonly prototype: Router;
644
645
  }
645
- declare const RouterExport: RouterConstructor;
646
+ type Router<StateT = koa.DefaultState, ContextT = koa.DefaultContext> = RouterInstance<StateT, ContextT>;
647
+ declare const Router: RouterConstructor;
646
648
 
647
649
  /**
648
650
  * Type definitions for @koa/router
@@ -738,7 +740,7 @@ type RouterParameterContext<StateT = DefaultState, ContextT = DefaultContext> =
738
740
  /**
739
741
  * Router instance
740
742
  */
741
- router: Router<StateT, ContextT>;
743
+ router: RouterInstance<StateT, ContextT>;
742
744
  /**
743
745
  * Matched route path (internal)
744
746
  */
@@ -816,7 +818,7 @@ type RouterContext<StateT = DefaultState, ContextT = DefaultContext, BodyT = unk
816
818
  /**
817
819
  * Router middleware function type
818
820
  */
819
- type RouterMiddleware<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> = (context: RouterContext<StateT, ContextT, BodyT>, next: () => Promise<unknown>) => unknown | Promise<unknown>;
821
+ type RouterMiddleware<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> = Middleware<StateT, RouterContext<StateT, ContextT, BodyT>, BodyT>;
820
822
  /**
821
823
  * HTTP method names in lowercase
822
824
  */
@@ -831,13 +833,13 @@ type RouterOptionsWithMethods<M extends string = string> = Omit<RouterOptions, '
831
833
  * Type for a dynamic HTTP method function on Router
832
834
  */
833
835
  type RouterMethodFunction<StateT = DefaultState, ContextT = DefaultContext> = {
834
- <T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
835
- <T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
836
+ <T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): RouterInstance<StateT, ContextT>;
837
+ <T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): RouterInstance<StateT, ContextT>;
836
838
  };
837
839
  /**
838
840
  * Router with additional HTTP methods based on the methods option.
839
841
  * Use createRouter() factory function for automatic type inference.
840
842
  */
841
- type RouterWithMethods<M extends string, StateT = DefaultState, ContextT = DefaultContext> = Router<StateT, ContextT> & Record<Lowercase<M>, RouterMethodFunction<StateT, ContextT>>;
843
+ type RouterWithMethods<M extends string, StateT = DefaultState, ContextT = DefaultContext> = RouterInstance<StateT, ContextT> & Record<Lowercase<M>, RouterMethodFunction<StateT, ContextT>>;
842
844
 
843
- export { type AllowedMethodsOptions, type HttpMethod, Layer, type LayerOptions, type MatchResult, RouterExport as Router, type RouterContext, Router as RouterInstance, type RouterMethodFunction, type RouterMiddleware, type RouterOptions, type RouterOptionsWithMethods, type RouterParameterMiddleware, type RouterWithMethods, type UrlOptions, RouterExport as default };
845
+ export { type AllowedMethodsOptions, type HttpMethod, Layer, type LayerOptions, type MatchResult, Router, type RouterContext, type RouterInstance, type RouterMethodFunction, type RouterMiddleware, type RouterOptions, type RouterOptionsWithMethods, type RouterParameterMiddleware, type RouterWithMethods, type UrlOptions, Router as default };
package/dist/index.d.ts CHANGED
@@ -172,6 +172,7 @@ declare class Layer<StateT = DefaultState, ContextT = DefaultContext, BodyT = un
172
172
  private _reconfigurePathMatching;
173
173
  }
174
174
 
175
+ type RouterInstance<StateT = koa.DefaultState, ContextT = koa.DefaultContext> = RouterImplementation<StateT, ContextT>;
175
176
  /**
176
177
  * Middleware with router property
177
178
  */
@@ -181,7 +182,7 @@ type RouterComposedMiddleware<StateT = koa.DefaultState, ContextT = koa.DefaultC
181
182
  /**
182
183
  * @module koa-router
183
184
  */
184
- declare class Router<StateT = koa.DefaultState, ContextT = koa.DefaultContext> {
185
+ declare class RouterImplementation<StateT = koa.DefaultState, ContextT = koa.DefaultContext> {
185
186
  opts: RouterOptions;
186
187
  methods: string[];
187
188
  exclusive: boolean;
@@ -642,7 +643,8 @@ interface RouterConstructor {
642
643
  url(path: string, ...parameters: unknown[]): string;
643
644
  readonly prototype: Router;
644
645
  }
645
- declare const RouterExport: RouterConstructor;
646
+ type Router<StateT = koa.DefaultState, ContextT = koa.DefaultContext> = RouterInstance<StateT, ContextT>;
647
+ declare const Router: RouterConstructor;
646
648
 
647
649
  /**
648
650
  * Type definitions for @koa/router
@@ -738,7 +740,7 @@ type RouterParameterContext<StateT = DefaultState, ContextT = DefaultContext> =
738
740
  /**
739
741
  * Router instance
740
742
  */
741
- router: Router<StateT, ContextT>;
743
+ router: RouterInstance<StateT, ContextT>;
742
744
  /**
743
745
  * Matched route path (internal)
744
746
  */
@@ -816,7 +818,7 @@ type RouterContext<StateT = DefaultState, ContextT = DefaultContext, BodyT = unk
816
818
  /**
817
819
  * Router middleware function type
818
820
  */
819
- type RouterMiddleware<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> = (context: RouterContext<StateT, ContextT, BodyT>, next: () => Promise<unknown>) => unknown | Promise<unknown>;
821
+ type RouterMiddleware<StateT = DefaultState, ContextT = DefaultContext, BodyT = unknown> = Middleware<StateT, RouterContext<StateT, ContextT, BodyT>, BodyT>;
820
822
  /**
821
823
  * HTTP method names in lowercase
822
824
  */
@@ -831,13 +833,13 @@ type RouterOptionsWithMethods<M extends string = string> = Omit<RouterOptions, '
831
833
  * Type for a dynamic HTTP method function on Router
832
834
  */
833
835
  type RouterMethodFunction<StateT = DefaultState, ContextT = DefaultContext> = {
834
- <T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
835
- <T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): Router<StateT, ContextT>;
836
+ <T = {}, U = {}, B = unknown>(name: string, path: string | RegExp, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): RouterInstance<StateT, ContextT>;
837
+ <T = {}, U = {}, B = unknown>(path: string | RegExp | Array<string | RegExp>, ...middleware: Array<RouterMiddleware<StateT & T, ContextT & U, B>>): RouterInstance<StateT, ContextT>;
836
838
  };
837
839
  /**
838
840
  * Router with additional HTTP methods based on the methods option.
839
841
  * Use createRouter() factory function for automatic type inference.
840
842
  */
841
- type RouterWithMethods<M extends string, StateT = DefaultState, ContextT = DefaultContext> = Router<StateT, ContextT> & Record<Lowercase<M>, RouterMethodFunction<StateT, ContextT>>;
843
+ type RouterWithMethods<M extends string, StateT = DefaultState, ContextT = DefaultContext> = RouterInstance<StateT, ContextT> & Record<Lowercase<M>, RouterMethodFunction<StateT, ContextT>>;
842
844
 
843
- export { type AllowedMethodsOptions, type HttpMethod, Layer, type LayerOptions, type MatchResult, RouterExport as Router, type RouterContext, Router as RouterInstance, type RouterMethodFunction, type RouterMiddleware, type RouterOptions, type RouterOptionsWithMethods, type RouterParameterMiddleware, type RouterWithMethods, type UrlOptions, RouterExport as default };
845
+ export { type AllowedMethodsOptions, type HttpMethod, Layer, type LayerOptions, type MatchResult, Router, type RouterContext, type RouterInstance, type RouterMethodFunction, type RouterMiddleware, type RouterOptions, type RouterOptionsWithMethods, type RouterParameterMiddleware, type RouterWithMethods, type UrlOptions, Router as default };
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
- Router: () => RouterExport,
33
+ Router: () => Router,
34
34
  default: () => router_default
35
35
  });
36
36
  module.exports = __toCommonJS(index_exports);
@@ -594,7 +594,7 @@ var debug = (0, import_debug.default)("koa-router");
594
594
 
595
595
  // src/router.ts
596
596
  var httpMethods = getAllHttpMethods();
597
- var Router = class {
597
+ var RouterImplementation = class {
598
598
  opts;
599
599
  methods;
600
600
  exclusive;
@@ -850,16 +850,10 @@ var Router = class {
850
850
  const previousPrefix = this.opts.prefix || "";
851
851
  this.opts.prefix = normalizedPrefix;
852
852
  for (const route of this.stack) {
853
- if (previousPrefix && typeof route.path === "string") {
854
- if (route.path.startsWith(previousPrefix)) {
855
- route.path = route.path.slice(previousPrefix.length) || "/";
856
- route.setPrefix(normalizedPrefix);
857
- } else {
858
- route.setPrefix(normalizedPrefix);
859
- }
860
- } else {
861
- route.setPrefix(normalizedPrefix);
853
+ if (previousPrefix && typeof route.path === "string" && route.path.startsWith(previousPrefix)) {
854
+ route.path = route.path.slice(previousPrefix.length) || "/";
862
855
  }
856
+ route.setPrefix(normalizedPrefix);
863
857
  }
864
858
  return this;
865
859
  }
@@ -1425,12 +1419,12 @@ var Router = class {
1425
1419
  return this._registerMethod("options", ...arguments_);
1426
1420
  }
1427
1421
  };
1428
- var RouterExport = Router;
1429
- var router_default = RouterExport;
1422
+ var Router = RouterImplementation;
1423
+ var router_default = Router;
1430
1424
  for (const httpMethod of httpMethods) {
1431
- const isAlreadyDefined = COMMON_HTTP_METHODS.includes(httpMethod) || httpMethod in Router.prototype;
1425
+ const isAlreadyDefined = COMMON_HTTP_METHODS.includes(httpMethod) || httpMethod in RouterImplementation.prototype;
1432
1426
  if (!isAlreadyDefined) {
1433
- Object.defineProperty(Router.prototype, httpMethod, {
1427
+ Object.defineProperty(RouterImplementation.prototype, httpMethod, {
1434
1428
  value: function(...arguments_) {
1435
1429
  return this._registerMethod(httpMethod, ...arguments_);
1436
1430
  },
package/dist/index.mjs CHANGED
@@ -557,7 +557,7 @@ var debug = debugModule("koa-router");
557
557
 
558
558
  // src/router.ts
559
559
  var httpMethods = getAllHttpMethods();
560
- var Router = class {
560
+ var RouterImplementation = class {
561
561
  opts;
562
562
  methods;
563
563
  exclusive;
@@ -813,16 +813,10 @@ var Router = class {
813
813
  const previousPrefix = this.opts.prefix || "";
814
814
  this.opts.prefix = normalizedPrefix;
815
815
  for (const route of this.stack) {
816
- if (previousPrefix && typeof route.path === "string") {
817
- if (route.path.startsWith(previousPrefix)) {
818
- route.path = route.path.slice(previousPrefix.length) || "/";
819
- route.setPrefix(normalizedPrefix);
820
- } else {
821
- route.setPrefix(normalizedPrefix);
822
- }
823
- } else {
824
- route.setPrefix(normalizedPrefix);
816
+ if (previousPrefix && typeof route.path === "string" && route.path.startsWith(previousPrefix)) {
817
+ route.path = route.path.slice(previousPrefix.length) || "/";
825
818
  }
819
+ route.setPrefix(normalizedPrefix);
826
820
  }
827
821
  return this;
828
822
  }
@@ -1388,12 +1382,12 @@ var Router = class {
1388
1382
  return this._registerMethod("options", ...arguments_);
1389
1383
  }
1390
1384
  };
1391
- var RouterExport = Router;
1392
- var router_default = RouterExport;
1385
+ var Router = RouterImplementation;
1386
+ var router_default = Router;
1393
1387
  for (const httpMethod of httpMethods) {
1394
- const isAlreadyDefined = COMMON_HTTP_METHODS.includes(httpMethod) || httpMethod in Router.prototype;
1388
+ const isAlreadyDefined = COMMON_HTTP_METHODS.includes(httpMethod) || httpMethod in RouterImplementation.prototype;
1395
1389
  if (!isAlreadyDefined) {
1396
- Object.defineProperty(Router.prototype, httpMethod, {
1390
+ Object.defineProperty(RouterImplementation.prototype, httpMethod, {
1397
1391
  value: function(...arguments_) {
1398
1392
  return this._registerMethod(httpMethod, ...arguments_);
1399
1393
  },
@@ -1404,6 +1398,6 @@ for (const httpMethod of httpMethods) {
1404
1398
  }
1405
1399
  }
1406
1400
  export {
1407
- RouterExport as Router,
1401
+ Router,
1408
1402
  router_default as default
1409
1403
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@koa/router",
3
3
  "description": "Router middleware for koa. Maintained by Forward Email and Lad.",
4
- "version": "15.1.0",
4
+ "version": "15.1.2",
5
5
  "author": "Alex Mingoia <talk@alexmingoia.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/koajs/router/issues",
@@ -35,19 +35,19 @@
35
35
  "path-to-regexp": "^8.3.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@commitlint/cli": "^20.2.0",
39
- "@commitlint/config-conventional": "^20.2.0",
38
+ "@commitlint/cli": "^20.3.0",
39
+ "@commitlint/config-conventional": "^20.3.0",
40
40
  "@koa/bodyparser": "^6.0.0",
41
41
  "@types/debug": "^4.1.12",
42
- "@types/jsonwebtoken": "^9.0.7",
42
+ "@types/jsonwebtoken": "^9.0.10",
43
43
  "@types/koa": "^3.0.1",
44
- "@types/node": "^25.0.1",
44
+ "@types/node": "^25.0.3",
45
45
  "@types/supertest": "^6.0.3",
46
- "@typescript-eslint/eslint-plugin": "^8.49.0",
47
- "@typescript-eslint/parser": "^8.49.0",
46
+ "@typescript-eslint/eslint-plugin": "^8.51.0",
47
+ "@typescript-eslint/parser": "^8.51.0",
48
48
  "c8": "^10.1.3",
49
- "chalk": "^5.4.1",
50
- "eslint": "^9.39.1",
49
+ "chalk": "^5.6.2",
50
+ "eslint": "^9.39.2",
51
51
  "eslint-plugin-unicorn": "^62.0.0",
52
52
  "husky": "^9.1.7",
53
53
  "joi": "^18.0.2",
@@ -108,7 +108,7 @@
108
108
  "lint": "npm run lint:ts",
109
109
  "test:core": "TS_NODE_PROJECT=tsconfig.ts-node.json node --require ts-node/register --test test/*.test.ts test/**/*.test.ts",
110
110
  "test:recipes": "TS_NODE_PROJECT=tsconfig.recipes.json node --require ts-node/register --test recipes/**/*.test.ts",
111
- "pretest:all": "npm run lint",
111
+ "pretest:all": "npm run lint && npm run build",
112
112
  "test:all": "TS_NODE_PROJECT=tsconfig.ts-node.json node --require ts-node/register --test test/*.test.ts test/**/*.test.ts recipes/**/*.test.ts",
113
113
  "test:coverage": "c8 npm run test:all",
114
114
  "ts:check": "tsc --noEmit --project tsconfig.typecheck.json",