@kubb/adapter-oas 4.33.4 → 4.35.0
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.cjs +8 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -29
- package/dist/index.js +9 -45
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/adapter.ts +2 -11
- package/src/index.ts +0 -1
- package/src/parser.ts +5 -5
- package/src/types.ts +0 -11
- package/src/devtools.ts +0 -60
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/adapter-oas",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.35.0",
|
|
4
4
|
"description": "OpenAPI / Swagger adapter for Kubb — converts OAS input into a @kubb/ast RootNode.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"openapi",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"openapi-types": "^12.1.3",
|
|
47
47
|
"remeda": "^2.33.6",
|
|
48
48
|
"swagger2openapi": "^7.0.8",
|
|
49
|
-
"@kubb/ast": "4.
|
|
50
|
-
"@kubb/core": "4.
|
|
49
|
+
"@kubb/ast": "4.35.0",
|
|
50
|
+
"@kubb/core": "4.35.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/swagger2openapi": "^7.0.4",
|
package/src/adapter.ts
CHANGED
|
@@ -2,7 +2,6 @@ import path from 'node:path'
|
|
|
2
2
|
import { createRoot } from '@kubb/ast'
|
|
3
3
|
import type { AdapterSource } from '@kubb/core'
|
|
4
4
|
import { defineAdapter } from '@kubb/core'
|
|
5
|
-
import { logAst } from './devtools.ts'
|
|
6
5
|
import { resolveServerUrl } from './oas/resolveServerUrl.ts'
|
|
7
6
|
import { parseFromConfig } from './oas/utils.ts'
|
|
8
7
|
import { createOasParser } from './parser.ts'
|
|
@@ -41,7 +40,6 @@ export const adapterOas = defineAdapter<OasAdapter>((options) => {
|
|
|
41
40
|
integerType = 'number',
|
|
42
41
|
unknownType = 'any',
|
|
43
42
|
emptySchemaType = unknownType,
|
|
44
|
-
devtools,
|
|
45
43
|
} = options
|
|
46
44
|
|
|
47
45
|
return {
|
|
@@ -58,7 +56,6 @@ export const adapterOas = defineAdapter<OasAdapter>((options) => {
|
|
|
58
56
|
integerType,
|
|
59
57
|
unknownType,
|
|
60
58
|
emptySchemaType,
|
|
61
|
-
devtools,
|
|
62
59
|
},
|
|
63
60
|
async parse(source) {
|
|
64
61
|
const fakeConfig = sourceToFakeConfig(source)
|
|
@@ -78,9 +75,9 @@ export const adapterOas = defineAdapter<OasAdapter>((options) => {
|
|
|
78
75
|
const baseURL = server?.url ? resolveServerUrl(server, serverVariables) : undefined
|
|
79
76
|
|
|
80
77
|
const parser = createOasParser(oas, { contentType, collisionDetection })
|
|
81
|
-
const root = parser.
|
|
78
|
+
const root = parser.parse({ dateType, integerType, unknownType, emptySchemaType })
|
|
82
79
|
|
|
83
|
-
|
|
80
|
+
return createRoot({
|
|
84
81
|
...root,
|
|
85
82
|
meta: {
|
|
86
83
|
title: oas.api.info?.title,
|
|
@@ -88,12 +85,6 @@ export const adapterOas = defineAdapter<OasAdapter>((options) => {
|
|
|
88
85
|
baseURL,
|
|
89
86
|
},
|
|
90
87
|
})
|
|
91
|
-
|
|
92
|
-
if (devtools) {
|
|
93
|
-
logAst(rootNode, devtools === true ? {} : devtools)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return rootNode
|
|
97
88
|
},
|
|
98
89
|
}
|
|
99
90
|
})
|
package/src/index.ts
CHANGED
package/src/parser.ts
CHANGED
|
@@ -231,7 +231,7 @@ export type OasParser = {
|
|
|
231
231
|
* Converts an OpenAPI/Swagger spec (wrapped in a Kubb `Oas` instance) into
|
|
232
232
|
* a `RootNode` — the top-level node of the `@kubb/ast` tree.
|
|
233
233
|
*/
|
|
234
|
-
|
|
234
|
+
parse: <TOptions extends Partial<Options> = object>(options?: TOptions) => RootNode
|
|
235
235
|
convertSchema: <TFormat extends string, TSchema extends SchemaObject & { format?: TFormat }, TOptions extends Partial<Options> = object>(
|
|
236
236
|
params: { schema: TSchema; name?: string },
|
|
237
237
|
options?: TOptions,
|
|
@@ -290,7 +290,7 @@ function applyDiscriminatorEnum(schema: SchemaObject): SchemaObject {
|
|
|
290
290
|
* Creates an OAS parser that converts an OpenAPI/Swagger spec into
|
|
291
291
|
* the `@kubb/ast` tree.
|
|
292
292
|
*
|
|
293
|
-
* Options are passed per-call to `
|
|
293
|
+
* Options are passed per-call to `parse` or `convertSchema` rather than
|
|
294
294
|
* at construction time, keeping the factory lightweight.
|
|
295
295
|
*
|
|
296
296
|
* This is the **kubb-parser** stage of the compilation lifecycle:
|
|
@@ -302,7 +302,7 @@ function applyDiscriminatorEnum(schema: SchemaObject): SchemaObject {
|
|
|
302
302
|
* @example
|
|
303
303
|
* ```ts
|
|
304
304
|
* const parser = createOasParser(oas)
|
|
305
|
-
* const root = parser.
|
|
305
|
+
* const root = parser.parse({ emptySchemaType: 'unknown' })
|
|
306
306
|
* ```
|
|
307
307
|
*/
|
|
308
308
|
export function createOasParser(oas: Oas, { contentType, collisionDetection }: OasParserOptions = {}): OasParser {
|
|
@@ -1052,7 +1052,7 @@ export function createOasParser(oas: Oas, { contentType, collisionDetection }: O
|
|
|
1052
1052
|
* Converts an OpenAPI/Swagger spec (wrapped in a Kubb `Oas` instance) into
|
|
1053
1053
|
* a `RootNode` — the top-level node of the `@kubb/ast` tree.
|
|
1054
1054
|
*/
|
|
1055
|
-
function
|
|
1055
|
+
function parse<TOptions extends Partial<Options> = object>(options?: TOptions): RootNode {
|
|
1056
1056
|
const mergedOptions: Options = { ...DEFAULT_OPTIONS, ...options }
|
|
1057
1057
|
|
|
1058
1058
|
const schemas: Array<SchemaNode> = Object.entries(schemaObjects).map(([name, schemaObject]) =>
|
|
@@ -1128,7 +1128,7 @@ export function createOasParser(oas: Oas, { contentType, collisionDetection }: O
|
|
|
1128
1128
|
}
|
|
1129
1129
|
|
|
1130
1130
|
return {
|
|
1131
|
-
|
|
1131
|
+
parse: parse,
|
|
1132
1132
|
convertSchema,
|
|
1133
1133
|
resolveRefs,
|
|
1134
1134
|
getImports,
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { AdapterFactoryOptions } from '@kubb/core'
|
|
2
|
-
import type { DevtoolsOptions } from './devtools.ts'
|
|
3
2
|
import type { Oas as OasClass } from './oas/Oas.ts'
|
|
4
3
|
import type { contentType } from './oas/types.ts'
|
|
5
4
|
|
|
@@ -69,15 +68,6 @@ export type OasAdapterOptions = {
|
|
|
69
68
|
* @default `unknownType`
|
|
70
69
|
*/
|
|
71
70
|
emptySchemaType?: 'any' | 'unknown' | 'void'
|
|
72
|
-
/**
|
|
73
|
-
* Enable visual AST inspection via Kubb Studio.
|
|
74
|
-
*
|
|
75
|
-
* - `true` — opens `https://studio.kubb.dev/ast?root=<encoded>` after every `parse()`.
|
|
76
|
-
* - `{ studioUrl }` — same, with a custom Studio URL (e.g. your self-hosted instance).
|
|
77
|
-
*
|
|
78
|
-
* Logs the URL to the console so you can click/open it during development or CI.
|
|
79
|
-
*/
|
|
80
|
-
devtools?: boolean | DevtoolsOptions
|
|
81
71
|
}
|
|
82
72
|
|
|
83
73
|
export type OasAdapterResolvedOptions = {
|
|
@@ -92,7 +82,6 @@ export type OasAdapterResolvedOptions = {
|
|
|
92
82
|
integerType: NonNullable<OasAdapterOptions['integerType']>
|
|
93
83
|
unknownType: NonNullable<OasAdapterOptions['unknownType']>
|
|
94
84
|
emptySchemaType: NonNullable<OasAdapterOptions['emptySchemaType']>
|
|
95
|
-
devtools: OasAdapterOptions['devtools']
|
|
96
85
|
}
|
|
97
86
|
|
|
98
87
|
export type OasAdapter = AdapterFactoryOptions<'oas', OasAdapterOptions, OasAdapterResolvedOptions>
|
package/src/devtools.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import type { RootNode } from '@kubb/ast/types'
|
|
2
|
-
import { deflateSync, inflateSync } from 'fflate'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Default Kubb Studio base URL used when no `studioUrl` override is provided.
|
|
6
|
-
*/
|
|
7
|
-
export const DEFAULT_STUDIO_URL = 'https://studio.kubb.dev'
|
|
8
|
-
|
|
9
|
-
export type DevtoolsOptions = {
|
|
10
|
-
/**
|
|
11
|
-
* Override the Kubb Studio base URL.
|
|
12
|
-
* @default 'https://studio.kubb.dev'
|
|
13
|
-
*/
|
|
14
|
-
studioUrl?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Encodes a `RootNode` as a compressed, URL-safe string.
|
|
19
|
-
*
|
|
20
|
-
* The JSON representation is deflate-compressed with {@link deflateSync} before
|
|
21
|
-
* base64url encoding, which typically reduces payload size by 70–80 % and
|
|
22
|
-
* keeps URLs well within browser and server path-length limits.
|
|
23
|
-
*
|
|
24
|
-
* Use {@link decodeAst} to reverse.
|
|
25
|
-
*/
|
|
26
|
-
export function encodeAst(root: RootNode): string {
|
|
27
|
-
const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(root)))
|
|
28
|
-
return Buffer.from(compressed).toString('base64url')
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Decodes a `RootNode` from a string produced by {@link encodeAst}.
|
|
33
|
-
*
|
|
34
|
-
* Works in both Node.js and the browser — no streaming APIs required.
|
|
35
|
-
*/
|
|
36
|
-
export function decodeAst(encoded: string): RootNode {
|
|
37
|
-
const bytes = Buffer.from(encoded, 'base64url')
|
|
38
|
-
return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as RootNode
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Constructs the Kubb Studio URL for visual AST inspection.
|
|
43
|
-
* The `root` is encoded and attached as the `?root=` query parameter so Studio
|
|
44
|
-
* can decode and render it without a round-trip to any server.
|
|
45
|
-
*/
|
|
46
|
-
export function getAstUrl(root: RootNode, options: DevtoolsOptions = {}): string {
|
|
47
|
-
const baseUrl = (options.studioUrl ?? DEFAULT_STUDIO_URL).replace(/\/$/, '')
|
|
48
|
-
return `${baseUrl}/ast?root=${encodeAst(root)}`
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Prints the Kubb Studio URL for the given `RootNode` — inspired by
|
|
53
|
-
* [testing-playground](https://testing-playground.com/).
|
|
54
|
-
*
|
|
55
|
-
* Drop this call next to an assertion during development or in a failing test
|
|
56
|
-
* to get an instantly-clickable link for visual debugging.
|
|
57
|
-
*/
|
|
58
|
-
export function logAst(root: RootNode, options: DevtoolsOptions = {}): void {
|
|
59
|
-
console.log(`\n ${getAstUrl(root, options)}\n`)
|
|
60
|
-
}
|