@devup-api/rsbuild-plugin
v0.1.16
Published
Rsbuild plugin for devup-api that generates TypeScript types from OpenAPI schemas.
Readme
@devup-api/rsbuild-plugin
Rsbuild plugin for devup-api that generates TypeScript types from OpenAPI schemas.
Installation
npm install @devup-api/rsbuild-plugin @devup-api/fetchUsage
Basic Setup
Add the plugin to your rsbuild.config.ts:
import { defineConfig } from '@rsbuild/core'
import { devupApiRsbuildPlugin } from '@devup-api/rsbuild-plugin'
export default defineConfig({
plugins: [devupApiRsbuildPlugin()],
})With Options
import { defineConfig } from '@rsbuild/core'
import { devupApiRsbuildPlugin } from '@devup-api/rsbuild-plugin'
export default defineConfig({
plugins: [
devupApiRsbuildPlugin({
openapiFile: './api/openapi.json',
convertCase: 'camel',
tempDir: 'temp'
})
],
})Options
interface DevupApiOptions {
/**
* OpenAPI file path
* @default 'openapi.json'
*/
openapiFile?: string
/**
* Temporary directory for storing generated files
* @default 'df'
*/
tempDir?: string
/**
* Case conversion type for API endpoint names and parameters
* @default 'camel'
*/
convertCase?: 'snake' | 'camel' | 'pascal' | 'maintain'
/**
* Whether to make all properties non-nullable by default
* @default false
*/
requestDefaultNonNullable?: boolean
/**
* Whether to make all request properties non-nullable by default
* @default true
*/
responseDefaultNonNullable?: boolean
/**
* Generate operationId-based Server Action wrappers.
* Disable with false or { enabled: false }.
* @default true
*/
serverActions?: boolean | {
enabled?: boolean
baseUrl?: string
}
}What It Does
- Reads your
openapi.jsonfile during build - Generates TypeScript interface definitions (
api.d.ts) - Creates a URL map and injects it as
process.env.DEVUP_API_URL_MAPvia Rsbuild's define feature - Generates Server Actions in
df/server.tsby default - Makes types available for use with
@devup-api/fetch
TypeScript Configuration
To use the generated types, add the generated type definitions to your tsconfig.json:
{
"compilerOptions": {
// ... your compiler options
},
"include": [
"src",
"df/**/*.d.ts"
]
}Note: If you've customized
tempDirin plugin options, adjust the path accordingly (e.g.,"your-temp-dir/**/*.d.ts").
Using the Generated Types
After the plugin runs, you can use the generated types with @devup-api/fetch:
import { createApi } from '@devup-api/fetch'
const api = createApi('https://api.example.com')
// Types are automatically available
const users = await api.get('getUsers', {})Cold Typing vs Bold Typing
devup-api uses a two-phase typing system:
- Cold Typing: Before the build runs, types are
anyto prevent type errors. Your code compiles and runs smoothly. - Bold Typing: After the build runs and
api.d.tsis generated, full type safety is enforced with strict type checking.
This ensures you can start coding immediately without waiting for the build, while still getting full type safety in production.
License
Apache 2.0
