@kubb/adapter-oas 5.0.0-alpha.16 → 5.0.0-alpha.18

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,52 +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 & {})
4
32
 
5
33
  /**
6
- * Controls how various OAS constructs are mapped to Kubb AST nodes.
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
+ * ```
7
44
  */
8
- export type ParserOptions = {
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
9
50
  /**
10
- * How `format: 'date-time'` schemas are represented. `false` falls through to a plain string.
51
+ * OAS 3.1: constrains the schema to a single fixed value (equivalent to a one-item `enum`).
11
52
  */
12
- dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
53
+ const?: string | number | boolean | null
13
54
  /**
14
- * Whether `type: 'integer'` and `format: 'int64'` produce `number` or `bigint` nodes.
55
+ * OAS 3.1: media type of the schema content. `'application/octet-stream'` on a `string` schema maps to `blob`.
15
56
  */
16
- integerType?: 'number' | 'bigint'
57
+ contentMediaType?: string
58
+ $ref?: string
17
59
  /**
18
- * AST type used when no schema type can be inferred.
60
+ * OAS 3.1: positional tuple items, replacing the multi-item `items` array from OAS 3.0.
19
61
  */
20
- unknownType: 'any' | 'unknown' | 'void'
62
+ prefixItems?: Array<SchemaObject | ReferenceObject>
21
63
  /**
22
- * AST type used for completely empty schemas (`{}`).
64
+ * JSON Schema: maps regex patterns to sub-schemas for validating additional properties.
23
65
  */
24
- emptySchemaType: 'any' | 'unknown' | 'void'
66
+ patternProperties?: Record<string, SchemaObject | boolean>
25
67
  /**
26
- * Suffix appended to derived enum names when building property schema names.
68
+ * Single-schema form of `items`. Narrowed from the base type to take precedence over the tuple overload.
27
69
  */
28
- enumSuffix: 'enum' | (string & {})
70
+ items?: SchemaObject | ReferenceObject
71
+ /**
72
+ * Enum values for this schema (narrowed from `unknown[]`).
73
+ */
74
+ enum?: Array<string | number | boolean | null>
29
75
  }
30
76
 
31
- export type OasAdapterOptions = {
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 = {
32
143
  /**
33
144
  * Validate the OpenAPI spec before parsing.
34
145
  * @default true
35
146
  */
36
147
  validate?: boolean
37
148
  /**
38
- * Override the `Oas` class (e.g. for custom subclass behavior).
39
- */
40
- oasClass?: typeof OasClass
41
- /**
42
- * Restrict which content-type is used when extracting request/response schemas.
43
- * 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.
44
151
  */
45
152
  contentType?: contentType
46
153
  /**
47
- * Which server to use from `oas.api.servers` when computing `baseURL`.
48
- * - `0` → first server, `1` → second server, etc.
49
- * - 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.
50
156
  */
51
157
  serverIndex?: number
52
158
  /**
@@ -54,45 +160,50 @@ export type OasAdapterOptions = {
54
160
  * Only used when `serverIndex` is set.
55
161
  *
56
162
  * @example
163
+ * ```ts
57
164
  * // spec server: "https://api.{env}.example.com"
58
165
  * serverVariables: { env: 'prod' }
59
166
  * // → baseURL: "https://api.prod.example.com"
167
+ * ```
60
168
  */
61
169
  serverVariables?: Record<string, string>
62
170
  /**
63
- * How the discriminator field should be interpreted.
64
- * - `'strict'` — uses `oneOf` schemas as defined.
65
- * - `'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`.
66
174
  * @default 'strict'
67
175
  */
68
176
  discriminator?: 'strict' | 'inherit'
69
- /**
70
- * How `format: 'date-time'` schemas are represented in the AST.
71
- * - `'string'` maps to a `datetime` string node.
72
- * - `'date'` maps to a JavaScript `Date` node.
73
- * - `false` falls through to a plain `string` node.
74
- * @default 'string'
75
- */
76
177
  } & Partial<ParserOptions>
77
178
 
78
- export type OasAdapterResolvedOptions = {
179
+ /**
180
+ * Resolved adapter options available at runtime after defaults have been applied.
181
+ */
182
+ export type AdapterOasResolvedOptions = {
79
183
  validate: boolean
80
- oasClass: OasAdapterOptions['oasClass']
81
- contentType: OasAdapterOptions['contentType']
82
- serverIndex: OasAdapterOptions['serverIndex']
83
- serverVariables: OasAdapterOptions['serverVariables']
84
- discriminator: NonNullable<OasAdapterOptions['discriminator']>
85
- dateType: NonNullable<OasAdapterOptions['dateType']>
86
- integerType: NonNullable<OasAdapterOptions['integerType']>
87
- unknownType: NonNullable<OasAdapterOptions['unknownType']>
88
- emptySchemaType: NonNullable<OasAdapterOptions['emptySchemaType']>
89
- 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']
90
193
  /**
91
194
  * Map from original `$ref` paths to their collision-resolved schema names.
92
- * Populated by the adapter after each `parse()` call.
93
- * 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
+ * ```
94
202
  */
95
203
  nameMapping: Map<string, string>
96
204
  }
97
205
 
98
- 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>
package/src/infer.ts DELETED
@@ -1,85 +0,0 @@
1
- import type {
2
- ArraySchemaNode,
3
- DateSchemaNode,
4
- DatetimeSchemaNode,
5
- EnumSchemaNode,
6
- IntersectionSchemaNode,
7
- NumberSchemaNode,
8
- ObjectSchemaNode,
9
- RefSchemaNode,
10
- ScalarSchemaNode,
11
- SchemaNode,
12
- StringSchemaNode,
13
- TimeSchemaNode,
14
- UnionSchemaNode,
15
- } from '@kubb/ast/types'
16
- import type { SchemaObject } from './oas/types.ts'
17
- import type { ParserOptions } from './types.ts'
18
-
19
- /**
20
- * Maps each `dateType` option value to the AST node produced by `format: 'date-time'`.
21
- */
22
- type DateTimeNodeByDateType = {
23
- date: DateSchemaNode
24
- string: DatetimeSchemaNode
25
- stringOffset: DatetimeSchemaNode
26
- stringLocal: DatetimeSchemaNode
27
- false: StringSchemaNode
28
- }
29
-
30
- /**
31
- * Resolves the AST node produced by `format: 'date-time'` based on the `dateType` option.
32
- */
33
- type ResolveDateTimeNode<TDateType extends ParserOptions['dateType']> = DateTimeNodeByDateType[TDateType extends keyof DateTimeNodeByDateType
34
- ? TDateType
35
- : 'string']
36
-
37
- /**
38
- * Single source of truth: ordered list of `[shape, SchemaNode]` pairs.
39
- * `InferSchemaNode` walks this tuple in order and returns the node type of the first matching entry.
40
- * Parameterized over `TDateType` so `format: 'date-time'` resolves to the correct node based on the option.
41
- */
42
- type SchemaNodeMap<TDateType extends ParserOptions['dateType'] = 'string'> = [
43
- [{ $ref: string }, RefSchemaNode],
44
- [{ allOf: ReadonlyArray<unknown>; properties: object }, IntersectionSchemaNode],
45
- [{ allOf: readonly [unknown, unknown, ...unknown[]] }, IntersectionSchemaNode],
46
- [{ allOf: ReadonlyArray<unknown> }, SchemaNode],
47
- [{ oneOf: ReadonlyArray<unknown> }, UnionSchemaNode],
48
- [{ anyOf: ReadonlyArray<unknown> }, UnionSchemaNode],
49
- [{ const: null }, ScalarSchemaNode],
50
- [{ const: string | number | boolean }, EnumSchemaNode],
51
- [{ type: ReadonlyArray<string> }, UnionSchemaNode],
52
- [{ type: 'array'; enum: ReadonlyArray<unknown> }, ArraySchemaNode],
53
- [{ enum: ReadonlyArray<unknown> }, EnumSchemaNode],
54
- [{ type: 'object' }, ObjectSchemaNode],
55
- [{ additionalProperties: boolean | {} }, ObjectSchemaNode],
56
- [{ type: 'array' }, ArraySchemaNode],
57
- [{ items: object }, ArraySchemaNode],
58
- [{ prefixItems: ReadonlyArray<unknown> }, ArraySchemaNode],
59
- [{ type: string; format: 'date-time' }, ResolveDateTimeNode<TDateType>],
60
- [{ type: string; format: 'date' }, DateSchemaNode],
61
- [{ type: string; format: 'time' }, TimeSchemaNode],
62
- [{ format: 'date-time' }, ResolveDateTimeNode<TDateType>],
63
- [{ format: 'date' }, DateSchemaNode],
64
- [{ format: 'time' }, TimeSchemaNode],
65
- [{ type: 'string' }, StringSchemaNode],
66
- [{ type: 'number' }, NumberSchemaNode],
67
- [{ type: 'integer' }, NumberSchemaNode],
68
- [{ type: 'bigint' }, NumberSchemaNode],
69
- [{ type: string }, ScalarSchemaNode],
70
- [{ minLength: number }, StringSchemaNode],
71
- [{ maxLength: number }, StringSchemaNode],
72
- [{ pattern: string }, StringSchemaNode],
73
- [{ minimum: number }, NumberSchemaNode],
74
- [{ maximum: number }, NumberSchemaNode],
75
- ]
76
-
77
- export type InferSchemaNode<
78
- TSchema extends SchemaObject,
79
- TDateType extends ParserOptions['dateType'] = 'string',
80
- TEntries extends ReadonlyArray<[object, SchemaNode]> = SchemaNodeMap<TDateType>,
81
- > = TEntries extends [infer TEntry extends [object, SchemaNode], ...infer TRest extends ReadonlyArray<[object, SchemaNode]>]
82
- ? TSchema extends TEntry[0]
83
- ? TEntry[1]
84
- : InferSchemaNode<TSchema, TDateType, TRest>
85
- : SchemaNode
package/src/naming.ts DELETED
@@ -1,25 +0,0 @@
1
- import { pascalCase } from '@internals/utils'
2
- import { narrowSchema } from '@kubb/ast'
3
- import type { SchemaNode } from '@kubb/ast/types'
4
-
5
- export function resolveChildName(parentName: string | undefined, propName: string): string | undefined {
6
- return parentName ? pascalCase([parentName, propName].join(' ')) : undefined
7
- }
8
-
9
- export function resolveEnumPropName(parentName: string | undefined, propName: string, enumSuffix: string): string {
10
- return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
11
- }
12
-
13
- export function applyEnumName(propNode: SchemaNode, parentName: string | undefined, propName: string, enumSuffix: string): SchemaNode {
14
- const enumNode = narrowSchema(propNode, 'enum')
15
-
16
- if (enumNode?.primitive === 'boolean') {
17
- return { ...propNode, name: undefined }
18
- }
19
-
20
- if (enumNode) {
21
- return { ...propNode, name: resolveEnumPropName(parentName, propName, enumSuffix) }
22
- }
23
-
24
- return propNode
25
- }