@kubb/swagger-ts 🦙 ​
With the Swagger TypeScript plugin you can create TypeScript types based on a Swagger file.
Installation ​
bun add @kubb/swagger-ts @kubb/swagger
bun add @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-ts @kubb/swagger
pnpm add @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-ts @kubb/swagger
npm install @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-ts @kubb/swagger
yarn add @kubb/swagger-ts @kubb/swagger
Options ​
output ​
Relative path to save the TypeScript types.
When output is a file it will save all models inside that file else it will create a file per schema item.
INFO
Type: string
Default: 'types'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({ output: './models' }),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({ output: './models' }),
],
})
groupBy ​
Group the TypeScript types based on the provided name.
type ​
Tag will group based on the operation tag inside the Swagger file.
Type: 'tag'
Required: true
output ​
Relative path to save the grouped TypeScript Types. {{tag}}
will be replaced by the current tagName.
Type: string
Example: models/{{tag}}Controller
=> models/PetController
Default: '${output}/{{tag}}Controller'
INFO
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
output: './types',
groupBy: { type: 'tag', output: './types/{{tag}}Controller' },
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
output: './types',
groupBy: { type: 'tag', output: './types/{{tag}}Controller' },
}),
],
})
enumType ​
Choose to use enum
or as const
for enums. asConst
will use camelCase for the naming. asPascalConst
will use PascalCase for the naming.
Type
enum PetType {
Dog = 'dog',
Cat = 'cat',
}
enum PetType {
Dog = 'dog',
Cat = 'cat',
}
const petType = {
Dog: 'dog',
Cat: 'cat',
} as const
const petType = {
Dog: 'dog',
Cat: 'cat',
} as const
const PetType = {
Dog: 'dog',
Cat: 'cat',
} as const
const PetType = {
Dog: 'dog',
Cat: 'cat',
} as const
INFO
Type: 'enum' | 'asConst' | 'asPascalConst'
Default: 'asConst'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
enumType: 'enum',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
enumType: 'enum',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
enumType: 'asConst',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
enumType: 'asConst',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
enumType: 'asPascalConst',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
enumType: 'asPascalConst',
}),
],
})
dateType ​
Choose to use date
or datetime
as JavaScript Date
instead of string
.
type
date: string
date: string
date: Date
date: Date
INFO
Type: 'string' | 'date'
Default: 'string'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
dateType: 'string',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
dateType: 'string',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
dateType: 'date',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
dateType: 'date',
}),
],
})
optionalType ​
Choose what to use as mode for an optional value.
type
type?: string
type?: string
type: string | undefined
type: string | undefined
type?: string | undefined
type?: string | undefined
INFO
Type: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
Default: 'questionToken'
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
optionalType: 'questionToken',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
optionalType: 'questionToken',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
optionalType: 'undefined',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
optionalType: 'undefined',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
optionalType: 'questionTokenAndUndefined',
}),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({
optionalType: 'questionTokenAndUndefined',
}),
],
})
skipBy ​
Array containing skipBy paramaters to exclude/skip tags/operations/methods/paths.
type
export type SkipBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
export type SkipBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
INFO
Type: Array<SkipBy>
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS(
{
skipBy: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS(
{
skipBy: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
overrideBy ​
Array containing overrideBy paramaters to override options
based on tags/operations/methods/paths.
type
export type OverrideBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
options: PluginOptions
}
export type OverrideBy = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
options: PluginOptions
}
INFO
Type: Array<OverrideBy>
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS(
{
overrideBy: [
{
type: 'tag',
pattern: 'pet',
options: {
output: './custom',
},
},
],
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS(
{
overrideBy: [
{
type: 'tag',
pattern: 'pet',
options: {
output: './custom',
},
},
],
},
),
],
})
transformers ​
name ​
Override the name of the TypeScript type that is getting generated, this will also override the name of the file.
INFO
Type: (name: string) => string
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS(
{
transformers: {
name: (name) => {
return `${name}Client`
},
},
},
),
],
})
import { defineConfig } from '@kubb/swagger'
import createSwagger from '@kubb/swagger'
import createSwaggerTS from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS(
{
transformers: {
name: (name) => {
return `${name}Client`
},
},
},
),
],
})