@kubb/adapter-oas 5.0.0-alpha.17 → 5.0.0-alpha.19

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