Created
September 17, 2022 02:56
-
-
Save denisborovikov/3e6e772d9d1897c150639872467d214f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you 👍