Skip to content

Generating types and SDKs

For the types to be generated, you need to have the OpenAPI schema available. Which means your API needs to be running without errors.

You can generate the types by running the following command (at root level):

Terminal window
pnpm generate

This will generate the types in the packages/openapi-generator/client folder. Those types are automatically imported by the frontend apps.

To use the generated API client in a frontend app:

import { client } from '@boilerstone/openapi-generator'
client.setConfig({
baseURL: 'http://localhost:3000',
// other options...
})
// Usage example
import { postControllerCreatePost } from '@boilerstone/openapi-generator'
const posts = await postControllerCreatePost()

For your types to be generated, you first need to ensure that the OpenAPI schema generated by your backend is correct.

If your types/zod schemas are never outputed in the OpenAPI schema, then the generator will not be able to generate them!

So some basic rules to follow:

  • Zod schemas without .meta() will not be generated.
  • Zod schemas which are not used by your controllers (in @TypedBody, @TypedParam, @TypedQuery, @TypedRoute.Get, @TypedRoute.Post, @TypedRoute.Put, @TypedRoute.Delete) will not be generated.
  • Except if you use the registerSchema() method from nzoth/server, which will automatically register all schemas used by your controllers.
  • Enum used in a schema, with z.enum(), will not be exported as runtime enums by default, despite the option in the generator config. You need to export a schema with meta, such as
export const postTypeSchema = z.enum(['text', 'image', 'video']).meta({
title: 'Post Type',
description: 'Type of the post',
})`

for the enum to be namely exported.

  • Don’t forget to commit your generated types to the repository, as they will be used for production builds.
  • If your type do not refresh, check out for API errors in your console. If the API is not running, the types generation can’t occur.