@riddl/core 0.2.1 → 0.2.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/routing.js +10 -1
- package/package.json +1 -1
package/dist/routing.js
CHANGED
|
@@ -55,8 +55,17 @@ const exec = (match, params, matchers) => {
|
|
|
55
55
|
return;
|
|
56
56
|
return result;
|
|
57
57
|
};
|
|
58
|
+
/**
|
|
59
|
+
* Order routes for matching: try static / more-specific patterns before generic ones.
|
|
60
|
+
* Previously shorter `id` won, so `/api/services/[id]` matched before `/api/services/modules`.
|
|
61
|
+
* 1. Fewer dynamic captures (`matcher.params`) first — literals beat `[param]` at the same depth.
|
|
62
|
+
* 2. Longer `id` first when capture counts tie — more path segments usually means more fixed segments.
|
|
63
|
+
*/
|
|
58
64
|
const routeSorter = (a, b) => {
|
|
59
|
-
|
|
65
|
+
const byParams = a.matcher.params.length - b.matcher.params.length;
|
|
66
|
+
if (byParams !== 0)
|
|
67
|
+
return byParams;
|
|
68
|
+
return b.id.length - a.id.length;
|
|
60
69
|
};
|
|
61
70
|
export const buildRouter = (routesMap, middlewaresMap) => {
|
|
62
71
|
const middlewareEntries = Object.entries(middlewaresMap).sort((a, b) => a[0].length - b[0].length);
|