@kubb/adapter-oas 5.0.0-alpha.9 → 5.0.0-beta.2

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/src/types.ts CHANGED
@@ -1,26 +1,155 @@
1
+ // external packages
2
+
1
3
  import type { AdapterFactoryOptions } from '@kubb/core'
2
- import type { Oas as OasClass } from './oas/Oas.ts'
3
- import type { contentType } from './oas/types.ts'
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 for selecting request/response schemas from an OpenAPI spec.
22
+ * Supports `'application/json'` or any other media type.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const ct: ContentType = 'application/vnd.api+json'
27
+ * ```
28
+ */
29
+ export type ContentType = 'application/json' | (string & {})
30
+
31
+ /**
32
+ * Extended OpenAPI 3.0 schema object that includes OpenAPI 3.1 and JSON Schema fields.
33
+ * The parser uses these additional fields to handle newer spec versions.
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
+ * Maps uppercase HTTP method names to lowercase 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
+ * HTTP method as a lowercase string (`'get' | 'post' | ...`).
92
+ */
93
+ export type HttpMethod = Lowercase<ast.HttpMethod>
4
94
 
5
- export type OasAdapterOptions = {
95
+ /**
96
+ * Normalized OpenAPI document after parsing.
97
+ */
98
+ export type Document = OASDocument
99
+
100
+ /**
101
+ * API operation extracted from an OpenAPI document.
102
+ */
103
+ export type Operation = OASOperation
104
+
105
+ /**
106
+ * Discriminator object for `oneOf`/`anyOf` schemas in OpenAPI.
107
+ */
108
+ export type DiscriminatorObject = OASDiscriminatorObject
109
+
110
+ /**
111
+ * OpenAPI reference object pointing to a schema definition via `$ref`.
112
+ */
113
+ export type ReferenceObject = OpenAPIV3.ReferenceObject
114
+
115
+ /**
116
+ * OpenAPI response object from a spec that contains schema, status code, and headers.
117
+ */
118
+ export type ResponseObject = OASResponseObject
119
+
120
+ /**
121
+ * OpenAPI media type object that maps a content-type string to its schema.
122
+ */
123
+ export type MediaTypeObject = OASMediaTypeObject
124
+
125
+ /**
126
+ * Configuration options for the OpenAPI adapter.
127
+ * Controls spec validation, content-type selection, and server URL resolution.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * adapterOas({
132
+ * validate: false,
133
+ * dateType: 'date',
134
+ * serverIndex: 0,
135
+ * serverVariables: { env: 'prod' },
136
+ * })
137
+ * ```
138
+ */
139
+ export type AdapterOasOptions = {
6
140
  /**
7
141
  * Validate the OpenAPI spec before parsing.
8
142
  * @default true
9
143
  */
10
144
  validate?: boolean
11
145
  /**
12
- * Override the `Oas` class (e.g. for custom subclass behavior).
146
+ * Preferred content-type used when extracting request/response schemas.
147
+ * Defaults to the first valid JSON media type found in the spec.
13
148
  */
14
- oasClass?: typeof OasClass
149
+ contentType?: ContentType
15
150
  /**
16
- * Restrict which content-type is used when extracting request/response schemas.
17
- * By default, the first valid JSON media type is used.
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`.
151
+ * Index into `oas.api.servers` for computing `baseURL`.
152
+ * `0` → first server, `1` second server. Omit to leave `baseURL` undefined.
24
153
  */
25
154
  serverIndex?: number
26
155
  /**
@@ -28,66 +157,50 @@ export type OasAdapterOptions = {
28
157
  * Only used when `serverIndex` is set.
29
158
  *
30
159
  * @example
160
+ * ```ts
31
161
  * // spec server: "https://api.{env}.example.com"
32
162
  * serverVariables: { env: 'prod' }
33
163
  * // → baseURL: "https://api.prod.example.com"
164
+ * ```
34
165
  */
35
166
  serverVariables?: Record<string, string>
36
167
  /**
37
- * How the discriminator field should be interpreted.
38
- * - `'strict'` — uses `oneOf` schemas as defined.
39
- * - `'inherit'` — replaces `oneOf` with the schema from `discriminator.mapping`.
168
+ * How the discriminator field is interpreted.
169
+ * - `'strict'` — uses `oneOf` schemas as written in the spec.
170
+ * - `'inherit'` — propagates discriminator values into child schemas from `discriminator.mapping`.
40
171
  * @default 'strict'
41
172
  */
42
173
  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
- }
174
+ } & Partial<ast.ParserOptions>
72
175
 
73
- export type OasAdapterResolvedOptions = {
176
+ /**
177
+ * Adapter options after defaults have been applied and schema name collisions resolved.
178
+ */
179
+ export type AdapterOasResolvedOptions = {
74
180
  validate: boolean
75
- oasClass: OasAdapterOptions['oasClass']
76
- contentType: OasAdapterOptions['contentType']
77
- serverIndex: OasAdapterOptions['serverIndex']
78
- serverVariables: OasAdapterOptions['serverVariables']
79
- discriminator: NonNullable<OasAdapterOptions['discriminator']>
80
- collisionDetection: boolean
81
- dateType: NonNullable<OasAdapterOptions['dateType']>
82
- integerType: NonNullable<OasAdapterOptions['integerType']>
83
- unknownType: NonNullable<OasAdapterOptions['unknownType']>
84
- emptySchemaType: NonNullable<OasAdapterOptions['emptySchemaType']>
181
+ contentType: AdapterOasOptions['contentType']
182
+ serverIndex: AdapterOasOptions['serverIndex']
183
+ serverVariables: AdapterOasOptions['serverVariables']
184
+ discriminator: NonNullable<AdapterOasOptions['discriminator']>
185
+ dateType: NonNullable<AdapterOasOptions['dateType']>
186
+ integerType: NonNullable<AdapterOasOptions['integerType']>
187
+ unknownType: NonNullable<AdapterOasOptions['unknownType']>
188
+ emptySchemaType: NonNullable<AdapterOasOptions['emptySchemaType']>
189
+ enumSuffix: AdapterOasOptions['enumSuffix']
85
190
  /**
86
191
  * Map from original `$ref` paths to their collision-resolved schema names.
87
- * Populated by the adapter after each `parse()` call.
88
- * e.g. `'#/components/schemas/Order'` → `'OrderSchema'`
192
+ * Populated after each `parse()` call.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * nameMapping.get('#/components/schemas/Order') // 'Order'
197
+ * nameMapping.get('#/components/responses/Order') // 'OrderResponse'
198
+ * ```
89
199
  */
90
200
  nameMapping: Map<string, string>
91
201
  }
92
202
 
93
- export type OasAdapter = AdapterFactoryOptions<'oas', OasAdapterOptions, OasAdapterResolvedOptions>
203
+ /**
204
+ * `@kubb/core` adapter factory type for the OpenAPI adapter.
205
+ */
206
+ export type AdapterOas = AdapterFactoryOptions<'oas', AdapterOasOptions, AdapterOasResolvedOptions, Document>