How to use the path-to-regexp.pathToRegexp function in path-to-regexp

To help you get started, we’ve selected a few path-to-regexp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github badges / shields / core / base-service / examples.js View on Github external
// 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) {
github cdimascio / express-openapi-validator / src / middlewares / openapi.metadata.ts View on Github external
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,
        };
      }
github querycap / reactorx / @reactorx / router / src / utils.tsx View on Github external
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;
  };
};
github framework7io / framework7 / src / core / modules / router / router-class.js View on Github external
pathsToMatch.forEach((pathToMatch) => {
        if (matched) return;
        matched = pathToRegexp(pathToMatch, keys).exec(path);
      });
github svrxjs / svrx / packages / svrx / lib / router / route.js View on Github external
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();
  }
github fastify / middie / middie.js View on Github external
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
github alexhoma / routex.js / src / server.js View on Github external
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) {
github zeit / now / packages / now-routing-utils / src / superstatic.ts View on Github external
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 };
}
github berstend / tiny-request-router / src / router.ts View on Github external
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
  }
}