Skip to content

Instantly share code, notes, and snippets.

@abodacs
Forked from denisborovikov/routes.ts
Created September 17, 2022 14:44
Show Gist options
  • Save abodacs/53debbe7f87182f77c830d31ba79a25b to your computer and use it in GitHub Desktop.
Save abodacs/53debbe7f87182f77c830d31ba79a25b to your computer and use it in GitHub Desktop.
const routes = {
home: '/',
transactions: '/transactions',
transactionDetails: '/transactions/:uuid',
}
const urls: Record<
keyof typeof routes,
{ get: (params?: any) => string; route: string }
> = new Proxy(routes, {
get(target: any, propKey: any) {
const route = target[propKey]
return {
get: (params: Record<string, string>, query?: Record<string, string>) =>
generatePath(route, params) +
(query ? `?${new URLSearchParams(query).toString()}` : ''),
route,
}
},
})
// <Route exact path={urls.home.route}>
// <HomePage />
// </Route>
// <Link href={urls.transactionDetails.get({ uuid: 123 } )} /> // /transactions/123
@abodacs
Copy link
Author

abodacs commented Jan 6, 2023

@abodacs
Copy link
Author

abodacs commented Jan 6, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment