-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { ReactNode } from "react"; | ||
|
||
interface Context {} // TODO: AppLoadContext? | ||
|
||
type MaybePromise<T> = T | Promise<T>; | ||
|
||
type Serializable = | ||
| undefined | ||
| null | ||
| boolean | ||
| string | ||
| symbol | ||
| number | ||
| Array<Serializable> | ||
| { [key: PropertyKey]: Serializable } | ||
| bigint | ||
| Date | ||
| URL | ||
| RegExp | ||
| Error | ||
| Map<Serializable, Serializable> | ||
| Set<Serializable> | ||
| Promise<Serializable>; | ||
|
||
export type TypedResponse<T = unknown> = Omit<Response, "json"> & { | ||
json(): Promise<T>; | ||
}; | ||
|
||
type DataFunctionReturnValue = | ||
| Serializable | ||
// TODO: | TypedDeferredData<Record<string, unknown>> // do we want to allow `defer()` for back compat? | ||
| TypedResponse<Record<string, unknown>>; | ||
|
||
// TODO: clientLoader and all the other route module export APIs (meta, handle, ErrorBoundary, etc.) | ||
|
||
export type ResponseStub = { | ||
status: number | undefined; | ||
headers: Headers; | ||
}; | ||
|
||
// loader | ||
type LoaderArgs<Param extends string> = { | ||
context: Context; | ||
request: Request; | ||
params: Record<Param, string>; | ||
response: ResponseStub; | ||
}; | ||
export type Loader<Param extends string> = ( | ||
args: LoaderArgs<Param> | ||
) => MaybePromise<DataFunctionReturnValue>; | ||
|
||
// action | ||
type ActionArgs<Param extends string> = { | ||
context: Context; | ||
request: Request; | ||
params: Record<Param, string>; | ||
response: ResponseStub; | ||
}; | ||
export type Action<Param extends string> = ( | ||
args: ActionArgs<Param> | ||
) => MaybePromise<DataFunctionReturnValue>; | ||
|
||
type Component<P extends string, L extends Loader<P>> = (args: { | ||
params: string extends P ? Record<never, string> : Record<P, string>; | ||
data: Awaited<ReturnType<L>>; | ||
}) => ReactNode; | ||
|
||
export const defineRoute$ = < | ||
const P extends string, | ||
L extends Loader<P>, | ||
A extends Action<P> | ||
>(route: { | ||
params?: P[]; | ||
loader?: L; | ||
action?: A; | ||
component?: Component<NoInfer<P>, NoInfer<L>>; | ||
}) => route; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
```ts | ||
export default createRoute$({ | ||
params: ["id"], // we want to know params at _build_ time! | ||
search: ["brand"], | ||
loader:, | ||
component:, | ||
}) | ||
``` | ||
|
||
```ts | ||
// routes.ts | ||
export default flatRoutes() | ||
|
||
export default remixRoutesDsl({ | ||
index: , | ||
layout: , | ||
"/about": "" | ||
}) | ||
|
||
let routes = [ | ||
{ | ||
layout: "./auth.tsx", | ||
children: [ | ||
{ | ||
path: "login", | ||
file: "./auth/login.tsx", | ||
}, | ||
{ | ||
path: "logout", | ||
file: "./auth/logout.tsx", | ||
}, | ||
{ | ||
path: "signup", | ||
file: "./auth/signup.tsx", | ||
}, | ||
], | ||
}, | ||
]; | ||
export default routes | ||
|
||
``` | ||
|
||
defineRoutes([ | ||
layout("./auth.tsx", [ | ||
route("login", "./auth/login.tsx"), | ||
route("logout", "./auth/logout.tsx"), | ||
route("signup", "./auth/signup.tsx"), | ||
]), | ||
layout("./app.tsx", [ | ||
route("home", "./app/dashboard.tsx"), | ||
route("settings", "./app/settings.tsx"), | ||
route("projects/:id", "./app/project.tsx"), | ||
layout("./app/calendar.tsx", [ | ||
route(":year/:month", "./app/calendar.tsx"), | ||
route("today", "./app/calendar.tsx"), | ||
route("new-calendar", "./app/new-calendar.tsx"), | ||
]), | ||
]), | ||
layout("./public-layout.tsx", [ | ||
index("./landing.tsx"), | ||
route("about", "./pages/about.tsx"), | ||
route("contact", "./pages/about.tsx"), | ||
]), | ||
}); |
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