Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active August 5, 2024 23:16
Show Gist options
  • Select an option

  • Save wilmoore/f9cf16b412d6eab2c6049a9b9994969a to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/f9cf16b412d6eab2c6049a9b9994969a to your computer and use it in GitHub Desktop.
Software Engineering :: Programming :: Languages :: JavaScript :: Library :: uri-templates :: example :: URI Template Routes

Software Engineering :: Programming :: Languages :: JavaScript :: Library :: uri-templates :: example :: URI Template Routes

⪼ Made with 💜 by Polyglot.

related

URI Template Routes

Get URI Template Parameter Keys
> Array.from('/users/{id}/comments/{commentId}'.matchAll(/{(?<parameterName>[0-0a-zA-Z]+)}/g))
[
  [
    '{id}',
    'id',
    index: 7,
    input: '/users/{id}/comments/{commentId}',
    groups: [Object: null prototype] { parameterName: 'id' }
  ],
  [
    '{commentId}',
    'commentId',
    index: 21,
    input: '/users/{id}/comments/{commentId}',
    groups: [Object: null prototype] { parameterName: 'commentId' }
  ]
]
Generate regular expression from URI Template to match URI Template (i.e. URI Template route as object key)
RegExp('/users/{id}/comments/{commentId}'.replace(/{(?<name>[0-9a-zA-Z]+)}/g, '{(?<$<name>>[0-9a-zA-Z]+)}'))
/\/users\/{(?<id>[0-9a-zA-Z]+)}\/comments\/{(?<commentId>[0-9a-zA-Z]+)}/
URI Templates
> const uriTemplate = require('uri-templates')

> const match = (requestUrlPathComponent) => Object.keys(routes).filter((templateString) => requestUrlPathComponent === templateString || uriTemplate(templateString).varNames.length && uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri(requestUrlPathComponent) || {})))

> let routes = {}
undefined

> routes['/users/{userId}/comments/{commentId}'] = { id: 1 }
{ id: 1 }

> routes['/users/{id}'] = { id: 2 }
{ id: 2 }

> routes['/users'] = { id: 3 }
{ id: 3 }

> routes['/search/{term}/{?q,lang}'] = { id: 4 }
{ id: 4 }

> routes
{
  '/users/{userId}/comments/{commentId}': { id: 1 },
  '/users/{id}': { id: 2 },
  '/users': { id: 3 },
  '/search/{term}/{?q,lang}': { id: 4 }
}

> Object.keys(routes).filter((templateString) => uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri('/search/proghorn/') || {})))


> Object.keys(routes).filter((templateString) => uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri('/users/123') || {})))
[ '/users/{id}' ]

> Object.keys(routes).filter((templateString) => uriTemplate(templateString).varNames.length && uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri('/users/123') || {})))

> Object.keys(routes).filter((templateString) => templateString === '/users' || uriTemplate(templateString).varNames.length && uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri('/users') || {})))

> Object.keys(routes).filter((templateString) => '/users' === templateString || uriTemplate(templateString).varNames.length && uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri('/users') || {})))
[ '/users' ]
...
const requested = (endpoint) => {
  return Object.keys(routes).filter((templateString) => templateString === endpoint || uriTemplate(templateString).varNames.length && uriTemplate(templateString).varNames.every((parameterName) => parameterName in (uriTemplate(templateString).fromUri(endpoint) || {}))).pop() || ''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment