Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with the path-to-regexp lib #77

Open
iliyaZelenko opened this issue May 1, 2020 · 1 comment
Open

Usage with the path-to-regexp lib #77

iliyaZelenko opened this issue May 1, 2020 · 1 comment

Comments

@iliyaZelenko
Copy link

Vue router uses path-to-regexp.

Example proposal for this package:

  {
    from: '/library/:category/:book', to: '/new-library/:book'
  }

It’s convenient when you can use this kind of parameters in URLs.

@iliyaZelenko
Copy link
Author

I created my middleware for redirects:

import { pathToRegexp, compile, parse } from 'path-to-regexp'

const redirectsBackwardCompatibility = [
  {
    from: '/article',
    to: '/library'
  },
  {
    from: '/article/post/:slug',
    to: '/library/article/:slug'
  },
  {
    from: '/community',
    to: '/communities'
  },
  {
    from: '/community/group/:group',
    to: '/communities/group/:group'
  },
  {
    from: '/community/group/:group/:section',
    to: '/communities/group/:group#:section'
  },
  {
    from: '/community/lenta/:community/:page',
    to: '/community/:community/page/:page'
  },
  {
    from: '/community/lenta_cat/:community/:category/:page',
    to: '/community/:community/category/:category/page/:page'
  },
  {
    from: '/user/lenta/:login/:page',
    to: '/user/:login/page/:page'
  },
  {
    from: '/user/lenta_cat/:login/:category/:page',
    to: '/user/:login/category/:category/page/:page'
  }
]

export default function (req, res, next) {
  const url = decodeURI(req.url)
  const matchedRule = redirectsBackwardCompatibility.find(i =>
    pathToRegexp(i.from).exec(url)
  )

  if (!matchedRule) return next()

  const re = pathToRegexp(matchedRule.from)
  const toNewPath = compile(matchedRule.to)
  const matchedRuleExecResult = re.exec(url)!
  // первый элемент - не нужная строка
  const paramsValues = matchedRuleExecResult.slice(1)
  const paramsKeys = parse(matchedRule.to)
    .filter(i => typeof i === 'object' && i.name)
    // @ts-ignore
    .map(i => i.name)
  const paramsFinal = paramsKeys.reduce((prev, curr, index) => {
    return {
      ...prev,
      [curr]: paramsValues[index]
    }
  }, {})
  const finalPath = toNewPath(paramsFinal)

  try {
    res.setHeader('Location', encodeURI(finalPath))
  } catch (error) {
    // Not passing the error as it's caused by URL that was user-provided so we
    // can't do anything about the error.
    return next()
  }

  res.statusCode = 301
  res.end()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant