@kubb/adapter-oas 5.8.0 → 5.8.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/OVERVIEW.md +29 -0
- package/dist/index.cjs +125 -114
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +126 -115
- package/dist/index.js.map +1 -1
- package/kubb-extension.json +181 -0
- package/package.json +5 -2
package/OVERVIEW.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# OpenAPI adapter
|
|
2
|
+
|
|
3
|
+
The OpenAPI adapter reads an OpenAPI 2.0, 3.0, or 3.1 document and converts every schema and operation into Kubb's AST. Every plugin in the build generates from that AST, so one adapter feeds them all.
|
|
4
|
+
|
|
5
|
+
Kubb uses `adapterOas()` by default. Set it on `defineConfig.adapter` only to change its options. The options apply to every plugin in the build.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { defineConfig } from 'kubb/config'
|
|
9
|
+
import { adapterOas } from '@kubb/adapter-oas'
|
|
10
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
11
|
+
|
|
12
|
+
export default defineConfig({
|
|
13
|
+
input: './petStore.yaml',
|
|
14
|
+
output: { path: './src/gen' },
|
|
15
|
+
adapter: adapterOas({ dateType: 'date', integerType: 'number' }),
|
|
16
|
+
plugins: [pluginTs()],
|
|
17
|
+
})
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Options worth knowing
|
|
21
|
+
|
|
22
|
+
- `validate` checks the spec before parsing and reports each problem as a warning. Turn it off to speed up a large spec. Run `kubb validate` to list every error.
|
|
23
|
+
- `dateType` sets how `date-time`, `date`, and `time` schemas are represented: ISO strings (the default), strings with or without a timezone offset, or JavaScript `Date` objects. Pass an object to set each format on its own.
|
|
24
|
+
- `integerType` defaults to `'bigint'`, which is exact for 64-bit IDs but does not survive `JSON.stringify`. Choose `'number'` for most JSON APIs.
|
|
25
|
+
- `unknownType` and `emptySchemaType` set the type for schemas Kubb cannot infer and for empty schemas (`{}`).
|
|
26
|
+
- `discriminator: 'propagate'` writes the discriminator's literal value into each child schema, so each branch is typed precisely.
|
|
27
|
+
- `enums: 'root'` lifts inline enums to reusable top-level schemas, named with `enumSuffix` (`PetStatusEnum`).
|
|
28
|
+
- `server` picks the entry in the spec's `servers` array that becomes the base URL and fills in its variables. It does not change where a generated client sends requests. Use the client plugin's own `baseURL` for that.
|
|
29
|
+
- `contentType` picks the media type when an operation defines several. Without it, Kubb uses the first JSON media type.
|
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,15 @@ let node_fs_promises = require("node:fs/promises");
|
|
|
5
5
|
let yaml = require("yaml");
|
|
6
6
|
let _scalar_openapi_upgrader = require("@scalar/openapi-upgrader");
|
|
7
7
|
let api_ref_bundler = require("api-ref-bundler");
|
|
8
|
+
//#region kubb-extension.json
|
|
9
|
+
var compatibility = {
|
|
10
|
+
"kubb": ">=5.8.0",
|
|
11
|
+
"node": ">=22"
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region package.json
|
|
15
|
+
var version = "5.8.2";
|
|
16
|
+
//#endregion
|
|
8
17
|
//#region src/constants.ts
|
|
9
18
|
/**
|
|
10
19
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -2346,131 +2355,133 @@ const adapterOasName = "oas";
|
|
|
2346
2355
|
* })
|
|
2347
2356
|
* ```
|
|
2348
2357
|
*/
|
|
2349
|
-
const adapterOas = (0, _kubb_kit.
|
|
2350
|
-
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
};
|
|
2358
|
-
const resolvedOptions = {
|
|
2358
|
+
const adapterOas = (0, _kubb_kit.defineExtension)({
|
|
2359
|
+
kind: "adapter",
|
|
2360
|
+
meta: {
|
|
2361
|
+
name: "oas",
|
|
2362
|
+
version,
|
|
2363
|
+
compatibility
|
|
2364
|
+
},
|
|
2365
|
+
setup: ({ validate = true, contentType, server, discriminator = "preserve", enums = "inline", dateType = DEFAULT_PARSER_OPTIONS.dateType, integerType = DEFAULT_PARSER_OPTIONS.integerType, unknownType = DEFAULT_PARSER_OPTIONS.unknownType, enumSuffix = DEFAULT_PARSER_OPTIONS.enumSuffix, emptySchemaType = unknownType || DEFAULT_PARSER_OPTIONS.emptySchemaType }) => ({
|
|
2359
2366
|
validate,
|
|
2360
2367
|
contentType,
|
|
2361
2368
|
server,
|
|
2362
2369
|
discriminator,
|
|
2363
2370
|
enums,
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
const
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
const
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2371
|
+
dateType,
|
|
2372
|
+
integerType,
|
|
2373
|
+
unknownType,
|
|
2374
|
+
emptySchemaType,
|
|
2375
|
+
enumSuffix
|
|
2376
|
+
}),
|
|
2377
|
+
build({ validate, contentType, server, discriminator, enums, ...parserOptions }) {
|
|
2378
|
+
let parsedDocument = null;
|
|
2379
|
+
const inputCache = /* @__PURE__ */ new WeakMap();
|
|
2380
|
+
function parseInput({ document, refs, schemas, parser }) {
|
|
2381
|
+
const { parseSchema } = parser;
|
|
2382
|
+
const parsedByName = /* @__PURE__ */ new Map();
|
|
2383
|
+
const refAliasMap = /* @__PURE__ */ new Map();
|
|
2384
|
+
const enumNames = [];
|
|
2385
|
+
const discriminatorParentNodes = [];
|
|
2386
|
+
const refGraph = /* @__PURE__ */ new Map();
|
|
2387
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
2388
|
+
const node = parseSchema({
|
|
2389
|
+
schema,
|
|
2390
|
+
name
|
|
2391
|
+
});
|
|
2392
|
+
parsedByName.set(name, node);
|
|
2393
|
+
const refs = scanSchema({
|
|
2394
|
+
node,
|
|
2395
|
+
name
|
|
2396
|
+
});
|
|
2397
|
+
if (node.name) refGraph.set(node.name, refs);
|
|
2398
|
+
if (node.type === "ref" && node.name && node.name !== name) refAliasMap.set(name, node);
|
|
2399
|
+
if (_kubb_kit.ast.narrowSchema(node, "enum") && node.name) enumNames.push(node.name);
|
|
2400
|
+
if (discriminator === "propagate" && (schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) discriminatorParentNodes.push(node);
|
|
2401
|
+
}
|
|
2402
|
+
const circularNames = [..._kubb_kit.ast.findCircularSchemasFromGraph(refGraph)];
|
|
2403
|
+
const discriminatorChildMap = discriminatorParentNodes.length > 0 ? buildDiscriminatorChildMap(discriminatorParentNodes) : null;
|
|
2404
|
+
const operationNodes = getOperations(document, refs).map((operation) => parseOperation({
|
|
2405
|
+
operation,
|
|
2406
|
+
refs,
|
|
2407
|
+
contentType,
|
|
2408
|
+
options: parserOptions,
|
|
2409
|
+
parseSchema
|
|
2410
|
+
}));
|
|
2411
|
+
const promotedEnums = enums === "root" ? collectInlineEnums([...parsedByName.values(), ...operationNodes], new Set(Object.keys(schemas))) : null;
|
|
2412
|
+
if (promotedEnums) for (const name of promotedEnums.keys()) enumNames.push(name);
|
|
2413
|
+
const schemaNodes = promotedEnums ? [...promotedEnums.values()] : [];
|
|
2414
|
+
for (const name of Object.keys(schemas)) {
|
|
2415
|
+
const aliasTarget = refAliasMap.get(name)?.name;
|
|
2416
|
+
const aliased = aliasTarget ? parsedByName.get(aliasTarget) : void 0;
|
|
2417
|
+
const parsed = parsedByName.get(name);
|
|
2418
|
+
const child = discriminatorChildMap?.get(name);
|
|
2419
|
+
const base = child ? patchDiscriminatorNode(parsed, child) : parsed;
|
|
2420
|
+
const node = aliased ? {
|
|
2421
|
+
...aliased,
|
|
2422
|
+
name
|
|
2423
|
+
} : base;
|
|
2424
|
+
schemaNodes.push(promotedEnums ? refPromotedEnums(node, promotedEnums) : node);
|
|
2425
|
+
}
|
|
2426
|
+
const operations = promotedEnums ? operationNodes.map((node) => refPromotedEnums(node, promotedEnums)) : operationNodes;
|
|
2427
|
+
return _kubb_kit.ast.factory.createInput({
|
|
2428
|
+
schemas: schemaNodes,
|
|
2429
|
+
operations,
|
|
2430
|
+
meta: {
|
|
2431
|
+
title: document.info?.title,
|
|
2432
|
+
description: document.info?.description,
|
|
2433
|
+
version: document.info?.version,
|
|
2434
|
+
baseURL: resolveBaseUrl({
|
|
2435
|
+
document,
|
|
2436
|
+
server
|
|
2437
|
+
}),
|
|
2438
|
+
circularNames,
|
|
2439
|
+
enumNames
|
|
2440
|
+
}
|
|
2384
2441
|
});
|
|
2385
|
-
if (node.name) refGraph.set(node.name, refs);
|
|
2386
|
-
if (node.type === "ref" && node.name && node.name !== name) refAliasMap.set(name, node);
|
|
2387
|
-
if (_kubb_kit.ast.narrowSchema(node, "enum") && node.name) enumNames.push(node.name);
|
|
2388
|
-
if (discriminator === "propagate" && (schema.oneOf ?? schema.anyOf) && schema.discriminator?.propertyName) discriminatorParentNodes.push(node);
|
|
2389
|
-
}
|
|
2390
|
-
const circularNames = [..._kubb_kit.ast.findCircularSchemasFromGraph(refGraph)];
|
|
2391
|
-
const discriminatorChildMap = discriminatorParentNodes.length > 0 ? buildDiscriminatorChildMap(discriminatorParentNodes) : null;
|
|
2392
|
-
const operationNodes = getOperations(document, refs).map((operation) => parseOperation({
|
|
2393
|
-
operation,
|
|
2394
|
-
refs,
|
|
2395
|
-
contentType,
|
|
2396
|
-
options: parserOptions,
|
|
2397
|
-
parseSchema
|
|
2398
|
-
}));
|
|
2399
|
-
const promotedEnums = enums === "root" ? collectInlineEnums([...parsedByName.values(), ...operationNodes], new Set(Object.keys(schemas))) : null;
|
|
2400
|
-
if (promotedEnums) for (const name of promotedEnums.keys()) enumNames.push(name);
|
|
2401
|
-
const schemaNodes = promotedEnums ? [...promotedEnums.values()] : [];
|
|
2402
|
-
for (const name of Object.keys(schemas)) {
|
|
2403
|
-
const aliasTarget = refAliasMap.get(name)?.name;
|
|
2404
|
-
const aliased = aliasTarget ? parsedByName.get(aliasTarget) : void 0;
|
|
2405
|
-
const parsed = parsedByName.get(name);
|
|
2406
|
-
const child = discriminatorChildMap?.get(name);
|
|
2407
|
-
const base = child ? patchDiscriminatorNode(parsed, child) : parsed;
|
|
2408
|
-
const node = aliased ? {
|
|
2409
|
-
...aliased,
|
|
2410
|
-
name
|
|
2411
|
-
} : base;
|
|
2412
|
-
schemaNodes.push(promotedEnums ? refPromotedEnums(node, promotedEnums) : node);
|
|
2413
2442
|
}
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
version: document.info?.version,
|
|
2422
|
-
baseURL: resolveBaseUrl({
|
|
2423
|
-
document,
|
|
2424
|
-
server
|
|
2425
|
-
}),
|
|
2426
|
-
circularNames,
|
|
2427
|
-
enumNames
|
|
2428
|
-
}
|
|
2429
|
-
});
|
|
2430
|
-
}
|
|
2431
|
-
return {
|
|
2432
|
-
name: "oas",
|
|
2433
|
-
options: resolvedOptions,
|
|
2434
|
-
get document() {
|
|
2435
|
-
return parsedDocument;
|
|
2436
|
-
},
|
|
2437
|
-
async validate(input, options) {
|
|
2438
|
-
await assertInputExists(input);
|
|
2439
|
-
const document = await parseDocument(input);
|
|
2440
|
-
assertDocument(document);
|
|
2441
|
-
await validateDocument(document, options);
|
|
2442
|
-
},
|
|
2443
|
-
async parse(source, { signal } = {}) {
|
|
2444
|
-
signal?.throwIfAborted();
|
|
2445
|
-
const cached = inputCache.get(source);
|
|
2446
|
-
if (cached) return cached;
|
|
2447
|
-
const promise = (async () => {
|
|
2448
|
-
const document = await parseFromConfig(source);
|
|
2449
|
-
signal?.throwIfAborted();
|
|
2443
|
+
return {
|
|
2444
|
+
get document() {
|
|
2445
|
+
return parsedDocument;
|
|
2446
|
+
},
|
|
2447
|
+
async validate(input, options) {
|
|
2448
|
+
await assertInputExists(input);
|
|
2449
|
+
const document = await parseDocument(input);
|
|
2450
2450
|
assertDocument(document);
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2451
|
+
await validateDocument(document, options);
|
|
2452
|
+
},
|
|
2453
|
+
async parse(source, { signal } = {}) {
|
|
2454
|
+
signal?.throwIfAborted();
|
|
2455
|
+
const cached = inputCache.get(source);
|
|
2456
|
+
if (cached) return cached;
|
|
2457
|
+
const promise = (async () => {
|
|
2458
|
+
const document = await parseFromConfig(source);
|
|
2459
|
+
signal?.throwIfAborted();
|
|
2460
|
+
assertDocument(document);
|
|
2461
|
+
if (validate) await validateDocument(document);
|
|
2462
|
+
parsedDocument = document;
|
|
2463
|
+
const refs = createRefs(document);
|
|
2464
|
+
const { schemas, renames } = getSchemas(document, { contentType }, refs);
|
|
2465
|
+
return parseInput({
|
|
2460
2466
|
document,
|
|
2461
2467
|
refs,
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2468
|
+
schemas,
|
|
2469
|
+
parser: createSchemaParser({
|
|
2470
|
+
document,
|
|
2471
|
+
refs,
|
|
2472
|
+
renames,
|
|
2473
|
+
options: parserOptions
|
|
2474
|
+
})
|
|
2475
|
+
});
|
|
2476
|
+
})();
|
|
2477
|
+
inputCache.set(source, promise);
|
|
2478
|
+
return promise.catch((error) => {
|
|
2479
|
+
if (signal?.aborted) inputCache.delete(source);
|
|
2480
|
+
throw error;
|
|
2465
2481
|
});
|
|
2466
|
-
}
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
if (signal?.aborted) inputCache.delete(source);
|
|
2470
|
-
throw error;
|
|
2471
|
-
});
|
|
2472
|
-
}
|
|
2473
|
-
};
|
|
2482
|
+
}
|
|
2483
|
+
};
|
|
2484
|
+
}
|
|
2474
2485
|
});
|
|
2475
2486
|
//#endregion
|
|
2476
2487
|
exports.adapterOas = adapterOas;
|