@kubb/adapter-oas 5.0.0-alpha.2 → 5.0.0-alpha.21

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,158 @@
1
+ // external packages
2
+
3
+ import { httpMethods } from '@kubb/ast'
4
+ import type { HttpMethod as AstHttpMethod, ParserOptions } from '@kubb/ast/types'
1
5
  import type { AdapterFactoryOptions } from '@kubb/core'
2
- import type { Oas as OasClass } from './oas/Oas.ts'
3
- 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 & {})
32
+
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
4
100
 
5
- export type OasAdapterOptions = {
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 = {
6
143
  /**
7
144
  * Validate the OpenAPI spec before parsing.
8
145
  * @default true
9
146
  */
10
147
  validate?: boolean
11
148
  /**
12
- * Override the `Oas` class (e.g. for custom subclass behavior).
149
+ * Preferred content-type used when extracting request/response schemas.
150
+ * Defaults to the first valid JSON media type found in the spec.
13
151
  */
14
- oasClass?: typeof OasClass
152
+ contentType?: ContentType
15
153
  /**
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`.
154
+ * Index into `oas.api.servers` for computing `baseURL`.
155
+ * `0` → first server, `1` second server. Omit to leave `baseURL` undefined.
24
156
  */
25
157
  serverIndex?: number
26
158
  /**
@@ -28,60 +160,50 @@ export type OasAdapterOptions = {
28
160
  * Only used when `serverIndex` is set.
29
161
  *
30
162
  * @example
163
+ * ```ts
31
164
  * // spec server: "https://api.{env}.example.com"
32
165
  * serverVariables: { env: 'prod' }
33
166
  * // → baseURL: "https://api.prod.example.com"
167
+ * ```
34
168
  */
35
169
  serverVariables?: Record<string, string>
36
170
  /**
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`.
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`.
40
174
  * @default 'strict'
41
175
  */
42
176
  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
- }
177
+ } & Partial<ParserOptions>
72
178
 
73
- export type OasAdapterResolvedOptions = {
179
+ /**
180
+ * Resolved adapter options available at runtime after defaults have been applied.
181
+ */
182
+ export type AdapterOasResolvedOptions = {
74
183
  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']>
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']
193
+ /**
194
+ * Map from original `$ref` paths to their collision-resolved schema names.
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
+ * ```
202
+ */
203
+ nameMapping: Map<string, string>
85
204
  }
86
205
 
87
- 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, Document>