Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Make sure we can build the full URL using these patterns.
try {
compile(pattern || ServiceClass.route.pattern, {
encode: encodeURIComponent,
})(namedParams)
} catch (e) {
throw Error(
`In example for ${
ServiceClass.name
} at index ${index}, ${e.message.toLowerCase()}`
)
}
// Make sure there are no extra keys.
let keys = []
pathToRegexp(pattern || ServiceClass.route.pattern, keys, {
strict: true,
sensitive: true,
})
keys = keys.map(({ name }) => name)
const extraKeys = Object.keys(namedParams).filter(k => !keys.includes(k))
if (extraKeys.length) {
throw Error(
`In example for ${
ServiceClass.name
} at index ${index}, namedParams contains unknown keys: ${extraKeys.join(
', '
)}`
)
}
if (example.keywords) {
const path = req.path;
const method = req.method;
const routeEntries = Object.entries(openApiContext.expressRouteMap);
for (const [expressRoute, methods] of routeEntries) {
const schema = methods[method];
const routePair = openApiContext.routePair(expressRoute);
const openApiRoute = routePair.openApiRoute;
const keys = [];
const strict = !!req.app.enabled('strict routing');
const sensitive = !!req.app.enabled('case sensitive routing');
const pathOpts = {
sensitive,
strict,
};
const regexp = pathToRegexp(expressRoute, keys, pathOpts);
const matchedRoute = regexp.exec(path);
if (matchedRoute) {
const paramKeys = keys.map(k => k.name);
const paramsVals = matchedRoute.slice(1);
const pathParams = _zipObject(paramKeys, paramsVals);
return {
schema,
// schema may or may not contain express and openApi routes,
// thus we include them here
expressRoute,
openApiRoute,
pathParams,
};
}
return (path: string, options: TokensToRegexpOptions & ParseOptions) => {
const cacheKey = `${options.end}${options.strict}${options.sensitive}`;
const pathCache = cache[cacheKey] || (cache[cacheKey] = {});
if (pathCache[path]) return pathCache[path];
const keys: Key[] = [];
const regexp = pathToRegexp(path, keys, options);
const result = { regexp, keys };
if (cacheCount < cacheLimit) {
pathCache[path] = result;
cacheCount++;
}
return result;
};
};
pathsToMatch.forEach((pathToMatch) => {
if (matched) return;
matched = pathToRegexp(pathToMatch, keys).exec(path);
});
constructor({ selector, method }) {
this.method = method || 'all';
this.selector = selector;
if (!selector) throw Error('selector is needed');
this.keys = [];
this.actions = [];
this.regexp = pathToRegexp(this.selector, this.keys);
this._injectAction();
}
function use (url, f) {
if (f === undefined) {
f = url
url = null
}
var regexp
if (url) {
regexp = pathToRegexp(sanitizePrefixUrl(url), [], {
end: false,
strict: false
})
}
if (Array.isArray(f)) {
for (var val of f) {
middlewares.push({
regexp,
fn: val
})
}
} else {
middlewares.push({
regexp,
fn: f
function createRoute({ name, pattern = name, page = name }) {
const routePattern = replaceIndexRoute(replaceStartingSlash(pattern));
const routePage = replaceStartingSlash(page);
const keys = [];
const regex = pathToRegexp(routePattern, keys);
function match(pathname) {
const matched = regex.exec(pathname);
if (!matched) {
return null;
}
const matchedRouteKeys = matched.slice(1);
return matchedRouteKeys.reduce(function transformParamsToObject(
parameters,
param,
key,
) {
if (param === undefined) {
function sourceToRegex(source: string): { src: string; segments: string[] } {
const keys: Key[] = [];
const r = pathToRegexp(source, keys, { strict: true });
const segments = keys.map(k => k.name).filter(isString);
return { src: r.source, segments };
}
private _push(
method: Method | MethodWildcard,
path: string,
handler: HandlerType,
options: RouteOptions
) {
const keys: Keys = []
if (path === '*') {
path = '(.*)'
}
const regexp = pathToRegexp(path, keys, options)
this.routes.push({ method, path, handler, keys, options, regexp })
return this
}
}