@tahminator/sapling 1.3.0 → 1.3.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.
@@ -14,7 +14,6 @@ import { _getRoutes } from "./route";
14
14
  import { Sapling } from "../helper/sapling";
15
15
  import { Html404ErrorPage } from "../html/404";
16
16
  import { ResponseEntity, RedirectView } from "../helper";
17
- const _usedPrefixes = new Set();
18
17
  export const _ControllerRegistry = new WeakMap();
19
18
  /**
20
19
  * Registers a class as an HTTP controller and registers its routes.
@@ -28,13 +27,9 @@ export function Controller({ prefix = "", deps = [] } = {
28
27
  }) {
29
28
  return (target) => {
30
29
  const targetClass = target;
31
- if (_usedPrefixes.has(prefix)) {
32
- throw new Error(`The prefix "${prefix}" is already in use by another constructor. Please resolve this issue.`);
33
- }
34
- _usedPrefixes.add(prefix);
35
30
  const router = Router();
36
31
  const routes = _getRoutes(target);
37
- const usedPaths = new Set();
32
+ const usedRoutes = new Set();
38
33
  _InjectableDeps.set(targetClass, deps);
39
34
  const controllerInstance = _resolve(targetClass);
40
35
  for (const { method, path, fnName } of routes) {
@@ -42,13 +37,14 @@ export function Controller({ prefix = "", deps = [] } = {
42
37
  if (typeof fn !== "function")
43
38
  continue;
44
39
  const fp = prefix + path;
40
+ const routeKey = method + " " + fp;
45
41
  // Only check for duplicates on non-middleware routes
46
- // Middleware (USE) can have duplicate paths
47
- if (method !== "USE" && usedPaths.has(fp)) {
48
- throw new Error(`Duplicate route path "${fp}" detected in controller "${target.name}"`);
42
+ // Middleware (USE) can have duplicate paths, and different HTTP methods can share paths
43
+ if (method !== "USE" && usedRoutes.has(routeKey)) {
44
+ throw new Error(`Duplicate route [${method}] "${fp}" detected in controller "${target.name}"`);
49
45
  }
50
46
  if (method !== "USE") {
51
- usedPaths.add(fp);
47
+ usedRoutes.add(routeKey);
52
48
  }
53
49
  const methodName = methodResolve[method];
54
50
  router[methodName](fp, (request, response, next) => __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tahminator/sapling",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "A library to help you write cleaner Express.js code",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",