@devup-api/webpack-plugin
v0.1.16
Published
Webpack plugin for devup-api that generates TypeScript types from OpenAPI schemas.
Readme
@devup-api/webpack-plugin
Webpack plugin for devup-api that generates TypeScript types from OpenAPI schemas.
Installation
npm install @devup-api/webpack-plugin @devup-api/fetchUsage
Basic Setup
Add the plugin to your webpack.config.js:
const { devupApiWebpackPlugin } = require('@devup-api/webpack-plugin')
module.exports = {
plugins: [new devupApiWebpackPlugin()],
}With Options
const { devupApiWebpackPlugin } = require('@devup-api/webpack-plugin')
module.exports = {
plugins: [
new devupApiWebpackPlugin({
openapiFile: './api/openapi.json',
convertCase: 'camel',
tempDir: 'temp'
})
],
}TypeScript Configuration
If using TypeScript, you can use:
import { devupApiWebpackPlugin } from '@devup-api/webpack-plugin'
import type { Configuration } from 'webpack'
const config: Configuration = {
plugins: [new devupApiWebpackPlugin()],
}
export default configOptions
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 (before compilation) - Generates TypeScript interface definitions (
api.d.ts) - Creates a URL map and injects it as
process.env.DEVUP_API_URL_MAPvia webpack DefinePlugin - 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
