Skip to main content
Version: 3.x

TanStack Start Adapter

TanStack Start is a full-stack React framework powered by TanStack Router, offering full-document SSR, streaming, server functions, and bundling capabilities.

The @zenstackhq/server/tanstack-start module provides a quick way to install a full set of CRUD API onto TanStack Start apps. Combined with ZenStack's powerful access policies, you can achieve a secure data backend without manually coding it.

This feature is contributed by @digoburigo.

Installation

npm install @zenstackhq/server

Mounting the API

You can use the TanStackStartHandler to create a handler for your API routes. TanStack Start uses file-based routing, so you'll typically create a catch-all route to handle all CRUD operations:

app/routes/api/$.ts
import { createFileRoute } from '@tanstack/react-router'
import { TanStackStartHandler } from '@zenstackhq/server/tanstack-start'
import { RPCApiHandler } from '@zenstackhq/server/api';
import { getSessionUser } from '~/auth';
import { client } from '~/db';
import { schema } from '~/zenstack/schema';

const handler = TanStackStartHandler({
apiHandler: new RPCApiHandler({ schema }),
// getSessionUser extracts the current session user from the request, its
// implementation depends on your auth solution
getClient: (request) => client.$setAuth(getSessionUser(request)),
})

export const Route = createFileRoute('/api/$')({
server: {
handlers: {
GET: handler,
POST: handler,
PUT: handler,
PATCH: handler,
DELETE: handler,
}
}
})

The TanStack Start handler takes the following options to initialize:

  • getClient (required)

    (request: Request) => ClientContract<Schema> | Promise<ClientContract<Schema>>

    A callback for getting a ZenStackClient instance for talking to the database. Usually you'll return a client instance with access policy enabled and user identity bound.

  • apiHandler (required)

    ApiHandler

    The API handler instance that determines the API specification.

Error Handling

Refer to the specific sections for RPC Handler and RESTful Handler.

Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below