| 20 | * modifier, custom pattern, or unnamed parameter is allowed. |
| 21 | */ |
| 22 | export function validateApiPath(path = '/') { |
| 23 | const tokens = parse(path); |
| 24 | for (const token of tokens) { |
| 25 | if (typeof token === 'string') continue; |
| 26 | if (typeof token === 'object') { |
| 27 | const name = token.name; |
| 28 | if (typeof name === 'string' && name !== '') { |
| 29 | throw new Error( |
| 30 | `Invalid path template: '${path}'. Please use {${name}} instead of ':${name}'`, |
| 31 | ); |
| 32 | } |
| 33 | if (typeof name === 'number') { |
| 34 | throw new Error(`Unnamed parameter is not allowed in path '${path}'`); |
| 35 | } |
| 36 | const valid = token.prefix.match(VALID_VARNAME_PATTERN); |
| 37 | if (!valid) { |
| 38 | throw new Error( |
| 39 | `Invalid parameter name '${token.prefix}' found in path '${path}'`, |
| 40 | ); |
| 41 | } |
| 42 | if (['?', '+', '*'].includes(token.modifier)) { |
| 43 | throw new Error( |
| 44 | `Parameter modifier '{${token.prefix}}${token.modifier}' is not allowed in path '${path}`, |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return path; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get all path variables. For example, `/root/{foo}/bar` => `['foo']` |