How to use the path-to-regexp.parse 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 dadi / web / test / unit / page.js View on Github external
paths.forEach(function(path) {
      p.routes = [path]

      var tokens = pathToRegexp.parse(path.path)
      var parts = {}

      // console.log(tokens)
      tokens.forEach(function(token) {
        if (typeof token === "object") {
          parts[token.name] = "whatever"
        }
      })

      var url = p.toPath(parts)
      var expected = pathToRegexp.compile(path.path)(parts)
      // console.log(url)

      url.should.eql(expected)
    })
github rill-js / rill / src / index.ts View on Github external
function toReg(pathname: string, keys: any[], options: any): RegExp {
  // First parse path into tokens.
  const tokens = parse(pathname);

  // Find the last token (checking for splat params).
  const splat: any = tokens[tokens.length - 1];

  // Check if the last token is a splat and make it optional.
  if (splat && splat.asterisk) {
    splat.optional = true;
  }

  // Convert the tokens to a regexp.
  const re = tokensToRegExp(tokens, options);

  // Assign keys to from regexp.
  (re as any).keys = keys;
  for (let i = 0, len = tokens.length; i < len; i++) {
    if (typeof tokens[i] === "object") {
github zuiidea / elegant / src / utils / request.js View on Github external
const fetch = (options) => {
  const { method = 'get', data = {} } = options
  let { url } = options
  const cloneData = lodash.cloneDeep(data)
  try {
    let domin = ''
    if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
      domin = url.match(/[a-zA-z]+:\/\/[^/]*/)[0]
      url = url.slice(domin.length)
    }
    const match = pathToRegexp.parse(url)
    url = pathToRegexp.compile(url)(data)
    for (const item of match) {
      if (item instanceof Object && item.name in cloneData) {
        delete cloneData[item.name]
      }
    }
    url = domin + url
  } catch (e) {
    console.log(e)
  }

  switch (method.toLowerCase()) {
    case 'get':
      return axios.get(url, {
        params: cloneData,
      })
github badges / shields / frontend / components / customizer / path-builder.tsx View on Github external
  const [tokens] = useState(() => parse(pattern))
  const [namedParams, setNamedParams] = useState(() =>
github nestjs / swagger / lib / swagger-explorer.ts View on Github external
private validateRoutePath(path: string): string {
    if (isUndefined(path)) {
      return '';
    }
    if (Array.isArray(path)) {
      path = head(path);
    }
    let pathWithParams = '';
    for (const item of pathToRegexp.parse(path)) {
      pathWithParams += isString(item) ? item : `${item.prefix}{${item.name}}`;
    }
    return pathWithParams === '/' ? '' : validatePath(pathWithParams);
  }
github njam / nuxt-i18n-module / src / index.js View on Github external
routesRouter = routesRouter.filter(route => {
      let tokens = pathToRegexp.parse(route)
      let params = tokens.filter(token => typeof token === 'object')
      return params.length === 1 && params[0].name === 'lang'
    })
    routesRouter.forEach(routeWithLang => {
github streamr-dev / streamr-platform / app / src / utils / defineRoute.js View on Github external
return (params: ?Object): string => {
        if (params) {
            const tokenNames = p2r.parse(route).map((t) => t.name).filter(Boolean)
            const queryKeys = Object.keys(params).filter((key) => !tokenNames.includes(key))

            return `${p2r.compile(route)(params)}?${qs.stringify(pick(params, queryKeys))}`.replace(/\?$/, '')
        }

        return route
    }
}
github zuiidea / antd-admin / src / utils / request.js View on Github external
export default function request(options) {
  let { data, url, method = 'get' } = options
  const cloneData = cloneDeep(data)

  try {
    let domain = ''
    const urlMatch = url.match(/[a-zA-z]+:\/\/[^/]*/)
    if (urlMatch) {
      ;[domain] = urlMatch
      url = url.slice(domain.length)
    }

    const match = pathToRegexp.parse(url)
    url = pathToRegexp.compile(url)(data)

    for (const item of match) {
      if (item instanceof Object && item.name in cloneData) {
        delete cloneData[item.name]
      }
    }
    url = domain + url
  } catch (e) {
    message.error(e.message)
  }

  options.url = url
  options.params = cloneData
  options.cancelToken = new CancelToken(cancel => {
    window.cancelRequest.set(Symbol(Date.now()), {
github liuxx001 / bird-front / src / utils / request.js View on Github external
let {
    method = 'post',
    data,
    fetchType,
    url
  } = options

  const cloneData = lodash.cloneDeep(data)

  try {
    let domin = ''
    if (url.match(/[a-zA-z]+:\/\/[^/]*/)) {
      domin = url.match(/[a-zA-z]+:\/\/[^/]*/)[0]
      url = url.slice(domin.length)
    }
    const match = pathToRegexp.parse(url)
    url = pathToRegexp.compile(url)(data)
    for (let item of match) {
      if (item instanceof Object && item.name in cloneData) {
        delete cloneData[item.name]
      }
    }
    url = domin + url
  } catch (e) {
    message.error(e.message)
  }

  if (fetchType === 'JSONP') {
    return new Promise((resolve, reject) => {
      jsonp(url, {
        param: `${qs.stringify(data)}&callback`,
        name: `jsonp_${new Date().getTime()}`,