Skip to content

Commit

Permalink
feat: add type-safe redirect util to react router plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
oedotme committed Feb 25, 2023
1 parent 8f52a43 commit 33223d9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion examples/react-router/plugin/src/routes.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Fragment, lazy, Suspense } from 'react'
import { createBrowserRouter, Outlet, RouterProvider } from 'react-router-dom'
import { components, hooks } from '@generouted/react-router/client'
import { components, hooks, utils } from '@generouted/react-router/client'

import app from './pages/_app'
import noMatch from './pages/404'
Expand Down Expand Up @@ -86,3 +86,4 @@ export const routes = [{ element: <App />, children: [...config, { path: '*', el
export const Routes = () => <RouterProvider router={createBrowserRouter(routes)} />
export const { Link, Navigate } = components<Path, Params>()
export const { useNavigate, useParams } = hooks<Path, Params>()
export const { rediect } = utils<Path, Params>()
1 change: 1 addition & 0 deletions plugins/react-router/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './components'
export * from './hooks'
export * from './utils'
14 changes: 14 additions & 0 deletions plugins/react-router/src/client/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { generatePath, redirect } from 'react-router-dom'

export const utils = <Path extends string, Params extends Record<string, any>>() => {
type ParamPath = keyof Params

type Init = number | ResponseInit
type RedirectOptions<P> = P extends ParamPath ? [Init & { params: Params[P] }] : [Init & { params?: never }] | []

return {
rediect: <P extends Path>(url: P, ...[options]: RedirectOptions<P>) => {
return redirect(options?.params ? generatePath(url, options.params) : url, options)
},
}
}
3 changes: 2 additions & 1 deletion plugins/react-router/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const template = `// Generouted, changes to this file will be overriden
import { Fragment, lazy, Suspense } from 'react'
import { createBrowserRouter, Outlet, RouterProvider } from 'react-router-dom'
import { components, hooks } from '@generouted/react-router/client'
import { components, hooks, utils } from '@generouted/react-router/client'
// imports
Expand All @@ -17,4 +17,5 @@ export const routes = [{ element: <App />, children: [...config, { path: '*', el
export const Routes = () => <RouterProvider router={createBrowserRouter(routes)} />
export const { Link, Navigate } = components<Path, Params>()
export const { useNavigate, useParams } = hooks<Path, Params>()
export const { rediect } = utils<Path, Params>()
`

0 comments on commit 33223d9

Please sign in to comment.