1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.toExpressPath = exports.getPathVariables = exports.validateApiPath = void 0;
|
8 | const path_to_regexp_1 = require("path-to-regexp");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const POSSIBLE_VARNAME_PATTERN = /\{([^\}]+)\}/g;
|
17 | const VALID_VARNAME_PATTERN = /^[A-Za-z0-9_]+$/;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function validateApiPath(path = '/') {
|
23 | const tokens = (0, path_to_regexp_1.parse)(path);
|
24 | for (const token of tokens) {
|
25 | if (typeof token === 'string')
|
26 | continue;
|
27 | if (typeof token === 'object') {
|
28 | const name = token.name;
|
29 | if (typeof name === 'string' && name !== '') {
|
30 | throw new Error(`Invalid path template: '${path}'. Please use {${name}} instead of ':${name}'`);
|
31 | }
|
32 | if (typeof name === 'number') {
|
33 | throw new Error(`Unnamed parameter is not allowed in path '${path}'`);
|
34 | }
|
35 | const valid = token.prefix.match(VALID_VARNAME_PATTERN);
|
36 | if (!valid) {
|
37 | throw new Error(`Invalid parameter name '${token.prefix}' found in path '${path}'`);
|
38 | }
|
39 | if (['?', '+', '*'].includes(token.modifier)) {
|
40 | throw new Error(`Parameter modifier '{${token.prefix}}${token.modifier}' is not allowed in path '${path}`);
|
41 | }
|
42 | }
|
43 | }
|
44 | return path;
|
45 | }
|
46 | exports.validateApiPath = validateApiPath;
|
47 |
|
48 |
|
49 |
|
50 | function getPathVariables(path) {
|
51 | return path.match(POSSIBLE_VARNAME_PATTERN);
|
52 | }
|
53 | exports.getPathVariables = getPathVariables;
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function toExpressPath(path) {
|
59 |
|
60 |
|
61 | return path.replace(POSSIBLE_VARNAME_PATTERN, '{:$1}').replace('.', '\\.');
|
62 | }
|
63 | exports.toExpressPath = toExpressPath;
|
64 |
|
\ | No newline at end of file |