@kubb/adapter-oas 5.0.0-beta.9 → 5.0.0-beta.91
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/LICENSE +17 -10
- package/README.md +26 -24
- package/dist/index.cjs +1284 -848
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +846 -79
- package/dist/index.js +1280 -839
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
- package/extension.yaml +0 -344
- package/src/adapter.ts +0 -130
- package/src/constants.ts +0 -122
- package/src/discriminator.ts +0 -108
- package/src/factory.ts +0 -162
- package/src/guards.ts +0 -68
- package/src/index.ts +0 -18
- package/src/parser.ts +0 -1002
- package/src/refs.ts +0 -74
- package/src/resolvers.ts +0 -544
- package/src/types.ts +0 -206
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/adapter-oas",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.91",
|
|
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",
|
|
7
7
|
"codegen",
|
|
8
8
|
"kubb",
|
|
9
|
+
"meta-framework",
|
|
9
10
|
"oas",
|
|
10
11
|
"openapi",
|
|
11
12
|
"swagger",
|
|
@@ -19,9 +20,7 @@
|
|
|
19
20
|
"directory": "packages/adapter-oas"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
22
|
-
"src",
|
|
23
23
|
"dist",
|
|
24
|
-
"extension.yaml",
|
|
25
24
|
"!/**/**.test.**",
|
|
26
25
|
"!/**/__tests__/**",
|
|
27
26
|
"!/**/__snapshots__/**"
|
|
@@ -43,14 +42,15 @@
|
|
|
43
42
|
"registry": "https://registry.npmjs.org/"
|
|
44
43
|
},
|
|
45
44
|
"dependencies": {
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"@kubb/core": "5.0.0-beta.
|
|
45
|
+
"@readme/openapi-parser": "^6.2.0",
|
|
46
|
+
"@scalar/openapi-upgrader": "^0.2.9",
|
|
47
|
+
"api-ref-bundler": "0.5.1",
|
|
48
|
+
"yaml": "^2.9.0",
|
|
49
|
+
"@kubb/core": "5.0.0-beta.91",
|
|
50
|
+
"@kubb/ast": "5.0.0-beta.91"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@types/
|
|
53
|
+
"@types/json-schema": "^7.0.15",
|
|
54
54
|
"openapi-types": "^12.1.3",
|
|
55
55
|
"@internals/utils": "0.0.0"
|
|
56
56
|
},
|
|
@@ -58,15 +58,15 @@
|
|
|
58
58
|
"node": ">=22"
|
|
59
59
|
},
|
|
60
60
|
"inlinedDependencies": {
|
|
61
|
+
"@types/json-schema": "7.0.15",
|
|
61
62
|
"openapi-types": "12.1.3"
|
|
62
63
|
},
|
|
63
64
|
"scripts": {
|
|
65
|
+
"bench": "vitest bench",
|
|
64
66
|
"build": "tsdown",
|
|
65
|
-
"clean": "
|
|
67
|
+
"clean": "node -e \"require('node:fs').rmSync('./dist', {recursive:true,force:true})\"",
|
|
66
68
|
"lint": "oxlint .",
|
|
67
69
|
"lint:fix": "oxlint --fix .",
|
|
68
|
-
"release": "pnpm publish --no-git-check",
|
|
69
|
-
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
70
70
|
"start": "tsdown --watch",
|
|
71
71
|
"test": "vitest --passWithNoTests",
|
|
72
72
|
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
|
package/extension.yaml
DELETED
|
@@ -1,344 +0,0 @@
|
|
|
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/src/adapter.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
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'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Stable string identifier for the OAS adapter used in Kubb's adapter registry.
|
|
11
|
-
*/
|
|
12
|
-
export const adapterOasName = 'oas' satisfies AdapterOas['name']
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Creates the default OpenAPI / Swagger adapter for Kubb.
|
|
16
|
-
*
|
|
17
|
-
* Parses the spec, optionally validates it, resolves the base URL, and converts
|
|
18
|
-
* everything into an `InputNode` that downstream plugins consume.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* import { defineConfig } from 'kubb'
|
|
23
|
-
* import { adapterOas } from '@kubb/adapter-oas'
|
|
24
|
-
* import { pluginTs } from '@kubb/plugin-ts'
|
|
25
|
-
*
|
|
26
|
-
* export default defineConfig({
|
|
27
|
-
* adapter: adapterOas({ dateType: 'date', serverIndex: 0 }),
|
|
28
|
-
* input: { path: './openapi.yaml' },
|
|
29
|
-
* plugins: [pluginTs()],
|
|
30
|
-
* })
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export const adapterOas = createAdapter<AdapterOas>((options) => {
|
|
34
|
-
const {
|
|
35
|
-
validate = true,
|
|
36
|
-
contentType,
|
|
37
|
-
serverIndex,
|
|
38
|
-
serverVariables,
|
|
39
|
-
discriminator = 'strict',
|
|
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,
|
|
45
|
-
} = options
|
|
46
|
-
|
|
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
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
name: adapterOasName,
|
|
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)
|
|
78
|
-
},
|
|
79
|
-
getImports(node, resolve) {
|
|
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
|
-
})
|
|
90
|
-
},
|
|
91
|
-
async parse(source) {
|
|
92
|
-
const document = await parseFromConfig(source)
|
|
93
|
-
|
|
94
|
-
if (validate) {
|
|
95
|
-
await validateDocument(document)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const server = serverIndex !== undefined ? document.servers?.at(serverIndex) : undefined
|
|
99
|
-
const baseURL = server?.url ? resolveServerUrl(server, serverVariables) : undefined
|
|
100
|
-
|
|
101
|
-
const { root: parsedRoot, nameMapping: parsedNameMapping } = parseOas(document, {
|
|
102
|
-
contentType,
|
|
103
|
-
dateType,
|
|
104
|
-
integerType,
|
|
105
|
-
unknownType,
|
|
106
|
-
emptySchemaType,
|
|
107
|
-
enumSuffix,
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
const node = discriminator === 'inherit' ? applyDiscriminatorInheritance(parsedRoot) : parsedRoot
|
|
111
|
-
|
|
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
|
|
116
|
-
|
|
117
|
-
inputNode = ast.createInput({
|
|
118
|
-
...node,
|
|
119
|
-
meta: {
|
|
120
|
-
title: document.info?.title,
|
|
121
|
-
description: document.info?.description,
|
|
122
|
-
version: document.info?.version,
|
|
123
|
-
baseURL,
|
|
124
|
-
},
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
return inputNode
|
|
128
|
-
},
|
|
129
|
-
}
|
|
130
|
-
})
|
package/src/constants.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Default parser options applied when no explicit options are provided.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* import { DEFAULT_PARSER_OPTIONS } from '@kubb/adapter-oas'
|
|
9
|
-
*
|
|
10
|
-
* const parser = createOasParser(oas)
|
|
11
|
-
* const root = parser.parse({ ...DEFAULT_PARSER_OPTIONS, dateType: 'date' })
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export const DEFAULT_PARSER_OPTIONS = {
|
|
15
|
-
dateType: 'string',
|
|
16
|
-
integerType: 'bigint',
|
|
17
|
-
unknownType: 'any',
|
|
18
|
-
emptySchemaType: 'any',
|
|
19
|
-
enumSuffix: 'enum',
|
|
20
|
-
} as const satisfies ast.ParserOptions
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* JSON-Pointer prefix for schemas declared under `components.schemas` in an OpenAPI document.
|
|
24
|
-
*
|
|
25
|
-
* Used when building or parsing `$ref` strings.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* `${SCHEMA_REF_PREFIX}Pet` // '#/components/schemas/Pet'
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export const SCHEMA_REF_PREFIX = '#/components/schemas/' as const
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* OpenAPI version string written into the stub document created during multi-spec merges.
|
|
36
|
-
*/
|
|
37
|
-
export const MERGE_OPENAPI_VERSION = '3.0.0' as const
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Fallback `info.title` placed in the stub document when merging multiple API files.
|
|
41
|
-
*/
|
|
42
|
-
export const MERGE_DEFAULT_TITLE = 'Merged API' as const
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Fallback `info.version` placed in the stub document when merging multiple API files.
|
|
46
|
-
*/
|
|
47
|
-
export const MERGE_DEFAULT_VERSION = '1.0.0' as const
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Set of JSON Schema keywords that prevent a schema fragment from being inlined during `allOf` flattening.
|
|
51
|
-
*
|
|
52
|
-
* A fragment that contains any of these keys carries structural meaning of its own and must stay as a separate
|
|
53
|
-
* intersection member rather than being merged into the parent.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* import { structuralKeys } from '@kubb/adapter-oas'
|
|
58
|
-
*
|
|
59
|
-
* const isStructural = Object.keys(fragment).some((key) => structuralKeys.has(key))
|
|
60
|
-
* // true when fragment has e.g. 'properties' or 'oneOf'
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export const structuralKeys = new Set(['properties', 'items', 'additionalProperties', 'oneOf', 'anyOf', 'allOf', 'not'] as const)
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Static map from OAS `format` strings to Kubb `SchemaType` values.
|
|
67
|
-
*
|
|
68
|
-
* Only formats whose AST type differs from the OAS `type` field appear here.
|
|
69
|
-
* Formats that depend on runtime options (`int64`, `date-time`, `date`, `time`) are handled separately
|
|
70
|
-
* in the parser. `ipv4` and `ipv6` map to their own dedicated schema types; `hostname` and
|
|
71
|
-
* `idn-hostname` map to `'url'` as the closest generic string-format type.
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```ts
|
|
75
|
-
* import { formatMap } from '@kubb/adapter-oas'
|
|
76
|
-
*
|
|
77
|
-
* formatMap['uuid'] // 'uuid'
|
|
78
|
-
* formatMap['binary'] // 'blob'
|
|
79
|
-
* formatMap['float'] // 'number'
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export const formatMap = {
|
|
83
|
-
uuid: 'uuid',
|
|
84
|
-
email: 'email',
|
|
85
|
-
'idn-email': 'email',
|
|
86
|
-
uri: 'url',
|
|
87
|
-
'uri-reference': 'url',
|
|
88
|
-
url: 'url',
|
|
89
|
-
ipv4: 'ipv4',
|
|
90
|
-
ipv6: 'ipv6',
|
|
91
|
-
hostname: 'url',
|
|
92
|
-
'idn-hostname': 'url',
|
|
93
|
-
binary: 'blob',
|
|
94
|
-
byte: 'blob',
|
|
95
|
-
// Numeric formats override the OAS `type` because format is more specific.
|
|
96
|
-
// See https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7
|
|
97
|
-
int32: 'integer',
|
|
98
|
-
float: 'number',
|
|
99
|
-
double: 'number',
|
|
100
|
-
} as const satisfies Record<string, ast.SchemaType>
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Vendor extension keys that attach human-readable labels to enum values, checked in priority order.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```ts
|
|
107
|
-
* import { enumExtensionKeys } from '@kubb/adapter-oas'
|
|
108
|
-
*
|
|
109
|
-
* const key = enumExtensionKeys.find((k) => k in schema) // 'x-enumNames' | 'x-enum-varnames' | undefined
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
export const enumExtensionKeys = ['x-enumNames', 'x-enum-varnames'] as const
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Maps `'any' | 'unknown' | 'void'` option strings to their `ScalarSchemaType` constant.
|
|
116
|
-
* Replaces a plain object lookup with a `Map` for explicit key membership testing via `.has()`.
|
|
117
|
-
*/
|
|
118
|
-
export const typeOptionMap = new Map<'any' | 'unknown' | 'void', ast.ScalarSchemaType>([
|
|
119
|
-
['any', ast.schemaTypes.any],
|
|
120
|
-
['unknown', ast.schemaTypes.unknown],
|
|
121
|
-
['void', ast.schemaTypes.void],
|
|
122
|
-
])
|