aws-lambda-router icon indicating copy to clipboard operation
aws-lambda-router copied to clipboard

How can I create path vars that aren't separated with "/"

Open sheldonj opened this issue 5 years ago • 0 comments

I have a route that looks something like this:

path: "thing/{var1}/{var2}/{var3}/{name}.{extension}"

But right now the code that extract path names and path values doesn't support this.

Now my regex foo is super lame. I was hoping someone could help me identify the fix for this. If it's something this lib doesnt want to support I can fork.

if it is, I dont mind submitted a PR with some guidance.

const extractPathValues = (pathExpression: string, httpPath: string) => {
  const pathExpressionPattern = pathExpression.replace(/{[\w]+}|:[\w]+/g, '([^/]+)')
  const pathValueRegex = new RegExp(`^${pathExpressionPattern}$`)
  const pathValues = pathValueRegex.exec(httpPath)
  return pathValues && pathValues.length > 0 ? pathValues.slice(1) : null
}

const extractPathNames = (pathExpression: string) => {
  const pathExpressionPattern = pathExpression.replace(/{[\w.]+}|:[\w.]+/g, '[:{]([\\w]+)}?')
  const pathNameRegex = new RegExp(`^${pathExpressionPattern}$`)
  const pathNames = pathNameRegex.exec(pathExpression)
  return pathNames && pathNames.length > 0 ? pathNames.slice(1) : null
}

sheldonj avatar Nov 10 '20 21:11 sheldonj