Skip to main content
Version: 3.x

Nuxt Adapter

The @zenstackhq/server/nuxt module provides a quick way to install a full set of CRUD API onto Nuxt 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

You can mount the API by creating a Nuxt server event handler like:

/server/api/model/[...].ts
import { createEventHandler } from '@zenstackhq/server/nuxt';
import { RPCApiHandler } from '@zenstackhq/server/api';
import { getSessionUser } from '~/auth';
import { client } from '~/db';
import { schema } from '~/zenstack/schema';

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

The Nuxt event handler takes the following options to initialize:

  • getClient (required)

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