UNPKG

edgerender

Version:
71 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.as_path_view = exports.clean_path = void 0; const utils_1 = require("./utils"); const MethodStrings = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; const clean_path = (pathname) => pathname.replace(/\/+$/, '') || '/'; exports.clean_path = clean_path; function as_path_view([key, view]) { if (typeof view == 'function') { view = { view }; } let allow; if (view.allow == undefined) { allow = new Set(['GET']); } else if (typeof view.allow == 'string') { allow = new Set([view.allow]); } else { allow = new Set(view.allow); } const invalid = [...allow].filter(m => !MethodStrings.includes(m)); if (invalid.length) { throw new Error(`${invalid.join(', ')} is not a valid method, should be: ${MethodStrings.join(', ')}`); } return { path: parse_path(key), view: view.view, allow }; } exports.as_path_view = as_path_view; const group_regex = /{(\w+)(?::(.+?))?}([^{]*)/g; function parse_path(path) { if (!path.startsWith('/')) { path = '/' + path; } if (!path.includes('{')) { if (path.includes('}')) { throw new Error(`invalid path "${path}", "}" with no matching start "{"`); } else { return exports.clean_path(path); } } let regex_str = ''; let first = true; let last_ended = 0; for (const m of path.matchAll(group_regex)) { if (first) { regex_str += utils_1.escape_regex(path.substr(0, m.index)); first = false; } regex_str += `(?<${m[1]}>${make_regex(m[2])})${utils_1.escape_regex(m[3])}`; last_ended = (m.index || 0) + m[0].length; } if (last_ended != path.length) { throw new Error(`invalid path, match expression "${path}" can't be interpreted`); } regex_str = `^${exports.clean_path(regex_str)}$`; return new RegExp(regex_str); } const word_regex = '[\\w\\-]+'; function make_regex(group) { if (!group || group == 'word') { return word_regex; } else if (group == 'int') { return '\\d+'; } else { return group; } } //# sourceMappingURL=request.js.map