@kubb/adapter-oas 5.0.0-alpha.9 → 5.0.0-beta.10
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.
- package/README.md +98 -0
- package/dist/index.cjs +1304 -1240
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +475 -144
- package/dist/index.js +1298 -1234
- package/dist/index.js.map +1 -1
- package/extension.yaml +344 -0
- package/package.json +33 -35
- package/src/adapter.ts +86 -78
- package/src/constants.ts +77 -69
- package/src/discriminator.ts +108 -0
- package/src/factory.ts +162 -0
- package/src/guards.ts +68 -0
- package/src/index.ts +17 -0
- package/src/parser.ts +565 -655
- package/src/refs.ts +74 -0
- package/src/resolvers.ts +544 -0
- package/src/types.ts +172 -59
- package/src/oas/Oas.ts +0 -623
- package/src/oas/resolveServerUrl.ts +0 -47
- package/src/oas/types.ts +0 -71
- package/src/oas/utils.ts +0 -402
- package/src/utils.ts +0 -168
package/extension.yaml
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
$schema: https://kubb.dev/schemas/extension.json
|
|
2
|
+
kind: adapter
|
|
3
|
+
id: adapter-oas
|
|
4
|
+
name: OpenAPI
|
|
5
|
+
description: Parse and convert OpenAPI 2.0, 3.0, and 3.1 specifications into Kubb's universal AST with full support for discriminators, date types, and server configuration.
|
|
6
|
+
category: openapi
|
|
7
|
+
type: official
|
|
8
|
+
npmPackage: '@kubb/adapter-oas'
|
|
9
|
+
docsPath: /adapters/adapter-oas
|
|
10
|
+
repo: https://github.com/kubb-labs/kubb
|
|
11
|
+
maintainers:
|
|
12
|
+
- name: Stijn Van Hulle
|
|
13
|
+
github: stijnvanhulle
|
|
14
|
+
compatibility:
|
|
15
|
+
kubb: '>=5.0.0'
|
|
16
|
+
node: '>=22'
|
|
17
|
+
tags:
|
|
18
|
+
- openapi
|
|
19
|
+
- swagger
|
|
20
|
+
- api-spec
|
|
21
|
+
- parser
|
|
22
|
+
- converter
|
|
23
|
+
resources:
|
|
24
|
+
documentation: https://kubb.dev/adapters/adapter-oas
|
|
25
|
+
repository: https://github.com/kubb-labs/kubb
|
|
26
|
+
issues: https://github.com/kubb-labs/kubb/issues
|
|
27
|
+
changelog: https://github.com/kubb-labs/kubb/blob/main/packages/adapter-oas/CHANGELOG.md
|
|
28
|
+
featured: true
|
|
29
|
+
icon:
|
|
30
|
+
light: https://kubb.dev/feature/openapi.svg
|
|
31
|
+
intro: |-
|
|
32
|
+
The OpenAPI adapter parses and converts your OpenAPI specification into Kubb's internal AST (Abstract Syntax Tree), which all downstream plugins consume.
|
|
33
|
+
|
|
34
|
+
The adapter is configured once in `defineConfig` and applies globally to all plugins.
|
|
35
|
+
options:
|
|
36
|
+
- name: validate
|
|
37
|
+
type: boolean
|
|
38
|
+
required: false
|
|
39
|
+
default: 'true'
|
|
40
|
+
description: Validate the OpenAPI spec before parsing using `@readme/openapi-parser`.
|
|
41
|
+
- name: contentType
|
|
42
|
+
type: "'application/json' | string"
|
|
43
|
+
required: false
|
|
44
|
+
description: Preferred content-type used when extracting request/response schemas from the spec. Defaults to the first valid JSON media type found in the spec.
|
|
45
|
+
codeBlock:
|
|
46
|
+
lang: typescript
|
|
47
|
+
title: kubb.config.ts
|
|
48
|
+
twoslash: false
|
|
49
|
+
code: |-
|
|
50
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
51
|
+
|
|
52
|
+
adapterOas({ contentType: 'application/vnd.api+json' })
|
|
53
|
+
- name: serverIndex
|
|
54
|
+
type: number
|
|
55
|
+
required: false
|
|
56
|
+
description: Index into the `servers` array from your OpenAPI spec for computing `baseURL`.
|
|
57
|
+
tip: Defining the server here will make it possible to use that endpoint as `baseURL` in other plugins.
|
|
58
|
+
examples:
|
|
59
|
+
- name: OpenAPI
|
|
60
|
+
files:
|
|
61
|
+
- lang: yaml
|
|
62
|
+
code: |-
|
|
63
|
+
openapi: 3.0.3
|
|
64
|
+
servers:
|
|
65
|
+
- url: http://petstore.swagger.io/api
|
|
66
|
+
- url: http://localhost:3000
|
|
67
|
+
- name: serverIndex 0
|
|
68
|
+
files:
|
|
69
|
+
- lang: typescript
|
|
70
|
+
code: |-
|
|
71
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
72
|
+
|
|
73
|
+
adapterOas({ serverIndex: 0 })
|
|
74
|
+
- name: serverIndex 1
|
|
75
|
+
files:
|
|
76
|
+
- lang: typescript
|
|
77
|
+
code: |-
|
|
78
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
79
|
+
|
|
80
|
+
adapterOas({ serverIndex: 1 })
|
|
81
|
+
- name: serverVariables
|
|
82
|
+
type: Record<string, string>
|
|
83
|
+
required: false
|
|
84
|
+
description: Override values for `{variable}` placeholders in the selected server URL. Only used when `serverIndex` is set. Variables not provided fall back to their `default` value from the spec.
|
|
85
|
+
examples:
|
|
86
|
+
- name: OpenAPI
|
|
87
|
+
files:
|
|
88
|
+
- lang: yaml
|
|
89
|
+
code: |-
|
|
90
|
+
openapi: 3.0.3
|
|
91
|
+
servers:
|
|
92
|
+
- url: https://api.{env}.example.com
|
|
93
|
+
variables:
|
|
94
|
+
env:
|
|
95
|
+
default: dev
|
|
96
|
+
enum: [dev, staging, prod]
|
|
97
|
+
- name: kubb.config.ts
|
|
98
|
+
files:
|
|
99
|
+
- lang: typescript
|
|
100
|
+
code: |-
|
|
101
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
102
|
+
|
|
103
|
+
adapterOas({
|
|
104
|
+
serverIndex: 0,
|
|
105
|
+
serverVariables: { env: 'prod' },
|
|
106
|
+
})
|
|
107
|
+
// Results in baseURL: https://api.prod.example.com
|
|
108
|
+
- name: discriminator
|
|
109
|
+
type: "'strict' | 'inherit'"
|
|
110
|
+
required: false
|
|
111
|
+
default: "'strict'"
|
|
112
|
+
description: How the discriminator field is interpreted when processing `oneOf`/`anyOf` schemas.
|
|
113
|
+
details:
|
|
114
|
+
- title: strict
|
|
115
|
+
body: Uses `oneOf` schemas as written in the spec. The discriminator is used for type narrowing but child schemas are not modified.
|
|
116
|
+
- title: inherit
|
|
117
|
+
body: Propagates the discriminator property with appropriate enum values into each child schema, ensuring type safety and enabling better code generation.
|
|
118
|
+
examples:
|
|
119
|
+
- name: OpenAPI
|
|
120
|
+
files:
|
|
121
|
+
- lang: yaml
|
|
122
|
+
code: |-
|
|
123
|
+
openapi: 3.0.3
|
|
124
|
+
components:
|
|
125
|
+
schemas:
|
|
126
|
+
Animal:
|
|
127
|
+
required: [type]
|
|
128
|
+
type: object
|
|
129
|
+
oneOf:
|
|
130
|
+
- $ref: '#/components/schemas/Cat'
|
|
131
|
+
- $ref: '#/components/schemas/Dog'
|
|
132
|
+
discriminator:
|
|
133
|
+
propertyName: type
|
|
134
|
+
mapping:
|
|
135
|
+
cat: '#/components/schemas/Cat'
|
|
136
|
+
dog: '#/components/schemas/Dog'
|
|
137
|
+
Cat:
|
|
138
|
+
type: object
|
|
139
|
+
properties:
|
|
140
|
+
type:
|
|
141
|
+
type: string
|
|
142
|
+
indoor:
|
|
143
|
+
type: boolean
|
|
144
|
+
Dog:
|
|
145
|
+
type: object
|
|
146
|
+
properties:
|
|
147
|
+
type:
|
|
148
|
+
type: string
|
|
149
|
+
name:
|
|
150
|
+
type: string
|
|
151
|
+
- name: strict
|
|
152
|
+
files:
|
|
153
|
+
- lang: typescript
|
|
154
|
+
code: |-
|
|
155
|
+
export type Cat = {
|
|
156
|
+
type: string
|
|
157
|
+
indoor?: boolean
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export type Dog = {
|
|
161
|
+
type: string
|
|
162
|
+
name?: string
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export type Animal = Cat | Dog
|
|
166
|
+
- name: inherit
|
|
167
|
+
files:
|
|
168
|
+
- lang: typescript
|
|
169
|
+
code: |-
|
|
170
|
+
export type Cat = {
|
|
171
|
+
type: 'cat'
|
|
172
|
+
indoor?: boolean
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export type Dog = {
|
|
176
|
+
type: 'dog'
|
|
177
|
+
name?: string
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export type Animal = Cat | Dog
|
|
181
|
+
- name: dateType
|
|
182
|
+
type: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
|
|
183
|
+
required: false
|
|
184
|
+
default: "'string'"
|
|
185
|
+
description: "How `format: 'date-time'` schemas are represented in the generated AST and downstream output."
|
|
186
|
+
details:
|
|
187
|
+
- title: 'false'
|
|
188
|
+
body: Falls through to a plain `string` type.
|
|
189
|
+
- title: "'string'"
|
|
190
|
+
body: Emits a datetime string node (e.g. `z.string().datetime()`).
|
|
191
|
+
- title: "'stringOffset'"
|
|
192
|
+
body: 'Emits a datetime node with timezone offset (`{ offset: true }`).'
|
|
193
|
+
- title: "'stringLocal'"
|
|
194
|
+
body: 'Emits a local datetime node (`{ local: true }`).'
|
|
195
|
+
- title: "'date'"
|
|
196
|
+
body: Emits a `date` node (JavaScript `Date` object).
|
|
197
|
+
examples:
|
|
198
|
+
- name: 'false'
|
|
199
|
+
files:
|
|
200
|
+
- lang: typescript
|
|
201
|
+
code: |-
|
|
202
|
+
// format: date-time → plain string
|
|
203
|
+
type CreatedAt = string
|
|
204
|
+
- name: "'string' (default)"
|
|
205
|
+
files:
|
|
206
|
+
- lang: typescript
|
|
207
|
+
code: |-
|
|
208
|
+
// format: date-time → datetime string
|
|
209
|
+
type CreatedAt = string // validated as ISO 8601 datetime
|
|
210
|
+
- name: "'stringOffset'"
|
|
211
|
+
files:
|
|
212
|
+
- lang: typescript
|
|
213
|
+
code: |-
|
|
214
|
+
// format: date-time → datetime string with offset
|
|
215
|
+
type CreatedAt = string // validated as ISO 8601 datetime with offset
|
|
216
|
+
- name: "'stringLocal'"
|
|
217
|
+
files:
|
|
218
|
+
- lang: typescript
|
|
219
|
+
code: |-
|
|
220
|
+
// format: date-time → local datetime string
|
|
221
|
+
type CreatedAt = string // validated as local ISO 8601 datetime
|
|
222
|
+
- name: "'date'"
|
|
223
|
+
files:
|
|
224
|
+
- lang: typescript
|
|
225
|
+
code: |-
|
|
226
|
+
// format: date-time → JavaScript Date
|
|
227
|
+
type CreatedAt = Date
|
|
228
|
+
- name: integerType
|
|
229
|
+
type: "'number' | 'bigint'"
|
|
230
|
+
required: false
|
|
231
|
+
default: "'bigint'"
|
|
232
|
+
description: "Whether `type: 'integer'` and `format: 'int64'` produce `number` or `bigint` nodes."
|
|
233
|
+
examples:
|
|
234
|
+
- name: "'number' (default)"
|
|
235
|
+
files:
|
|
236
|
+
- lang: typescript
|
|
237
|
+
code: |-
|
|
238
|
+
type Pet = {
|
|
239
|
+
id: number
|
|
240
|
+
}
|
|
241
|
+
- name: "'bigint'"
|
|
242
|
+
files:
|
|
243
|
+
- lang: typescript
|
|
244
|
+
code: |-
|
|
245
|
+
type Pet = {
|
|
246
|
+
id: bigint
|
|
247
|
+
}
|
|
248
|
+
- name: unknownType
|
|
249
|
+
type: "'any' | 'unknown' | 'void'"
|
|
250
|
+
required: false
|
|
251
|
+
default: "'any'"
|
|
252
|
+
description: AST type used when no schema type can be inferred from the OpenAPI spec.
|
|
253
|
+
examples:
|
|
254
|
+
- name: "'any' (default)"
|
|
255
|
+
files:
|
|
256
|
+
- lang: typescript
|
|
257
|
+
code: |-
|
|
258
|
+
type Pet = {
|
|
259
|
+
extra: any
|
|
260
|
+
}
|
|
261
|
+
- name: "'unknown'"
|
|
262
|
+
files:
|
|
263
|
+
- lang: typescript
|
|
264
|
+
code: |-
|
|
265
|
+
type Pet = {
|
|
266
|
+
extra: unknown
|
|
267
|
+
}
|
|
268
|
+
- name: "'void'"
|
|
269
|
+
files:
|
|
270
|
+
- lang: typescript
|
|
271
|
+
code: |-
|
|
272
|
+
type Pet = {
|
|
273
|
+
extra: void
|
|
274
|
+
}
|
|
275
|
+
- name: emptySchemaType
|
|
276
|
+
type: "'any' | 'unknown' | 'void'"
|
|
277
|
+
required: false
|
|
278
|
+
default: unknownType | 'any'
|
|
279
|
+
description: AST type used for completely empty schemas (`{}`).
|
|
280
|
+
tip: When not set, `emptySchemaType` falls back to the value of `unknownType`. Set it explicitly only when you want a different type for empty schemas than for unresolvable ones.
|
|
281
|
+
examples:
|
|
282
|
+
- name: "'any' (default)"
|
|
283
|
+
files:
|
|
284
|
+
- lang: typescript
|
|
285
|
+
code: |-
|
|
286
|
+
// empty schema {} → any
|
|
287
|
+
type EmptyModel = any
|
|
288
|
+
- name: "'unknown'"
|
|
289
|
+
files:
|
|
290
|
+
- lang: typescript
|
|
291
|
+
code: |-
|
|
292
|
+
// empty schema {} → unknown
|
|
293
|
+
type EmptyModel = unknown
|
|
294
|
+
- name: enumSuffix
|
|
295
|
+
type: string
|
|
296
|
+
required: false
|
|
297
|
+
default: "'enum'"
|
|
298
|
+
description: Suffix appended to derived enum names when building nested property schema names.
|
|
299
|
+
examples:
|
|
300
|
+
- name: "'enum' (default)"
|
|
301
|
+
files:
|
|
302
|
+
- lang: typescript
|
|
303
|
+
code: |-
|
|
304
|
+
// property `status` with inline enum values
|
|
305
|
+
const statusEnum = { available: 'available', pending: 'pending' } as const
|
|
306
|
+
type StatusEnum = (typeof statusEnum)[keyof typeof statusEnum]
|
|
307
|
+
- name: "'type'"
|
|
308
|
+
files:
|
|
309
|
+
- lang: typescript
|
|
310
|
+
code: |-
|
|
311
|
+
// enumSuffix: 'type'
|
|
312
|
+
const statusType = { available: 'available', pending: 'pending' } as const
|
|
313
|
+
type StatusType = (typeof statusType)[keyof typeof statusType]
|
|
314
|
+
examples:
|
|
315
|
+
- name: kubb.config.ts
|
|
316
|
+
files:
|
|
317
|
+
- lang: typescript
|
|
318
|
+
code: |-
|
|
319
|
+
import { defineConfig } from 'kubb'
|
|
320
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
321
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
322
|
+
|
|
323
|
+
export default defineConfig({
|
|
324
|
+
input: {
|
|
325
|
+
path: './petStore.yaml',
|
|
326
|
+
},
|
|
327
|
+
output: {
|
|
328
|
+
path: './src/gen',
|
|
329
|
+
},
|
|
330
|
+
adapter: adapterOas({
|
|
331
|
+
validate: true,
|
|
332
|
+
serverIndex: 0,
|
|
333
|
+
serverVariables: { env: 'prod' },
|
|
334
|
+
discriminator: 'inherit',
|
|
335
|
+
dateType: 'date',
|
|
336
|
+
integerType: 'number',
|
|
337
|
+
unknownType: 'unknown',
|
|
338
|
+
emptySchemaType: 'unknown',
|
|
339
|
+
enumSuffix: 'enum',
|
|
340
|
+
}),
|
|
341
|
+
plugins: [
|
|
342
|
+
pluginTs(),
|
|
343
|
+
],
|
|
344
|
+
})
|
package/package.json
CHANGED
|
@@ -1,25 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/adapter-oas",
|
|
3
|
-
"version": "5.0.0-
|
|
4
|
-
"description": "OpenAPI
|
|
3
|
+
"version": "5.0.0-beta.10",
|
|
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
|
-
"openapi",
|
|
7
|
-
"swagger",
|
|
8
|
-
"oas",
|
|
9
6
|
"adapter",
|
|
10
|
-
"kubb",
|
|
11
7
|
"codegen",
|
|
8
|
+
"kubb",
|
|
9
|
+
"oas",
|
|
10
|
+
"openapi",
|
|
11
|
+
"swagger",
|
|
12
12
|
"typescript"
|
|
13
13
|
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "stijnvanhulle",
|
|
14
16
|
"repository": {
|
|
15
17
|
"type": "git",
|
|
16
18
|
"url": "git+https://github.com/kubb-labs/kubb.git",
|
|
17
19
|
"directory": "packages/adapter-oas"
|
|
18
20
|
},
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"dist",
|
|
24
|
+
"extension.yaml",
|
|
25
|
+
"!/**/**.test.**",
|
|
26
|
+
"!/**/__tests__/**",
|
|
27
|
+
"!/**/__snapshots__/**"
|
|
28
|
+
],
|
|
22
29
|
"type": "module",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"main": "./dist/index.cjs",
|
|
32
|
+
"module": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
23
34
|
"exports": {
|
|
24
35
|
".": {
|
|
25
36
|
"import": "./dist/index.js",
|
|
@@ -27,46 +38,33 @@
|
|
|
27
38
|
},
|
|
28
39
|
"./package.json": "./package.json"
|
|
29
40
|
},
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"!/**/**.test.**",
|
|
35
|
-
"!/**/__tests__/**",
|
|
36
|
-
"!/**/__snapshots__/**"
|
|
37
|
-
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"registry": "https://registry.npmjs.org/"
|
|
44
|
+
},
|
|
38
45
|
"dependencies": {
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"fflate": "^0.8.2",
|
|
43
|
-
"jsonpointer": "^5.0.1",
|
|
44
|
-
"oas": "^31.1.2",
|
|
45
|
-
"oas-normalize": "^16.0.2",
|
|
46
|
-
"openapi-types": "^12.1.3",
|
|
47
|
-
"remeda": "^2.33.6",
|
|
46
|
+
"@redocly/openapi-core": "^2.30.4",
|
|
47
|
+
"oas": "^32.1.18",
|
|
48
|
+
"oas-normalize": "^16.0.4",
|
|
48
49
|
"swagger2openapi": "^7.0.8",
|
|
49
|
-
"@kubb/
|
|
50
|
-
"@kubb/core": "5.0.0-alpha.9"
|
|
50
|
+
"@kubb/core": "5.0.0-beta.10"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/swagger2openapi": "^7.0.4",
|
|
54
|
+
"openapi-types": "^12.1.3",
|
|
54
55
|
"@internals/utils": "0.0.0"
|
|
55
56
|
},
|
|
56
57
|
"engines": {
|
|
57
58
|
"node": ">=22"
|
|
58
59
|
},
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"registry": "https://registry.npmjs.org/"
|
|
60
|
+
"inlinedDependencies": {
|
|
61
|
+
"openapi-types": "12.1.3"
|
|
62
62
|
},
|
|
63
|
-
"main": "./dist/index.cjs",
|
|
64
|
-
"module": "./dist/index.js",
|
|
65
63
|
"scripts": {
|
|
66
64
|
"build": "tsdown",
|
|
67
65
|
"clean": "npx rimraf ./dist",
|
|
68
|
-
"lint": "
|
|
69
|
-
"lint:fix": "
|
|
66
|
+
"lint": "oxlint .",
|
|
67
|
+
"lint:fix": "oxlint --fix .",
|
|
70
68
|
"release": "pnpm publish --no-git-check",
|
|
71
69
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
72
70
|
"start": "tsdown --watch",
|
package/src/adapter.ts
CHANGED
|
@@ -1,122 +1,130 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import type { OasAdapter } from './types.ts'
|
|
9
|
-
import { getImports } from './utils.ts'
|
|
1
|
+
import { ast, createAdapter } from '@kubb/core'
|
|
2
|
+
import { DEFAULT_PARSER_OPTIONS } from './constants.ts'
|
|
3
|
+
import { applyDiscriminatorInheritance } from './discriminator.ts'
|
|
4
|
+
import { parseDocument, parseFromConfig, validateDocument } from './factory.ts'
|
|
5
|
+
import { parseOas } from './parser.ts'
|
|
6
|
+
import { resolveServerUrl } from './resolvers.ts'
|
|
7
|
+
import type { AdapterOas, Document } from './types.ts'
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Stable string identifier for the OAS adapter used in Kubb's adapter registry.
|
|
11
|
+
*/
|
|
12
|
+
export const adapterOasName = 'oas' satisfies AdapterOas['name']
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
|
-
* Creates
|
|
15
|
+
* Creates the default OpenAPI / Swagger adapter for Kubb.
|
|
15
16
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
17
|
+
* Parses the spec, optionally validates it, resolves the base URL, and converts
|
|
18
|
+
* everything into an `InputNode` that downstream plugins consume.
|
|
18
19
|
*
|
|
19
20
|
* @example
|
|
20
21
|
* ```ts
|
|
21
|
-
* import { defineConfig } from '
|
|
22
|
+
* import { defineConfig } from 'kubb'
|
|
22
23
|
* import { adapterOas } from '@kubb/adapter-oas'
|
|
24
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
23
25
|
*
|
|
24
26
|
* export default defineConfig({
|
|
25
|
-
* adapter: adapterOas({
|
|
26
|
-
* input:
|
|
27
|
-
* plugins: [pluginTs()
|
|
27
|
+
* adapter: adapterOas({ dateType: 'date', serverIndex: 0 }),
|
|
28
|
+
* input: { path: './openapi.yaml' },
|
|
29
|
+
* plugins: [pluginTs()],
|
|
28
30
|
* })
|
|
29
31
|
* ```
|
|
30
32
|
*/
|
|
31
|
-
export const adapterOas = createAdapter<
|
|
33
|
+
export const adapterOas = createAdapter<AdapterOas>((options) => {
|
|
32
34
|
const {
|
|
33
35
|
validate = true,
|
|
34
|
-
oasClass,
|
|
35
36
|
contentType,
|
|
36
37
|
serverIndex,
|
|
37
38
|
serverVariables,
|
|
38
39
|
discriminator = 'strict',
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
emptySchemaType = unknownType,
|
|
40
|
+
dateType = DEFAULT_PARSER_OPTIONS.dateType,
|
|
41
|
+
integerType = DEFAULT_PARSER_OPTIONS.integerType,
|
|
42
|
+
unknownType = DEFAULT_PARSER_OPTIONS.unknownType,
|
|
43
|
+
enumSuffix = DEFAULT_PARSER_OPTIONS.enumSuffix,
|
|
44
|
+
emptySchemaType = unknownType || DEFAULT_PARSER_OPTIONS.emptySchemaType,
|
|
44
45
|
} = options
|
|
45
46
|
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
// Let-binding so parse() can replace it with a simple reassignment (no clear+loop).
|
|
48
|
+
let nameMapping = new Map<string, string>()
|
|
49
|
+
let parsedDocument: Document | null
|
|
50
|
+
let inputNode: ast.InputNode | null
|
|
49
51
|
|
|
50
52
|
return {
|
|
51
53
|
name: adapterOasName,
|
|
52
|
-
options
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
54
|
+
get options() {
|
|
55
|
+
return {
|
|
56
|
+
validate,
|
|
57
|
+
contentType,
|
|
58
|
+
serverIndex,
|
|
59
|
+
serverVariables,
|
|
60
|
+
discriminator,
|
|
61
|
+
dateType,
|
|
62
|
+
integerType,
|
|
63
|
+
unknownType,
|
|
64
|
+
emptySchemaType,
|
|
65
|
+
enumSuffix,
|
|
66
|
+
nameMapping,
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
get document() {
|
|
70
|
+
return parsedDocument
|
|
71
|
+
},
|
|
72
|
+
get inputNode() {
|
|
73
|
+
return inputNode
|
|
74
|
+
},
|
|
75
|
+
async validate(input, options) {
|
|
76
|
+
const document = await parseDocument(input)
|
|
77
|
+
await validateDocument(document, options)
|
|
65
78
|
},
|
|
66
79
|
getImports(node, resolve) {
|
|
67
|
-
return
|
|
80
|
+
return ast.collectImports({
|
|
81
|
+
node,
|
|
82
|
+
nameMapping,
|
|
83
|
+
resolve: (schemaName) => {
|
|
84
|
+
const result = resolve(schemaName)
|
|
85
|
+
if (!result) return
|
|
86
|
+
|
|
87
|
+
return ast.createImport({ name: [result.name], path: result.path })
|
|
88
|
+
},
|
|
89
|
+
})
|
|
68
90
|
},
|
|
69
91
|
async parse(source) {
|
|
70
|
-
const
|
|
71
|
-
const oas = await parseFromConfig(fakeConfig, oasClass)
|
|
72
|
-
|
|
73
|
-
oas.setOptions({ contentType, discriminator, collisionDetection })
|
|
92
|
+
const document = await parseFromConfig(source)
|
|
74
93
|
|
|
75
94
|
if (validate) {
|
|
76
|
-
|
|
77
|
-
await oas.validate()
|
|
78
|
-
} catch (_err) {
|
|
79
|
-
// Validation failures are non-fatal — mirror plugin-oas behavior
|
|
80
|
-
}
|
|
95
|
+
await validateDocument(document)
|
|
81
96
|
}
|
|
82
97
|
|
|
83
|
-
const server = serverIndex !== undefined ?
|
|
98
|
+
const server = serverIndex !== undefined ? document.servers?.at(serverIndex) : undefined
|
|
84
99
|
const baseURL = server?.url ? resolveServerUrl(server, serverVariables) : undefined
|
|
85
100
|
|
|
86
|
-
const
|
|
101
|
+
const { root: parsedRoot, nameMapping: parsedNameMapping } = parseOas(document, {
|
|
102
|
+
contentType,
|
|
103
|
+
dateType,
|
|
104
|
+
integerType,
|
|
105
|
+
unknownType,
|
|
106
|
+
emptySchemaType,
|
|
107
|
+
enumSuffix,
|
|
108
|
+
})
|
|
87
109
|
|
|
88
|
-
|
|
89
|
-
nameMapping.clear()
|
|
90
|
-
for (const [key, value] of parser.nameMapping) {
|
|
91
|
-
nameMapping.set(key, value)
|
|
92
|
-
}
|
|
110
|
+
const node = discriminator === 'inherit' ? applyDiscriminatorInheritance(parsedRoot) : parsedRoot
|
|
93
111
|
|
|
94
|
-
|
|
112
|
+
// This must happen after parseOas() because legacy enum remapping is finalized there.
|
|
113
|
+
nameMapping = parsedNameMapping
|
|
114
|
+
// Expose the raw document so consumers (e.g. plugin-redoc) can access it.
|
|
115
|
+
parsedDocument = document
|
|
95
116
|
|
|
96
|
-
|
|
97
|
-
...
|
|
117
|
+
inputNode = ast.createInput({
|
|
118
|
+
...node,
|
|
98
119
|
meta: {
|
|
99
|
-
title:
|
|
100
|
-
description:
|
|
101
|
-
version:
|
|
120
|
+
title: document.info?.title,
|
|
121
|
+
description: document.info?.description,
|
|
122
|
+
version: document.info?.version,
|
|
102
123
|
baseURL,
|
|
103
124
|
},
|
|
104
125
|
})
|
|
126
|
+
|
|
127
|
+
return inputNode
|
|
105
128
|
},
|
|
106
129
|
}
|
|
107
130
|
})
|
|
108
|
-
|
|
109
|
-
// TODO: remove once parseFromConfig accepts AdapterSource directly
|
|
110
|
-
function sourceToFakeConfig(source: AdapterSource): Parameters<typeof parseFromConfig>[0] {
|
|
111
|
-
switch (source.type) {
|
|
112
|
-
case 'path':
|
|
113
|
-
return { root: path.dirname(source.path), input: { path: source.path } } as Parameters<typeof parseFromConfig>[0]
|
|
114
|
-
case 'data':
|
|
115
|
-
return { root: process.cwd(), input: { data: source.data } } as Parameters<typeof parseFromConfig>[0]
|
|
116
|
-
case 'paths':
|
|
117
|
-
return {
|
|
118
|
-
root: source.paths[0] ? path.dirname(source.paths[0]) : process.cwd(),
|
|
119
|
-
input: source.paths.map((p) => ({ path: p })),
|
|
120
|
-
} as Parameters<typeof parseFromConfig>[0]
|
|
121
|
-
}
|
|
122
|
-
}
|