Skip to main content
Version: 3.x

SvelteKit Adapter

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

Installation

npm install @zenstackhq/server

Mounting the API

There are two ways to mount the ZenStack API in SvelteKit: as API routes or server hooks (deprecated).

API Routes

You can mount the services at specific path as API routes like the following:

/src/routes/api/model/[...path]/+server.ts
import { SvelteKitRouteHandler } from "@zenstackhq/server/sveltekit";
import { RPCApiHandler } from '@zenstackhq/server/api';
import { getSessionUser } from '~/auth';
import { db } from '~/db';
import { schema } from '~/zenstack/schema';

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

export const GET = handler;
export const POST = handler;
export const PUT = handler;
export const DELETE = handler;
export const PATCH = handler;

Please note that the wildcard route parameter must be named path as expected by the adapter implementation.

The API router handler takes the following options to initialize:

  • getClient (required)

    (event: RequestEvent) => 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.

Server Hooks

warning

The server hooks method is deprecated and will be removed in a future release. Use API routes instead.

You can mount the API by creating SvelteKit server hooks like:

/src/hooks.server.ts
import { SvelteKitHandler } from '@zenstackhq/server/sveltekit';
import { RPCApiHandler } from '@zenstackhq/server/api';
import { getSessionUser } from '~/auth';
import { db } from '~/db';
import { schema } from '~/zenstack/schema';

export const handle = SvelteKitHandler({
apiHandler: new RPCApiHandler({ schema }),
prefix: '/api/model',
// getSessionUser extracts the current session user from the request, its
// implementation depends on your auth solution
getClient: (event) => db.$setAuth(getSessionUser(event)),
});
tip

You can use the sequence helper to compose multiple server hooks.

The hooks handler takes the following options to initialize:

  • prefix

    string

    Prefix for the mounted API endpoints. E.g.: /api/model.

  • getClient (required)

    (event: RequestEvent) => 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