@kubb/adapter-oas 5.0.0-alpha.7 → 5.0.0-alpha.71
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/dist/index.cjs +1203 -1215
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +524 -135
- package/dist/index.js +1194 -1209
- package/dist/index.js.map +1 -1
- package/package.json +32 -35
- package/src/adapter.ts +82 -78
- package/src/constants.ts +78 -66
- package/src/discriminator.ts +108 -0
- package/src/factory.ts +165 -0
- package/src/guards.ts +68 -0
- package/src/index.ts +17 -0
- package/src/parser.ts +546 -655
- package/src/refs.ts +59 -0
- package/src/resolvers.ts +552 -0
- package/src/types.ts +174 -59
- package/src/oas/Oas.ts +0 -616
- 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 -161
package/src/types.ts
CHANGED
|
@@ -1,26 +1,157 @@
|
|
|
1
|
+
// external packages
|
|
2
|
+
|
|
1
3
|
import type { AdapterFactoryOptions } from '@kubb/core'
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
+
import { ast } from '@kubb/core'
|
|
5
|
+
import type { Operation as OASOperation } from 'oas/operation'
|
|
6
|
+
import type {
|
|
7
|
+
DiscriminatorObject as OASDiscriminatorObject,
|
|
8
|
+
OASDocument,
|
|
9
|
+
MediaTypeObject as OASMediaTypeObject,
|
|
10
|
+
ResponseObject as OASResponseObject,
|
|
11
|
+
SchemaObject as OASSchemaObject,
|
|
12
|
+
} from 'oas/types'
|
|
13
|
+
import type { OpenAPIV3 } from 'openapi-types'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Re-exports of `openapi-types` for use by adapter consumers.
|
|
17
|
+
*/
|
|
18
|
+
export type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Content-type string used when selecting request/response schemas from a spec.
|
|
22
|
+
*
|
|
23
|
+
* Accepts `'application/json'` or any arbitrary media type string.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const ct: contentType = 'application/vnd.api+json'
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export type ContentType = 'application/json' | (string & {})
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Augments `oas`'s `SchemaObject` with OAS 3.1 / JSON Schema fields the parser needs.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const schema: SchemaObject = {
|
|
38
|
+
* type: 'string',
|
|
39
|
+
* const: 'dog',
|
|
40
|
+
* contentMediaType: 'application/octet-stream',
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export type SchemaObject = OASSchemaObject & {
|
|
45
|
+
/**
|
|
46
|
+
* OAS 3.0 vendor extension: marks a schema as nullable without using `type: ['null', ...]`.
|
|
47
|
+
*/
|
|
48
|
+
'x-nullable'?: boolean
|
|
49
|
+
/**
|
|
50
|
+
* OAS 3.1: constrains the schema to a single fixed value (equivalent to a one-item `enum`).
|
|
51
|
+
*/
|
|
52
|
+
const?: string | number | boolean | null
|
|
53
|
+
/**
|
|
54
|
+
* OAS 3.1: media type of the schema content. `'application/octet-stream'` on a `string` schema maps to `blob`.
|
|
55
|
+
*/
|
|
56
|
+
contentMediaType?: string
|
|
57
|
+
$ref?: string
|
|
58
|
+
/**
|
|
59
|
+
* OAS 3.1: positional tuple items, replacing the multi-item `items` array from OAS 3.0.
|
|
60
|
+
*/
|
|
61
|
+
prefixItems?: Array<SchemaObject | ReferenceObject>
|
|
62
|
+
/**
|
|
63
|
+
* JSON Schema: maps regex patterns to sub-schemas for validating additional properties.
|
|
64
|
+
*/
|
|
65
|
+
patternProperties?: Record<string, SchemaObject | boolean>
|
|
66
|
+
/**
|
|
67
|
+
* Single-schema form of `items`. Narrowed from the base type to take precedence over the tuple overload.
|
|
68
|
+
*/
|
|
69
|
+
items?: SchemaObject | ReferenceObject
|
|
70
|
+
/**
|
|
71
|
+
* Enum values for this schema (narrowed from `unknown[]`).
|
|
72
|
+
*/
|
|
73
|
+
enum?: Array<string | number | boolean | null>
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Uppercase → lowercase HTTP method map, re-exported for backwards compatibility.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* HttpMethods['GET'] // 'get'
|
|
82
|
+
* HttpMethods['POST'] // 'post'
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export const HttpMethods = Object.fromEntries(Object.entries(ast.httpMethods).map(([lower, upper]) => [upper, lower])) as Record<
|
|
86
|
+
Uppercase<ast.HttpMethod>,
|
|
87
|
+
Lowercase<ast.HttpMethod>
|
|
88
|
+
>
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Lowercase HTTP method string as used by the `oas` package (`'get' | 'post' | ...`).
|
|
92
|
+
*/
|
|
93
|
+
export type HttpMethod = Lowercase<ast.HttpMethod>
|
|
4
94
|
|
|
5
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Normalized OpenAPI document type used throughout the adapter.
|
|
97
|
+
*/
|
|
98
|
+
export type Document = OASDocument
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Operation wrapper type returned by the `oas` package.
|
|
102
|
+
*/
|
|
103
|
+
export type Operation = OASOperation
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* OpenAPI `discriminator` object attached to a `oneOf`/`anyOf` schema.
|
|
107
|
+
*/
|
|
108
|
+
export type DiscriminatorObject = OASDiscriminatorObject
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* OpenAPI `$ref` pointer object (`{ $ref: string }`).
|
|
112
|
+
*/
|
|
113
|
+
export type ReferenceObject = OpenAPIV3.ReferenceObject
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* OpenAPI response object type (may contain `content`, `description`, `headers`).
|
|
117
|
+
*/
|
|
118
|
+
export type ResponseObject = OASResponseObject
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* OpenAPI media type object that maps a content-type to a schema.
|
|
122
|
+
*/
|
|
123
|
+
export type MediaTypeObject = OASMediaTypeObject
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* User-facing options for `adapterOas(...)`.
|
|
127
|
+
*
|
|
128
|
+
* Extends `ParserOptions` from `@kubb/ast` with adapter-specific controls
|
|
129
|
+
* like spec validation and server URL selection.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* adapterOas({
|
|
134
|
+
* validate: false,
|
|
135
|
+
* dateType: 'date',
|
|
136
|
+
* serverIndex: 0,
|
|
137
|
+
* serverVariables: { env: 'prod' },
|
|
138
|
+
* })
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export type AdapterOasOptions = {
|
|
6
142
|
/**
|
|
7
143
|
* Validate the OpenAPI spec before parsing.
|
|
8
144
|
* @default true
|
|
9
145
|
*/
|
|
10
146
|
validate?: boolean
|
|
11
147
|
/**
|
|
12
|
-
*
|
|
148
|
+
* Preferred content-type used when extracting request/response schemas.
|
|
149
|
+
* Defaults to the first valid JSON media type found in the spec.
|
|
13
150
|
*/
|
|
14
|
-
|
|
151
|
+
contentType?: ContentType
|
|
15
152
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
contentType?: contentType
|
|
20
|
-
/**
|
|
21
|
-
* Which server to use from `oas.api.servers` when computing `baseURL`.
|
|
22
|
-
* - `0` → first server, `1` → second server, etc.
|
|
23
|
-
* - When omitted, `baseURL` in the resulting `RootNode.meta` is `undefined`.
|
|
153
|
+
* Index into `oas.api.servers` for computing `baseURL`.
|
|
154
|
+
* `0` → first server, `1` → second server. Omit to leave `baseURL` undefined.
|
|
24
155
|
*/
|
|
25
156
|
serverIndex?: number
|
|
26
157
|
/**
|
|
@@ -28,66 +159,50 @@ export type OasAdapterOptions = {
|
|
|
28
159
|
* Only used when `serverIndex` is set.
|
|
29
160
|
*
|
|
30
161
|
* @example
|
|
162
|
+
* ```ts
|
|
31
163
|
* // spec server: "https://api.{env}.example.com"
|
|
32
164
|
* serverVariables: { env: 'prod' }
|
|
33
165
|
* // → baseURL: "https://api.prod.example.com"
|
|
166
|
+
* ```
|
|
34
167
|
*/
|
|
35
168
|
serverVariables?: Record<string, string>
|
|
36
169
|
/**
|
|
37
|
-
* How the discriminator field
|
|
38
|
-
* - `'strict'` — uses `oneOf` schemas as
|
|
39
|
-
* - `'inherit'` —
|
|
170
|
+
* How the discriminator field is interpreted.
|
|
171
|
+
* - `'strict'` — uses `oneOf` schemas as written in the spec.
|
|
172
|
+
* - `'inherit'` — propagates discriminator values into child schemas from `discriminator.mapping`.
|
|
40
173
|
* @default 'strict'
|
|
41
174
|
*/
|
|
42
175
|
discriminator?: 'strict' | 'inherit'
|
|
43
|
-
|
|
44
|
-
* Automatically resolve name collisions across schema components.
|
|
45
|
-
* @default false
|
|
46
|
-
*/
|
|
47
|
-
collisionDetection?: boolean
|
|
48
|
-
/**
|
|
49
|
-
* How `format: 'date-time'` schemas are represented in the AST.
|
|
50
|
-
* - `'string'` maps to a `datetime` string node.
|
|
51
|
-
* - `'date'` maps to a JavaScript `Date` node.
|
|
52
|
-
* - `false` falls through to a plain `string` node.
|
|
53
|
-
* @default 'string'
|
|
54
|
-
*/
|
|
55
|
-
dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
|
|
56
|
-
/**
|
|
57
|
-
* Whether `type: 'integer'` / `format: 'int64'` produces `number` or `bigint` nodes.
|
|
58
|
-
* @default 'number'
|
|
59
|
-
*/
|
|
60
|
-
integerType?: 'number' | 'bigint'
|
|
61
|
-
/**
|
|
62
|
-
* AST type used when no schema type can be inferred.
|
|
63
|
-
* @default 'any'
|
|
64
|
-
*/
|
|
65
|
-
unknownType?: 'any' | 'unknown' | 'void'
|
|
66
|
-
/**
|
|
67
|
-
* AST type used for completely empty schemas (`{}`).
|
|
68
|
-
* @default `unknownType`
|
|
69
|
-
*/
|
|
70
|
-
emptySchemaType?: 'any' | 'unknown' | 'void'
|
|
71
|
-
}
|
|
176
|
+
} & Partial<ast.ParserOptions>
|
|
72
177
|
|
|
73
|
-
|
|
178
|
+
/**
|
|
179
|
+
* Resolved adapter options available at runtime after defaults have been applied.
|
|
180
|
+
*/
|
|
181
|
+
export type AdapterOasResolvedOptions = {
|
|
74
182
|
validate: boolean
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
emptySchemaType: NonNullable<OasAdapterOptions['emptySchemaType']>
|
|
183
|
+
contentType: AdapterOasOptions['contentType']
|
|
184
|
+
serverIndex: AdapterOasOptions['serverIndex']
|
|
185
|
+
serverVariables: AdapterOasOptions['serverVariables']
|
|
186
|
+
discriminator: NonNullable<AdapterOasOptions['discriminator']>
|
|
187
|
+
dateType: NonNullable<AdapterOasOptions['dateType']>
|
|
188
|
+
integerType: NonNullable<AdapterOasOptions['integerType']>
|
|
189
|
+
unknownType: NonNullable<AdapterOasOptions['unknownType']>
|
|
190
|
+
emptySchemaType: NonNullable<AdapterOasOptions['emptySchemaType']>
|
|
191
|
+
enumSuffix: AdapterOasOptions['enumSuffix']
|
|
85
192
|
/**
|
|
86
193
|
* Map from original `$ref` paths to their collision-resolved schema names.
|
|
87
|
-
* Populated
|
|
88
|
-
*
|
|
194
|
+
* Populated after each `parse()` call.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* nameMapping.get('#/components/schemas/Order') // 'Order'
|
|
199
|
+
* nameMapping.get('#/components/responses/Order') // 'OrderResponse'
|
|
200
|
+
* ```
|
|
89
201
|
*/
|
|
90
202
|
nameMapping: Map<string, string>
|
|
91
203
|
}
|
|
92
204
|
|
|
93
|
-
|
|
205
|
+
/**
|
|
206
|
+
* `@kubb/core` adapter factory type for the OpenAPI adapter.
|
|
207
|
+
*/
|
|
208
|
+
export type AdapterOas = AdapterFactoryOptions<'oas', AdapterOasOptions, AdapterOasResolvedOptions, Document>
|