b9865ba32e
* Add check: true behavior to custom routes * Update adding dev routes * Add checking of pages and dynamic routes for check: true * Fix hasPage binding * Add tests for check: true behavior * Update regex checking * Make changes based on review * Update to handle rewrites in serverless loader * Update to not change req.url * Make sure to always parse dynamic route params * Export all of pathToRegexp from path-match Co-authored-by: Joe Haddad <timer150@gmail.com>
33 lines
798 B
TypeScript
33 lines
798 B
TypeScript
import * as pathToRegexp from 'path-to-regexp'
|
|
|
|
export { pathToRegexp }
|
|
|
|
export default (customRoute = false) => {
|
|
return (path: string) => {
|
|
const matcher = pathToRegexp.match(path, {
|
|
sensitive: false,
|
|
delimiter: '/',
|
|
...(customRoute ? { strict: true } : undefined),
|
|
decode: decodeParam,
|
|
})
|
|
|
|
return (pathname: string | undefined, params?: any) => {
|
|
const res = pathname == null ? false : matcher(pathname)
|
|
if (!res) {
|
|
return false
|
|
}
|
|
return { ...params, ...res.params }
|
|
}
|
|
}
|
|
}
|
|
|
|
function decodeParam(param: string) {
|
|
try {
|
|
return decodeURIComponent(param)
|
|
} catch (_) {
|
|
const err = new Error('failed to decode param')
|
|
// @ts-ignore DECODE_FAILED is handled
|
|
err.code = 'DECODE_FAILED'
|
|
throw err
|
|
}
|
|
}
|