@kubb/adapter-oas 5.0.0-beta.75 → 5.0.0-beta.76
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/LICENSE +17 -10
- package/README.md +100 -0
- package/dist/index.cjs +1398 -864
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +845 -126
- package/dist/index.js +1401 -859
- package/dist/index.js.map +1 -1
- package/package.json +14 -10
- package/src/adapter.ts +0 -126
- package/src/constants.ts +0 -122
- package/src/discriminator.ts +0 -108
- package/src/factory.ts +0 -165
- package/src/guards.ts +0 -68
- package/src/index.ts +0 -18
- package/src/parser.ts +0 -1002
- package/src/refs.ts +0 -59
- package/src/resolvers.ts +0 -544
- package/src/types.ts +0 -206
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/adapter-oas",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
4
|
-
"description": "OpenAPI
|
|
3
|
+
"version": "5.0.0-beta.76",
|
|
4
|
+
"description": "OpenAPI and Swagger adapter for Kubb. Parses and validates OAS 2.0/3.x specifications into a @kubb/ast RootNode for downstream code generation plugins.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adapter",
|
|
7
7
|
"codegen",
|
|
8
8
|
"kubb",
|
|
9
|
+
"meta-framework",
|
|
9
10
|
"oas",
|
|
10
11
|
"openapi",
|
|
11
12
|
"swagger",
|
|
@@ -19,7 +20,6 @@
|
|
|
19
20
|
"directory": "packages/adapter-oas"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
22
|
-
"src",
|
|
23
23
|
"dist",
|
|
24
24
|
"!/**/**.test.**",
|
|
25
25
|
"!/**/__tests__/**",
|
|
@@ -42,14 +42,15 @@
|
|
|
42
42
|
"registry": "https://registry.npmjs.org/"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"@kubb/
|
|
45
|
+
"@readme/openapi-parser": "^6.1.3",
|
|
46
|
+
"@scalar/openapi-upgrader": "^0.2.9",
|
|
47
|
+
"api-ref-bundler": "0.5.1",
|
|
48
|
+
"yaml": "^2.9.0",
|
|
49
|
+
"@kubb/ast": "5.0.0-beta.76",
|
|
50
|
+
"@kubb/core": "5.0.0-beta.76"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"@types/
|
|
53
|
+
"@types/json-schema": "^7.0.15",
|
|
53
54
|
"openapi-types": "^12.1.3",
|
|
54
55
|
"@internals/utils": "0.0.0"
|
|
55
56
|
},
|
|
@@ -57,15 +58,18 @@
|
|
|
57
58
|
"node": ">=22"
|
|
58
59
|
},
|
|
59
60
|
"inlinedDependencies": {
|
|
61
|
+
"@types/json-schema": "7.0.15",
|
|
60
62
|
"openapi-types": "12.1.3"
|
|
61
63
|
},
|
|
62
64
|
"scripts": {
|
|
65
|
+
"bench": "vitest bench",
|
|
63
66
|
"build": "tsdown",
|
|
64
|
-
"clean": "
|
|
67
|
+
"clean": "node -e \"require('node:fs').rmSync('./dist', {recursive:true,force:true})\"",
|
|
65
68
|
"lint": "oxlint .",
|
|
66
69
|
"lint:fix": "oxlint --fix .",
|
|
67
70
|
"release": "pnpm publish --no-git-check",
|
|
68
71
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
72
|
+
"release:stage": "pnpm stage publish --no-git-check",
|
|
69
73
|
"start": "tsdown --watch",
|
|
70
74
|
"test": "vitest --passWithNoTests",
|
|
71
75
|
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
|
package/src/adapter.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { ast, createAdapter } from '@kubb/core'
|
|
2
|
-
import { DEFAULT_PARSER_OPTIONS } from './constants.ts'
|
|
3
|
-
import { applyDiscriminatorInheritance } from './discriminator.ts'
|
|
4
|
-
import { parseFromConfig, validateDocument } from './factory.ts'
|
|
5
|
-
import { parseOas } from './parser.ts'
|
|
6
|
-
import { resolveServerUrl } from './resolvers.ts'
|
|
7
|
-
import type { AdapterOas, Document } from './types.ts'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Stable string identifier for the OAS adapter used in Kubb's adapter registry.
|
|
11
|
-
*/
|
|
12
|
-
export const adapterOasName = 'oas' satisfies AdapterOas['name']
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Creates the default OpenAPI / Swagger adapter for Kubb.
|
|
16
|
-
*
|
|
17
|
-
* Parses the spec, optionally validates it, resolves the base URL, and converts
|
|
18
|
-
* everything into an `InputNode` that downstream plugins consume.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* import { defineConfig } from 'kubb'
|
|
23
|
-
* import { adapterOas } from '@kubb/adapter-oas'
|
|
24
|
-
* import { pluginTs } from '@kubb/plugin-ts'
|
|
25
|
-
*
|
|
26
|
-
* export default defineConfig({
|
|
27
|
-
* adapter: adapterOas({ dateType: 'date', serverIndex: 0 }),
|
|
28
|
-
* input: { path: './openapi.yaml' },
|
|
29
|
-
* plugins: [pluginTs()],
|
|
30
|
-
* })
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export const adapterOas = createAdapter<AdapterOas>((options) => {
|
|
34
|
-
const {
|
|
35
|
-
validate = true,
|
|
36
|
-
contentType,
|
|
37
|
-
serverIndex,
|
|
38
|
-
serverVariables,
|
|
39
|
-
discriminator = 'strict',
|
|
40
|
-
dateType = DEFAULT_PARSER_OPTIONS.dateType,
|
|
41
|
-
integerType = DEFAULT_PARSER_OPTIONS.integerType,
|
|
42
|
-
unknownType = DEFAULT_PARSER_OPTIONS.unknownType,
|
|
43
|
-
enumSuffix = DEFAULT_PARSER_OPTIONS.enumSuffix,
|
|
44
|
-
emptySchemaType = unknownType || DEFAULT_PARSER_OPTIONS.emptySchemaType,
|
|
45
|
-
} = options
|
|
46
|
-
|
|
47
|
-
// Let-binding so parse() can replace it with a simple reassignment (no clear+loop).
|
|
48
|
-
let nameMapping = new Map<string, string>()
|
|
49
|
-
let parsedDocument: Document | null
|
|
50
|
-
let inputNode: ast.InputNode | null
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
name: adapterOasName,
|
|
54
|
-
get options() {
|
|
55
|
-
return {
|
|
56
|
-
validate,
|
|
57
|
-
contentType,
|
|
58
|
-
serverIndex,
|
|
59
|
-
serverVariables,
|
|
60
|
-
discriminator,
|
|
61
|
-
dateType,
|
|
62
|
-
integerType,
|
|
63
|
-
unknownType,
|
|
64
|
-
emptySchemaType,
|
|
65
|
-
enumSuffix,
|
|
66
|
-
nameMapping,
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
get document() {
|
|
70
|
-
return parsedDocument
|
|
71
|
-
},
|
|
72
|
-
get inputNode() {
|
|
73
|
-
return inputNode
|
|
74
|
-
},
|
|
75
|
-
getImports(node, resolve) {
|
|
76
|
-
return ast.collectImports({
|
|
77
|
-
node,
|
|
78
|
-
nameMapping,
|
|
79
|
-
resolve: (schemaName) => {
|
|
80
|
-
const result = resolve(schemaName)
|
|
81
|
-
if (!result) return
|
|
82
|
-
|
|
83
|
-
return ast.createImport({ name: [result.name], path: result.path })
|
|
84
|
-
},
|
|
85
|
-
})
|
|
86
|
-
},
|
|
87
|
-
async parse(source) {
|
|
88
|
-
const document = await parseFromConfig(source)
|
|
89
|
-
|
|
90
|
-
if (validate) {
|
|
91
|
-
await validateDocument(document)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const server = serverIndex !== undefined ? document.servers?.at(serverIndex) : undefined
|
|
95
|
-
const baseURL = server?.url ? resolveServerUrl(server, serverVariables) : undefined
|
|
96
|
-
|
|
97
|
-
const { root: parsedRoot, nameMapping: parsedNameMapping } = parseOas(document, {
|
|
98
|
-
contentType,
|
|
99
|
-
dateType,
|
|
100
|
-
integerType,
|
|
101
|
-
unknownType,
|
|
102
|
-
emptySchemaType,
|
|
103
|
-
enumSuffix,
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
const node = discriminator === 'inherit' ? applyDiscriminatorInheritance(parsedRoot) : parsedRoot
|
|
107
|
-
|
|
108
|
-
// This must happen after parseOas() because legacy enum remapping is finalized there.
|
|
109
|
-
nameMapping = parsedNameMapping
|
|
110
|
-
// Expose the raw document so consumers (e.g. plugin-redoc) can access it.
|
|
111
|
-
parsedDocument = document
|
|
112
|
-
|
|
113
|
-
inputNode = ast.createInput({
|
|
114
|
-
...node,
|
|
115
|
-
meta: {
|
|
116
|
-
title: document.info?.title,
|
|
117
|
-
description: document.info?.description,
|
|
118
|
-
version: document.info?.version,
|
|
119
|
-
baseURL,
|
|
120
|
-
},
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
return inputNode
|
|
124
|
-
},
|
|
125
|
-
}
|
|
126
|
-
})
|
package/src/constants.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Default parser options applied when no explicit options are provided.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* import { DEFAULT_PARSER_OPTIONS } from '@kubb/adapter-oas'
|
|
9
|
-
*
|
|
10
|
-
* const parser = createOasParser(oas)
|
|
11
|
-
* const root = parser.parse({ ...DEFAULT_PARSER_OPTIONS, dateType: 'date' })
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export const DEFAULT_PARSER_OPTIONS = {
|
|
15
|
-
dateType: 'string',
|
|
16
|
-
integerType: 'number',
|
|
17
|
-
unknownType: 'any',
|
|
18
|
-
emptySchemaType: 'any',
|
|
19
|
-
enumSuffix: 'enum',
|
|
20
|
-
} as const satisfies ast.ParserOptions
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* JSON-Pointer prefix for schemas declared under `components.schemas` in an OpenAPI document.
|
|
24
|
-
*
|
|
25
|
-
* Used when building or parsing `$ref` strings.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* `${SCHEMA_REF_PREFIX}Pet` // '#/components/schemas/Pet'
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export const SCHEMA_REF_PREFIX = '#/components/schemas/' as const
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* OpenAPI version string written into the stub document created during multi-spec merges.
|
|
36
|
-
*/
|
|
37
|
-
export const MERGE_OPENAPI_VERSION = '3.0.0' as const
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Fallback `info.title` placed in the stub document when merging multiple API files.
|
|
41
|
-
*/
|
|
42
|
-
export const MERGE_DEFAULT_TITLE = 'Merged API' as const
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Fallback `info.version` placed in the stub document when merging multiple API files.
|
|
46
|
-
*/
|
|
47
|
-
export const MERGE_DEFAULT_VERSION = '1.0.0' as const
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Set of JSON Schema keywords that prevent a schema fragment from being inlined during `allOf` flattening.
|
|
51
|
-
*
|
|
52
|
-
* A fragment that contains any of these keys carries structural meaning of its own and must stay as a separate
|
|
53
|
-
* intersection member rather than being merged into the parent.
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* import { structuralKeys } from '@kubb/adapter-oas'
|
|
58
|
-
*
|
|
59
|
-
* const isStructural = Object.keys(fragment).some((key) => structuralKeys.has(key))
|
|
60
|
-
* // true when fragment has e.g. 'properties' or 'oneOf'
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export const structuralKeys = new Set(['properties', 'items', 'additionalProperties', 'oneOf', 'anyOf', 'allOf', 'not'] as const)
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Static map from OAS `format` strings to Kubb `SchemaType` values.
|
|
67
|
-
*
|
|
68
|
-
* Only formats whose AST type differs from the OAS `type` field appear here.
|
|
69
|
-
* Formats that depend on runtime options (`int64`, `date-time`, `date`, `time`) are handled separately
|
|
70
|
-
* in the parser. `ipv4` and `ipv6` map to their own dedicated schema types; `hostname` and
|
|
71
|
-
* `idn-hostname` map to `'url'` as the closest generic string-format type.
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```ts
|
|
75
|
-
* import { formatMap } from '@kubb/adapter-oas'
|
|
76
|
-
*
|
|
77
|
-
* formatMap['uuid'] // 'uuid'
|
|
78
|
-
* formatMap['binary'] // 'blob'
|
|
79
|
-
* formatMap['float'] // 'number'
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export const formatMap = {
|
|
83
|
-
uuid: 'uuid',
|
|
84
|
-
email: 'email',
|
|
85
|
-
'idn-email': 'email',
|
|
86
|
-
uri: 'url',
|
|
87
|
-
'uri-reference': 'url',
|
|
88
|
-
url: 'url',
|
|
89
|
-
ipv4: 'ipv4',
|
|
90
|
-
ipv6: 'ipv6',
|
|
91
|
-
hostname: 'url',
|
|
92
|
-
'idn-hostname': 'url',
|
|
93
|
-
binary: 'blob',
|
|
94
|
-
byte: 'blob',
|
|
95
|
-
// Numeric formats override the OAS `type` because format is more specific.
|
|
96
|
-
// See https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7
|
|
97
|
-
int32: 'integer',
|
|
98
|
-
float: 'number',
|
|
99
|
-
double: 'number',
|
|
100
|
-
} as const satisfies Record<string, ast.SchemaType>
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Vendor extension keys that attach human-readable labels to enum values, checked in priority order.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```ts
|
|
107
|
-
* import { enumExtensionKeys } from '@kubb/adapter-oas'
|
|
108
|
-
*
|
|
109
|
-
* const key = enumExtensionKeys.find((k) => k in schema) // 'x-enumNames' | 'x-enum-varnames' | undefined
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
export const enumExtensionKeys = ['x-enumNames', 'x-enum-varnames'] as const
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Maps `'any' | 'unknown' | 'void'` option strings to their `ScalarSchemaType` constant.
|
|
116
|
-
* Replaces a plain object lookup with a `Map` for explicit key membership testing via `.has()`.
|
|
117
|
-
*/
|
|
118
|
-
export const typeOptionMap = new Map<'any' | 'unknown' | 'void', ast.ScalarSchemaType>([
|
|
119
|
-
['any', ast.schemaTypes.any],
|
|
120
|
-
['unknown', ast.schemaTypes.unknown],
|
|
121
|
-
['void', ast.schemaTypes.void],
|
|
122
|
-
])
|
package/src/discriminator.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
|
|
3
|
-
type DiscriminatorTarget = {
|
|
4
|
-
propertyName: string
|
|
5
|
-
enumValues: Array<string | number | boolean>
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Injects discriminator enum values into child schemas so they know which value identifies them.
|
|
10
|
-
*
|
|
11
|
-
* Finds every union schema in `input.schemas` that has a `discriminatorPropertyName`, collects the
|
|
12
|
-
* enum value each union member is mapped to, then adds (or replaces) that property on the matching
|
|
13
|
-
* child object schema.
|
|
14
|
-
*
|
|
15
|
-
* Returns a new `InputNode` — the original is never mutated.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const { root } = parseOas(document, options)
|
|
20
|
-
* const next = applyDiscriminatorInheritance(root)
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export function applyDiscriminatorInheritance(root: ast.InputNode): ast.InputNode {
|
|
24
|
-
const childMap = new Map<string, DiscriminatorTarget>()
|
|
25
|
-
|
|
26
|
-
for (const schema of root.schemas) {
|
|
27
|
-
// Case 1: top-level schema is a union (oneOf/anyOf with discriminator)
|
|
28
|
-
// Case 2: top-level schema is an intersection wrapping a union (oneOf/anyOf + shared properties)
|
|
29
|
-
let unionNode = ast.narrowSchema(schema, 'union')
|
|
30
|
-
|
|
31
|
-
if (!unionNode) {
|
|
32
|
-
const intersectionMembers = ast.narrowSchema(schema, 'intersection')?.members
|
|
33
|
-
if (intersectionMembers) {
|
|
34
|
-
for (const m of intersectionMembers) {
|
|
35
|
-
const u = ast.narrowSchema(m, 'union')
|
|
36
|
-
if (u) {
|
|
37
|
-
unionNode = u
|
|
38
|
-
break
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (!unionNode?.discriminatorPropertyName || !unionNode.members) continue
|
|
45
|
-
|
|
46
|
-
const { discriminatorPropertyName, members } = unionNode
|
|
47
|
-
|
|
48
|
-
for (const member of members) {
|
|
49
|
-
// Members with a discriminant value are intersections: [RefSchemaNode, ObjectSchemaNode]
|
|
50
|
-
const intersectionNode = ast.narrowSchema(member, 'intersection')
|
|
51
|
-
if (!intersectionNode?.members) continue
|
|
52
|
-
|
|
53
|
-
let refNode: ReturnType<typeof ast.narrowSchema<'ref'>> | undefined
|
|
54
|
-
let objNode: ReturnType<typeof ast.narrowSchema<'object'>> | undefined
|
|
55
|
-
|
|
56
|
-
for (const m of intersectionNode.members) {
|
|
57
|
-
refNode ??= ast.narrowSchema(m, 'ref')
|
|
58
|
-
objNode ??= ast.narrowSchema(m, 'object')
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (!refNode?.name || !objNode) continue
|
|
62
|
-
|
|
63
|
-
const prop = objNode.properties.find((p) => p.name === discriminatorPropertyName)
|
|
64
|
-
const enumNode = prop ? ast.narrowSchema(prop.schema, 'enum') : undefined
|
|
65
|
-
if (!enumNode?.enumValues?.length) continue
|
|
66
|
-
|
|
67
|
-
const enumValues = enumNode.enumValues.filter((v): v is string | number | boolean => v !== null)
|
|
68
|
-
if (!enumValues.length) continue
|
|
69
|
-
|
|
70
|
-
const existing = childMap.get(refNode.name)
|
|
71
|
-
if (existing) {
|
|
72
|
-
existing.enumValues.push(...enumValues)
|
|
73
|
-
} else {
|
|
74
|
-
childMap.set(refNode.name, {
|
|
75
|
-
propertyName: discriminatorPropertyName,
|
|
76
|
-
enumValues: [...enumValues],
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (childMap.size === 0) return root
|
|
83
|
-
|
|
84
|
-
return ast.transform(root, {
|
|
85
|
-
schema(node, { parent }) {
|
|
86
|
-
if (parent?.kind !== 'Input' || !node.name) return
|
|
87
|
-
|
|
88
|
-
const entry = childMap.get(node.name)
|
|
89
|
-
if (!entry) return
|
|
90
|
-
|
|
91
|
-
const objectNode = ast.narrowSchema(node, 'object')
|
|
92
|
-
if (!objectNode) return
|
|
93
|
-
|
|
94
|
-
const { propertyName, enumValues } = entry
|
|
95
|
-
const enumSchema = ast.createSchema({ type: 'enum', enumValues })
|
|
96
|
-
const newProp = ast.createProperty({
|
|
97
|
-
name: propertyName,
|
|
98
|
-
required: true,
|
|
99
|
-
schema: enumSchema,
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
const existingIdx = objectNode.properties.findIndex((p) => p.name === propertyName)
|
|
103
|
-
const newProperties = existingIdx >= 0 ? objectNode.properties.map((p, i) => (i === existingIdx ? newProp : p)) : [...objectNode.properties, newProp]
|
|
104
|
-
|
|
105
|
-
return { ...objectNode, properties: newProperties }
|
|
106
|
-
},
|
|
107
|
-
})
|
|
108
|
-
}
|
package/src/factory.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import { mergeDeep, URLPath } from '@internals/utils'
|
|
3
|
-
import type { AdapterSource } from '@kubb/core'
|
|
4
|
-
import { bundle, loadConfig } from '@redocly/openapi-core'
|
|
5
|
-
import OASNormalize from 'oas-normalize'
|
|
6
|
-
import swagger2openapi from 'swagger2openapi'
|
|
7
|
-
import { MERGE_DEFAULT_TITLE, MERGE_DEFAULT_VERSION, MERGE_OPENAPI_VERSION } from './constants.ts'
|
|
8
|
-
import { isOpenApiV2Document } from './guards.ts'
|
|
9
|
-
import type { Document } from './types.ts'
|
|
10
|
-
|
|
11
|
-
export type ParseOptions = {
|
|
12
|
-
canBundle?: boolean
|
|
13
|
-
enablePaths?: boolean
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type ValidateDocumentOptions = {
|
|
17
|
-
throwOnError?: boolean
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Loads and dereferences an OpenAPI document, returning the raw `Document`.
|
|
22
|
-
*
|
|
23
|
-
* Accepts a file path string or an already-parsed document object. File paths are bundled via
|
|
24
|
-
* Redocly to resolve external `$ref`s. Swagger 2.0 documents are automatically up-converted
|
|
25
|
-
* to OpenAPI 3.0 via `swagger2openapi`.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* const document = await parseDocument('./openapi.yaml')
|
|
30
|
-
* const document = await parse(rawDocumentObject, { canBundle: false })
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export async function parseDocument(pathOrApi: string | Document, { canBundle = true, enablePaths = true }: ParseOptions = {}): Promise<Document> {
|
|
34
|
-
if (typeof pathOrApi === 'string' && canBundle) {
|
|
35
|
-
const config = await loadConfig()
|
|
36
|
-
const bundleResults = await bundle({
|
|
37
|
-
ref: pathOrApi,
|
|
38
|
-
config,
|
|
39
|
-
base: pathOrApi,
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
return parseDocument(bundleResults.bundle.parsed as string, {
|
|
43
|
-
canBundle,
|
|
44
|
-
enablePaths,
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const oasNormalize = new OASNormalize(pathOrApi, {
|
|
49
|
-
enablePaths,
|
|
50
|
-
colorizeErrors: true,
|
|
51
|
-
})
|
|
52
|
-
const document = (await oasNormalize.load()) as Document
|
|
53
|
-
|
|
54
|
-
if (isOpenApiV2Document(document)) {
|
|
55
|
-
const { openapi } = await swagger2openapi.convertObj(document, {
|
|
56
|
-
anchors: true,
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
return openapi as Document
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return document
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Deep-merges multiple OpenAPI documents into a single `Document`.
|
|
67
|
-
*
|
|
68
|
-
* Each document is parsed independently then recursively merged via `mergeDeep` from `@internals/utils`.
|
|
69
|
-
* Throws when the input array is empty.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* const document = await mergeDocuments(['./pets.yaml', './orders.yaml'])
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
76
|
-
export async function mergeDocuments(pathOrApi: Array<string | Document>): Promise<Document> {
|
|
77
|
-
const documents: Document[] = []
|
|
78
|
-
for (const p of pathOrApi) {
|
|
79
|
-
documents.push(await parseDocument(p, { enablePaths: false, canBundle: false }))
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (documents.length === 0) {
|
|
83
|
-
throw new Error('No OAS documents provided for merging.')
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const seed: Document = {
|
|
87
|
-
openapi: MERGE_OPENAPI_VERSION,
|
|
88
|
-
info: { title: MERGE_DEFAULT_TITLE, version: MERGE_DEFAULT_VERSION },
|
|
89
|
-
paths: {},
|
|
90
|
-
components: { schemas: {} },
|
|
91
|
-
} as Document
|
|
92
|
-
|
|
93
|
-
const merged = documents.reduce(
|
|
94
|
-
(acc, current) => mergeDeep(acc as Record<string, unknown>, current as Record<string, unknown>),
|
|
95
|
-
seed as Record<string, unknown>,
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
return parseDocument(merged as Document)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Creates a `Document` from an `AdapterSource`.
|
|
103
|
-
*
|
|
104
|
-
* Handles all three source types:
|
|
105
|
-
* - `{ type: 'path' }` — resolves and bundles a local file path or remote URL.
|
|
106
|
-
* - `{ type: 'paths' }` — merges multiple file paths into a single document.
|
|
107
|
-
* - `{ type: 'data' }` — parses an inline string (YAML/JSON) or raw object.
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* ```ts
|
|
111
|
-
* const document = await parseFromConfig({ type: 'path', path: './openapi.yaml' })
|
|
112
|
-
* const document = await parseFromConfig({ type: 'data', data: '{"openapi":"3.0.0",...}' })
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
export function parseFromConfig(source: AdapterSource): Promise<Document> {
|
|
116
|
-
if (source.type === 'data') {
|
|
117
|
-
if (typeof source.data === 'object') {
|
|
118
|
-
return parseDocument(structuredClone(source.data) as Document)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return parseDocument(source.data as string, { canBundle: false })
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (source.type === 'paths') {
|
|
125
|
-
return mergeDocuments(source.paths)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// type === 'path'
|
|
129
|
-
if (new URLPath(source.path).isURL) {
|
|
130
|
-
return parseDocument(source.path)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return parseDocument(path.resolve(path.dirname(source.path), source.path))
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Validates an OpenAPI document using `oas-normalize` with colorized error output.
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
* ```ts
|
|
141
|
-
* await validateDocument(document)
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
export async function validateDocument(document: Document, { throwOnError = false }: ValidateDocumentOptions = {}): Promise<void> {
|
|
145
|
-
try {
|
|
146
|
-
const oasNormalize = new OASNormalize(document, {
|
|
147
|
-
enablePaths: true,
|
|
148
|
-
colorizeErrors: true,
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
await oasNormalize.validate({
|
|
152
|
-
parser: {
|
|
153
|
-
validate: {
|
|
154
|
-
errors: { colorize: true },
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
})
|
|
158
|
-
} catch (error) {
|
|
159
|
-
if (throwOnError) {
|
|
160
|
-
throw error
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Validation failures are non-fatal — mirror plugin-oas behavior
|
|
164
|
-
}
|
|
165
|
-
}
|
package/src/guards.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { isPlainObject } from '@internals/utils'
|
|
2
|
-
import type { OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
|
|
3
|
-
import type { DiscriminatorObject, SchemaObject } from './types.ts'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns `true` when `doc` is a Swagger 2.0 document (no `openapi` key).
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* if (isOpenApiV2Document(doc)) {
|
|
11
|
-
* // doc is OpenAPIV2.Document
|
|
12
|
-
* }
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
export function isOpenApiV2Document(doc: unknown): doc is OpenAPIV2.Document {
|
|
16
|
-
return !!doc && isPlainObject(doc) && !('openapi' in doc)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Returns `true` when a schema should be treated as nullable.
|
|
21
|
-
*
|
|
22
|
-
* Recognizes all nullable signals across OAS versions: `nullable: true` (OAS 3.0),
|
|
23
|
-
* `x-nullable: true` (vendor extension), `type: 'null'`, and `type: ['null', ...]` (OAS 3.1).
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* isNullable({ type: 'string', nullable: true }) // true
|
|
28
|
-
* isNullable({ type: ['string', 'null'] }) // true
|
|
29
|
-
* isNullable({ type: 'string' }) // false
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {
|
|
33
|
-
const explicitNullable = schema?.nullable ?? schema?.['x-nullable']
|
|
34
|
-
if (explicitNullable === true) return true
|
|
35
|
-
|
|
36
|
-
const schemaType = schema?.type
|
|
37
|
-
if (schemaType === 'null') return true
|
|
38
|
-
if (Array.isArray(schemaType)) return schemaType.includes('null')
|
|
39
|
-
|
|
40
|
-
return false
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Returns `true` when `obj` is an OpenAPI `$ref` pointer object.
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* isReference({ $ref: '#/components/schemas/Pet' }) // true
|
|
49
|
-
* isReference({ type: 'string' }) // false
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export function isReference(obj?: unknown): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {
|
|
53
|
-
return !!obj && typeof obj === 'object' && '$ref' in obj
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Returns `true` when `obj` is a schema with a structured OAS 3.x `discriminator` object.
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```ts
|
|
61
|
-
* isDiscriminator({ discriminator: { propertyName: 'type', mapping: {} } }) // true
|
|
62
|
-
* isDiscriminator({ discriminator: 'type' }) // false (Swagger 2 string form)
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export function isDiscriminator(obj?: unknown): obj is SchemaObject & { discriminator: DiscriminatorObject } {
|
|
66
|
-
const record = obj as Record<string, unknown>
|
|
67
|
-
return !!obj && !!record['discriminator'] && typeof record['discriminator'] !== 'string'
|
|
68
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export { adapterOas, adapterOasName } from './adapter.ts'
|
|
2
|
-
export type { ParseOptions, ValidateDocumentOptions } from './factory.ts'
|
|
3
|
-
export { mergeDocuments, parseDocument, parseFromConfig, validateDocument } from './factory.ts'
|
|
4
|
-
export type {
|
|
5
|
-
AdapterOas,
|
|
6
|
-
AdapterOasOptions,
|
|
7
|
-
AdapterOasResolvedOptions,
|
|
8
|
-
ContentType,
|
|
9
|
-
DiscriminatorObject,
|
|
10
|
-
Document,
|
|
11
|
-
HttpMethod,
|
|
12
|
-
MediaTypeObject,
|
|
13
|
-
Operation,
|
|
14
|
-
ReferenceObject,
|
|
15
|
-
ResponseObject,
|
|
16
|
-
SchemaObject,
|
|
17
|
-
} from './types.ts'
|
|
18
|
-
export { HttpMethods } from './types.ts'
|