updated remix integration docs

This commit is contained in:
dswbx
2025-02-19 14:28:22 +01:00
parent ee7dcd44f4
commit b436758fe6
2 changed files with 55 additions and 38 deletions

View File

@@ -9,28 +9,52 @@ Install bknd as a dependency:
<InstallBknd /> <InstallBknd />
## Serve the API ## Serve the API
Create a new api splat route file at `app/routes/api.$.ts`: Since Remix doesn't support middleware yet, we need a helper file to initialize the App to import from. Create a new file at `app/bknd.ts`:
```ts ```ts app/bknd.ts
// app/routes/api.$.ts import { type RemixBkndConfig, getApp as getBkndApp } from "bknd/adapter/remix";
import { serve } from "bknd/adapter/remix";
const handler = serve({ const config = {
connection: { connection: {
url: "http://localhost:8080" url: "file:data.db"
} }
}); } as const satisfies RemixBkndConfig;
export async function getApp(args?: { request: Request }) {
return await getBkndApp(config, args);
}
export async function getApi(args?: { request: Request }) {
const app = await getApp(args);
if (args) {
const api = app.getApi(args.request);
await api.verifyAuth();
return api;
}
return app.getApi();
}
```
Create a new api splat route file at `app/routes/api.$.ts`:
```ts app/routes/api.$.ts
import { getApp } from "~/bknd";
const handler = async (args: { request: Request }) => {
const app = await getApp(args);
return app.fetch(args.request);
};
export const loader = handler; export const loader = handler;
export const action = handler; export const action = handler;
``` ```
For more information about the connection object, refer to the [Database](/usage/database) guide. For more information about the connection object, refer to the [Database](/usage/database) guide.
Now make sure that you wrap your root layout with the `ClientProvider` so that all components Now make sure that you wrap your root layout with the `ClientProvider` so that all components share the same context. Also add the user context to both the `Outlet` and the provider:
share the same context: ```tsx app/root.tsx
```tsx import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
// app/root.tsx import { useLoaderData, Outlet } from "@remix-run/react";
import { withApi } from "bknd/adapter/remix" import { ClientProvider } from "bknd/client";
import { type Api, ClientProvider } from "bknd/client"; import { getApi } from "~/bknd";
export function Layout(props) { export function Layout(props) {
// nothing to change here, just for orientation // nothing to change here, just for orientation
@@ -39,27 +63,18 @@ export function Layout(props) {
); );
} }
// add the api to the `AppLoadContext` export const loader = async (args: LoaderFunctionArgs) => {
// so you don't have to manually type it again const api = await getApi(args);
declare module "@remix-run/server-runtime" {
export interface AppLoadContext {
api: Api;
}
}
// export a loader that initiates the API
// and passes it down to args.context.api
export const loader = withApi(async (args: LoaderFunctionArgs, api: Api) => {
return { return {
user: api.getUser() user: api.getUser()
}; };
}); };
export default function App() { export default function App() {
const { user } = useLoaderData<typeof loader>(); const data = useLoaderData<typeof loader>();
return ( return (
<ClientProvider user={user}> <ClientProvider user={data.user}>
<Outlet /> <Outlet context={data} />
</ClientProvider> </ClientProvider>
); );
} }
@@ -67,24 +82,29 @@ export default function App() {
## Enabling the Admin UI ## Enabling the Admin UI
Create a new splat route file at `app/routes/admin.$.tsx`: Create a new splat route file at `app/routes/admin.$.tsx`:
```tsx ```tsx app/routes/admin.$.tsx
// app/routes/admin.$.tsx
import { adminPage } from "bknd/adapter/remix"; import { adminPage } from "bknd/adapter/remix";
import "bknd/dist/styles.css"; import "bknd/dist/styles.css";
export default adminPage({ export default adminPage({
config: { basepath: "/admin" } config: {
basepath: "/admin",
logo_return_path: "/../",
color_scheme: "system"
}
}); });
``` ```
## Example usage of the API ## Example usage of the API
Since the API has already been constructed in the root layout, you can now use it in any page: Since the API has already been constructed in the root layout, you can now use it in any page:
```tsx ```tsx app/routes/_index.tsx
// app/routes/_index.tsx
import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
import { useLoaderData } from "@remix-run/react"; import { useLoaderData } from "@remix-run/react";
import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
import { getApi } from "~/bknd";
export const loader = async ({ context: { api } }: LoaderFunctionArgs) => { export const loader = async (args: LoaderFunctionArgs) => {
// use authentication from request
const api = await getApi(args);
const { data } = await api.data.readMany("todos"); const { data } = await api.data.readMany("todos");
return { data, user: api.getUser() }; return { data, user: api.getUser() };
}; };

View File

@@ -31,9 +31,6 @@ export const loader = async (args: LoaderFunctionArgs) => {
export default function App() { export default function App() {
const data = useLoaderData<typeof loader>(); const data = useLoaderData<typeof loader>();
// add user to the client provider to indicate
// that you're authed using cookie
return ( return (
<ClientProvider user={data.user}> <ClientProvider user={data.user}>
<Outlet context={data} /> <Outlet context={data} />