|
2 | 2 | const createMatchHandler = (updateParams) =>
|
3 | 3 | updateParams
|
4 | 4 | ? (req, res, params) => {
|
5 |
| - req.params = params; |
6 |
| - return true; |
| 5 | + req.params = params |
| 6 | + return true |
7 | 7 | }
|
8 |
| - : () => true; |
| 8 | + : () => true |
9 | 9 |
|
10 |
| -const defaultHandler = () => false; |
| 10 | +const defaultHandler = () => false |
11 | 11 |
|
12 | 12 | // Router cache for reusing router instances
|
13 |
| -const routerCache = new WeakMap(); |
| 13 | +const routerCache = new WeakMap() |
14 | 14 |
|
15 |
| -function normalizeEndpoint(endpoint) { |
16 |
| - if (typeof endpoint === "string") { |
17 |
| - return { url: endpoint, methods: ["GET"], updateParams: false }; |
| 15 | +function normalizeEndpoint (endpoint) { |
| 16 | + if (typeof endpoint === 'string') { |
| 17 | + return { url: endpoint, methods: ['GET'], updateParams: false } |
18 | 18 | }
|
19 | 19 | return {
|
20 |
| - methods: endpoint.methods || ["GET"], |
| 20 | + methods: endpoint.methods || ['GET'], |
21 | 21 | url: endpoint.url,
|
22 | 22 | version: endpoint.version,
|
23 |
| - updateParams: endpoint.updateParams || false, |
24 |
| - }; |
| 23 | + updateParams: endpoint.updateParams || false |
| 24 | + } |
25 | 25 | }
|
26 | 26 |
|
27 |
| -module.exports = function (routerOpts = {}, routerFactory = require("find-my-way")) { |
28 |
| - function exec(options, isIff = true) { |
29 |
| - const middleware = this; |
30 |
| - let router = null; |
31 |
| - let customFn = null; |
| 27 | +module.exports = function (routerOpts = {}, routerFactory = require('find-my-way')) { |
| 28 | + function exec (options, isIff = true) { |
| 29 | + const middleware = this |
| 30 | + let router = null |
| 31 | + let customFn = null |
32 | 32 |
|
33 | 33 | // Process options efficiently
|
34 |
| - if (typeof options === "function") { |
35 |
| - customFn = options; |
| 34 | + if (typeof options === 'function') { |
| 35 | + customFn = options |
36 | 36 | } else {
|
37 |
| - const endpoints = Array.isArray(options) ? options : options?.endpoints; |
| 37 | + const endpoints = Array.isArray(options) ? options : options?.endpoints |
38 | 38 |
|
39 | 39 | if (endpoints?.length) {
|
40 | 40 | // Try to get cached router first
|
41 |
| - let cache = routerCache.get(routerOpts); |
| 41 | + let cache = routerCache.get(routerOpts) |
42 | 42 | if (!cache) {
|
43 |
| - cache = new Map(); |
44 |
| - routerCache.set(routerOpts, cache); |
| 43 | + cache = new Map() |
| 44 | + routerCache.set(routerOpts, cache) |
45 | 45 | }
|
46 | 46 |
|
47 |
| - const cacheKey = JSON.stringify(endpoints); |
48 |
| - router = cache.get(cacheKey); |
| 47 | + const cacheKey = JSON.stringify(endpoints) |
| 48 | + router = cache.get(cacheKey) |
49 | 49 |
|
50 | 50 | if (!router) {
|
51 |
| - router = routerFactory({ ...routerOpts, defaultRoute: defaultHandler }); |
| 51 | + router = routerFactory({ ...routerOpts, defaultRoute: defaultHandler }) |
52 | 52 |
|
53 | 53 | // Normalize and register routes
|
54 |
| - const normalized = endpoints.map(normalizeEndpoint); |
| 54 | + const normalized = endpoints.map(normalizeEndpoint) |
55 | 55 | for (const { methods, url, version, updateParams } of normalized) {
|
56 |
| - const handler = createMatchHandler(updateParams); |
| 56 | + const handler = createMatchHandler(updateParams) |
57 | 57 |
|
58 | 58 | if (version) {
|
59 |
| - router.on(methods, url, { constraints: { version } }, handler); |
| 59 | + router.on(methods, url, { constraints: { version } }, handler) |
60 | 60 | } else {
|
61 |
| - router.on(methods, url, handler); |
| 61 | + router.on(methods, url, handler) |
62 | 62 | }
|
63 | 63 | }
|
64 | 64 |
|
65 |
| - cache.set(cacheKey, router); |
| 65 | + cache.set(cacheKey, router) |
66 | 66 | }
|
67 | 67 | }
|
68 | 68 |
|
69 | 69 | if (options?.custom) {
|
70 |
| - customFn = options.custom; |
| 70 | + customFn = options.custom |
71 | 71 | }
|
72 | 72 | }
|
73 | 73 |
|
74 | 74 | // Optimized execution function
|
75 | 75 | const result = function (req, res, next) {
|
76 |
| - let shouldExecute = false; |
| 76 | + let shouldExecute = false |
77 | 77 |
|
78 | 78 | if (customFn) {
|
79 |
| - shouldExecute = customFn(req); |
| 79 | + shouldExecute = customFn(req) |
80 | 80 | } else if (router) {
|
81 |
| - shouldExecute = router.lookup(req, res); |
| 81 | + shouldExecute = router.lookup(req, res) |
82 | 82 | }
|
83 | 83 |
|
84 | 84 | // Simplified logic: execute middleware if conditions match
|
85 | 85 | if ((isIff && shouldExecute) || (!isIff && !shouldExecute)) {
|
86 |
| - return middleware(req, res, next); |
| 86 | + return middleware(req, res, next) |
87 | 87 | }
|
88 | 88 |
|
89 |
| - return next(); |
90 |
| - }; |
| 89 | + return next() |
| 90 | + } |
91 | 91 |
|
92 | 92 | // Allow chaining
|
93 |
| - result.iff = iff; |
94 |
| - result.unless = unless; |
| 93 | + result.iff = iff |
| 94 | + result.unless = unless |
95 | 95 |
|
96 |
| - return result; |
| 96 | + return result |
97 | 97 | }
|
98 | 98 |
|
99 |
| - function iff(options) { |
100 |
| - return exec.call(this, options, true); |
| 99 | + function iff (options) { |
| 100 | + return exec.call(this, options, true) |
101 | 101 | }
|
102 |
| - function unless(options) { |
103 |
| - return exec.call(this, options, false); |
| 102 | + function unless (options) { |
| 103 | + return exec.call(this, options, false) |
104 | 104 | }
|
105 | 105 |
|
106 | 106 | return function (middleware) {
|
107 |
| - middleware.iff = iff; |
108 |
| - middleware.unless = unless; |
| 107 | + middleware.iff = iff |
| 108 | + middleware.unless = unless |
109 | 109 |
|
110 |
| - return middleware; |
111 |
| - }; |
112 |
| -}; |
| 110 | + return middleware |
| 111 | + } |
| 112 | +} |
0 commit comments