UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

99 lines 3.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PathAnalyzer = void 0; const common_1 = require("@nestjs/common"); const path_1 = __importDefault(require("path")); const path_to_regexp_1 = require("path-to-regexp"); var PathAnalyzer; (function (PathAnalyzer) { PathAnalyzer.join = (...args) => "/" + _Trim(path_1.default .join(...args.filter((s) => !!s.length)) .split("\\") .join("/")); PathAnalyzer.joinWithGlobalPrefix = (props) => { var _a; return PathAnalyzer.join(isGlobalPrefixExcluded(props.exclude, (_a = props.excludePath) !== null && _a !== void 0 ? _a : props.path, props.method) ? "" : props.globalPrefix, props.path); }; PathAnalyzer.escape = (str) => { const args = _Parse(str); if (args === null) return null; return ("/" + args .map((arg) => (arg.type === "param" ? `:${arg.value}` : arg.value)) .join("/")); }; PathAnalyzer.parameters = (str) => { const args = _Parse(str); if (args === null) return null; return args.filter((arg) => arg.type === "param").map((arg) => arg.value); }; function _Parse(str) { const tokens = (() => { try { return (0, path_to_regexp_1.parse)(path_1.default.join(str).split("\\").join("/")); } catch (_a) { return null; } })(); if (tokens === null) return null; const output = []; for (const key of tokens) { if (typeof key === "string") output.push({ type: "path", value: _Trim(key), }); else if (typeof key.name === "number" || _Trim(key.name) === "") return null; else output.push({ type: "param", value: _Trim(key.name), }); } return output; } function _Trim(str) { if (str[0] === "/") str = str.substring(1); if (str[str.length - 1] === "/") str = str.substring(0, str.length - 1); return str; } function isGlobalPrefixExcluded(exclude, path, method) { if (exclude === undefined) return false; const requestMethod = REQUEST_METHODS[method]; if (requestMethod === undefined) return false; const location = PathAnalyzer.join(path); return exclude.some((route) => { var _a; const routeMethod = (_a = route.requestMethod) !== null && _a !== void 0 ? _a : route.method; if (routeMethod !== undefined && routeMethod !== common_1.RequestMethod.ALL && routeMethod !== requestMethod) return false; if (route.pathRegex instanceof RegExp) return route.pathRegex.test(PathAnalyzer.join(location)); return PathAnalyzer.join(route.path) === location; }); } })(PathAnalyzer || (exports.PathAnalyzer = PathAnalyzer = {})); const REQUEST_METHODS = { DELETE: common_1.RequestMethod.DELETE, GET: common_1.RequestMethod.GET, HEAD: common_1.RequestMethod.HEAD, OPTIONS: common_1.RequestMethod.OPTIONS, PATCH: common_1.RequestMethod.PATCH, POST: common_1.RequestMethod.POST, PUT: common_1.RequestMethod.PUT, }; //# sourceMappingURL=PathAnalyzer.js.map