@xyd-js/openapi 0.1.0-build.168

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +1517 -0
  2. package/LICENSE +21 -0
  3. package/README.md +3 -0
  4. package/__fixtures__/-2.complex.openai/input.yaml +39848 -0
  5. package/__fixtures__/-2.complex.openai/output.json +321646 -0
  6. package/__fixtures__/-2.complex.openai/pluginOasOpenai.ts +553 -0
  7. package/__fixtures__/-3.random/input.yaml +234 -0
  8. package/__fixtures__/-3.random/output.json +1140 -0
  9. package/__fixtures__/1.basic/input.yaml +226 -0
  10. package/__fixtures__/1.basic/output.json +1919 -0
  11. package/__fixtures__/2.more/input.yaml +76 -0
  12. package/__fixtures__/2.more/output.json +327 -0
  13. package/__fixtures__/3.multiple-responses/input.yaml +48 -0
  14. package/__fixtures__/3.multiple-responses/output.json +311 -0
  15. package/__fixtures__/5.xdocs.codeLanguages/input.yaml +231 -0
  16. package/__fixtures__/5.xdocs.codeLanguages/output.json +1879 -0
  17. package/__fixtures__/5.xdocs.sidebar/input.yaml +256 -0
  18. package/__fixtures__/5.xdocs.sidebar/output.json +843 -0
  19. package/__fixtures__/6.codeSamples/input.yaml +75 -0
  20. package/__fixtures__/6.codeSamples/output.json +293 -0
  21. package/__tests__/oapSchemaToReferences.test.ts +82 -0
  22. package/__tests__/utils.ts +81 -0
  23. package/dist/index.cjs +2154 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +40 -0
  26. package/dist/index.d.ts +40 -0
  27. package/dist/index.js +2119 -0
  28. package/dist/index.js.map +1 -0
  29. package/examples/basic/index.ts +20 -0
  30. package/examples/basic/index2.ts +36 -0
  31. package/examples/basic/openapi.yaml +124 -0
  32. package/examples/dist/index.cjs +2 -0
  33. package/examples/dist/index.cjs.map +1 -0
  34. package/examples/dist/index.d.cts +2 -0
  35. package/examples/dist/index.d.ts +2 -0
  36. package/examples/dist/index.js +2 -0
  37. package/examples/dist/index.js.map +1 -0
  38. package/examples/semi/index.ts +16 -0
  39. package/examples/semi/openapi.yaml +365 -0
  40. package/examples/semi/references.json +500 -0
  41. package/examples/webhooks/index.ts +16 -0
  42. package/examples/webhooks/openapi.yaml +248 -0
  43. package/examples/webhooks/references.json +895 -0
  44. package/index.ts +12 -0
  45. package/package.json +31 -0
  46. package/src/const.ts +14 -0
  47. package/src/converters/oas-componentSchemas.ts +205 -0
  48. package/src/converters/oas-examples.ts +530 -0
  49. package/src/converters/oas-parameters.ts +41 -0
  50. package/src/converters/oas-paths.ts +354 -0
  51. package/src/converters/oas-requestBody.ts +57 -0
  52. package/src/converters/oas-responses.ts +76 -0
  53. package/src/converters/oas-schema.ts +141 -0
  54. package/src/index.ts +21 -0
  55. package/src/oas-core.ts +579 -0
  56. package/src/types.ts +18 -0
  57. package/src/utils.ts +157 -0
  58. package/src/xdocs/index.ts +18 -0
  59. package/src/xdocs/pluginSidebar.ts +580 -0
  60. package/src/xdocs/types.ts +26 -0
  61. package/tsconfig.json +18 -0
  62. package/tsup.config.ts +19 -0
  63. package/tsup.examples-config.ts +30 -0
  64. package/vitest.config.ts +7 -0
package/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export type {uniformOasOptions, SelectorMethod} from "./src/types";
2
+
3
+ export {
4
+ deferencedOpenAPI,
5
+ oapSchemaToReferences,
6
+
7
+ oapResponseOperationToUniformDefinition,
8
+
9
+ getXDocs,
10
+ uniformPluginXDocsSidebar
11
+ } from "./src";
12
+
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@xyd-js/openapi",
3
+ "version": "0.1.0-build.168",
4
+ "description": "",
5
+ "main": "./dist/index.js",
6
+ "type": "module",
7
+ "dependencies": {
8
+ "@apidevtools/json-schema-ref-parser": "^12.0.2",
9
+ "@har-sdk/openapi-sampler": "^2.4.2",
10
+ "@readme/oas-to-snippet": "^28.0.0",
11
+ "js-yaml": "^4.1.0",
12
+ "oas": "^25.0.3",
13
+ "openapi-types": "^12.1.3",
14
+ "@xyd-js/openapi-sampler": "0.1.0-build.138",
15
+ "@xyd-js/uniform": "0.1.0-build.172"
16
+ },
17
+ "devDependencies": {
18
+ "@types/js-yaml": "^4.0.9",
19
+ "rimraf": "^3.0.2",
20
+ "tsup": "^8.3.0",
21
+ "vitest": "^2.1.1"
22
+ },
23
+ "scripts": {
24
+ "clean": "rimraf build",
25
+ "prebuild": "pnpm clean",
26
+ "build": "tsup",
27
+ "build:examples": "tsup --config tsup.examples-config.ts",
28
+ "test": "vitest",
29
+ "ci:test": "vitest run"
30
+ }
31
+ }
package/src/const.ts ADDED
@@ -0,0 +1,14 @@
1
+ export const SUPPORTED_HTTP_METHODS: string[] = [
2
+ 'get',
3
+ 'put',
4
+ 'patch',
5
+ 'post',
6
+ 'delete',
7
+ // 'options',
8
+ // 'head',
9
+ // 'trace'
10
+ ];
11
+
12
+ export const BUILT_IN_PROPERTIES: { [key: string]: boolean } = {
13
+ "__internal_getRefPath": true,
14
+ }
@@ -0,0 +1,205 @@
1
+ import {OpenAPIV3} from "openapi-types";
2
+
3
+ import {
4
+ Reference,
5
+ Definition,
6
+ DefinitionProperty,
7
+ ReferenceType,
8
+ OpenAPIReferenceContext, SymbolDef,
9
+ ExampleGroup,
10
+ CodeBlockTab,
11
+ } from "@xyd-js/uniform";
12
+
13
+ import {OasJSONSchema, uniformOasOptions} from "../types";
14
+ import {schemaObjectToUniformDefinitionProperties} from "../oas-core";
15
+
16
+ export function schemaComponentsToUniformReferences(
17
+ openapi: OpenAPIV3.Document,
18
+ options?: uniformOasOptions,
19
+ ): Reference[] {
20
+ const references: Reference[] = [];
21
+
22
+ if (!openapi.components?.schemas) {
23
+ return references;
24
+ }
25
+
26
+ for (const [componentSchemaName, componentSchema] of Object.entries(openapi.components.schemas)) {
27
+ if (options?.regions && options.regions.length > 0) {
28
+ if (!options.regions.some(region => region === "/components/schemas/" + componentSchemaName)) {
29
+ continue
30
+ }
31
+ }
32
+
33
+ if ('$ref' in componentSchema) {
34
+ console.warn(`Skipping reference object: ${componentSchemaName}`);
35
+ continue; // Skip reference objects
36
+ }
37
+
38
+ let properties: DefinitionProperty[] = [];
39
+ let rootProperty: DefinitionProperty | undefined = undefined;
40
+ const respProperties = schemaObjectToUniformDefinitionProperties(componentSchema, false) || [];
41
+ if (Array.isArray(respProperties)) {
42
+ properties = respProperties
43
+ } else {
44
+ rootProperty = respProperties
45
+ }
46
+
47
+ const symbolDef = definitionPropertyTypeDef(componentSchema)
48
+
49
+ const definition: Definition = {
50
+ title: componentSchemaName,
51
+ properties,
52
+ rootProperty,
53
+ meta: [],
54
+ symbolDef,
55
+ };
56
+
57
+ // Create reference
58
+ const reference: Reference = {
59
+ title: componentSchemaName,
60
+ description: componentSchema.description || "",
61
+ canonical: `objects/${componentSchemaName}`,
62
+ definitions: [definition],
63
+ examples: {
64
+ groups: createSchemaExampleGroup(componentSchema as OpenAPIV3.SchemaObject)
65
+ },
66
+ type: ReferenceType.REST_COMPONENT_SCHEMA,
67
+ context: {
68
+ componentSchema: componentSchemaName,
69
+ group: ["Objects"]
70
+ } as OpenAPIReferenceContext
71
+ };
72
+
73
+ // TODO: !!!! better api !!!!
74
+ reference.__UNSAFE_selector = function __UNSAFE_selector(selector: string) {
75
+ switch (selector) {
76
+ case "[schema]": {
77
+ return openapi
78
+ }
79
+
80
+ case "[component]": {
81
+ return componentSchema
82
+ }
83
+
84
+ default:
85
+ return null
86
+ }
87
+ }
88
+
89
+ references.push(reference);
90
+ }
91
+
92
+ return references;
93
+ }
94
+
95
+ function createSchemaExampleGroup(schema: OpenAPIV3.SchemaObject, map: any): ExampleGroup[] {
96
+ const example = generateSchemaExample(schema);
97
+ if (!example) {
98
+ return [];
99
+ }
100
+
101
+ const tabs: CodeBlockTab[] = [{
102
+ title: 'json',
103
+ language: 'json',
104
+ code: JSON.stringify(example, null, 2)
105
+ }];
106
+
107
+ return [{
108
+ description: 'Example',
109
+ examples: [{
110
+ codeblock: {
111
+ tabs
112
+ }
113
+ }]
114
+ }];
115
+ }
116
+
117
+ function definitionPropertyTypeDef(
118
+ schema: OpenAPIV3.SchemaObject | undefined,
119
+ ) {
120
+ if (!schema) {
121
+ return
122
+ }
123
+
124
+ let typeDef: SymbolDef | undefined
125
+ let oasSchema = schema as OasJSONSchema
126
+ if (oasSchema.type === "array") {
127
+ oasSchema = oasSchema.items as OasJSONSchema
128
+ }
129
+ if (oasSchema?.__internal_getRefPath) {
130
+ const symbolId = oasSchema.__internal_getRefPath()
131
+
132
+ typeDef = {
133
+ id: symbolId,
134
+ }
135
+ }
136
+
137
+ return typeDef
138
+ }
139
+
140
+ function generateSchemaExample(
141
+ schema: OpenAPIV3.SchemaObject,
142
+ visitedExample?: Map<OpenAPIV3.SchemaObject, any>,
143
+ parent?: any
144
+ ): any {
145
+ if (!schema) {
146
+ return null;
147
+ }
148
+ if (!visitedExample) {
149
+ visitedExample = new Map<OpenAPIV3.SchemaObject, any>();
150
+ }
151
+
152
+ const cached = visitedExample.get(schema)
153
+ if (cached) {
154
+ return JSON.parse(JSON.stringify(cached)); // Return a deep copy of the cached example
155
+ }
156
+ if (parent) {
157
+ visitedExample.set(schema, parent);
158
+ }
159
+
160
+ // Handle examples array
161
+ if ('examples' in schema && Array.isArray(schema.examples)) {
162
+ const v = schema.examples[0];
163
+ visitedExample.set(schema, v);
164
+ return v; // Return the first example from the examples array
165
+ }
166
+
167
+ // Handle single example
168
+ if ('example' in schema && schema.example !== undefined) {
169
+ const v = schema.example;
170
+ visitedExample.set(schema, v);
171
+ return v; // Return the single example if it exists
172
+ }
173
+
174
+ // Handle object type with properties
175
+ if (schema.type === 'object' && schema.properties) {
176
+ const result: Record<string, any> = {};
177
+ for (const [propName, propSchema] of Object.entries(schema.properties)) {
178
+ result[propName] = generateSchemaExample(propSchema as OpenAPIV3.SchemaObject, visitedExample, result);
179
+ }
180
+ visitedExample.set(schema, result);
181
+ return result;
182
+ }
183
+
184
+ // Handle array type
185
+ if (schema.type === 'array' && schema.items) {
186
+ const itemExample = generateSchemaExample(schema.items as OpenAPIV3.SchemaObject, visitedExample);
187
+ const v = itemExample ? [itemExample] : [];
188
+ visitedExample.set(schema, v);
189
+
190
+ return v; // Return an array with a single item example
191
+ }
192
+
193
+ // Handle primitive types with default values
194
+ switch (schema.type) {
195
+ case 'string':
196
+ return '';
197
+ case 'number':
198
+ case 'integer':
199
+ return 0;
200
+ case 'boolean':
201
+ return false;
202
+ default:
203
+ return null;
204
+ }
205
+ }