createLocalClient
The createLocalClient function allows you to use your typed API client in a server-side environment (like Next.js Server Components, getServerSideProps, or scripts). Instead of making actual HTTP network requests, it calls your request handlers directly in-memory while preserving the same convenient API and type safety as the browser client.
You typically create a local client instance alongside your API definition or in a server-only utility file.
import { createLocalClient } from "@farbenmeer/tapi/server";import { api } from "./api"; // Your ApiDefinition
export const serverClient = createLocalClient(api);Then you can use it in your server code:
// app/page.tsx (Server Component)import { serverClient } from "@/server-client";
export default async function Page() { // Calls the handler function directly, no HTTP request involved const users = await serverClient.users.get();
return ( <ul> {users.map(user => <li key={user.id}>{user.name}</li>)} </ul> );}Signature
Section titled “Signature”function createLocalClient<Routes>( api: ApiDefinition<Routes>): Client<Routes>Parameters
Section titled “Parameters”Type: ApiDefinition
The API definition object returned by defineApi(). This contains the complete map of your routes and handlers.
How It Works
Section titled “How It Works”createLocalClient creates a standard createFetchClient but overrides the internal fetch implementation.
- It initializes your API using
createRequestHandler. - When you call a method like
.get(), it constructs aRequestobject representing that call. - It passes this request directly to the request handler logic.
- It parses the returned
Responseand returns the typed data.
This eliminates the overhead of serialization, network latency, and self-signed certificate issues that can occur when a server tries to fetch from itself via HTTP.
Context Mocking
Section titled “Context Mocking”Since createLocalClient runs inside your server process, the “requests” it generates are synthetic. They will have a base URL of http://localhost.
If your authorize functions or handlers rely on specific headers (like Cookie or Authorization) that would normally come from an incoming browser request, you need to make sure you pass this context explicitly when using the local client:
const headerStore = await headers()serverClient.users.post({ name: "Milo Mock"}, { headers: { Authorization: headerStore.get("Authorization") }})