Client

lucent.client() is a typed client factory.

It wraps Eden Treaty and returns the same client shape as treaty<LucentApp>(...).

For a standalone fetch-only client with no Elysia or Eden dependency, use the separate lucent-sdk package with a generated sdk.json manifest.

import { lucent } from "@codesordinatestudio/lucent";
import { redirect } from "react-router";
import type { LucentApp } from "./lucent-types";

const API_URL = "http://localhost:3000";

export const sdkClient = (token?: string) =>
  lucent.client<LucentApp>({
    url: API_URL,
    token,
    onResponse: (response) => {
      if (response.status === 401 && !response.url.includes("login")) {
        redirect("/?error=Your session has expired. Please log in again.");
      }
    },
  });

That means you can keep the same usage pattern as Eden Treaty:

const client = sdkClient(token);
const { data } = await client.api.posts.get();

How token Is Passed

When you call:

sdkClient("abc123");

Lucent sends:

Authorization: Bearer abc123

Internally, lucent.client() turns:

{ token: "abc123" }

into:

headers: {
  Authorization: "Bearer abc123";
}

Server Usage

In a server environment, use the same pattern.

Create the client inside the request or server function, then pass the token you already have for that request:

export async function loadUser(token?: string) {
  const client = sdkClient(token);
  return client.api.users.me.get();
}

The important part is not whether it runs in the browser or on the server. The important part is that the token is passed into your client factory when you create the client for that request.

Cookies

lucent.client() also defaults to:

fetch: {
  credentials: "include";
}

So cookie-based auth works naturally in the browser.

If you are making server-to-server requests and need cookie auth there too, forward the cookie header from the incoming request with headers.

Notes

  • token is optional
  • onRequest, onResponse, headers, fetch, and fetcher are forwarded to Eden Treaty
  • the returned client is fully typed from LucentApp
  • use client.api... the same way you already do with treaty(...)

Standalone SDK

Lucent can also generate a portable sdk.json manifest from the same metadata pipeline that powers lucent-types.ts.

That manifest is intended to be downloaded or bundled into a client app, then consumed by the standalone fetch-based lucent-sdk package:

import manifest from "./sdk.json";
import { createLucentSDK } from "lucent-sdk";

const sdk = createLucentSDK({
  baseUrl: "http://localhost:3000",
  manifest,
});

const result = await sdk.collection("posts").list({ page: 1 });

Every request resolves to { data, error, status }.