@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/dist/index.js ADDED
@@ -0,0 +1,2119 @@
1
+ // src/const.ts
2
+ var SUPPORTED_HTTP_METHODS = [
3
+ "get",
4
+ "put",
5
+ "patch",
6
+ "post",
7
+ "delete"
8
+ // 'options',
9
+ // 'head',
10
+ // 'trace'
11
+ ];
12
+ var BUILT_IN_PROPERTIES = {
13
+ "__internal_getRefPath": true
14
+ };
15
+
16
+ // src/oas-core.ts
17
+ import {
18
+ DEFINED_DEFINITION_PROPERTY_TYPE
19
+ } from "@xyd-js/uniform";
20
+ function schemaObjectToUniformDefinitionProperties(schemaObject, rootProperty, visitedRefs) {
21
+ var _a, _b;
22
+ if ("$ref" in schemaObject) {
23
+ console.warn("Reference objects are not supported in schemaObjectToUniformDefinitionProperties");
24
+ return null;
25
+ }
26
+ const properties = [];
27
+ if ("anyOf" in schemaObject && schemaObject.anyOf) {
28
+ const property = schemaObjectToUniformDefinitionProperty("", schemaObject, false, false, visitedRefs);
29
+ if (property) {
30
+ if (rootProperty) {
31
+ return property;
32
+ }
33
+ properties.push(property);
34
+ }
35
+ } else if ("oneOf" in schemaObject && schemaObject.oneOf) {
36
+ const property = schemaObjectToUniformDefinitionProperty("", schemaObject, false, false, visitedRefs);
37
+ if (property) {
38
+ if (rootProperty) {
39
+ return property;
40
+ }
41
+ properties.push(property);
42
+ }
43
+ } else if ("allOf" in schemaObject && schemaObject.allOf) {
44
+ const componentPaths = [];
45
+ for (const schema of schemaObject.allOf) {
46
+ if ("$ref" in schema) {
47
+ console.warn("$ref is not supported in allOf schemas");
48
+ continue;
49
+ }
50
+ const oasSchema2 = schema;
51
+ if (oasSchema2.__internal_getRefPath) {
52
+ const refPath = oasSchema2.__internal_getRefPath();
53
+ if (typeof refPath === "string") {
54
+ componentPaths.push(refPath);
55
+ } else if (Array.isArray(refPath)) {
56
+ componentPaths.push(...refPath);
57
+ } else {
58
+ console.warn("Invalid refPath type in allOf schema", oasSchema2);
59
+ }
60
+ }
61
+ if ("properties" in schema && schema.properties) {
62
+ for (const [propName, propSchema] of Object.entries(schema.properties)) {
63
+ if (BUILT_IN_PROPERTIES[propName]) {
64
+ continue;
65
+ }
66
+ if ("$ref" in propSchema) {
67
+ console.warn("$ref is not supported in allOf properties");
68
+ continue;
69
+ }
70
+ const property = schemaObjectToUniformDefinitionProperty(
71
+ propName,
72
+ propSchema,
73
+ (_a = schema.required) == null ? void 0 : _a.includes(propName),
74
+ (propSchema == null ? void 0 : propSchema.type) === "array" ? true : false,
75
+ visitedRefs
76
+ );
77
+ if (property) {
78
+ const existingPropertyIndex = properties.findIndex((p) => p.name === propName);
79
+ if (existingPropertyIndex >= 0) {
80
+ const existingProperty = properties[existingPropertyIndex];
81
+ properties[existingPropertyIndex] = {
82
+ ...existingProperty,
83
+ ...property,
84
+ description: property.description || existingProperty.description || "",
85
+ meta: [...existingProperty.meta || [], ...property.meta || []]
86
+ };
87
+ } else {
88
+ properties.push(property);
89
+ }
90
+ }
91
+ }
92
+ }
93
+ }
94
+ const oasSchema = schemaObject;
95
+ oasSchema.__internal_getRefPath = () => componentPaths;
96
+ } else if ("properties" in schemaObject && schemaObject.properties) {
97
+ for (const [propName, propSchema] of Object.entries(schemaObject.properties)) {
98
+ if (BUILT_IN_PROPERTIES[propName]) {
99
+ continue;
100
+ }
101
+ if (typeof propSchema === "object" && "$ref" in propSchema) {
102
+ console.warn("$ref is not supported in properties");
103
+ continue;
104
+ }
105
+ const property = schemaObjectToUniformDefinitionProperty(
106
+ propName,
107
+ propSchema,
108
+ (_b = schemaObject.required) == null ? void 0 : _b.includes(propName),
109
+ (propSchema == null ? void 0 : propSchema.type) === "array" ? true : false,
110
+ visitedRefs
111
+ );
112
+ if (property) {
113
+ properties.push(property);
114
+ }
115
+ }
116
+ }
117
+ return properties;
118
+ }
119
+ function schemaObjectToUniformDefinitionProperty(name, schema, required, arrayOf, visitedRefs, parentProperty) {
120
+ var _a, _b, _c, _d;
121
+ if (name === "__UNSAFE_refPath") {
122
+ return null;
123
+ }
124
+ if (!schema) return null;
125
+ if (!visitedRefs) {
126
+ visitedRefs = /* @__PURE__ */ new Map();
127
+ }
128
+ let refPath = "";
129
+ if ("__UNSAFE_refPath" in schema && typeof schema.__UNSAFE_refPath === "function") {
130
+ refPath = schema.__UNSAFE_refPath();
131
+ const defProp = visitedRefs.get(refPath);
132
+ if (defProp) {
133
+ return JSON.parse(JSON.stringify(defProp));
134
+ }
135
+ }
136
+ if (parentProperty) {
137
+ visitedRefs.set(refPath, parentProperty);
138
+ }
139
+ if ("anyOf" in schema && schema.anyOf) {
140
+ const componentPaths = [];
141
+ const properties = [];
142
+ for (const variantSchema of schema.anyOf) {
143
+ if ("$ref" in variantSchema) {
144
+ console.warn("$ref is not supported in anyOf schemas");
145
+ continue;
146
+ }
147
+ const oasSchema2 = variantSchema;
148
+ if (oasSchema2.__internal_getRefPath) {
149
+ const refPath2 = oasSchema2.__internal_getRefPath();
150
+ if (typeof refPath2 === "string") {
151
+ componentPaths.push(refPath2);
152
+ } else if (Array.isArray(refPath2)) {
153
+ componentPaths.push(...refPath2);
154
+ } else {
155
+ console.warn("Invalid refPath type in anyOf schema", oasSchema2);
156
+ }
157
+ }
158
+ const property2 = schemaObjectToUniformDefinitionProperty(name, variantSchema, required, false, visitedRefs);
159
+ if (property2) {
160
+ if (isMergeType(property2.type)) {
161
+ properties.push(...property2.properties || []);
162
+ } else {
163
+ properties.push({
164
+ ...property2,
165
+ name: variantSchema.title || property2.name || ""
166
+ });
167
+ }
168
+ }
169
+ }
170
+ const oasSchema = schema;
171
+ oasSchema.__internal_getRefPath = () => componentPaths;
172
+ const prop = {
173
+ name,
174
+ type: DEFINED_DEFINITION_PROPERTY_TYPE.UNION,
175
+ description: schema.description || "",
176
+ properties
177
+ };
178
+ if (refPath) {
179
+ visitedRefs.set(refPath, prop);
180
+ }
181
+ return prop;
182
+ }
183
+ const meta = schemaObjectToUniformDefinitionPropertyMeta({
184
+ ...schema,
185
+ required: required ? [name] : void 0
186
+ }, name);
187
+ if ("oneOf" in schema && schema.oneOf) {
188
+ const componentPaths = [];
189
+ const properties = [];
190
+ for (const variantSchema of schema.oneOf) {
191
+ if ("$ref" in variantSchema) {
192
+ console.warn("$ref is not supported in oneOf schemas");
193
+ continue;
194
+ }
195
+ const oasSchema2 = variantSchema;
196
+ if (oasSchema2.__internal_getRefPath) {
197
+ const refPath2 = oasSchema2.__internal_getRefPath();
198
+ if (typeof refPath2 === "string") {
199
+ componentPaths.push(refPath2);
200
+ } else if (Array.isArray(refPath2)) {
201
+ componentPaths.push(...refPath2);
202
+ } else {
203
+ console.warn("Invalid refPath type in allOf schema", oasSchema2);
204
+ }
205
+ }
206
+ const property2 = schemaObjectToUniformDefinitionProperty(name, variantSchema, required, false, visitedRefs);
207
+ if (property2) {
208
+ properties.push({
209
+ ...property2,
210
+ name: variantSchema.title || property2.name || ""
211
+ });
212
+ }
213
+ }
214
+ const oasSchema = schema;
215
+ oasSchema.__internal_getRefPath = () => componentPaths;
216
+ const prop = {
217
+ name,
218
+ type: DEFINED_DEFINITION_PROPERTY_TYPE.XOR,
219
+ description: schema.description || "",
220
+ properties,
221
+ meta
222
+ };
223
+ if (refPath) {
224
+ visitedRefs.set(refPath, prop);
225
+ }
226
+ return prop;
227
+ }
228
+ if ("allOf" in schema && schema.allOf) {
229
+ const componentPaths = [];
230
+ const mergedProperty = {
231
+ name,
232
+ type: schema.type || "",
233
+ description: schema.description || "",
234
+ properties: [],
235
+ meta
236
+ };
237
+ for (const variantSchema of schema.allOf) {
238
+ if ("$ref" in variantSchema) {
239
+ console.warn("$ref is not supported in allOf schemas");
240
+ continue;
241
+ }
242
+ const oasSchema2 = variantSchema;
243
+ if (oasSchema2.__internal_getRefPath) {
244
+ const refPath2 = oasSchema2.__internal_getRefPath();
245
+ if (typeof refPath2 === "string") {
246
+ componentPaths.push(refPath2);
247
+ } else if (Array.isArray(refPath2)) {
248
+ componentPaths.push(...refPath2);
249
+ } else {
250
+ console.warn("Invalid refPath type in allOf schema", oasSchema2);
251
+ }
252
+ }
253
+ if (!mergedProperty.type) {
254
+ if (typeof variantSchema.type === "string") {
255
+ mergedProperty.type = variantSchema.type || "";
256
+ }
257
+ }
258
+ if ("properties" in variantSchema && variantSchema.properties) {
259
+ for (const [propName, propSchema] of Object.entries(variantSchema.properties)) {
260
+ if (BUILT_IN_PROPERTIES[propName]) {
261
+ continue;
262
+ }
263
+ if ("$ref" in propSchema) {
264
+ console.warn("$ref is not supported in allOf properties");
265
+ continue;
266
+ }
267
+ const property2 = schemaObjectToUniformDefinitionProperty(
268
+ propName,
269
+ propSchema,
270
+ (_a = variantSchema.required) == null ? void 0 : _a.includes(propName),
271
+ false,
272
+ visitedRefs
273
+ );
274
+ if (property2 && mergedProperty.properties) {
275
+ const existingPropertyIndex = mergedProperty.properties.findIndex((p) => p.name === propName);
276
+ if (existingPropertyIndex >= 0) {
277
+ const existingProperty = mergedProperty.properties[existingPropertyIndex];
278
+ mergedProperty.properties[existingPropertyIndex] = {
279
+ ...existingProperty,
280
+ ...property2,
281
+ description: property2.description || existingProperty.description || "",
282
+ meta: [...existingProperty.meta || [], ...property2.meta || []]
283
+ };
284
+ } else {
285
+ mergedProperty.properties.push(property2);
286
+ }
287
+ }
288
+ }
289
+ } else {
290
+ const property2 = schemaObjectToUniformDefinitionProperty(
291
+ "",
292
+ variantSchema,
293
+ false,
294
+ false,
295
+ visitedRefs
296
+ );
297
+ if (property2) {
298
+ if (isOfType(property2.type)) {
299
+ mergedProperty.ofProperty = property2;
300
+ } else {
301
+ if (!((_b = mergedProperty.properties) == null ? void 0 : _b.length)) {
302
+ mergedProperty.properties = [];
303
+ }
304
+ if (mergedProperty.ofProperty) {
305
+ mergedProperty.description = property2.description || mergedProperty.ofProperty.description || "";
306
+ } else {
307
+ mergedProperty.properties.push(property2);
308
+ }
309
+ }
310
+ }
311
+ }
312
+ }
313
+ const oasSchema = schema;
314
+ oasSchema.__internal_getRefPath = () => componentPaths;
315
+ if (refPath) {
316
+ visitedRefs.set(refPath, mergedProperty);
317
+ }
318
+ return mergedProperty;
319
+ }
320
+ const property = {
321
+ name,
322
+ type: schema.type || "object",
323
+ description: schema.description || "",
324
+ meta
325
+ };
326
+ if (schema.enum) {
327
+ const enumProperties = schemaObjectToUniformDefinitionProperties({
328
+ properties: schema.enum.reduce((acc, enumName) => ({
329
+ ...acc,
330
+ [enumName]: {
331
+ type: schema.type
332
+ }
333
+ }), {})
334
+ });
335
+ if (!Array.isArray(enumProperties)) {
336
+ return property;
337
+ }
338
+ meta.push({
339
+ name: "enum-type",
340
+ value: schema.type
341
+ });
342
+ const enumProperty = {
343
+ name,
344
+ type: DEFINED_DEFINITION_PROPERTY_TYPE.ENUM,
345
+ description: schema.description || "",
346
+ meta,
347
+ properties: enumProperties || []
348
+ };
349
+ if (refPath) {
350
+ visitedRefs.set(refPath, enumProperty);
351
+ }
352
+ return enumProperty;
353
+ } else {
354
+ if ("properties" in schema && schema.properties) {
355
+ property.properties = [];
356
+ for (const [propName, propSchema] of Object.entries(schema.properties)) {
357
+ if (BUILT_IN_PROPERTIES[propName]) {
358
+ continue;
359
+ }
360
+ if ("$ref" in propSchema) {
361
+ console.warn("$ref is not supported in properties");
362
+ continue;
363
+ }
364
+ const nestedProperty = schemaObjectToUniformDefinitionProperty(
365
+ propName,
366
+ propSchema,
367
+ (_c = schema.required) == null ? void 0 : _c.includes(propName),
368
+ (propSchema == null ? void 0 : propSchema.type) === "array" ? true : false,
369
+ visitedRefs
370
+ );
371
+ if (nestedProperty) {
372
+ property.properties.push(nestedProperty);
373
+ }
374
+ }
375
+ } else if (schema.type === "array" && schema.items && !("$ref" in schema.items)) {
376
+ const arrayProperty = {
377
+ name,
378
+ type: DEFINED_DEFINITION_PROPERTY_TYPE.ARRAY,
379
+ description: schema.description || "",
380
+ meta,
381
+ properties: []
382
+ };
383
+ const itemsProperty = schemaObjectToUniformDefinitionProperty("", schema.items, required, true, visitedRefs, arrayProperty);
384
+ if (itemsProperty) {
385
+ if (arrayOf || isOfType(itemsProperty.type) || ((_d = itemsProperty.ofProperty) == null ? void 0 : _d.type)) {
386
+ arrayProperty.ofProperty = {
387
+ name: "",
388
+ type: itemsProperty.type,
389
+ properties: itemsProperty.properties || [],
390
+ description: itemsProperty.description || "",
391
+ meta: itemsProperty.meta || [],
392
+ ofProperty: itemsProperty.ofProperty || void 0
393
+ };
394
+ } else {
395
+ arrayProperty.properties = [itemsProperty];
396
+ }
397
+ }
398
+ if (refPath) {
399
+ visitedRefs.set(refPath, arrayProperty);
400
+ }
401
+ return arrayProperty;
402
+ }
403
+ }
404
+ if (arrayOf) {
405
+ const prop = {
406
+ type: property.type,
407
+ name: "",
408
+ description: "",
409
+ ofProperty: property
410
+ };
411
+ if (refPath) {
412
+ visitedRefs.set(refPath, prop);
413
+ }
414
+ }
415
+ if (refPath) {
416
+ visitedRefs.set(refPath, property);
417
+ }
418
+ return property;
419
+ }
420
+ function schemaObjectToUniformDefinitionPropertyMeta(objProp, name) {
421
+ const meta = [];
422
+ if (!objProp) {
423
+ return meta;
424
+ }
425
+ if (typeof objProp.required === "boolean" && objProp.required) {
426
+ meta.push({
427
+ name: "required",
428
+ value: "true"
429
+ });
430
+ } else if (Array.isArray(objProp.required)) {
431
+ for (const req of objProp.required) {
432
+ if (req === name) {
433
+ meta.push({
434
+ name: "required",
435
+ value: "true"
436
+ });
437
+ }
438
+ }
439
+ }
440
+ if (objProp.deprecated) {
441
+ meta.push({
442
+ name: "deprecated",
443
+ value: "true"
444
+ });
445
+ }
446
+ if ("default" in objProp) {
447
+ meta.push({
448
+ name: "defaults",
449
+ value: objProp.default
450
+ });
451
+ }
452
+ if ("nullable" in objProp) {
453
+ meta.push({
454
+ name: "nullable",
455
+ value: "true"
456
+ });
457
+ }
458
+ if ("example" in objProp) {
459
+ const example = typeof objProp.example === "object" ? JSON.stringify(objProp.example) : objProp.example;
460
+ meta.push({
461
+ name: "example",
462
+ value: example
463
+ });
464
+ }
465
+ if ("examples" in objProp) {
466
+ meta.push({
467
+ name: "examples",
468
+ value: objProp.examples
469
+ });
470
+ }
471
+ if ("maximum" in objProp) {
472
+ meta.push({
473
+ name: "maximum",
474
+ value: objProp.maximum
475
+ });
476
+ }
477
+ if ("minimum" in objProp) {
478
+ meta.push({
479
+ name: "minimum",
480
+ value: objProp.minimum
481
+ });
482
+ }
483
+ return meta;
484
+ }
485
+ function isMergeType(type) {
486
+ return type === DEFINED_DEFINITION_PROPERTY_TYPE.XOR || type === DEFINED_DEFINITION_PROPERTY_TYPE.UNION;
487
+ }
488
+ function isOfType(type) {
489
+ return type === DEFINED_DEFINITION_PROPERTY_TYPE.XOR || type === DEFINED_DEFINITION_PROPERTY_TYPE.UNION || type === DEFINED_DEFINITION_PROPERTY_TYPE.ARRAY;
490
+ }
491
+
492
+ // src/converters/oas-parameters.ts
493
+ function oapParametersToDefinitionProperties(parameters) {
494
+ const parameterIn = {};
495
+ parameters.forEach((param) => {
496
+ if (!parameterIn[param.in]) {
497
+ parameterIn[param.in] = [];
498
+ }
499
+ const schema = param.schema;
500
+ const meta = [
501
+ ...schemaObjectToUniformDefinitionPropertyMeta(schema, param.name) || [],
502
+ ...schemaObjectToUniformDefinitionPropertyMeta(param, param.name) || []
503
+ ];
504
+ let oapV2Type = "";
505
+ if ("type" in param) {
506
+ oapV2Type = param.type;
507
+ }
508
+ const property = {
509
+ name: param.name,
510
+ type: (schema == null ? void 0 : schema.type) || oapV2Type || "",
511
+ description: param.description || "",
512
+ meta
513
+ };
514
+ parameterIn[param.in].push(property);
515
+ });
516
+ return parameterIn;
517
+ }
518
+
519
+ // src/converters/oas-paths.ts
520
+ import {
521
+ ReferenceCategory
522
+ } from "@xyd-js/uniform";
523
+
524
+ // src/converters/oas-requestBody.ts
525
+ import { DEFINED_DEFINITION_PROPERTY_TYPE as DEFINED_DEFINITION_PROPERTY_TYPE2 } from "@xyd-js/uniform";
526
+ function oapRequestBodyToDefinitionProperties(reqBody, contentType) {
527
+ const schema = reqBody.content[contentType].schema;
528
+ if (!schema) {
529
+ return null;
530
+ }
531
+ let schemaObject;
532
+ if (schema.allOf || schema.anyOf || schema.oneOf) {
533
+ return schemaObjectToUniformDefinitionProperties(schema);
534
+ }
535
+ let array = false;
536
+ switch (schema.type) {
537
+ case "object": {
538
+ schemaObject = schema;
539
+ break;
540
+ }
541
+ case "array": {
542
+ const arrSchema = schema;
543
+ const items = arrSchema.items;
544
+ schemaObject = items;
545
+ array = true;
546
+ break;
547
+ }
548
+ default:
549
+ break;
550
+ }
551
+ if (!schemaObject) {
552
+ return null;
553
+ }
554
+ const properties = schemaObjectToUniformDefinitionProperties(schemaObject);
555
+ if (array) {
556
+ return {
557
+ type: DEFINED_DEFINITION_PROPERTY_TYPE2.ARRAY,
558
+ properties
559
+ };
560
+ }
561
+ return properties;
562
+ }
563
+
564
+ // src/converters/oas-responses.ts
565
+ import { DEFINED_DEFINITION_PROPERTY_TYPE as DEFINED_DEFINITION_PROPERTY_TYPE3 } from "@xyd-js/uniform";
566
+ function oasResponseToDefinitionProperties(responses, code, contentType) {
567
+ var _a;
568
+ let schemaObject;
569
+ let responseObject;
570
+ if (responses[code]) {
571
+ responseObject = responses[code];
572
+ if (!(responseObject == null ? void 0 : responseObject.content)) {
573
+ return null;
574
+ }
575
+ schemaObject = (_a = responseObject == null ? void 0 : responseObject.content[contentType]) == null ? void 0 : _a.schema;
576
+ }
577
+ if (!schemaObject) {
578
+ return {
579
+ properties: [
580
+ {
581
+ description: (responseObject == null ? void 0 : responseObject.description) || "",
582
+ name: "",
583
+ type: ""
584
+ }
585
+ ]
586
+ };
587
+ }
588
+ let array = false;
589
+ switch (schemaObject.type) {
590
+ case "array":
591
+ const arrSchema = schemaObject;
592
+ const items = arrSchema.items;
593
+ schemaObject = items;
594
+ array = true;
595
+ default:
596
+ break;
597
+ }
598
+ const properties = schemaObjectToUniformDefinitionProperties(schemaObject, true);
599
+ let description = "";
600
+ if (schemaObject.allOf) {
601
+ for (const item of schemaObject.allOf) {
602
+ if ("description" in item) {
603
+ description += item.description + "\n";
604
+ }
605
+ }
606
+ }
607
+ if (array) {
608
+ return {
609
+ properties: {
610
+ type: DEFINED_DEFINITION_PROPERTY_TYPE3.ARRAY,
611
+ properties
612
+ }
613
+ };
614
+ }
615
+ return {
616
+ properties: properties || [],
617
+ description: description || ""
618
+ };
619
+ }
620
+
621
+ // src/utils.ts
622
+ import path from "path";
623
+ import fs from "fs/promises";
624
+ import yaml from "js-yaml";
625
+
626
+ // ../../node_modules/.pnpm/github-slugger@2.0.0/node_modules/github-slugger/regex.js
627
+ var regex = /[\0-\x1F!-,\.\/:-@\[-\^`\{-\xA9\xAB-\xB4\xB6-\xB9\xBB-\xBF\xD7\xF7\u02C2-\u02C5\u02D2-\u02DF\u02E5-\u02EB\u02ED\u02EF-\u02FF\u0375\u0378\u0379\u037E\u0380-\u0385\u0387\u038B\u038D\u03A2\u03F6\u0482\u0530\u0557\u0558\u055A-\u055F\u0589-\u0590\u05BE\u05C0\u05C3\u05C6\u05C8-\u05CF\u05EB-\u05EE\u05F3-\u060F\u061B-\u061F\u066A-\u066D\u06D4\u06DD\u06DE\u06E9\u06FD\u06FE\u0700-\u070F\u074B\u074C\u07B2-\u07BF\u07F6-\u07F9\u07FB\u07FC\u07FE\u07FF\u082E-\u083F\u085C-\u085F\u086B-\u089F\u08B5\u08C8-\u08D2\u08E2\u0964\u0965\u0970\u0984\u098D\u098E\u0991\u0992\u09A9\u09B1\u09B3-\u09B5\u09BA\u09BB\u09C5\u09C6\u09C9\u09CA\u09CF-\u09D6\u09D8-\u09DB\u09DE\u09E4\u09E5\u09F2-\u09FB\u09FD\u09FF\u0A00\u0A04\u0A0B-\u0A0E\u0A11\u0A12\u0A29\u0A31\u0A34\u0A37\u0A3A\u0A3B\u0A3D\u0A43-\u0A46\u0A49\u0A4A\u0A4E-\u0A50\u0A52-\u0A58\u0A5D\u0A5F-\u0A65\u0A76-\u0A80\u0A84\u0A8E\u0A92\u0AA9\u0AB1\u0AB4\u0ABA\u0ABB\u0AC6\u0ACA\u0ACE\u0ACF\u0AD1-\u0ADF\u0AE4\u0AE5\u0AF0-\u0AF8\u0B00\u0B04\u0B0D\u0B0E\u0B11\u0B12\u0B29\u0B31\u0B34\u0B3A\u0B3B\u0B45\u0B46\u0B49\u0B4A\u0B4E-\u0B54\u0B58-\u0B5B\u0B5E\u0B64\u0B65\u0B70\u0B72-\u0B81\u0B84\u0B8B-\u0B8D\u0B91\u0B96-\u0B98\u0B9B\u0B9D\u0BA0-\u0BA2\u0BA5-\u0BA7\u0BAB-\u0BAD\u0BBA-\u0BBD\u0BC3-\u0BC5\u0BC9\u0BCE\u0BCF\u0BD1-\u0BD6\u0BD8-\u0BE5\u0BF0-\u0BFF\u0C0D\u0C11\u0C29\u0C3A-\u0C3C\u0C45\u0C49\u0C4E-\u0C54\u0C57\u0C5B-\u0C5F\u0C64\u0C65\u0C70-\u0C7F\u0C84\u0C8D\u0C91\u0CA9\u0CB4\u0CBA\u0CBB\u0CC5\u0CC9\u0CCE-\u0CD4\u0CD7-\u0CDD\u0CDF\u0CE4\u0CE5\u0CF0\u0CF3-\u0CFF\u0D0D\u0D11\u0D45\u0D49\u0D4F-\u0D53\u0D58-\u0D5E\u0D64\u0D65\u0D70-\u0D79\u0D80\u0D84\u0D97-\u0D99\u0DB2\u0DBC\u0DBE\u0DBF\u0DC7-\u0DC9\u0DCB-\u0DCE\u0DD5\u0DD7\u0DE0-\u0DE5\u0DF0\u0DF1\u0DF4-\u0E00\u0E3B-\u0E3F\u0E4F\u0E5A-\u0E80\u0E83\u0E85\u0E8B\u0EA4\u0EA6\u0EBE\u0EBF\u0EC5\u0EC7\u0ECE\u0ECF\u0EDA\u0EDB\u0EE0-\u0EFF\u0F01-\u0F17\u0F1A-\u0F1F\u0F2A-\u0F34\u0F36\u0F38\u0F3A-\u0F3D\u0F48\u0F6D-\u0F70\u0F85\u0F98\u0FBD-\u0FC5\u0FC7-\u0FFF\u104A-\u104F\u109E\u109F\u10C6\u10C8-\u10CC\u10CE\u10CF\u10FB\u1249\u124E\u124F\u1257\u1259\u125E\u125F\u1289\u128E\u128F\u12B1\u12B6\u12B7\u12BF\u12C1\u12C6\u12C7\u12D7\u1311\u1316\u1317\u135B\u135C\u1360-\u137F\u1390-\u139F\u13F6\u13F7\u13FE-\u1400\u166D\u166E\u1680\u169B-\u169F\u16EB-\u16ED\u16F9-\u16FF\u170D\u1715-\u171F\u1735-\u173F\u1754-\u175F\u176D\u1771\u1774-\u177F\u17D4-\u17D6\u17D8-\u17DB\u17DE\u17DF\u17EA-\u180A\u180E\u180F\u181A-\u181F\u1879-\u187F\u18AB-\u18AF\u18F6-\u18FF\u191F\u192C-\u192F\u193C-\u1945\u196E\u196F\u1975-\u197F\u19AC-\u19AF\u19CA-\u19CF\u19DA-\u19FF\u1A1C-\u1A1F\u1A5F\u1A7D\u1A7E\u1A8A-\u1A8F\u1A9A-\u1AA6\u1AA8-\u1AAF\u1AC1-\u1AFF\u1B4C-\u1B4F\u1B5A-\u1B6A\u1B74-\u1B7F\u1BF4-\u1BFF\u1C38-\u1C3F\u1C4A-\u1C4C\u1C7E\u1C7F\u1C89-\u1C8F\u1CBB\u1CBC\u1CC0-\u1CCF\u1CD3\u1CFB-\u1CFF\u1DFA\u1F16\u1F17\u1F1E\u1F1F\u1F46\u1F47\u1F4E\u1F4F\u1F58\u1F5A\u1F5C\u1F5E\u1F7E\u1F7F\u1FB5\u1FBD\u1FBF-\u1FC1\u1FC5\u1FCD-\u1FCF\u1FD4\u1FD5\u1FDC-\u1FDF\u1FED-\u1FF1\u1FF5\u1FFD-\u203E\u2041-\u2053\u2055-\u2070\u2072-\u207E\u2080-\u208F\u209D-\u20CF\u20F1-\u2101\u2103-\u2106\u2108\u2109\u2114\u2116-\u2118\u211E-\u2123\u2125\u2127\u2129\u212E\u213A\u213B\u2140-\u2144\u214A-\u214D\u214F-\u215F\u2189-\u24B5\u24EA-\u2BFF\u2C2F\u2C5F\u2CE5-\u2CEA\u2CF4-\u2CFF\u2D26\u2D28-\u2D2C\u2D2E\u2D2F\u2D68-\u2D6E\u2D70-\u2D7E\u2D97-\u2D9F\u2DA7\u2DAF\u2DB7\u2DBF\u2DC7\u2DCF\u2DD7\u2DDF\u2E00-\u2E2E\u2E30-\u3004\u3008-\u3020\u3030\u3036\u3037\u303D-\u3040\u3097\u3098\u309B\u309C\u30A0\u30FB\u3100-\u3104\u3130\u318F-\u319F\u31C0-\u31EF\u3200-\u33FF\u4DC0-\u4DFF\u9FFD-\u9FFF\uA48D-\uA4CF\uA4FE\uA4FF\uA60D-\uA60F\uA62C-\uA63F\uA673\uA67E\uA6F2-\uA716\uA720\uA721\uA789\uA78A\uA7C0\uA7C1\uA7CB-\uA7F4\uA828-\uA82B\uA82D-\uA83F\uA874-\uA87F\uA8C6-\uA8CF\uA8DA-\uA8DF\uA8F8-\uA8FA\uA8FC\uA92E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DB40[\uDC00-\uDCFF\uDDF0-\uDFFF]/g;
628
+
629
+ // ../../node_modules/.pnpm/github-slugger@2.0.0/node_modules/github-slugger/index.js
630
+ var own = Object.hasOwnProperty;
631
+ var BananaSlug = class {
632
+ /**
633
+ * Create a new slug class.
634
+ */
635
+ constructor() {
636
+ this.occurrences;
637
+ this.reset();
638
+ }
639
+ /**
640
+ * Generate a unique slug.
641
+ *
642
+ * Tracks previously generated slugs: repeated calls with the same value
643
+ * will result in different slugs.
644
+ * Use the `slug` function to get same slugs.
645
+ *
646
+ * @param {string} value
647
+ * String of text to slugify
648
+ * @param {boolean} [maintainCase=false]
649
+ * Keep the current case, otherwise make all lowercase
650
+ * @return {string}
651
+ * A unique slug string
652
+ */
653
+ slug(value, maintainCase) {
654
+ const self = this;
655
+ let result = slug(value, maintainCase === true);
656
+ const originalSlug = result;
657
+ while (own.call(self.occurrences, result)) {
658
+ self.occurrences[originalSlug]++;
659
+ result = originalSlug + "-" + self.occurrences[originalSlug];
660
+ }
661
+ self.occurrences[result] = 0;
662
+ return result;
663
+ }
664
+ /**
665
+ * Reset - Forget all previous slugs
666
+ *
667
+ * @return void
668
+ */
669
+ reset() {
670
+ this.occurrences = /* @__PURE__ */ Object.create(null);
671
+ }
672
+ };
673
+ function slug(value, maintainCase) {
674
+ if (typeof value !== "string") return "";
675
+ if (!maintainCase) value = value.toLowerCase();
676
+ return value.replace(regex, "").replace(/ /g, "-");
677
+ }
678
+
679
+ // src/utils.ts
680
+ import $refParser from "@apidevtools/json-schema-ref-parser";
681
+ import { ReferenceType } from "@xyd-js/uniform";
682
+ function slug2(str) {
683
+ const slugger = new BananaSlug();
684
+ return slugger.slug(str);
685
+ }
686
+ async function deferencedOpenAPI(openApiPath) {
687
+ const openApiSpec = await readOpenApiSpec(openApiPath);
688
+ if (!openApiSpec) {
689
+ return;
690
+ }
691
+ const cwd = process.cwd();
692
+ const remoteOasPath = openApiPath.startsWith("http://") || openApiPath.startsWith("https://") ? true : false;
693
+ const circularRefs = /* @__PURE__ */ new Set();
694
+ const options = {
695
+ dereference: {
696
+ onCircular(defPath) {
697
+ const v = `#${defPath.split("#")[1]}`;
698
+ circularRefs.add(v);
699
+ },
700
+ // circular: true, // ignore circular references,
701
+ // onCi
702
+ onDereference(defPath, value, parent) {
703
+ if (circularRefs.has(defPath) || circularRefs.has(path.join(defPath, "properties"))) {
704
+ value.__UNSAFE_circular = true;
705
+ }
706
+ if (value && typeof value === "object") {
707
+ value.__UNSAFE_refPath = () => defPath;
708
+ }
709
+ if (parent && typeof parent === "object") {
710
+ parent.__UNSAFE_refPath = () => defPath;
711
+ }
712
+ }
713
+ }
714
+ };
715
+ if (remoteOasPath) {
716
+ if (!options.resolve) {
717
+ options.resolve = {};
718
+ }
719
+ options.resolve.file = {
720
+ read: async (file) => {
721
+ let rel = path.relative(cwd, file.url);
722
+ rel = rel.split(path.sep).join("/");
723
+ const absoluteUrl = new URL(rel, openApiPath).href;
724
+ const res = await fetch(absoluteUrl);
725
+ if (!res.ok) {
726
+ throw new Error(`Failed to fetch ${absoluteUrl}: ${res.status}`);
727
+ }
728
+ let content;
729
+ if (file.extension === ".json" || file.extension === ".yaml" || file.extension === ".yml") {
730
+ if (file.extension === ".json") {
731
+ content = await res.json();
732
+ } else {
733
+ content = yaml.load(await res.text());
734
+ }
735
+ } else {
736
+ content = await res.text();
737
+ }
738
+ return content;
739
+ }
740
+ };
741
+ }
742
+ await $refParser.dereference(openApiSpec, options);
743
+ return openApiSpec;
744
+ }
745
+ async function readOpenApiSpec(filePath) {
746
+ let content;
747
+ let fromUrl = false;
748
+ if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
749
+ const response = await fetch(filePath);
750
+ if (!response.ok) {
751
+ throw new Error(`Failed to fetch OpenAPI spec from URL: ${response.statusText}`);
752
+ }
753
+ content = await response.text();
754
+ fromUrl = true;
755
+ } else {
756
+ try {
757
+ await fs.access(filePath);
758
+ } catch (error) {
759
+ console.log(`\u26A0\uFE0F "(openapi): ${filePath}" is defined in the docs.json navigation but the file does not exist.`);
760
+ return;
761
+ }
762
+ content = await fs.readFile(filePath, "utf-8");
763
+ }
764
+ const ext = path.extname(filePath).toLowerCase();
765
+ if (ext === ".yaml" || ext === ".yml") {
766
+ return yaml.load(content);
767
+ } else if (ext === ".json") {
768
+ return JSON.parse(content);
769
+ } else if (fromUrl && content.startsWith("{")) {
770
+ try {
771
+ return JSON.parse(content);
772
+ } catch (error) {
773
+ throw new Error(`Failed to parse JSON from URL: ${error}`);
774
+ }
775
+ } else {
776
+ throw new Error("Unsupported file format. Use JSON or YAML.");
777
+ }
778
+ }
779
+ function httpMethodToUniformMethod(method) {
780
+ switch (method) {
781
+ case "get":
782
+ return ReferenceType.REST_HTTP_GET;
783
+ case "put":
784
+ return ReferenceType.REST_HTTP_PUT;
785
+ case "patch":
786
+ return ReferenceType.REST_HTTP_PATCH;
787
+ case "post":
788
+ return ReferenceType.REST_HTTP_POST;
789
+ case "delete":
790
+ return ReferenceType.REST_HTTP_DELETE;
791
+ case "options":
792
+ return ReferenceType.REST_HTTP_OPTIONS;
793
+ case "head":
794
+ return ReferenceType.REST_HTTP_HEAD;
795
+ case "trace":
796
+ return ReferenceType.REST_HTTP_TRACE;
797
+ default:
798
+ return null;
799
+ }
800
+ }
801
+
802
+ // src/converters/oas-paths.ts
803
+ import path2 from "path";
804
+ function oapPathToReference(schema, httpMethod, path4, oapPath) {
805
+ const mType = httpMethodToUniformMethod(httpMethod);
806
+ if (!mType) {
807
+ console.error(`Unsupported method: ${httpMethod}`);
808
+ return null;
809
+ }
810
+ const definitions = [];
811
+ const exampleGroups = [];
812
+ const oapMethod = oapPath == null ? void 0 : oapPath[httpMethod];
813
+ if (!oapMethod) {
814
+ return null;
815
+ }
816
+ const tag = getFirstTag(oapMethod);
817
+ const group = [tag];
818
+ const endpointRef = {
819
+ title: title(oapMethod, httpMethod, path4),
820
+ canonical: canonical(oapMethod, httpMethod, path4),
821
+ description: (oapMethod == null ? void 0 : oapMethod.description) || (oapMethod == null ? void 0 : oapMethod.summary),
822
+ type: mType,
823
+ category: ReferenceCategory.REST,
824
+ context: {
825
+ method: httpMethod,
826
+ path: `${encodeURIComponent(path4)}`,
827
+ fullPath: path4,
828
+ group
829
+ },
830
+ examples: {
831
+ groups: exampleGroups
832
+ },
833
+ definitions
834
+ };
835
+ if (oapMethod.parameters) {
836
+ const parameters = oapMethod.parameters;
837
+ const paramtersMap = oapParametersToDefinitionProperties(parameters);
838
+ Object.entries(paramtersMap).forEach(([key, definitionProperties]) => {
839
+ let title2;
840
+ switch (key) {
841
+ case "path":
842
+ title2 = "Path parameters";
843
+ break;
844
+ case "query":
845
+ title2 = "Query parameters";
846
+ break;
847
+ case "header":
848
+ title2 = "Headers";
849
+ break;
850
+ default:
851
+ console.error(`Unsupported parameter type: ${key} for ${httpMethod} ${path4}`);
852
+ return;
853
+ }
854
+ definitions.push({
855
+ title: title2,
856
+ properties: definitionProperties
857
+ });
858
+ });
859
+ }
860
+ definitions.push(...oapOperationToDefinitions(oapMethod));
861
+ endpointRef.__UNSAFE_selector = function __UNSAFE_selector(selector) {
862
+ switch (selector) {
863
+ case "[schema]": {
864
+ return schema;
865
+ }
866
+ case "[method]": {
867
+ return {
868
+ oapPath,
869
+ httpMethod,
870
+ path: path4
871
+ };
872
+ }
873
+ case "[method] [path]": {
874
+ return oapMethod;
875
+ }
876
+ default:
877
+ return null;
878
+ }
879
+ };
880
+ return endpointRef;
881
+ }
882
+ function oapOperationToDefinitions(oapMethod) {
883
+ const definitions = [];
884
+ if (oapMethod.requestBody) {
885
+ const definition = oapRequestOperationToUniformDefinition(oapMethod);
886
+ definitions.push(definition);
887
+ }
888
+ if (oapMethod.responses) {
889
+ const definition = oapResponseOperationToUniformDefinition(oapMethod);
890
+ definitions.push(definition);
891
+ }
892
+ return definitions;
893
+ }
894
+ function oapRequestOperationToUniformDefinition(oapOperation) {
895
+ var _a;
896
+ const reqBody = oapOperation.requestBody;
897
+ const variants = [];
898
+ for (const contentType of Object.keys(reqBody.content)) {
899
+ const schema = (_a = reqBody.content[contentType]) == null ? void 0 : _a.schema;
900
+ let properties = [];
901
+ let rootProperty;
902
+ let propertiesResp = oapRequestBodyToDefinitionProperties(reqBody, contentType) || [];
903
+ if (Array.isArray(propertiesResp)) {
904
+ properties = propertiesResp;
905
+ } else {
906
+ rootProperty = propertiesResp;
907
+ }
908
+ const meta2 = [
909
+ {
910
+ name: "contentType",
911
+ value: contentType || ""
912
+ }
913
+ ];
914
+ if (schema == null ? void 0 : schema.required) {
915
+ meta2.push({
916
+ name: "required",
917
+ value: schema.required ? "true" : "false"
918
+ });
919
+ }
920
+ variants.push({
921
+ title: contentType,
922
+ description: schema.description || "",
923
+ properties,
924
+ rootProperty,
925
+ meta: meta2,
926
+ symbolDef: definitionPropertyTypeDef(schema)
927
+ });
928
+ }
929
+ const meta = [];
930
+ if (reqBody.required) {
931
+ meta.push({
932
+ name: "required",
933
+ value: "true"
934
+ });
935
+ }
936
+ return {
937
+ title: "Request body",
938
+ variants,
939
+ properties: [],
940
+ meta
941
+ };
942
+ }
943
+ function oapResponseOperationToUniformDefinition(oapOperation) {
944
+ const responses = oapOperation.responses;
945
+ const variants = [];
946
+ Object.keys(responses).forEach((code) => {
947
+ var _a;
948
+ const responseObject = responses[code];
949
+ if (!(responseObject == null ? void 0 : responseObject.content)) {
950
+ variants.push({
951
+ title: code,
952
+ description: responseObject.description,
953
+ properties: [],
954
+ meta: [
955
+ {
956
+ name: "status",
957
+ value: code || ""
958
+ }
959
+ ]
960
+ });
961
+ return null;
962
+ }
963
+ const contentTypes = Object.keys(responseObject.content);
964
+ for (const contentType of contentTypes) {
965
+ let properties = [];
966
+ let rootProperty;
967
+ const schema = (_a = responseObject.content[contentType]) == null ? void 0 : _a.schema;
968
+ const respProperties = oasResponseToDefinitionProperties(responses, code, contentType) || [];
969
+ if (respProperties && "properties" in respProperties && (respProperties == null ? void 0 : respProperties.properties)) {
970
+ if (Array.isArray(respProperties.properties)) {
971
+ properties = respProperties.properties;
972
+ } else {
973
+ rootProperty = respProperties.properties;
974
+ }
975
+ }
976
+ let definitionDescription = "";
977
+ if ("description" in respProperties) {
978
+ definitionDescription = respProperties.description || "";
979
+ }
980
+ variants.push({
981
+ title: code,
982
+ description: responseObject.description,
983
+ properties,
984
+ rootProperty,
985
+ meta: [
986
+ {
987
+ name: "status",
988
+ value: code || ""
989
+ },
990
+ {
991
+ name: "contentType",
992
+ value: contentType || ""
993
+ },
994
+ {
995
+ name: "definitionDescription",
996
+ value: definitionDescription
997
+ }
998
+ ],
999
+ symbolDef: definitionPropertyTypeDef(schema)
1000
+ });
1001
+ }
1002
+ });
1003
+ return {
1004
+ title: "Response",
1005
+ variants,
1006
+ properties: []
1007
+ };
1008
+ }
1009
+ function definitionPropertyTypeDef(schema) {
1010
+ if (!schema) {
1011
+ return;
1012
+ }
1013
+ let typeDef;
1014
+ let oasSchema = schema;
1015
+ if (oasSchema.type === "array") {
1016
+ oasSchema = oasSchema.items;
1017
+ }
1018
+ if (oasSchema == null ? void 0 : oasSchema.__internal_getRefPath) {
1019
+ const symbolId = oasSchema.__internal_getRefPath();
1020
+ typeDef = {
1021
+ id: symbolId
1022
+ };
1023
+ }
1024
+ return typeDef;
1025
+ }
1026
+ function title(oapMethod, httpMethod, httpPath) {
1027
+ const tit = (oapMethod == null ? void 0 : oapMethod.summary) || oapMethod.operationId || "";
1028
+ if (tit) {
1029
+ return tit;
1030
+ }
1031
+ if (!httpMethod || !httpPath) {
1032
+ throw new Error("httpMethod and path are required to generate title");
1033
+ }
1034
+ return path2.join(httpMethod, cleanPath(httpPath));
1035
+ }
1036
+ function canonical(oapMethod, httpMethod, httpPath) {
1037
+ let canon = oapMethod.operationId || slug2((oapMethod == null ? void 0 : oapMethod.summary) || "");
1038
+ if (canon) {
1039
+ return canon;
1040
+ }
1041
+ if (!httpMethod || !httpPath) {
1042
+ throw new Error("httpMethod and path are required to generate canonical");
1043
+ }
1044
+ return path2.join(httpMethod, cleanPath(httpPath));
1045
+ }
1046
+ function getFirstTag(oapMethod) {
1047
+ for (const tag of (oapMethod == null ? void 0 : oapMethod.tags) || []) {
1048
+ return tag;
1049
+ }
1050
+ return "";
1051
+ }
1052
+ function cleanPath(httpPath) {
1053
+ return httpPath.replace(/\{([^}]+)\}/g, "$1");
1054
+ }
1055
+
1056
+ // src/converters/oas-schema.ts
1057
+ import path3 from "path";
1058
+ import Oas from "oas";
1059
+
1060
+ // src/converters/oas-examples.ts
1061
+ import oasToSnippet from "@readme/oas-to-snippet";
1062
+ import { sample as openApiSampler } from "@xyd-js/openapi-sampler";
1063
+
1064
+ // src/xdocs/index.ts
1065
+ function xDocsLanguages(oasDoc) {
1066
+ const xDocs = getXDocs(oasDoc);
1067
+ if (!xDocs) {
1068
+ return null;
1069
+ }
1070
+ return (xDocs == null ? void 0 : xDocs.codeLanguages) ?? null;
1071
+ }
1072
+ function getXDocs(oasDoc) {
1073
+ if (!("x-docs" in oasDoc)) {
1074
+ return null;
1075
+ }
1076
+ return oasDoc["x-docs"];
1077
+ }
1078
+
1079
+ // src/converters/oas-examples.ts
1080
+ var DEFAULT_CODE_LANGUAGES = ["shell", "javascript", "python", "go"];
1081
+ function oapExamples(oas, operation, visitedExamples) {
1082
+ const exampleGroups = [
1083
+ ...reqExamples(operation, oas, visitedExamples),
1084
+ ...resBodyExmaples(operation, oas, visitedExamples)
1085
+ ];
1086
+ return exampleGroups;
1087
+ }
1088
+ function langFallback(lang) {
1089
+ const langLower = lang.toLowerCase();
1090
+ switch (langLower) {
1091
+ case "curl": {
1092
+ return "shell";
1093
+ }
1094
+ }
1095
+ return langLower;
1096
+ }
1097
+ function reqExamples(operation, oas, vistedExamples) {
1098
+ const exampleGroups = [];
1099
+ const examples = [];
1100
+ const tabs = [];
1101
+ if (operation.schema["x-codeSamples"]) {
1102
+ const codeSamples = operation.schema["x-codeSamples"];
1103
+ const groupedByLabel = /* @__PURE__ */ new Map();
1104
+ codeSamples.forEach((sample) => {
1105
+ const label = sample.label || "default";
1106
+ if (!groupedByLabel.has(label)) {
1107
+ groupedByLabel.set(label, []);
1108
+ }
1109
+ groupedByLabel.get(label).push(sample);
1110
+ });
1111
+ groupedByLabel.forEach((samples, label) => {
1112
+ const codeSampleTabs = samples.map((sample) => ({
1113
+ title: sample.lang,
1114
+ language: langFallback(sample.lang),
1115
+ code: sample.source
1116
+ }));
1117
+ if (codeSampleTabs.length > 0) {
1118
+ examples.push({
1119
+ codeblock: {
1120
+ title: label === "default" ? "Example request" : label,
1121
+ tabs: codeSampleTabs
1122
+ }
1123
+ });
1124
+ }
1125
+ });
1126
+ if (examples.length > 0) {
1127
+ exampleGroups.push({
1128
+ description: "Example request",
1129
+ examples
1130
+ });
1131
+ return exampleGroups;
1132
+ }
1133
+ }
1134
+ const paramData = operation.schema.parameters ? operation.schema.parameters.reduce((acc, param) => {
1135
+ const location = param.in || "query";
1136
+ if (!acc[location]) {
1137
+ acc[location] = {};
1138
+ }
1139
+ let value = param.example;
1140
+ if (!value && param.schema) {
1141
+ value = openApiSampler(sanitizeSchema(param.schema));
1142
+ }
1143
+ if (value !== void 0) {
1144
+ acc[location][param.name] = value;
1145
+ }
1146
+ return acc;
1147
+ }, {}) : {};
1148
+ let bodyData = {};
1149
+ if (operation.schema.requestBody) {
1150
+ const body = operation.schema.requestBody;
1151
+ const contentTypes = Object.keys(body.content);
1152
+ if (contentTypes.length > 0) {
1153
+ const contentType = contentTypes[contentTypes.length - 1];
1154
+ const content = body.content[contentType];
1155
+ let schema = content == null ? void 0 : content.schema;
1156
+ if (schema) {
1157
+ schema = fixAllOfBug(schema);
1158
+ schema = sanitizeSchema(schema);
1159
+ let requestData;
1160
+ if (content.examples) {
1161
+ const requestExample = content.examples["request"];
1162
+ if (requestExample && "value" in requestExample) {
1163
+ requestData = requestExample.value;
1164
+ }
1165
+ }
1166
+ if (!requestData) {
1167
+ requestData = sampleFromSchema(schema);
1168
+ }
1169
+ if (contentType === "application/x-www-form-urlencoded") {
1170
+ bodyData = { formData: requestData };
1171
+ } else {
1172
+ bodyData = { body: requestData };
1173
+ }
1174
+ }
1175
+ }
1176
+ }
1177
+ const hasRequestBody = operation.schema.requestBody !== void 0;
1178
+ const hasParameters = Object.keys(paramData).length > 0;
1179
+ if (hasParameters || hasRequestBody || !hasRequestBody && !hasParameters) {
1180
+ const langs = xDocsLanguages(operation.api) || DEFAULT_CODE_LANGUAGES;
1181
+ langs.forEach((lang) => {
1182
+ var _a, _b, _c;
1183
+ let snippetOperation = operation;
1184
+ const v = (_c = (_b = (_a = operation == null ? void 0 : operation.api) == null ? void 0 : _a.paths) == null ? void 0 : _b[operation == null ? void 0 : operation.path]) == null ? void 0 : _c[operation == null ? void 0 : operation.method];
1185
+ if (v) {
1186
+ snippetOperation = fixCircularReferences(v);
1187
+ }
1188
+ const { code } = oasToSnippet(oas, snippetOperation, {
1189
+ ...paramData,
1190
+ ...bodyData
1191
+ }, null, lang);
1192
+ tabs.push({
1193
+ title: lang,
1194
+ language: lang,
1195
+ code: code || ""
1196
+ });
1197
+ });
1198
+ if (tabs.length > 0) {
1199
+ examples.push({
1200
+ codeblock: {
1201
+ tabs
1202
+ }
1203
+ });
1204
+ }
1205
+ if (examples.length > 0) {
1206
+ exampleGroups.push({
1207
+ description: "Example request",
1208
+ examples
1209
+ });
1210
+ }
1211
+ }
1212
+ return exampleGroups;
1213
+ }
1214
+ function resBodyExmaples(operation, oas, vistedExamples) {
1215
+ const exampleGroups = [];
1216
+ if (operation.schema.responses) {
1217
+ const responses = operation.schema.responses;
1218
+ const examples = [];
1219
+ Object.entries(responses).forEach(([status, r]) => {
1220
+ const response = r;
1221
+ if (!response.content) {
1222
+ return;
1223
+ }
1224
+ const contentTypes = Object.keys(response.content);
1225
+ if (contentTypes.length === 0) {
1226
+ return;
1227
+ }
1228
+ const tabs = [];
1229
+ for (const contentType of contentTypes) {
1230
+ const content = response.content[contentType];
1231
+ const schema = content == null ? void 0 : content.schema;
1232
+ if (!schema) {
1233
+ continue;
1234
+ }
1235
+ let responseData;
1236
+ if (content.examples) {
1237
+ const responseExample = content.examples["response"];
1238
+ if (responseExample && "value" in responseExample) {
1239
+ responseData = responseExample.value;
1240
+ } else {
1241
+ const namedExamples = [];
1242
+ const exampleNames = Object.keys(content.examples);
1243
+ exampleNames.forEach((exampleName) => {
1244
+ var _a;
1245
+ const data = (_a = content == null ? void 0 : content.examples) == null ? void 0 : _a[exampleName];
1246
+ if (!data || !("value" in data) || typeof data.value != "object") {
1247
+ return;
1248
+ }
1249
+ namedExamples.push({
1250
+ description: "",
1251
+ codeblock: {
1252
+ title: exampleName,
1253
+ tabs: [
1254
+ {
1255
+ title: "application/json",
1256
+ // TODO: support multiple types
1257
+ language: "json",
1258
+ code: JSON.stringify(data.value, null, 2) || ""
1259
+ }
1260
+ ]
1261
+ }
1262
+ });
1263
+ });
1264
+ if (namedExamples.length === 1) {
1265
+ const firstCodeblock = namedExamples[0].codeblock;
1266
+ tabs.push(
1267
+ ...firstCodeblock.tabs.map((tab) => ({
1268
+ ...tab,
1269
+ title: contentType
1270
+ }))
1271
+ );
1272
+ } else {
1273
+ exampleGroups.push({
1274
+ description: "",
1275
+ examples: namedExamples
1276
+ });
1277
+ }
1278
+ continue;
1279
+ }
1280
+ } else if (content.example) {
1281
+ responseData = content.example;
1282
+ }
1283
+ if (!responseData) {
1284
+ responseData = sampleFromSchema(schema);
1285
+ }
1286
+ let extension = "text";
1287
+ switch (contentType) {
1288
+ case "application/json":
1289
+ case "application/problem+json":
1290
+ case "application/vnd.api+json": {
1291
+ extension = "json";
1292
+ break;
1293
+ }
1294
+ case "application/xml":
1295
+ case "text/xml":
1296
+ case "application/problem+xml": {
1297
+ extension = "xml";
1298
+ break;
1299
+ }
1300
+ }
1301
+ tabs.push({
1302
+ title: contentType,
1303
+ language: extension,
1304
+ code: JSON.stringify(responseData, null, 2) || ""
1305
+ });
1306
+ }
1307
+ if (tabs.length > 0) {
1308
+ examples.push({
1309
+ codeblock: {
1310
+ title: status,
1311
+ tabs
1312
+ }
1313
+ });
1314
+ }
1315
+ });
1316
+ if (examples.length > 0) {
1317
+ exampleGroups.push({
1318
+ description: "Example response",
1319
+ examples
1320
+ });
1321
+ }
1322
+ }
1323
+ return exampleGroups;
1324
+ }
1325
+ function fixAllOfBug(schema) {
1326
+ const modifiedSchema = { ...schema };
1327
+ if (schema == null ? void 0 : schema.allOf) {
1328
+ schema.allOf.forEach((prop, i) => {
1329
+ var _a;
1330
+ const propObj = prop;
1331
+ if ("properties" in propObj && !propObj["properties"]) {
1332
+ (_a = modifiedSchema.allOf) == null ? true : delete _a[i];
1333
+ }
1334
+ });
1335
+ }
1336
+ return modifiedSchema;
1337
+ }
1338
+ function sanitizeSchema(schema, vistedExamples = /* @__PURE__ */ new Map(), parent) {
1339
+ if (vistedExamples.has(schema)) {
1340
+ const cached = vistedExamples.get(schema);
1341
+ if (typeof cached === "object") {
1342
+ return JSON.parse(JSON.stringify(cached));
1343
+ }
1344
+ return cached;
1345
+ }
1346
+ if (parent) {
1347
+ vistedExamples.set(schema, parent);
1348
+ }
1349
+ if (!schema || typeof schema !== "object") {
1350
+ vistedExamples.set(schema, schema);
1351
+ return schema;
1352
+ }
1353
+ if (Array.isArray(schema)) {
1354
+ const v = schema.map((item) => sanitizeSchema(item, vistedExamples));
1355
+ vistedExamples.set(schema, v);
1356
+ return v;
1357
+ }
1358
+ const cleaned = {};
1359
+ for (const [key, value] of Object.entries(schema)) {
1360
+ if (key === "__UNSAFE_refPath") {
1361
+ continue;
1362
+ }
1363
+ if (!BUILT_IN_PROPERTIES[key]) {
1364
+ cleaned[key] = sanitizeSchema(value, vistedExamples, cleaned);
1365
+ }
1366
+ }
1367
+ vistedExamples.set(schema, cleaned);
1368
+ return cleaned;
1369
+ }
1370
+ function sampleFromSchema(schema) {
1371
+ var _a, _b;
1372
+ let jsonSchema = null;
1373
+ let multiSpec = null;
1374
+ if ((_a = schema.oneOf) == null ? void 0 : _a.length) {
1375
+ multiSpec = schema.oneOf;
1376
+ } else if ((_b = schema.anyOf) == null ? void 0 : _b.length) {
1377
+ multiSpec = schema.anyOf;
1378
+ }
1379
+ if (multiSpec == null ? void 0 : multiSpec.length) {
1380
+ for (let i = multiSpec.length - 1; i >= 0; i--) {
1381
+ const spec = multiSpec[i];
1382
+ const sanitized = sanitizeSchema(spec);
1383
+ if (!sanitized) {
1384
+ continue;
1385
+ }
1386
+ jsonSchema = sanitized;
1387
+ if (!(jsonSchema == null ? void 0 : jsonSchema.properties)) {
1388
+ continue;
1389
+ }
1390
+ break;
1391
+ }
1392
+ } else {
1393
+ jsonSchema = sanitizeSchema(schema);
1394
+ }
1395
+ if (jsonSchema) {
1396
+ return openApiSampler(jsonSchema);
1397
+ }
1398
+ return null;
1399
+ }
1400
+ function fixCircularReferences(schema, visited = /* @__PURE__ */ new WeakMap()) {
1401
+ if (!schema || typeof schema !== "object") {
1402
+ return schema;
1403
+ }
1404
+ if (visited.has(schema)) {
1405
+ return visited.get(schema);
1406
+ }
1407
+ if (schema.__UNSAFE_circular) {
1408
+ const simplified = {
1409
+ type: "object",
1410
+ description: "Circular reference detected - schema simplified"
1411
+ };
1412
+ visited.set(schema, simplified);
1413
+ return simplified;
1414
+ }
1415
+ if (Array.isArray(schema)) {
1416
+ const result = schema.map((item) => fixCircularReferences(item, visited));
1417
+ visited.set(schema, result);
1418
+ return result;
1419
+ }
1420
+ const fixedSchema = {};
1421
+ visited.set(schema, fixedSchema);
1422
+ for (const [key, value] of Object.entries(schema)) {
1423
+ if (key === "__UNSAFE_circular" || key === "__UNSAFE_refPath") {
1424
+ continue;
1425
+ }
1426
+ if (typeof value === "object" && value !== null) {
1427
+ fixedSchema[key] = fixCircularReferences(value, visited);
1428
+ } else {
1429
+ fixedSchema[key] = value;
1430
+ }
1431
+ }
1432
+ return fixedSchema;
1433
+ }
1434
+
1435
+ // src/converters/oas-componentSchemas.ts
1436
+ import {
1437
+ ReferenceType as ReferenceType2
1438
+ } from "@xyd-js/uniform";
1439
+ function schemaComponentsToUniformReferences(openapi, options) {
1440
+ var _a;
1441
+ const references = [];
1442
+ if (!((_a = openapi.components) == null ? void 0 : _a.schemas)) {
1443
+ return references;
1444
+ }
1445
+ for (const [componentSchemaName, componentSchema] of Object.entries(openapi.components.schemas)) {
1446
+ if ((options == null ? void 0 : options.regions) && options.regions.length > 0) {
1447
+ if (!options.regions.some((region) => region === "/components/schemas/" + componentSchemaName)) {
1448
+ continue;
1449
+ }
1450
+ }
1451
+ if ("$ref" in componentSchema) {
1452
+ console.warn(`Skipping reference object: ${componentSchemaName}`);
1453
+ continue;
1454
+ }
1455
+ let properties = [];
1456
+ let rootProperty = void 0;
1457
+ const respProperties = schemaObjectToUniformDefinitionProperties(componentSchema, false) || [];
1458
+ if (Array.isArray(respProperties)) {
1459
+ properties = respProperties;
1460
+ } else {
1461
+ rootProperty = respProperties;
1462
+ }
1463
+ const symbolDef = definitionPropertyTypeDef2(componentSchema);
1464
+ const definition = {
1465
+ title: componentSchemaName,
1466
+ properties,
1467
+ rootProperty,
1468
+ meta: [],
1469
+ symbolDef
1470
+ };
1471
+ const reference = {
1472
+ title: componentSchemaName,
1473
+ description: componentSchema.description || "",
1474
+ canonical: `objects/${componentSchemaName}`,
1475
+ definitions: [definition],
1476
+ examples: {
1477
+ groups: createSchemaExampleGroup(componentSchema)
1478
+ },
1479
+ type: ReferenceType2.REST_COMPONENT_SCHEMA,
1480
+ context: {
1481
+ componentSchema: componentSchemaName,
1482
+ group: ["Objects"]
1483
+ }
1484
+ };
1485
+ reference.__UNSAFE_selector = function __UNSAFE_selector(selector) {
1486
+ switch (selector) {
1487
+ case "[schema]": {
1488
+ return openapi;
1489
+ }
1490
+ case "[component]": {
1491
+ return componentSchema;
1492
+ }
1493
+ default:
1494
+ return null;
1495
+ }
1496
+ };
1497
+ references.push(reference);
1498
+ }
1499
+ return references;
1500
+ }
1501
+ function createSchemaExampleGroup(schema, map) {
1502
+ const example = generateSchemaExample(schema);
1503
+ if (!example) {
1504
+ return [];
1505
+ }
1506
+ const tabs = [{
1507
+ title: "json",
1508
+ language: "json",
1509
+ code: JSON.stringify(example, null, 2)
1510
+ }];
1511
+ return [{
1512
+ description: "Example",
1513
+ examples: [{
1514
+ codeblock: {
1515
+ tabs
1516
+ }
1517
+ }]
1518
+ }];
1519
+ }
1520
+ function definitionPropertyTypeDef2(schema) {
1521
+ if (!schema) {
1522
+ return;
1523
+ }
1524
+ let typeDef;
1525
+ let oasSchema = schema;
1526
+ if (oasSchema.type === "array") {
1527
+ oasSchema = oasSchema.items;
1528
+ }
1529
+ if (oasSchema == null ? void 0 : oasSchema.__internal_getRefPath) {
1530
+ const symbolId = oasSchema.__internal_getRefPath();
1531
+ typeDef = {
1532
+ id: symbolId
1533
+ };
1534
+ }
1535
+ return typeDef;
1536
+ }
1537
+ function generateSchemaExample(schema, visitedExample, parent) {
1538
+ if (!schema) {
1539
+ return null;
1540
+ }
1541
+ if (!visitedExample) {
1542
+ visitedExample = /* @__PURE__ */ new Map();
1543
+ }
1544
+ const cached = visitedExample.get(schema);
1545
+ if (cached) {
1546
+ return JSON.parse(JSON.stringify(cached));
1547
+ }
1548
+ if (parent) {
1549
+ visitedExample.set(schema, parent);
1550
+ }
1551
+ if ("examples" in schema && Array.isArray(schema.examples)) {
1552
+ const v = schema.examples[0];
1553
+ visitedExample.set(schema, v);
1554
+ return v;
1555
+ }
1556
+ if ("example" in schema && schema.example !== void 0) {
1557
+ const v = schema.example;
1558
+ visitedExample.set(schema, v);
1559
+ return v;
1560
+ }
1561
+ if (schema.type === "object" && schema.properties) {
1562
+ const result = {};
1563
+ for (const [propName, propSchema] of Object.entries(schema.properties)) {
1564
+ result[propName] = generateSchemaExample(propSchema, visitedExample, result);
1565
+ }
1566
+ visitedExample.set(schema, result);
1567
+ return result;
1568
+ }
1569
+ if (schema.type === "array" && schema.items) {
1570
+ const itemExample = generateSchemaExample(schema.items, visitedExample);
1571
+ const v = itemExample ? [itemExample] : [];
1572
+ visitedExample.set(schema, v);
1573
+ return v;
1574
+ }
1575
+ switch (schema.type) {
1576
+ case "string":
1577
+ return "";
1578
+ case "number":
1579
+ case "integer":
1580
+ return 0;
1581
+ case "boolean":
1582
+ return false;
1583
+ default:
1584
+ return null;
1585
+ }
1586
+ }
1587
+
1588
+ // src/converters/oas-schema.ts
1589
+ function oapSchemaToReferences(schema, options) {
1590
+ var _a, _b;
1591
+ if (!schema) {
1592
+ return [];
1593
+ }
1594
+ const references = [];
1595
+ const oas = new Oas(schema);
1596
+ const server = ((_b = (_a = schema.servers) == null ? void 0 : _a[0]) == null ? void 0 : _b.url) || "";
1597
+ Object.entries(schema.paths).forEach(([endpointPath, oapPath]) => {
1598
+ SUPPORTED_HTTP_METHODS.forEach((eachMethod) => {
1599
+ var _a2, _b2, _c, _d;
1600
+ const httpMethod = eachMethod.toLowerCase();
1601
+ const found = httpMethodToUniformMethod(httpMethod);
1602
+ if (!found) {
1603
+ console.warn(`Unsupported method: ${httpMethod} for path: ${endpointPath}`);
1604
+ return;
1605
+ }
1606
+ if ((options == null ? void 0 : options.regions) && options.regions.length > 0) {
1607
+ const regionKey = `${eachMethod.toUpperCase()} ${endpointPath}`;
1608
+ if (!options.regions.some((region) => region === regionKey)) {
1609
+ return;
1610
+ }
1611
+ }
1612
+ const reference = oapPathToReference(
1613
+ schema,
1614
+ httpMethod,
1615
+ endpointPath,
1616
+ oapPath
1617
+ );
1618
+ if (reference) {
1619
+ const ctx = reference.context;
1620
+ ctx.path = endpointPath;
1621
+ ctx.fullPath = path3.join(server, endpointPath);
1622
+ const operation = oas.operation(endpointPath, httpMethod);
1623
+ reference.examples.groups = oapExamples(oas, operation);
1624
+ const scopes = [];
1625
+ const oapMethod = oapPath == null ? void 0 : oapPath[httpMethod];
1626
+ if ((_a2 = schema == null ? void 0 : schema.security) == null ? void 0 : _a2.length) {
1627
+ for (const security of schema.security) {
1628
+ for (const securityKey of Object.keys(security)) {
1629
+ if (securityKey === "oauth2" || securityKey === "OAuth2") {
1630
+ const securityScopes = security[securityKey];
1631
+ if (Array.isArray(securityScopes)) {
1632
+ scopes.push(...securityScopes);
1633
+ }
1634
+ }
1635
+ }
1636
+ }
1637
+ }
1638
+ if (oapMethod == null ? void 0 : oapMethod.security) {
1639
+ if (!((_b2 = oapMethod == null ? void 0 : oapMethod.security) == null ? void 0 : _b2.length)) {
1640
+ scopes.length = 0;
1641
+ }
1642
+ for (const security of oapMethod.security) {
1643
+ for (const securityKey of Object.keys(security)) {
1644
+ const securityScheme = (_d = (_c = schema == null ? void 0 : schema.components) == null ? void 0 : _c.securitySchemes) == null ? void 0 : _d[securityKey];
1645
+ if (securityScheme && "type" in securityScheme && securityScheme.type === "oauth2") {
1646
+ const methodScopes = security[securityKey];
1647
+ if (Array.isArray(methodScopes)) {
1648
+ scopes.push(...methodScopes);
1649
+ }
1650
+ }
1651
+ }
1652
+ }
1653
+ }
1654
+ ctx.scopes = scopes;
1655
+ references.push(reference);
1656
+ }
1657
+ });
1658
+ });
1659
+ const schemas = schemaComponentsToUniformReferences(
1660
+ schema,
1661
+ options
1662
+ );
1663
+ references.push(...schemas);
1664
+ const tags = oas.getTags();
1665
+ sortReferencesByTags(references, tags);
1666
+ references.__internal_options = () => options;
1667
+ return references;
1668
+ }
1669
+ function sortReferencesByTags(references, tags) {
1670
+ return references.sort((prev, next) => {
1671
+ var _a, _b;
1672
+ const aTags = ((_a = prev.context) == null ? void 0 : _a.group) || [];
1673
+ const bTags = ((_b = next.context) == null ? void 0 : _b.group) || [];
1674
+ for (const tag of tags) {
1675
+ const aIndex = aTags.indexOf(tag);
1676
+ const bIndex = bTags.indexOf(tag);
1677
+ if (aIndex !== -1 && bIndex !== -1) {
1678
+ return aIndex - bIndex;
1679
+ }
1680
+ if (aIndex !== -1) return -1;
1681
+ if (bIndex !== -1) return 1;
1682
+ }
1683
+ return (aTags[0] || "").localeCompare(bTags[0] || "");
1684
+ });
1685
+ }
1686
+
1687
+ // src/xdocs/pluginSidebar.ts
1688
+ function uniformPluginXDocsSidebar({
1689
+ references,
1690
+ defer
1691
+ }) {
1692
+ let schema;
1693
+ const refByOperationId = {};
1694
+ const refByComponentSchema = {};
1695
+ defer(() => {
1696
+ var _a;
1697
+ if (typeof references.__internal_options === "function") {
1698
+ const options = references.__internal_options();
1699
+ if ((_a = options == null ? void 0 : options.regions) == null ? void 0 : _a.length) {
1700
+ return {};
1701
+ }
1702
+ }
1703
+ const output = [];
1704
+ if (!schema) {
1705
+ return {};
1706
+ }
1707
+ const xDocs = schema["x-docs"];
1708
+ if (!(xDocs == null ? void 0 : xDocs.sidebar)) {
1709
+ return {};
1710
+ }
1711
+ const navigationMap = {};
1712
+ for (let i = 0; i < xDocs.sidebar.length; i++) {
1713
+ const navGroup = xDocs.sidebar[i];
1714
+ if (!navGroup) {
1715
+ continue;
1716
+ }
1717
+ const uniqueKey = `${navGroup.group}-${i}`;
1718
+ navigationMap[uniqueKey] = {
1719
+ id: navGroup.group,
1720
+ title: navGroup.group,
1721
+ beta: false,
1722
+ index: i
1723
+ };
1724
+ }
1725
+ for (let i = 0; i < xDocs.sidebar.length; i++) {
1726
+ const group = xDocs.sidebar[i];
1727
+ const uniqueKey = `${group.group}-${i}`;
1728
+ const navGroup = navigationMap[uniqueKey];
1729
+ if (!navGroup) {
1730
+ console.warn(`No navigation group found for group: ${group.group}`);
1731
+ continue;
1732
+ }
1733
+ if (!Array.isArray(group.pages)) {
1734
+ continue;
1735
+ }
1736
+ processGroupPages(xDocs, group.pages, [group.group], navGroup, output);
1737
+ }
1738
+ if (Array.isArray(references)) {
1739
+ references.length = 0;
1740
+ references.push(...output);
1741
+ } else {
1742
+ references = output[0] || references;
1743
+ }
1744
+ return {};
1745
+ });
1746
+ function processGroupPages(xDocs, pages, groupPath, navGroup, output, parentPath) {
1747
+ for (const page of pages) {
1748
+ if ("pages" in page && Array.isArray(page.pages)) {
1749
+ processGroupPages(xDocs, page.pages, [...groupPath, page.group], navGroup, output, page.path);
1750
+ } else if ("type" in page && "key" in page) {
1751
+ processPage(xDocs, page, groupPath, navGroup, output, parentPath);
1752
+ }
1753
+ }
1754
+ }
1755
+ function processPage(xDocs, page, groupPath, navGroup, output, parentPath) {
1756
+ let uniformRef;
1757
+ switch (page.type) {
1758
+ case "endpoint": {
1759
+ const operationRef = refByOperationId[page.key];
1760
+ if (!operationRef) {
1761
+ console.warn(`No operation found for key: ${page.key} in group ${groupPath.join("/")}`);
1762
+ return;
1763
+ }
1764
+ uniformRef = operationRef;
1765
+ break;
1766
+ }
1767
+ case "object": {
1768
+ const componentRef = refByComponentSchema[page.key];
1769
+ if (!componentRef) {
1770
+ console.warn(`No component schema found for key: ${page.key} in group ${groupPath.join("/")}`);
1771
+ return;
1772
+ }
1773
+ const selector = componentRef.__UNSAFE_selector;
1774
+ if (!selector || typeof selector !== "function") {
1775
+ return;
1776
+ }
1777
+ const component = selector("[component]");
1778
+ if (!component) {
1779
+ console.warn(`No component schema found for key: ${page.key} in group ${groupPath.join("/")}`);
1780
+ return;
1781
+ }
1782
+ let componentMeta;
1783
+ if (component.allOf) {
1784
+ let found = false;
1785
+ for (const item of component.allOf) {
1786
+ const docsMeta = item["x-docs"];
1787
+ if (docsMeta && found) {
1788
+ console.warn(`Multiple x-docs found in allOf for component schema: ${page.key} in group ${groupPath.join("/")}`);
1789
+ }
1790
+ if (docsMeta) {
1791
+ found = true;
1792
+ componentMeta = docsMeta;
1793
+ break;
1794
+ }
1795
+ }
1796
+ if (!found) {
1797
+ console.warn(`No x-docs found in allOf for component schema: ${page.key} in group ${groupPath.join("/")}`);
1798
+ return;
1799
+ }
1800
+ } else {
1801
+ const docsMeta = component["x-docs"];
1802
+ if (docsMeta) {
1803
+ componentMeta = docsMeta;
1804
+ }
1805
+ }
1806
+ uniformRef = componentRef;
1807
+ if (!componentMeta) {
1808
+ break;
1809
+ }
1810
+ componentRef.title = componentMeta.name || componentRef.title;
1811
+ if (componentMeta.example) {
1812
+ const exampleGroups = oasXDocsExamples(componentMeta.example);
1813
+ uniformRef.examples = {
1814
+ groups: exampleGroups
1815
+ };
1816
+ }
1817
+ break;
1818
+ }
1819
+ default: {
1820
+ console.warn(`Unknown page type: ${page.type} in group ${groupPath.join("/")}`);
1821
+ return;
1822
+ }
1823
+ }
1824
+ if (!uniformRef) {
1825
+ return;
1826
+ }
1827
+ if (xDocs.sidebarPathStrategy === "inherit") {
1828
+ const ctx = uniformRef.context;
1829
+ let firstPart = "";
1830
+ if (parentPath) {
1831
+ firstPart = parentPath;
1832
+ } else {
1833
+ firstPart = (ctx == null ? void 0 : ctx.path) || "";
1834
+ }
1835
+ const canonical2 = joinPaths(firstPart || "", page.path);
1836
+ if (canonical2) {
1837
+ uniformRef.canonical = canonical2;
1838
+ }
1839
+ } else if (page.path) {
1840
+ uniformRef.canonical = joinPaths(parentPath, page.path);
1841
+ } else if (parentPath) {
1842
+ uniformRef.canonical = parentPath;
1843
+ }
1844
+ if (!uniformRef.context) {
1845
+ uniformRef.context = {};
1846
+ }
1847
+ uniformRef.context.group = groupPath;
1848
+ output.push(uniformRef);
1849
+ }
1850
+ return function pluginXDocsSidebarInner(ref) {
1851
+ var _a, _b;
1852
+ const selector = ref.__UNSAFE_selector;
1853
+ if (!selector || typeof selector !== "function") {
1854
+ return;
1855
+ }
1856
+ const oapSchema = selector("[schema]");
1857
+ if (!oapSchema) {
1858
+ return;
1859
+ }
1860
+ schema = oapSchema;
1861
+ const ctx = ref.context;
1862
+ if (ctx == null ? void 0 : ctx.componentSchema) {
1863
+ refByComponentSchema[ctx.componentSchema] = ref;
1864
+ }
1865
+ const methodPath = selector("[method] [path]");
1866
+ if (!methodPath) {
1867
+ return;
1868
+ }
1869
+ const oapMethod = selector("[method]");
1870
+ if (!oapMethod) {
1871
+ return;
1872
+ }
1873
+ const operationId = methodPath.operationId;
1874
+ if (operationId) {
1875
+ refByOperationId[operationId] = ref;
1876
+ }
1877
+ const methodId = (((_a = oapMethod == null ? void 0 : oapMethod.httpMethod) == null ? void 0 : _a.toUpperCase()) + " " + (oapMethod == null ? void 0 : oapMethod.path) || "").trim();
1878
+ if (methodId) {
1879
+ refByOperationId[methodId] = ref;
1880
+ }
1881
+ const meta = methodPath["x-docs"];
1882
+ if (!meta) {
1883
+ return;
1884
+ }
1885
+ if (meta.name) {
1886
+ ref.title = meta.name;
1887
+ }
1888
+ if (meta.group) {
1889
+ if (ref.context) {
1890
+ ref.context.group = [meta.group];
1891
+ }
1892
+ }
1893
+ if (!ref.description) {
1894
+ ref.description = methodPath.summary || "";
1895
+ }
1896
+ if (meta.examples) {
1897
+ const exampleGroups = oasXDocsExamples(meta.examples);
1898
+ ref.examples = {
1899
+ groups: exampleGroups
1900
+ };
1901
+ }
1902
+ if (meta.returns) {
1903
+ if ((_b = ref.definitions) == null ? void 0 : _b.length) {
1904
+ ref.definitions[ref.definitions.length - 1] = {
1905
+ title: ref.definitions[ref.definitions.length - 1].title,
1906
+ description: meta.returns,
1907
+ properties: []
1908
+ };
1909
+ } else {
1910
+ ref.definitions = [
1911
+ {
1912
+ title: "Response",
1913
+ description: meta.returns,
1914
+ properties: []
1915
+ }
1916
+ ];
1917
+ }
1918
+ }
1919
+ };
1920
+ }
1921
+ function oasXDocsExamples(examples) {
1922
+ const groups = [];
1923
+ if (examples) {
1924
+ if (Array.isArray(examples)) {
1925
+ const requestExamples = [];
1926
+ examples.forEach((example) => {
1927
+ if (example.request) {
1928
+ const tabs = [];
1929
+ if (typeof example.request === "string") {
1930
+ tabs.push({
1931
+ title: "",
1932
+ language: "json",
1933
+ code: example.request
1934
+ });
1935
+ } else {
1936
+ for (let lang of Object.keys(example.request)) {
1937
+ const code = example.request[lang] || "";
1938
+ const language = lang === "curl" ? "bash" : lang === "node.js" ? "js" : lang;
1939
+ tabs.push({
1940
+ title: lang,
1941
+ language,
1942
+ code
1943
+ });
1944
+ }
1945
+ }
1946
+ if (tabs.length > 0) {
1947
+ requestExamples.push({
1948
+ description: example.title || "",
1949
+ codeblock: {
1950
+ title: example.title || "",
1951
+ tabs
1952
+ }
1953
+ });
1954
+ }
1955
+ }
1956
+ });
1957
+ if (requestExamples.length > 0) {
1958
+ groups.push({
1959
+ description: "Example request",
1960
+ examples: requestExamples
1961
+ });
1962
+ }
1963
+ const responseExamples = [];
1964
+ examples.forEach((example) => {
1965
+ if (example.response) {
1966
+ const tabs = [];
1967
+ if (typeof example.response === "string") {
1968
+ tabs.push({
1969
+ title: "",
1970
+ language: "json",
1971
+ code: example.response
1972
+ });
1973
+ } else {
1974
+ for (let lang of Object.keys(example.response)) {
1975
+ const code = example.response[lang] || "";
1976
+ const language = lang === "curl" ? "bash" : lang === "node.js" ? "js" : lang;
1977
+ tabs.push({
1978
+ title: lang,
1979
+ language,
1980
+ code
1981
+ });
1982
+ }
1983
+ }
1984
+ if (tabs.length > 0) {
1985
+ responseExamples.push({
1986
+ description: example.title || "",
1987
+ codeblock: {
1988
+ title: example.title || "",
1989
+ tabs
1990
+ }
1991
+ });
1992
+ }
1993
+ }
1994
+ });
1995
+ if (responseExamples.length > 0) {
1996
+ groups.push({
1997
+ description: "Example response",
1998
+ examples: responseExamples
1999
+ });
2000
+ }
2001
+ } else {
2002
+ if (typeof examples === "string") {
2003
+ groups.push({
2004
+ description: "Example",
2005
+ examples: [
2006
+ {
2007
+ description: "",
2008
+ codeblock: {
2009
+ tabs: [
2010
+ {
2011
+ title: "",
2012
+ language: "json",
2013
+ code: examples
2014
+ }
2015
+ ]
2016
+ }
2017
+ }
2018
+ ]
2019
+ });
2020
+ } else {
2021
+ if (examples.request) {
2022
+ const tabs = [];
2023
+ if (typeof examples.request === "string") {
2024
+ tabs.push({
2025
+ title: "",
2026
+ language: "json",
2027
+ code: examples.request || ""
2028
+ });
2029
+ } else {
2030
+ for (let lang of Object.keys(examples.request)) {
2031
+ const code = examples.request[lang] || "";
2032
+ switch (lang) {
2033
+ case "curl":
2034
+ lang = "bash";
2035
+ break;
2036
+ case "node.js":
2037
+ lang = "js";
2038
+ break;
2039
+ default:
2040
+ break;
2041
+ }
2042
+ tabs.push({
2043
+ title: lang,
2044
+ language: lang,
2045
+ code
2046
+ });
2047
+ }
2048
+ }
2049
+ groups.push({
2050
+ description: "Example request",
2051
+ examples: [
2052
+ {
2053
+ description: "",
2054
+ codeblock: {
2055
+ tabs
2056
+ }
2057
+ }
2058
+ ]
2059
+ });
2060
+ }
2061
+ if (examples.response) {
2062
+ const tabs = [];
2063
+ if (typeof examples.response === "string") {
2064
+ tabs.push({
2065
+ title: "",
2066
+ language: "json",
2067
+ code: examples.response || ""
2068
+ });
2069
+ } else {
2070
+ for (let lang of Object.keys(examples.response)) {
2071
+ const code = examples.response[lang] || "";
2072
+ switch (lang) {
2073
+ case "curl":
2074
+ lang = "bash";
2075
+ break;
2076
+ case "node.js":
2077
+ lang = "js";
2078
+ break;
2079
+ default:
2080
+ break;
2081
+ }
2082
+ tabs.push({
2083
+ title: lang,
2084
+ language: lang,
2085
+ code
2086
+ });
2087
+ }
2088
+ }
2089
+ groups.push({
2090
+ description: "Example response",
2091
+ examples: [
2092
+ {
2093
+ description: "",
2094
+ codeblock: {
2095
+ tabs
2096
+ }
2097
+ }
2098
+ ]
2099
+ });
2100
+ }
2101
+ }
2102
+ }
2103
+ }
2104
+ return groups;
2105
+ }
2106
+ function joinPaths(...paths) {
2107
+ return paths.filter(Boolean).map((path4) => {
2108
+ path4 = path4.replace(/^\/+|\/+$/g, "");
2109
+ return path4 ? `/${path4}` : "";
2110
+ }).join("").replace(/\/+/g, "/").replace(/\/\{[^}]+\}/g, "").replace(/\/:[^/]+/g, "");
2111
+ }
2112
+ export {
2113
+ deferencedOpenAPI,
2114
+ getXDocs,
2115
+ oapResponseOperationToUniformDefinition,
2116
+ oapSchemaToReferences,
2117
+ uniformPluginXDocsSidebar
2118
+ };
2119
+ //# sourceMappingURL=index.js.map