@kubb/adapter-oas 5.8.1 → 5.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,277 @@
1
+ # Options
2
+
3
+ Pass these options to `adapterOas()`. All of them are optional.
4
+
5
+ ## Options overview
6
+
7
+ | Option | Purpose |
8
+ | ------------------------------------- | ------------------------------------------------------------ |
9
+ | [`validate`](#validate) | Validate the spec before parsing. |
10
+ | [`contentType`](#contenttype) | Preferred media type for request and response schemas. |
11
+ | [`server`](#server) | Which spec server Kubb resolves into the document `baseURL`. |
12
+ | [`discriminator`](#discriminator) | How `discriminator` fields are interpreted. |
13
+ | [`enums`](#enums) | Where inline enums live. |
14
+ | [`dateType`](#datetype) | How `date-time`, `date`, and `time` schemas are represented. |
15
+ | [`integerType`](#integertype) | How integers map to TypeScript. |
16
+ | [`unknownType`](#unknowntype) | Type for schemas Kubb cannot infer. |
17
+ | [`emptySchemaType`](#emptyschematype) | Type for empty schemas. |
18
+ | [`enumSuffix`](#enumsuffix) | Suffix for derived enum names. |
19
+
20
+ ## Option details
21
+
22
+ ### validate
23
+
24
+ Validates the OpenAPI spec with `@readme/openapi-parser` before parsing. Each problem is reported as a [`KUBB_INVALID_SPEC`](https://kubb.dev/docs/5.x/reference/diagnostics#kubb-invalid-spec) warning, and generation continues. Set it to `false` to skip the check, which makes generation faster on a large spec. Run [`kubb validate`](https://kubb.dev/docs/5.x/reference/commands/validate) to list every error.
25
+
26
+ | | |
27
+ | -------- | --------- |
28
+ | Type | `boolean` |
29
+ | Required | `false` |
30
+ | Default | `true` |
31
+
32
+ ### contentType
33
+
34
+ Media type to keep when an operation declares several, for both request bodies and responses. Without a value, Kubb keeps every media type the operation declares.
35
+
36
+ | | |
37
+ | -------- | ------------------------------ |
38
+ | Type | `'application/json' \| string` |
39
+ | Required | `false` |
40
+
41
+ ### server
42
+
43
+ Selects which entry in the spec's `servers` array Kubb resolves into the document `baseURL`, filling in any `{variable}` placeholders. `server.index` points at one of the spec's servers, usually `0` for the primary one. `server.variables` supplies placeholder values, falling back to each variable's `default` from the spec. Omit `server` and `baseURL` resolves to `null`.
44
+
45
+ | | |
46
+ | -------- | -------------------------------------------------------- |
47
+ | Type | `{ index?: number, variables?: Record<string, string> }` |
48
+ | Required | `false` |
49
+
50
+ The resolved `baseURL` reaches banner functions through `BannerMeta.baseURL` but does not set request URLs on its own. To change where a generated client sends requests, use that plugin's own `baseURL` option ([`@kubb/plugin-fetch`](https://kubb.dev/extensions/kubb-labs/plugin-fetch), [`@kubb/plugin-axios`](https://kubb.dev/extensions/kubb-labs/plugin-axios), [`@kubb/plugin-msw`](https://kubb.dev/extensions/kubb-labs/plugin-msw)).
51
+
52
+ With a spec server of `https://api.{env}.example.com`, `server: { index: 0, variables: { env: 'prod' } }` resolves `baseURL` to `https://api.prod.example.com`.
53
+
54
+ ### discriminator
55
+
56
+ How `discriminator` fields on `oneOf`/`anyOf` schemas are interpreted.
57
+
58
+ | | |
59
+ | -------- | --------------------------- |
60
+ | Type | `'preserve' \| 'propagate'` |
61
+ | Required | `false` |
62
+ | Default | `'preserve'` |
63
+
64
+ ::field-group
65
+
66
+ :::field{name="'preserve'"}
67
+ Keeps child schemas exactly as written, though the discriminator still narrows types at the call site.
68
+ :::
69
+
70
+ :::field{name="'propagate'"}
71
+ Pushes the discriminator property with its literal value into each child schema, so each branch's `type` field is precisely typed.
72
+ :::
73
+
74
+ ::
75
+
76
+ ::code-group
77
+
78
+ ```yaml [OpenAPI spec]
79
+ openapi: 3.0.3
80
+ components:
81
+ schemas:
82
+ Animal:
83
+ required: [type]
84
+ type: object
85
+ oneOf:
86
+ - $ref: '#/components/schemas/Cat'
87
+ - $ref: '#/components/schemas/Dog'
88
+ discriminator:
89
+ propertyName: type
90
+ mapping:
91
+ cat: '#/components/schemas/Cat'
92
+ dog: '#/components/schemas/Dog'
93
+ Cat:
94
+ type: object
95
+ properties:
96
+ type: { type: string }
97
+ indoor: { type: boolean }
98
+ Dog:
99
+ type: object
100
+ properties:
101
+ type: { type: string }
102
+ name: { type: string }
103
+ ```
104
+
105
+ ```typescript ['preserve' (default)]
106
+ export type Cat = { type: string; indoor?: boolean }
107
+ export type Dog = { type: string; name?: string }
108
+ export type Animal = Cat | Dog
109
+ ```
110
+
111
+ ```typescript ['propagate']
112
+ export type Cat = { type: 'cat'; indoor?: boolean }
113
+ export type Dog = { type: 'dog'; name?: string }
114
+ export type Animal = Cat | Dog
115
+ ```
116
+
117
+ ::
118
+
119
+ ### enums
120
+
121
+ Where inline enums live.
122
+
123
+ | | |
124
+ | -------- | -------------------- |
125
+ | Type | `'inline' \| 'root'` |
126
+ | Required | `false` |
127
+ | Default | `'inline'` |
128
+
129
+ ::field-group
130
+
131
+ :::field{name="'inline'"}
132
+ Keeps each enum on the property that declares it.
133
+ :::
134
+
135
+ :::field{name="'root'"}
136
+ Lifts every inline enum to a reusable top-level schema named after its context (for example `PetStatusEnum`) and references it wherever it appears.
137
+ :::
138
+
139
+ ::
140
+
141
+ For an enum with values `active` and `inactive` on `Pet.status`:
142
+
143
+ ::code-group
144
+
145
+ ```typescript ['inline' (default)]
146
+ export type Pet = { status?: 'active' | 'inactive' }
147
+ ```
148
+
149
+ ```typescript ['root']
150
+ export type PetStatusEnum = 'active' | 'inactive'
151
+ export type Pet = { status?: PetStatusEnum }
152
+ ```
153
+
154
+ ::
155
+
156
+ ### dateType
157
+
158
+ How `date-time`, `date`, and `time` schemas are represented downstream.
159
+
160
+ | | |
161
+ | -------- | ----------------------------------------------------------------------------------------------- |
162
+ | Type | `false \| 'string' \| 'stringOffset' \| 'stringLocal' \| 'date' \| { dateTime?, date?, time? }` |
163
+ | Required | `false` |
164
+ | Default | `'string'` |
165
+
166
+ Pass a single value to apply it to all three formats:
167
+
168
+ ::field-group
169
+
170
+ :::field{name="false"}
171
+ Represents date and time values as plain strings without format validation.
172
+ :::
173
+
174
+ :::field{name="'string'"}
175
+ Represents `date-time`, `date`, and `time` as ISO 8601 strings. The generated TypeScript type is `string`.
176
+ :::
177
+
178
+ :::field{name="'stringOffset'"}
179
+ Represents `date-time` as a string with a timezone offset. The `date` and `time` formats fall back to `'string'`.
180
+ :::
181
+
182
+ :::field{name="'stringLocal'"}
183
+ Represents `date-time` as a local string without a timezone. The `date` and `time` formats fall back to `'string'`.
184
+ :::
185
+
186
+ :::field{name="'date'"}
187
+ Represents date and time values as JavaScript `Date` objects. JSON values need parsing to revive them as `Date` objects.
188
+ :::
189
+
190
+ ::
191
+
192
+ The string variants all emit `string` at the TypeScript type level. Schema plugins such as `@kubb/plugin-zod` validate the offset or local format.
193
+
194
+ Pass an object to set `dateTime`, `date`, and `time` independently. A key you leave out defaults to `'string'`, regardless of the other keys.
195
+
196
+ ```ts
197
+ adapterOas({
198
+ dateType: {
199
+ dateTime: 'date', // Date object for timestamps
200
+ date: 'string',
201
+ time: 'string',
202
+ },
203
+ })
204
+ ```
205
+
206
+ ### integerType
207
+
208
+ How `type: integer` (and `format: int64`) maps to TypeScript.
209
+
210
+ | | |
211
+ | -------- | ---------------------- |
212
+ | Type | `'number' \| 'bigint'` |
213
+ | Required | `false` |
214
+ | Default | `'bigint'` |
215
+
216
+ ::field-group
217
+
218
+ :::field{name="'bigint'"}
219
+ Represents 64-bit IDs exactly, but `JSON.stringify` and `JSON.parse` cannot round-trip it. Use it only when you handle bigint serialization yourself.
220
+ :::
221
+
222
+ :::field{name="'number'"}
223
+ Fits most JSON APIs. It loses precision above `Number.MAX_SAFE_INTEGER`.
224
+ :::
225
+
226
+ ::
227
+
228
+ A schema with `type: string` stays a `string` whatever its `format`, so the [ProtoJSON](https://protobuf.dev/programming-guides/json/#int64-strings) `int64` strings that gRPC-gateway emits stay strings. `@kubb/plugin-zod` validates them with a digits regex, and `@kubb/plugin-faker` mocks them as numeric strings.
229
+
230
+ ### unknownType
231
+
232
+ AST type used when a schema's type cannot be inferred from the spec (`additionalProperties: true`, a missing `type`, and similar).
233
+
234
+ | | |
235
+ | -------- | ------------------------------ |
236
+ | Type | `'any' \| 'unknown' \| 'void'` |
237
+ | Required | `false` |
238
+ | Default | `'unknown'` |
239
+
240
+ ::field-group
241
+
242
+ :::field{name="'unknown'"}
243
+ Requires callers to narrow the value before using it.
244
+ :::
245
+
246
+ :::field{name="'any'"}
247
+ Allows callers to use the value without type checking.
248
+ :::
249
+
250
+ :::field{name="'void'"}
251
+ Represents a value callers should not use. Choose it when matching a legacy API that uses `void`.
252
+ :::
253
+
254
+ ::
255
+
256
+ ### emptySchemaType
257
+
258
+ AST type used for empty schemas (`{}`). It takes the same values as [`unknownType`](#unknowntype) and follows it unless you set it.
259
+
260
+ | | |
261
+ | -------- | -------------------------------------- |
262
+ | Type | `'any' \| 'unknown' \| 'void'` |
263
+ | Required | `false` |
264
+ | Default | `unknownType` (`'unknown'` by default) |
265
+
266
+ > [!TIP]
267
+ > A common pairing sets `unknownType: 'unknown'` for safety and `emptySchemaType: 'any'` so empty 204 response bodies stay easy to use.
268
+
269
+ ### enumSuffix
270
+
271
+ Suffix for the names Kubb gives inline enums: schema name, property name, and suffix, in PascalCase. The `status` enum on `Pet` becomes `PetStatusEnum`, or `PetStatusType` with `enumSuffix: 'type'`.
272
+
273
+ | | |
274
+ | -------- | -------- |
275
+ | Type | `string` |
276
+ | Required | `false` |
277
+ | Default | `'enum'` |
@@ -1,10 +1,23 @@
1
1
  # OpenAPI adapter
2
2
 
3
- The OpenAPI adapter reads an OpenAPI 2.0, 3.0, or 3.1 document and converts every schema and operation into Kubb's AST. Every plugin in the build generates from that AST, so one adapter feeds them all.
3
+ The OpenAPI adapter reads an OpenAPI 2.0, 3.0, or 3.1 document and converts every schema and operation into Kubb's AST. Every plugin in the build generates code from that AST.
4
4
 
5
- Kubb uses `adapterOas()` by default. Set it on `defineConfig.adapter` only to change its options. The options apply to every plugin in the build.
5
+ Kubb uses `adapterOas()` by default. Set it on `defineConfig.adapter` only to change its [options](https://kubb.dev/extensions/kubb-labs/adapter-oas/options). The options apply to every plugin in the build.
6
6
 
7
- ```ts
7
+ ## Options worth knowing
8
+
9
+ - `validate` checks the spec before parsing and reports each problem as a warning. Turn it off to speed up a large spec. Run `kubb validate` to list every error.
10
+ - `dateType` sets how `date-time`, `date`, and `time` schemas are represented: ISO strings (the default), strings with or without a timezone offset, or JavaScript `Date` objects. Pass an object to set each format on its own.
11
+ - `integerType` defaults to `'bigint'`, which is exact for 64-bit IDs but does not survive `JSON.stringify`. Choose `'number'` for most JSON APIs.
12
+ - `unknownType` and `emptySchemaType` set the type for schemas Kubb cannot infer and for empty schemas (`{}`).
13
+ - `discriminator: 'propagate'` writes the discriminator's literal value into each child schema, so each branch is typed precisely.
14
+ - `enums: 'root'` lifts inline enums to reusable top-level schemas, named with `enumSuffix` (`PetStatusEnum`).
15
+ - `server` picks the entry in the spec's `servers` array that becomes the base URL and fills in its variables. It does not change where a generated client sends requests. Use the client plugin's own `baseURL` for that.
16
+ - `contentType` picks the media type when an operation defines several. Without it, Kubb uses the first JSON media type.
17
+
18
+ ## Example
19
+
20
+ ```typescript [kubb.config.ts]
8
21
  import { defineConfig } from 'kubb/config'
9
22
  import { adapterOas } from '@kubb/adapter-oas'
10
23
  import { pluginTs } from '@kubb/plugin-ts'
@@ -16,14 +29,3 @@ export default defineConfig({
16
29
  plugins: [pluginTs()],
17
30
  })
18
31
  ```
19
-
20
- ## Options worth knowing
21
-
22
- - `validate` checks the spec before parsing and reports each problem as a warning. Turn it off to speed up a large spec. Run `kubb validate` to list every error.
23
- - `dateType` sets how `date-time`, `date`, and `time` schemas are represented: ISO strings (the default), strings with or without a timezone offset, or JavaScript `Date` objects. Pass an object to set each format on its own.
24
- - `integerType` defaults to `'bigint'`, which is exact for 64-bit IDs but does not survive `JSON.stringify`. Choose `'number'` for most JSON APIs.
25
- - `unknownType` and `emptySchemaType` set the type for schemas Kubb cannot infer and for empty schemas (`{}`).
26
- - `discriminator: 'propagate'` writes the discriminator's literal value into each child schema, so each branch is typed precisely.
27
- - `enums: 'root'` lifts inline enums to reusable top-level schemas, named with `enumSuffix` (`PetStatusEnum`).
28
- - `server` picks the entry in the spec's `servers` array that becomes the base URL and fills in its variables. It does not change where a generated client sends requests. Use the client plugin's own `baseURL` for that.
29
- - `contentType` picks the media type when an operation defines several. Without it, Kubb uses the first JSON media type.
@@ -10,7 +10,8 @@
10
10
  "light": "https://kubb.dev/feature/openapi.svg"
11
11
  },
12
12
  "tags": ["openapi", "swagger", "api-spec", "parser", "converter"],
13
- "docs": "https://kubb.dev/adapters/adapter-oas",
13
+ "docs": "https://kubb.dev/extensions/kubb-labs/adapter-oas",
14
+ "maintainers": [{ "name": "Stijn Van Hulle", "github": "stijnvanhulle" }],
14
15
  "compatibility": {
15
16
  "kubb": ">=5.8.0",
16
17
  "node": ">=22"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/adapter-oas",
3
- "version": "5.8.1",
3
+ "version": "5.8.3",
4
4
  "description": "OpenAPI and Swagger adapter for Kubb. Parses and validates OAS 2.0/3.x specifications into a @kubb/ast RootNode for downstream code generation plugins.",
5
5
  "keywords": [
6
6
  "adapter",
@@ -12,7 +12,7 @@
12
12
  "swagger",
13
13
  "typescript"
14
14
  ],
15
- "homepage": "https://kubb.dev/adapters/adapter-oas",
15
+ "homepage": "https://kubb.dev/extensions/kubb-labs/adapter-oas",
16
16
  "license": "MIT",
17
17
  "author": "stijnvanhulle",
18
18
  "repository": {
@@ -37,7 +37,7 @@
37
37
  "files": [
38
38
  "dist",
39
39
  "kubb-extension.json",
40
- "OVERVIEW.md",
40
+ "docs",
41
41
  "!/**/**.test.**",
42
42
  "!/**/__tests__/**",
43
43
  "!/**/__snapshots__/**"
@@ -61,7 +61,7 @@
61
61
  "registry": "https://registry.npmjs.org/"
62
62
  },
63
63
  "dependencies": {
64
- "@kubb/kit": "5.8.1",
64
+ "@kubb/kit": "5.8.3",
65
65
  "@readme/openapi-parser": "^9.1.0",
66
66
  "@scalar/openapi-upgrader": "^0.4.3",
67
67
  "api-ref-bundler": "0.5.1",