Skip to content

@kubb/plugin-oas 🦙 ​

With the Swagger plugin, you can create a JSON schema out of a Swagger file. Inside this package, you can also use some utils to create your own Swagger plugin. We already provide a react-query plugin but if you want to create a plugin for SWR you can use this package to get the core utils.(check if a schema is v2 or v3, validate the schema, generate a OAS object, ...).


We are using Oas to convert a YAML/JSON to an Oas class(see oasParser) that will contain a lot of extra logic(read the $ref, get all the operations, get all models, ...).

The Swagger plugin also contains some classes and functions that can be used in your own plugin that needs Swagger:

  • For example, we have getReference that will return the ref based on the spec.

  • Next to that we also have the class OperationGenerator. This class contains the building blocks of getting the request, response, params, ....
    Just call this.getSchemas and you will retrieve an object contains all the info you need to set up a TypeScript type, React-Query hook,....

Installation ​

shell
bun add @kubb/plugin-oas
shell
pnpm add @kubb/plugin-oas
shell
npm install @kubb/plugin-oas
shell
yarn add @kubb/plugin-oas

Options ​

validate ​

Validate your input based on @readme/openapi-parser

INFO

Type: boolean
Default: true

typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({
  validate: true,
})

output ​

output.path ​

Relative path to save the JSON models.
False will not generate the schema JSONs.

INFO

Type: string | false
Default: 'schemas'

typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({
  output: {
    path: './json',
  },
})
typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({
  output: false
})

serverIndex ​

Which server to use from the array of servers.url[serverIndex]

For example:

  • 0 will return http://petstore.swagger.io/api
  • 1 will return http://localhost:3000

INFO

Type: number
Default: 0

yaml
openapi: 3.0.3
info:
  title: Swagger Example
  description:
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
servers:
- url: http://petstore.swagger.io/api
- url: http://localhost:3000
typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({ serverIndex: 0 })
typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({ serverIndex: 1 })

contentType ​

Define which contentType should be used. By default, this is set based on the first used contentType.

TYPE

typescript
export type contentType = 'application/json' | (string & {})

INFO

Type: contentType

typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({ contentType: 'application/json' })

experimentalFilter ​

INFO

typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({
  experimentalFilter: {
    methods: ['get'],
  },
})

experimentalSort ​

INFO

typescript
import { pluginOas } from '@kubb/plugin-oas'

const plugin = pluginOas({
  experimentalSort: {
    properties: ['description', 'default', 'type']
  },
})

Example ​

typescript
import { defineConfig } from '@kubb/core'
import { pluginOas } from '@kubb/plugin-oas'

export default defineConfig({
  input: {
    path: './petStore.yaml',
  },
  output: {
    path: './src/gen',
  },
  plugins: [
    pluginOas({
      validate: true,
      output: {
        path: './json',
      },
      serverIndex: 0,
      contentType: 'application/json',
    }),
  ],
})

Released under the MIT License.