@kubb/adapter-oas 5.0.0-alpha.4 → 5.0.0-alpha.41

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.d.ts CHANGED
@@ -1,115 +1,133 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import * as _kubb_core0 from "@kubb/core";
3
- import { AdapterFactoryOptions } from "@kubb/core";
4
- import { DiscriminatorObject, OASDocument, SchemaObject } from "oas/types";
5
- import BaseOas from "oas";
6
- import * as oas_normalize_lib_types0 from "oas-normalize/lib/types";
7
- import { Operation } from "oas/operation";
2
+ import * as _$_kubb_core0 from "@kubb/core";
3
+ import { AdapterFactoryOptions, AdapterSource, ast } from "@kubb/core";
4
+ import { DiscriminatorObject as DiscriminatorObject$1, MediaTypeObject as MediaTypeObject$1, OASDocument, ResponseObject as ResponseObject$1, SchemaObject as SchemaObject$1 } from "oas/types";
5
+ import { Operation as Operation$1 } from "oas/operation";
8
6
  import { OpenAPIV3 } from "openapi-types";
9
7
 
10
- //#region src/oas/types.d.ts
11
- type contentType = 'application/json' | (string & {});
12
- type SchemaObject$1 = SchemaObject & {
8
+ //#region src/types.d.ts
9
+ /**
10
+ * Content-type string used when selecting request/response schemas from a spec.
11
+ *
12
+ * Accepts `'application/json'` or any arbitrary media type string.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const ct: contentType = 'application/vnd.api+json'
17
+ * ```
18
+ */
19
+ type ContentType = 'application/json' | (string & {});
20
+ /**
21
+ * Augments `oas`'s `SchemaObject` with OAS 3.1 / JSON Schema fields the parser needs.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const schema: SchemaObject = {
26
+ * type: 'string',
27
+ * const: 'dog',
28
+ * contentMediaType: 'application/octet-stream',
29
+ * }
30
+ * ```
31
+ */
32
+ type SchemaObject = SchemaObject$1 & {
13
33
  /**
14
- * OAS 3.1 extension: allows marking a schema as nullable even when `type` does not include `'null'`.
34
+ * OAS 3.0 vendor extension: marks a schema as nullable without using `type: ['null', ...]`.
15
35
  */
16
36
  'x-nullable'?: boolean;
17
37
  /**
18
- * OAS 3.1: constrains the schema to a single fixed value.
19
- * Semantically equivalent to a one-item `enum`.
38
+ * OAS 3.1: constrains the schema to a single fixed value (equivalent to a one-item `enum`).
20
39
  */
21
40
  const?: string | number | boolean | null;
22
41
  /**
23
- * OAS 3.1: specifies the media type of the schema content.
24
- * When set to `'application/octet-stream'` on a `string` schema, the schema is treated as binary (`blob`).
42
+ * OAS 3.1: media type of the schema content. `'application/octet-stream'` on a `string` schema maps to `blob`.
25
43
  */
26
44
  contentMediaType?: string;
27
45
  $ref?: string;
28
46
  /**
29
- * OAS 3.1 / JSON Schema: positional items in a tuple schema.
30
- * Replaces the OAS 3.0 multi-item `items` array syntax.
47
+ * OAS 3.1: positional tuple items, replacing the multi-item `items` array from OAS 3.0.
31
48
  */
32
- prefixItems?: Array<SchemaObject$1 | ReferenceObject>;
49
+ prefixItems?: Array<SchemaObject | ReferenceObject>;
33
50
  /**
34
- * JSON Schema: maps regex patterns to sub-schemas for additional property validation.
51
+ * JSON Schema: maps regex patterns to sub-schemas for validating additional properties.
35
52
  */
36
- patternProperties?: Record<string, SchemaObject$1 | boolean>;
53
+ patternProperties?: Record<string, SchemaObject | boolean>;
37
54
  /**
38
- * OAS 3.0 / JSON Schema: single-schema form.
39
- * The OAS base type already includes this, but we re-declare it here to ensure
40
- * the single-schema overload takes precedence over the multi-schema tuple form.
55
+ * Single-schema form of `items`. Narrowed from the base type to take precedence over the tuple overload.
41
56
  */
42
- items?: SchemaObject$1 | ReferenceObject;
57
+ items?: SchemaObject | ReferenceObject;
43
58
  /**
44
- * Enum values for this schema (narrowed from `unknown[]` in the base type).
59
+ * Enum values for this schema (narrowed from `unknown[]`).
45
60
  */
46
61
  enum?: Array<string | number | boolean | null>;
47
62
  };
63
+ /**
64
+ * Uppercase → lowercase HTTP method map, re-exported for backwards compatibility.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * HttpMethods['GET'] // 'get'
69
+ * HttpMethods['POST'] // 'post'
70
+ * ```
71
+ */
72
+ declare const HttpMethods: Record<Uppercase<ast.HttpMethod>, Lowercase<ast.HttpMethod>>;
73
+ /**
74
+ * Lowercase HTTP method string as used by the `oas` package (`'get' | 'post' | ...`).
75
+ */
76
+ type HttpMethod = Lowercase<ast.HttpMethod>;
77
+ /**
78
+ * Normalized OpenAPI document type used throughout the adapter.
79
+ */
48
80
  type Document = OASDocument;
49
- type Operation$1 = Operation;
50
- type DiscriminatorObject$1 = DiscriminatorObject;
81
+ /**
82
+ * Operation wrapper type returned by the `oas` package.
83
+ */
84
+ type Operation = Operation$1;
85
+ /**
86
+ * OpenAPI `discriminator` object attached to a `oneOf`/`anyOf` schema.
87
+ */
88
+ type DiscriminatorObject = DiscriminatorObject$1;
89
+ /**
90
+ * OpenAPI `$ref` pointer object (`{ $ref: string }`).
91
+ */
51
92
  type ReferenceObject = OpenAPIV3.ReferenceObject;
52
- //#endregion
53
- //#region src/oas/Oas.d.ts
54
- type OasOptions = {
55
- contentType?: contentType;
56
- discriminator?: 'strict' | 'inherit';
57
- /**
58
- * Resolve name collisions when schemas from different components share the same name (case-insensitive).
59
- * @default false
60
- */
61
- collisionDetection?: boolean;
62
- };
63
- declare class Oas extends BaseOas {
64
- #private;
65
- document: Document;
66
- constructor(document: Document);
67
- setOptions(options: OasOptions): void;
68
- get options(): OasOptions;
69
- get<T = unknown>($ref: string): T | null;
70
- getKey($ref: string): string | undefined;
71
- set($ref: string, value: unknown): false | undefined;
72
- getDiscriminator(schema: SchemaObject$1 | null): DiscriminatorObject$1 | null;
73
- dereferenceWithRef<T = unknown>(schema?: T): T;
74
- getResponseSchema(operation: Operation$1, statusCode: string | number): SchemaObject$1;
75
- getRequestSchema(operation: Operation$1): SchemaObject$1 | undefined;
76
- getParametersSchema(operation: Operation$1, inKey: 'path' | 'query' | 'header'): SchemaObject$1 | null;
77
- validate(): Promise<oas_normalize_lib_types0.ValidationResult>;
78
- flattenSchema(schema: SchemaObject$1 | null): SchemaObject$1 | null;
79
- /**
80
- * Get schemas from OpenAPI components (schemas, responses, requestBodies).
81
- * Returns schemas in dependency order along with name mapping for collision resolution.
82
- */
83
- getSchemas(options?: {
84
- contentType?: contentType;
85
- includes?: Array<'schemas' | 'responses' | 'requestBodies'>;
86
- collisionDetection?: boolean;
87
- }): {
88
- schemas: Record<string, SchemaObject$1>;
89
- nameMapping: Map<string, string>;
90
- };
91
- }
92
- //#endregion
93
- //#region src/types.d.ts
94
- type OasAdapterOptions = {
93
+ /**
94
+ * OpenAPI response object type (may contain `content`, `description`, `headers`).
95
+ */
96
+ type ResponseObject = ResponseObject$1;
97
+ /**
98
+ * OpenAPI media type object that maps a content-type to a schema.
99
+ */
100
+ type MediaTypeObject = MediaTypeObject$1;
101
+ /**
102
+ * User-facing options for `adapterOas(...)`.
103
+ *
104
+ * Extends `ParserOptions` from `@kubb/ast` with adapter-specific controls
105
+ * like spec validation and server URL selection.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * adapterOas({
110
+ * validate: false,
111
+ * dateType: 'date',
112
+ * serverIndex: 0,
113
+ * serverVariables: { env: 'prod' },
114
+ * })
115
+ * ```
116
+ */
117
+ type AdapterOasOptions = {
95
118
  /**
96
119
  * Validate the OpenAPI spec before parsing.
97
120
  * @default true
98
121
  */
99
122
  validate?: boolean;
100
123
  /**
101
- * Override the `Oas` class (e.g. for custom subclass behavior).
124
+ * Preferred content-type used when extracting request/response schemas.
125
+ * Defaults to the first valid JSON media type found in the spec.
102
126
  */
103
- oasClass?: typeof Oas;
127
+ contentType?: ContentType;
104
128
  /**
105
- * Restrict which content-type is used when extracting request/response schemas.
106
- * By default, the first valid JSON media type is used.
107
- */
108
- contentType?: contentType;
109
- /**
110
- * Which server to use from `oas.api.servers` when computing `baseURL`.
111
- * - `0` → first server, `1` → second server, etc.
112
- * - When omitted, `baseURL` in the resulting `RootNode.meta` is `undefined`.
129
+ * Index into `oas.api.servers` for computing `baseURL`.
130
+ * `0` → first server, `1` second server. Omit to leave `baseURL` undefined.
113
131
  */
114
132
  serverIndex?: number;
115
133
  /**
@@ -117,89 +135,141 @@ type OasAdapterOptions = {
117
135
  * Only used when `serverIndex` is set.
118
136
  *
119
137
  * @example
138
+ * ```ts
120
139
  * // spec server: "https://api.{env}.example.com"
121
140
  * serverVariables: { env: 'prod' }
122
141
  * // → baseURL: "https://api.prod.example.com"
142
+ * ```
123
143
  */
124
144
  serverVariables?: Record<string, string>;
125
145
  /**
126
- * How the discriminator field should be interpreted.
127
- * - `'strict'` — uses `oneOf` schemas as defined.
128
- * - `'inherit'` — replaces `oneOf` with the schema from `discriminator.mapping`.
146
+ * How the discriminator field is interpreted.
147
+ * - `'strict'` — uses `oneOf` schemas as written in the spec.
148
+ * - `'inherit'` — propagates discriminator values into child schemas from `discriminator.mapping`.
129
149
  * @default 'strict'
130
150
  */
131
151
  discriminator?: 'strict' | 'inherit';
132
- /**
133
- * Automatically resolve name collisions across schema components.
134
- * @default false
135
- */
136
- collisionDetection?: boolean;
137
- /**
138
- * How `format: 'date-time'` schemas are represented in the AST.
139
- * - `'string'` maps to a `datetime` string node.
140
- * - `'date'` maps to a JavaScript `Date` node.
141
- * - `false` falls through to a plain `string` node.
142
- * @default 'string'
143
- */
144
- dateType?: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
145
- /**
146
- * Whether `type: 'integer'` / `format: 'int64'` produces `number` or `bigint` nodes.
147
- * @default 'number'
148
- */
149
- integerType?: 'number' | 'bigint';
150
- /**
151
- * AST type used when no schema type can be inferred.
152
- * @default 'any'
153
- */
154
- unknownType?: 'any' | 'unknown' | 'void';
155
- /**
156
- * AST type used for completely empty schemas (`{}`).
157
- * @default `unknownType`
158
- */
159
- emptySchemaType?: 'any' | 'unknown' | 'void';
160
- };
161
- type OasAdapterResolvedOptions = {
152
+ } & Partial<ast.ParserOptions>;
153
+ /**
154
+ * Resolved adapter options available at runtime after defaults have been applied.
155
+ */
156
+ type AdapterOasResolvedOptions = {
162
157
  validate: boolean;
163
- oasClass: OasAdapterOptions['oasClass'];
164
- contentType: OasAdapterOptions['contentType'];
165
- serverIndex: OasAdapterOptions['serverIndex'];
166
- serverVariables: OasAdapterOptions['serverVariables'];
167
- discriminator: NonNullable<OasAdapterOptions['discriminator']>;
168
- collisionDetection: boolean;
169
- dateType: NonNullable<OasAdapterOptions['dateType']>;
170
- integerType: NonNullable<OasAdapterOptions['integerType']>;
171
- unknownType: NonNullable<OasAdapterOptions['unknownType']>;
172
- emptySchemaType: NonNullable<OasAdapterOptions['emptySchemaType']>;
158
+ contentType: AdapterOasOptions['contentType'];
159
+ serverIndex: AdapterOasOptions['serverIndex'];
160
+ serverVariables: AdapterOasOptions['serverVariables'];
161
+ discriminator: NonNullable<AdapterOasOptions['discriminator']>;
162
+ dateType: NonNullable<AdapterOasOptions['dateType']>;
163
+ integerType: NonNullable<AdapterOasOptions['integerType']>;
164
+ unknownType: NonNullable<AdapterOasOptions['unknownType']>;
165
+ emptySchemaType: NonNullable<AdapterOasOptions['emptySchemaType']>;
166
+ enumSuffix: AdapterOasOptions['enumSuffix'];
173
167
  /**
174
168
  * Map from original `$ref` paths to their collision-resolved schema names.
175
- * Populated by the adapter after each `parse()` call.
176
- * e.g. `'#/components/schemas/Order'` → `'OrderSchema'`
169
+ * Populated after each `parse()` call.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * nameMapping.get('#/components/schemas/Order') // 'Order'
174
+ * nameMapping.get('#/components/responses/Order') // 'OrderResponse'
175
+ * ```
177
176
  */
178
177
  nameMapping: Map<string, string>;
179
178
  };
180
- type OasAdapter = AdapterFactoryOptions<'oas', OasAdapterOptions, OasAdapterResolvedOptions>;
179
+ /**
180
+ * `@kubb/core` adapter factory type for the OpenAPI adapter.
181
+ */
182
+ type AdapterOas = AdapterFactoryOptions<'oas', AdapterOasOptions, AdapterOasResolvedOptions, Document>;
181
183
  //#endregion
182
184
  //#region src/adapter.d.ts
185
+ /**
186
+ * Stable string identifier for the OAS adapter used in Kubb's adapter registry.
187
+ */
183
188
  declare const adapterOasName = "oas";
184
189
  /**
185
- * Creates an OpenAPI / Swagger adapter for Kubb.
190
+ * Creates the default OpenAPI / Swagger adapter for Kubb.
186
191
  *
187
- * This is the default adapter you can omit it from your config when using
188
- * an OpenAPI spec, but supplying it explicitly lets you pass options.
192
+ * Parses the spec, optionally validates it, resolves the base URL, and converts
193
+ * everything into an `InputNode` that downstream plugins consume.
189
194
  *
190
195
  * @example
191
196
  * ```ts
192
197
  * import { defineConfig } from '@kubb/core'
193
198
  * import { adapterOas } from '@kubb/adapter-oas'
199
+ * import { pluginTs } from '@kubb/plugin-ts'
194
200
  *
195
201
  * export default defineConfig({
196
- * adapter: adapterOas({ validate: true, dateType: 'date' }),
197
- * input: { path: './openapi.yaml' },
198
- * plugins: [pluginTs(), pluginZod()],
202
+ * adapter: adapterOas({ dateType: 'date', serverIndex: 0 }),
203
+ * input: { path: './openapi.yaml' },
204
+ * plugins: [pluginTs()],
199
205
  * })
200
206
  * ```
201
207
  */
202
- declare const adapterOas: (options?: OasAdapterOptions | undefined) => _kubb_core0.Adapter<OasAdapter>;
208
+ declare const adapterOas: (options?: AdapterOasOptions | undefined) => _$_kubb_core0.Adapter<AdapterOas>;
209
+ //#endregion
210
+ //#region src/factory.d.ts
211
+ type ParseOptions = {
212
+ canBundle?: boolean;
213
+ enablePaths?: boolean;
214
+ };
215
+ type ValidateDocumentOptions = {
216
+ throwOnError?: boolean;
217
+ };
218
+ /**
219
+ * Loads and dereferences an OpenAPI document, returning the raw `Document`.
220
+ *
221
+ * Accepts a file path string or an already-parsed document object. File paths are bundled via
222
+ * Redocly to resolve external `$ref`s. Swagger 2.0 documents are automatically up-converted
223
+ * to OpenAPI 3.0 via `swagger2openapi`.
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * const document = await parseDocument('./openapi.yaml')
228
+ * const document = await parse(rawDocumentObject, { canBundle: false })
229
+ * ```
230
+ */
231
+ declare function parseDocument(pathOrApi: string | Document, {
232
+ canBundle,
233
+ enablePaths
234
+ }?: ParseOptions): Promise<Document>;
235
+ /**
236
+ * Deep-merges multiple OpenAPI documents into a single `Document`.
237
+ *
238
+ * Each document is parsed independently then recursively merged with `remeda`'s `mergeDeep`.
239
+ * Throws when the input array is empty.
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * const document = await mergeDocuments(['./pets.yaml', './orders.yaml'])
244
+ * ```
245
+ */
246
+ declare function mergeDocuments(pathOrApi: Array<string | Document>): Promise<Document>;
247
+ /**
248
+ * Creates a `Document` from an `AdapterSource`.
249
+ *
250
+ * Handles all three source types:
251
+ * - `{ type: 'path' }` — resolves and bundles a local file path or remote URL.
252
+ * - `{ type: 'paths' }` — merges multiple file paths into a single document.
253
+ * - `{ type: 'data' }` — parses an inline string (YAML/JSON) or raw object.
254
+ *
255
+ * @example
256
+ * ```ts
257
+ * const document = await parseFromConfig({ type: 'path', path: './openapi.yaml' })
258
+ * const document = await parseFromConfig({ type: 'data', data: '{"openapi":"3.0.0",...}' })
259
+ * ```
260
+ */
261
+ declare function parseFromConfig(source: AdapterSource): Promise<Document>;
262
+ /**
263
+ * Validates an OpenAPI document using `oas-normalize` with colorized error output.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * await validateDocument(document)
268
+ * ```
269
+ */
270
+ declare function validateDocument(document: Document, {
271
+ throwOnError
272
+ }?: ValidateDocumentOptions): Promise<void>;
203
273
  //#endregion
204
- export { adapterOas, adapterOasName };
274
+ export { type AdapterOas, type AdapterOasOptions, type AdapterOasResolvedOptions, type ContentType, type DiscriminatorObject, type Document, type HttpMethod, HttpMethods, type MediaTypeObject, type Operation, type ParseOptions, type ReferenceObject, type ResponseObject, type SchemaObject, type ValidateDocumentOptions, adapterOas, adapterOasName, mergeDocuments, parseDocument, parseFromConfig, validateDocument };
205
275
  //# sourceMappingURL=index.d.ts.map