@xyd-js/openapi 0.1.0-xyd.11 → 0.1.0-xyd.13
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/CHANGELOG.md +16 -0
- package/__fixtures__/-2.complex.openai/input.yaml +39848 -0
- package/__fixtures__/-2.complex.openai/output.json +321646 -0
- package/__fixtures__/-2.complex.openai/pluginOasOpenai.ts +553 -0
- package/__fixtures__/1.basic/input.yaml +226 -0
- package/__fixtures__/1.basic/output.json +1919 -0
- package/__fixtures__/2.more/input.yaml +76 -0
- package/__fixtures__/2.more/output.json +292 -0
- package/__fixtures__/3.multiple-responses/input.yaml +48 -0
- package/__fixtures__/3.multiple-responses/output.json +266 -0
- package/__fixtures__/4.abc/input.yaml +639 -0
- package/__fixtures__/4.abc/output.json +3828 -0
- package/__fixtures__/5.xdocs.codeLanguages/input.yaml +231 -0
- package/__fixtures__/5.xdocs.codeLanguages/output.json +1879 -0
- package/__fixtures__/5.xdocs.sidebar/input.yaml +256 -0
- package/__fixtures__/5.xdocs.sidebar/output.json +843 -0
- package/__fixtures__/6.codeSamples/input.yaml +75 -0
- package/__fixtures__/6.codeSamples/output.json +293 -0
- package/__tests__/oapSchemaToReferences.test.ts +88 -0
- package/__tests__/utils.ts +81 -0
- package/dist/index.cjs +1859 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +36 -4
- package/dist/index.d.ts +36 -4
- package/dist/index.js +1855 -155
- package/dist/index.js.map +1 -1
- package/index.ts +10 -2
- package/package.json +11 -6
- package/src/const.ts +5 -1
- package/src/converters/oas-componentSchemas.ts +205 -0
- package/src/converters/oas-examples.ts +417 -0
- package/src/{parameters.ts → converters/oas-parameters.ts} +17 -3
- package/src/converters/oas-paths.ts +354 -0
- package/src/{requestBody.ts → converters/oas-requestBody.ts} +30 -10
- package/src/converters/oas-responses.ts +76 -0
- package/src/converters/oas-schema.ts +141 -0
- package/src/index.ts +13 -5
- package/src/oas-core.ts +579 -0
- package/src/types.ts +18 -0
- package/src/utils.ts +103 -90
- package/src/xdocs/index.ts +18 -0
- package/src/xdocs/pluginSidebar.ts +580 -0
- package/src/xdocs/types.ts +26 -0
- package/vitest.config.ts +7 -0
- package/src/examples.ts +0 -116
- package/src/paths.ts +0 -103
- package/src/properties.ts +0 -37
- package/src/responses.ts +0 -38
- package/src/schema.ts +0 -62
package/src/examples.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import {OpenAPIV3} from "openapi-types";
|
|
2
|
-
import Oas from "oas";
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
import {Operation} from 'oas/operation'; // TODO: fix ts
|
|
5
|
-
import oasToSnippet from "@readme/oas-to-snippet";
|
|
6
|
-
import OpenAPISampler from "openapi-sampler";
|
|
7
|
-
import type {JSONSchema7} from "json-schema";
|
|
8
|
-
|
|
9
|
-
import {ExampleGroup, Example} from "@xyd-js/uniform";
|
|
10
|
-
|
|
11
|
-
// TODO: option with another languages
|
|
12
|
-
// TODO: uniform plugins
|
|
13
|
-
// TODO: fs uniform plugins
|
|
14
|
-
export function oapExamples(
|
|
15
|
-
oas: Oas,
|
|
16
|
-
operation: Operation
|
|
17
|
-
): ExampleGroup[] {
|
|
18
|
-
const exampleGroups: ExampleGroup[] = []
|
|
19
|
-
|
|
20
|
-
if (operation.schema.requestBody) {
|
|
21
|
-
const body = operation.schema.requestBody as OpenAPIV3.RequestBodyObject
|
|
22
|
-
const schema = fixAllOfBug(
|
|
23
|
-
body.content["application/json"].schema as JSONSchema7
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
if (!schema) {
|
|
27
|
-
return exampleGroups
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const fakeData = OpenAPISampler.sample(schema)
|
|
31
|
-
|
|
32
|
-
// TODO: snippet languages options
|
|
33
|
-
const {code} = oasToSnippet(oas, operation, {
|
|
34
|
-
body: fakeData
|
|
35
|
-
}, null, "shell");
|
|
36
|
-
|
|
37
|
-
const examples: Example[] = []
|
|
38
|
-
|
|
39
|
-
examples.push({
|
|
40
|
-
codeblock: {
|
|
41
|
-
tabs: [{
|
|
42
|
-
title: "curl",
|
|
43
|
-
language: "curl",
|
|
44
|
-
code: code || "",
|
|
45
|
-
}]
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
exampleGroups.push({
|
|
50
|
-
description: "Example request",
|
|
51
|
-
examples
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (operation.schema.responses) {
|
|
56
|
-
const responses = operation.schema.responses as OpenAPIV3.ResponsesObject
|
|
57
|
-
|
|
58
|
-
const examples: Example[] = []
|
|
59
|
-
|
|
60
|
-
Object.entries(responses).forEach(([status, r]) => {
|
|
61
|
-
const response = r as OpenAPIV3.ResponseObject
|
|
62
|
-
const schema = response?.content?.["application/json"].schema as JSONSchema7
|
|
63
|
-
|
|
64
|
-
if (!schema) {
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const fakeData = OpenAPISampler.sample(schema)
|
|
69
|
-
|
|
70
|
-
examples.push({
|
|
71
|
-
codeblock: {
|
|
72
|
-
title: status,
|
|
73
|
-
tabs: [{
|
|
74
|
-
title: "json",
|
|
75
|
-
language: "json",
|
|
76
|
-
code: JSON.stringify(fakeData, null, 2) || "",
|
|
77
|
-
}]
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
exampleGroups.push({
|
|
83
|
-
description: "Response",
|
|
84
|
-
examples
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return exampleGroups
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/*
|
|
92
|
-
fixAllOfBug fixes below case:
|
|
93
|
-
|
|
94
|
-
```yaml
|
|
95
|
-
allOf:
|
|
96
|
-
- $ref: '#/components/schemas/SomeSchema'
|
|
97
|
-
- type: object
|
|
98
|
-
required:
|
|
99
|
-
properties:
|
|
100
|
-
```
|
|
101
|
-
*/
|
|
102
|
-
function fixAllOfBug(schema: JSONSchema7) {
|
|
103
|
-
const modifiedSchema = {...schema}
|
|
104
|
-
|
|
105
|
-
if (schema.allOf) {
|
|
106
|
-
schema.allOf.forEach((prop, i) => {
|
|
107
|
-
const propObj = prop as object
|
|
108
|
-
|
|
109
|
-
if ("properties" in propObj && !propObj["properties"]) {
|
|
110
|
-
delete modifiedSchema.allOf?.[i]
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return modifiedSchema
|
|
116
|
-
}
|
package/src/paths.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import {OpenAPIV3} from "openapi-types";
|
|
2
|
-
import {Definition, ExampleGroup, Reference, ReferenceCategory} from "@xyd-js/uniform";
|
|
3
|
-
|
|
4
|
-
import {oapParametersToDefinitionProperties} from "./parameters";
|
|
5
|
-
import {oapRequestBodyToDefinitionProperties} from "./requestBody";
|
|
6
|
-
import {oapResponseToDefinitionProperties} from "./responses";
|
|
7
|
-
import {
|
|
8
|
-
httpMethodToUniformMethod,
|
|
9
|
-
toPascalCase
|
|
10
|
-
} from "./utils";
|
|
11
|
-
|
|
12
|
-
// oapPathToReference converts an OpenAPI path to a uniform Reference
|
|
13
|
-
export function oapPathToReference(
|
|
14
|
-
httpMethod: "get" | "put" | "post" | "delete" | "patch", // TODO: ts type
|
|
15
|
-
path: string,
|
|
16
|
-
oapPath: OpenAPIV3.PathItemObject,
|
|
17
|
-
): Reference | null {
|
|
18
|
-
const mType = httpMethodToUniformMethod(httpMethod)
|
|
19
|
-
|
|
20
|
-
if (!mType) {
|
|
21
|
-
console.error(`Unsupported method v222: ${httpMethod}`)
|
|
22
|
-
return null
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const definitions: Definition[] = []
|
|
26
|
-
const exampleGroups: ExampleGroup[] = []
|
|
27
|
-
|
|
28
|
-
const oapMethod = oapPath?.[httpMethod] as OpenAPIV3.OperationObject
|
|
29
|
-
|
|
30
|
-
if (!oapMethod) {
|
|
31
|
-
return null
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const endpointRef: Reference = {
|
|
35
|
-
title: oapMethod?.summary || "",
|
|
36
|
-
canonical: toPascalCase(oapMethod?.summary || ""),
|
|
37
|
-
description: oapMethod?.description || "",
|
|
38
|
-
|
|
39
|
-
category: ReferenceCategory.REST,
|
|
40
|
-
type: mType,
|
|
41
|
-
|
|
42
|
-
context: {
|
|
43
|
-
method: httpMethod,
|
|
44
|
-
|
|
45
|
-
path: `${encodeURIComponent(path)}`,
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
examples: {
|
|
49
|
-
groups: exampleGroups,
|
|
50
|
-
},
|
|
51
|
-
definitions: definitions,
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (oapMethod.parameters) {
|
|
55
|
-
const parameters = oapMethod.parameters as OpenAPIV3.ParameterObject[]
|
|
56
|
-
|
|
57
|
-
const paramtersMap = oapParametersToDefinitionProperties(parameters) // TODO: finish
|
|
58
|
-
|
|
59
|
-
Object.entries(paramtersMap).forEach(([key, definitionProperties]) => {
|
|
60
|
-
let title: string
|
|
61
|
-
|
|
62
|
-
switch (key) {
|
|
63
|
-
case 'path':
|
|
64
|
-
title = "Path parameters"
|
|
65
|
-
break
|
|
66
|
-
case 'query':
|
|
67
|
-
title = "Query"
|
|
68
|
-
break
|
|
69
|
-
case 'header':
|
|
70
|
-
title = "Header"
|
|
71
|
-
break
|
|
72
|
-
default:
|
|
73
|
-
console.error(`Unsupported parameter type: ${key} for ${httpMethod} ${path}`)
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
definitions.push({
|
|
78
|
-
title,
|
|
79
|
-
properties: definitionProperties
|
|
80
|
-
})
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (oapMethod.requestBody) {
|
|
85
|
-
const reqBody = oapMethod.requestBody as OpenAPIV3.RequestBodyObject
|
|
86
|
-
|
|
87
|
-
definitions.push({
|
|
88
|
-
title: 'Request body',
|
|
89
|
-
properties: oapRequestBodyToDefinitionProperties(reqBody) || []
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (oapMethod.responses) {
|
|
94
|
-
const responses = oapMethod.responses as OpenAPIV3.ResponsesObject
|
|
95
|
-
|
|
96
|
-
definitions.push({
|
|
97
|
-
title: 'Response',
|
|
98
|
-
properties: oapResponseToDefinitionProperties(responses) || []
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return endpointRef
|
|
103
|
-
}
|
package/src/properties.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import {OpenAPIV3} from "openapi-types";
|
|
2
|
-
import {DefinitionProperty} from "@xyd-js/uniform";
|
|
3
|
-
|
|
4
|
-
// schemaObjectToDefinitionProperties converts OpenAPI schema object to uniform DefinitionProperty[]
|
|
5
|
-
export function schemaObjectToDefinitionProperties(v: OpenAPIV3.SchemaObject): DefinitionProperty[] {
|
|
6
|
-
return Object.entries(v.properties || {}).map(([name, prop]) => {
|
|
7
|
-
let objProp = prop as OpenAPIV3.SchemaObject
|
|
8
|
-
|
|
9
|
-
let merged: DefinitionProperty[] = []
|
|
10
|
-
|
|
11
|
-
if (objProp.allOf) {
|
|
12
|
-
merged = objProp.allOf.reduce((acc, of) => {
|
|
13
|
-
return [
|
|
14
|
-
...acc,
|
|
15
|
-
...schemaObjectToDefinitionProperties(of as OpenAPIV3.SchemaObject)
|
|
16
|
-
]
|
|
17
|
-
}, [] as DefinitionProperty[])
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (objProp.type === "array") {
|
|
21
|
-
const items = objProp.items as OpenAPIV3.SchemaObject
|
|
22
|
-
|
|
23
|
-
merged = schemaObjectToDefinitionProperties(items)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return {
|
|
27
|
-
name: name,
|
|
28
|
-
type: objProp.type || "",
|
|
29
|
-
description: objProp.description || "",
|
|
30
|
-
properties: (
|
|
31
|
-
merged?.length
|
|
32
|
-
? merged
|
|
33
|
-
: objProp.properties ? schemaObjectToDefinitionProperties(objProp) : []
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
}
|
package/src/responses.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {OpenAPIV3} from "openapi-types";
|
|
2
|
-
import {DefinitionProperty} from "@xyd-js/uniform";
|
|
3
|
-
|
|
4
|
-
import {schemaObjectToDefinitionProperties} from "./properties";
|
|
5
|
-
|
|
6
|
-
export function oapResponseToDefinitionProperties(
|
|
7
|
-
responses: OpenAPIV3.ResponsesObject
|
|
8
|
-
): DefinitionProperty[] | null {
|
|
9
|
-
let schemaObject: OpenAPIV3.SchemaObject | undefined
|
|
10
|
-
// TODO: support another statuses
|
|
11
|
-
if (responses["default"]) {
|
|
12
|
-
const w = responses["default"] as OpenAPIV3.ResponseObject
|
|
13
|
-
|
|
14
|
-
schemaObject = w?.content?.['application/json']?.schema as OpenAPIV3.SchemaObject
|
|
15
|
-
} else if (responses["200"]) {
|
|
16
|
-
const w = responses["200"] as OpenAPIV3.ResponseObject
|
|
17
|
-
|
|
18
|
-
schemaObject = w?.content?.['application/json']?.schema as OpenAPIV3.SchemaObject
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (!schemaObject) {
|
|
22
|
-
return null
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
switch (schemaObject.type) {
|
|
26
|
-
case 'array':
|
|
27
|
-
const arrSchema = schemaObject as OpenAPIV3.ArraySchemaObject
|
|
28
|
-
|
|
29
|
-
const items = arrSchema.items as OpenAPIV3.SchemaObject
|
|
30
|
-
|
|
31
|
-
schemaObject = items
|
|
32
|
-
break
|
|
33
|
-
default:
|
|
34
|
-
break
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return schemaObjectToDefinitionProperties(schemaObject)
|
|
38
|
-
}
|
package/src/schema.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import {OpenAPIV3} from "openapi-types";
|
|
2
|
-
import Oas from "oas";
|
|
3
|
-
|
|
4
|
-
import type {Reference, OpenAPIReferenceContext} from "@xyd-js/uniform";
|
|
5
|
-
|
|
6
|
-
import {SUPPORTED_HTTP_METHODS} from "./const";
|
|
7
|
-
import {oapPathToReference} from "./paths";
|
|
8
|
-
import {oapExamples} from "./examples";
|
|
9
|
-
|
|
10
|
-
// TODO: support one-of
|
|
11
|
-
// TODO: support $ref - currently we use $refParser.dereference that converts $ref into objects
|
|
12
|
-
// TODO: better method check system - currently we need to manually check that in few methods
|
|
13
|
-
|
|
14
|
-
// oapSchemaToReferences converts an OpenAPI schema to a list of uniform References
|
|
15
|
-
export function oapSchemaToReferences(
|
|
16
|
-
schema: OpenAPIV3.Document
|
|
17
|
-
): Reference[] {
|
|
18
|
-
const references: Reference[] = [];
|
|
19
|
-
const oas = new Oas(schema as any);
|
|
20
|
-
|
|
21
|
-
const server = schema.servers?.[0]?.url || ""
|
|
22
|
-
|
|
23
|
-
Object.entries(schema.paths).forEach(([path, oapPath]) => {
|
|
24
|
-
SUPPORTED_HTTP_METHODS.forEach((eachMethod) => {
|
|
25
|
-
const httpMethod = eachMethod.toLowerCase()
|
|
26
|
-
|
|
27
|
-
switch (httpMethod) {
|
|
28
|
-
case 'get':
|
|
29
|
-
break
|
|
30
|
-
case 'put':
|
|
31
|
-
break
|
|
32
|
-
case 'post':
|
|
33
|
-
break
|
|
34
|
-
case 'delete':
|
|
35
|
-
break
|
|
36
|
-
case 'patch':
|
|
37
|
-
break
|
|
38
|
-
default:
|
|
39
|
-
console.error(`Unsupported method v111: ${httpMethod}`)
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const reference = oapPathToReference(
|
|
44
|
-
httpMethod,
|
|
45
|
-
path,
|
|
46
|
-
oapPath as OpenAPIV3.PathItemObject
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
if (reference) {
|
|
50
|
-
const ctx = reference.context as OpenAPIReferenceContext
|
|
51
|
-
ctx.path = `${encodeURIComponent(server)}${encodeURIComponent(path)}` // TODO: it should be inside `oapPathToReference` ?
|
|
52
|
-
|
|
53
|
-
const operation = oas.operation(path, httpMethod);
|
|
54
|
-
reference.examples.groups = oapExamples(oas, operation)
|
|
55
|
-
|
|
56
|
-
references.push(reference)
|
|
57
|
-
}
|
|
58
|
-
})
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
return references
|
|
62
|
-
}
|