@gitbook/react-openapi 0.7.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/InteractiveSection.d.ts +4 -6
  3. package/dist/InteractiveSection.jsx +96 -0
  4. package/dist/Markdown.d.ts +1 -2
  5. package/dist/Markdown.jsx +5 -0
  6. package/dist/OpenAPICodeSample.d.ts +2 -4
  7. package/dist/OpenAPICodeSample.jsx +143 -0
  8. package/dist/OpenAPIDisclosure.d.ts +12 -0
  9. package/dist/OpenAPIDisclosure.jsx +32 -0
  10. package/dist/OpenAPIDisclosureGroup.d.ts +19 -0
  11. package/dist/OpenAPIDisclosureGroup.jsx +81 -0
  12. package/dist/OpenAPIOperation.d.ts +2 -4
  13. package/dist/OpenAPIOperation.jsx +51 -0
  14. package/dist/OpenAPIOperationContext.d.ts +16 -0
  15. package/dist/OpenAPIOperationContext.jsx +26 -0
  16. package/dist/OpenAPIPath.d.ts +8 -0
  17. package/dist/OpenAPIPath.jsx +54 -0
  18. package/dist/OpenAPIRequestBody.d.ts +3 -4
  19. package/dist/OpenAPIRequestBody.jsx +19 -0
  20. package/dist/OpenAPIResponse.d.ts +4 -4
  21. package/dist/OpenAPIResponse.jsx +49 -0
  22. package/dist/OpenAPIResponseExample.d.ts +2 -4
  23. package/dist/OpenAPIResponseExample.jsx +108 -0
  24. package/dist/OpenAPIResponses.d.ts +3 -4
  25. package/dist/OpenAPIResponses.jsx +36 -0
  26. package/dist/OpenAPISchema.d.ts +11 -8
  27. package/dist/OpenAPISchema.jsx +295 -0
  28. package/dist/OpenAPISchemaName.d.ts +12 -0
  29. package/dist/OpenAPISchemaName.jsx +15 -0
  30. package/dist/OpenAPISecurities.d.ts +2 -4
  31. package/dist/OpenAPISecurities.jsx +55 -0
  32. package/dist/OpenAPIServerURL.d.ts +2 -3
  33. package/dist/OpenAPIServerURL.jsx +67 -0
  34. package/dist/OpenAPIServerURLVariable.d.ts +2 -3
  35. package/dist/OpenAPIServerURLVariable.jsx +8 -0
  36. package/dist/OpenAPISpec.d.ts +3 -4
  37. package/dist/OpenAPISpec.jsx +91 -0
  38. package/dist/OpenAPITabs.d.ts +25 -0
  39. package/dist/OpenAPITabs.jsx +67 -0
  40. package/dist/ScalarApiButton.d.ts +3 -3
  41. package/dist/ScalarApiButton.jsx +51 -0
  42. package/dist/code-samples.d.ts +4 -0
  43. package/dist/code-samples.js +103 -38
  44. package/dist/fetchOpenAPIOperation.d.ts +9 -54
  45. package/dist/fetchOpenAPIOperation.js +178 -107
  46. package/dist/generateSchemaExample.d.ts +2 -2
  47. package/dist/generateSchemaExample.js +28 -100
  48. package/dist/index.d.ts +3 -2
  49. package/dist/index.js +2 -1
  50. package/dist/resolveOpenAPIOperation.d.ts +11 -0
  51. package/dist/resolveOpenAPIOperation.js +194 -0
  52. package/dist/stringifyOpenAPI.d.ts +4 -0
  53. package/dist/stringifyOpenAPI.js +6 -0
  54. package/dist/tsconfig.build.tsbuildinfo +1 -0
  55. package/dist/tsconfig.tsbuildinfo +1 -1
  56. package/dist/types.d.ts +11 -12
  57. package/dist/utils.d.ts +6 -1
  58. package/dist/utils.js +15 -2
  59. package/package.json +11 -10
  60. package/src/InteractiveSection.tsx +119 -78
  61. package/src/Markdown.tsx +2 -3
  62. package/src/OpenAPICodeSample.tsx +35 -21
  63. package/src/OpenAPIDisclosure.tsx +50 -0
  64. package/src/OpenAPIDisclosureGroup.tsx +136 -0
  65. package/src/OpenAPIOperation.tsx +36 -42
  66. package/src/OpenAPIOperationContext.tsx +45 -0
  67. package/src/OpenAPIPath.tsx +65 -0
  68. package/src/OpenAPIRequestBody.tsx +3 -14
  69. package/src/OpenAPIResponse.tsx +39 -43
  70. package/src/OpenAPIResponseExample.tsx +89 -31
  71. package/src/OpenAPIResponses.tsx +51 -15
  72. package/src/OpenAPISchema.test.ts +1 -1
  73. package/src/OpenAPISchema.tsx +124 -92
  74. package/src/OpenAPISchemaName.tsx +27 -0
  75. package/src/OpenAPISecurities.tsx +45 -24
  76. package/src/OpenAPIServerURL.tsx +17 -10
  77. package/src/OpenAPIServerURLVariable.tsx +2 -4
  78. package/src/OpenAPISpec.tsx +56 -53
  79. package/src/OpenAPITabs.tsx +113 -0
  80. package/src/ScalarApiButton.tsx +84 -7
  81. package/src/code-samples.test.ts +51 -0
  82. package/src/code-samples.ts +95 -31
  83. package/src/generateSchemaExample.ts +25 -151
  84. package/src/index.ts +3 -2
  85. package/src/resolveOpenAPIOperation.test.ts +177 -0
  86. package/src/resolveOpenAPIOperation.ts +163 -0
  87. package/src/stringifyOpenAPI.ts +6 -0
  88. package/src/types.ts +17 -10
  89. package/src/utils.ts +17 -2
  90. package/dist/InteractiveSection.js +0 -47
  91. package/dist/Markdown.js +0 -6
  92. package/dist/OpenAPICodeSample.js +0 -110
  93. package/dist/OpenAPIOperation.js +0 -38
  94. package/dist/OpenAPIRequestBody.js +0 -18
  95. package/dist/OpenAPIResponse.js +0 -32
  96. package/dist/OpenAPIResponseExample.js +0 -54
  97. package/dist/OpenAPIResponses.js +0 -18
  98. package/dist/OpenAPISchema.js +0 -235
  99. package/dist/OpenAPISchema.test.d.ts +0 -1
  100. package/dist/OpenAPISchema.test.js +0 -91
  101. package/dist/OpenAPISecurities.js +0 -42
  102. package/dist/OpenAPIServerURL.js +0 -51
  103. package/dist/OpenAPIServerURLVariable.js +0 -10
  104. package/dist/OpenAPISpec.js +0 -70
  105. package/dist/ScalarApiButton.js +0 -14
  106. package/dist/fetchOpenAPIOperation.test.d.ts +0 -1
  107. package/dist/fetchOpenAPIOperation.test.js +0 -152
  108. package/dist/resolveOpenAPIPath.d.ts +0 -7
  109. package/dist/resolveOpenAPIPath.js +0 -112
  110. package/dist/resolveOpenAPIPath.test.d.ts +0 -1
  111. package/dist/resolveOpenAPIPath.test.js +0 -39
  112. package/src/fetchOpenAPIOperation.test.ts +0 -185
  113. package/src/fetchOpenAPIOperation.ts +0 -230
  114. package/src/resolveOpenAPIPath.test.ts +0 -60
  115. package/src/resolveOpenAPIPath.ts +0 -145
@@ -1,124 +1,195 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
23
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
49
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
50
+ if (ar || !(i in from)) {
51
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
52
+ ar[i] = from[i];
53
+ }
54
+ }
55
+ return to.concat(ar || Array.prototype.slice.call(from));
56
+ };
1
57
  import { toJSON, fromJSON } from 'flatted';
2
- import YAML from 'yaml';
3
- import swagger2openapi from 'swagger2openapi';
4
- import { resolveOpenAPIPath } from './resolveOpenAPIPath';
58
+ import { OpenAPIParseError, dereference, } from '@gitbook/openapi-parser';
59
+ import { noReference } from './utils';
5
60
  export { toJSON, fromJSON };
6
61
  /**
7
62
  * Resolve an OpenAPI operation in a file and compile it to a more usable format.
8
63
  */
9
- export async function fetchOpenAPIOperation(input, rawFetcher) {
10
- const fetcher = cacheFetcher(rawFetcher);
11
- let operation = await resolveOpenAPIPath(input.url, ['paths', input.path, input.method], fetcher);
12
- if (!operation) {
13
- return null;
14
- }
15
- const specData = await fetcher.fetch(input.url);
16
- // Resolve common parameters
17
- const commonParameters = await resolveOpenAPIPath(input.url, ['paths', input.path, 'parameters'], fetcher);
18
- if (commonParameters) {
19
- operation = {
20
- ...operation,
21
- parameters: [...commonParameters, ...(operation.parameters ?? [])],
22
- };
23
- }
24
- // Resolve servers
25
- const servers = await resolveOpenAPIPath(input.url, ['servers'], fetcher);
26
- // Resolve securities
27
- const securities = [];
28
- for (const security of operation.security ?? []) {
29
- const securityKey = Object.keys(security)[0];
30
- const securityScheme = await resolveOpenAPIPath(input.url, ['components', 'securitySchemes', securityKey], fetcher);
31
- if (securityScheme) {
32
- securities.push([securityKey, securityScheme]);
33
- }
64
+ export function fetchOpenAPIOperation(input, fetcher) {
65
+ return __awaiter(this, void 0, void 0, function () {
66
+ var filesystem, schema, operation, commonParameters, servers, security, securities, _i, security_1, entry, securityKey, securityScheme;
67
+ var _a, _b, _c, _d, _e, _f;
68
+ return __generator(this, function (_g) {
69
+ switch (_g.label) {
70
+ case 0: return [4 /*yield*/, fetcher.fetch(input.url)];
71
+ case 1:
72
+ filesystem = _g.sent();
73
+ return [4 /*yield*/, memoDereferenceFilesystem(filesystem, input.url)];
74
+ case 2:
75
+ schema = _g.sent();
76
+ operation = getOperationByPathAndMethod(schema, input.path, input.method);
77
+ if (!operation) {
78
+ return [2 /*return*/, null];
79
+ }
80
+ commonParameters = getPathObjectParameter(schema, input.path);
81
+ if (commonParameters) {
82
+ operation = __assign(__assign({}, operation), { parameters: __spreadArray(__spreadArray([], commonParameters, true), ((_a = operation.parameters) !== null && _a !== void 0 ? _a : []), true) });
83
+ }
84
+ servers = 'servers' in schema ? ((_b = schema.servers) !== null && _b !== void 0 ? _b : []) : [];
85
+ security = flattenSecurities((_d = (_c = operation.security) !== null && _c !== void 0 ? _c : schema.security) !== null && _d !== void 0 ? _d : []);
86
+ securities = [];
87
+ for (_i = 0, security_1 = security; _i < security_1.length; _i++) {
88
+ entry = security_1[_i];
89
+ securityKey = Object.keys(entry)[0];
90
+ if (securityKey) {
91
+ securityScheme = (_f = (_e = schema.components) === null || _e === void 0 ? void 0 : _e.securitySchemes) === null || _f === void 0 ? void 0 : _f[securityKey];
92
+ if (securityScheme) {
93
+ securities.push([securityKey, noReference(securityScheme)]);
94
+ }
95
+ }
96
+ }
97
+ return [2 /*return*/, {
98
+ servers: servers,
99
+ operation: operation,
100
+ method: input.method,
101
+ path: input.path,
102
+ securities: securities,
103
+ 'x-codeSamples': typeof schema['x-codeSamples'] === 'boolean' ? schema['x-codeSamples'] : undefined,
104
+ 'x-hideTryItPanel': typeof schema['x-hideTryItPanel'] === 'boolean'
105
+ ? schema['x-hideTryItPanel']
106
+ : undefined,
107
+ }];
108
+ }
109
+ });
110
+ });
111
+ }
112
+ var dereferenceCache = new WeakMap();
113
+ /**
114
+ * Memoized version of `dereferenceSchema`.
115
+ */
116
+ function memoDereferenceFilesystem(filesystem, url) {
117
+ if (dereferenceCache.has(filesystem)) {
118
+ return dereferenceCache.get(filesystem);
34
119
  }
35
- return {
36
- servers: servers ?? [],
37
- operation,
38
- method: input.method,
39
- path: input.path,
40
- securities,
41
- 'x-codeSamples': typeof specData['x-codeSamples'] === 'boolean' ? specData['x-codeSamples'] : undefined,
42
- 'x-hideTryItPanel': typeof specData['x-hideTryItPanel'] === 'boolean'
43
- ? specData['x-hideTryItPanel']
44
- : undefined,
45
- };
120
+ var promise = dereferenceFilesystem(filesystem, url);
121
+ dereferenceCache.set(filesystem, promise);
122
+ return promise;
46
123
  }
47
- function cacheFetcher(fetcher) {
48
- const cache = new Map();
49
- return {
50
- async fetch(url) {
51
- if (cache.has(url)) {
52
- return cache.get(url);
124
+ /**
125
+ * Dereference an OpenAPI schema.
126
+ */
127
+ function dereferenceFilesystem(filesystem, url) {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ var result;
130
+ return __generator(this, function (_a) {
131
+ switch (_a.label) {
132
+ case 0: return [4 /*yield*/, dereference(filesystem)];
133
+ case 1:
134
+ result = _a.sent();
135
+ if (!result.schema) {
136
+ throw new OpenAPIParseError('Failed to dereference OpenAPI document', url, 'failed-dereference');
137
+ }
138
+ return [2 /*return*/, result.schema];
53
139
  }
54
- const promise = fetcher.fetch(url);
55
- cache.set(url, promise);
56
- return promise;
57
- },
58
- parseMarkdown: fetcher.parseMarkdown,
59
- };
140
+ });
141
+ });
60
142
  }
61
143
  /**
62
- * Parse a raw string into an OpenAPI document.
63
- * It will also convert Swagger 2.0 to OpenAPI 3.0.
64
- * It can throw an `OpenAPIFetchError` if the document is invalid.
144
+ * Get a path object from its path.
65
145
  */
66
- export async function parseOpenAPIV3(url, text) {
67
- // Parse the JSON or YAML
68
- let data;
69
- // Try with JSON
70
- try {
71
- data = JSON.parse(text);
146
+ function getPathObject(schema, path) {
147
+ var _a;
148
+ if ((_a = schema.paths) === null || _a === void 0 ? void 0 : _a[path]) {
149
+ return schema.paths[path];
72
150
  }
73
- catch (jsonError) {
74
- try {
75
- // Try with YAML
76
- data = YAML.parse(text);
77
- }
78
- catch (yamlError) {
79
- if (yamlError instanceof Error && yamlError.name.startsWith('YAML')) {
80
- throw new OpenAPIFetchError('Failed to parse YAML: ' + yamlError.message, url);
81
- }
82
- else {
83
- throw yamlError;
84
- }
85
- }
151
+ return null;
152
+ }
153
+ /**
154
+ * Resolve parameters from a path in an OpenAPI schema.
155
+ */
156
+ function getPathObjectParameter(schema, path) {
157
+ var pathObject = getPathObject(schema, path);
158
+ if (pathObject === null || pathObject === void 0 ? void 0 : pathObject.parameters) {
159
+ return pathObject.parameters.map(noReference);
86
160
  }
87
- // Convert Swagger 2.0 to OpenAPI 3.0
88
- // @ts-ignore
89
- if (data && data.swagger) {
90
- try {
91
- // Convert Swagger 2.0 to OpenAPI 3.0
92
- // @ts-ignore
93
- const result = (await swagger2openapi.convertObj(data, {
94
- resolve: false,
95
- resolveInternal: false,
96
- laxDefaults: true,
97
- laxurls: true,
98
- lint: false,
99
- prevalidate: false,
100
- anchors: true,
101
- patch: true,
102
- }));
103
- data = result.openapi;
104
- }
105
- catch (error) {
106
- if (error.name === 'S2OError') {
107
- throw new OpenAPIFetchError('Failed to convert Swagger 2.0 to OpenAPI 3.0: ' + error.message, url);
108
- }
109
- else {
110
- throw error;
111
- }
112
- }
161
+ return null;
162
+ }
163
+ /**
164
+ * Get an operation by its path and method.
165
+ */
166
+ function getOperationByPathAndMethod(schema, path, method) {
167
+ // Types are buffy for OpenAPIV3_1.OperationObject, so we use v3
168
+ var pathObject = getPathObject(schema, path);
169
+ if (!pathObject) {
170
+ return null;
113
171
  }
114
- // @ts-ignore
115
- return data;
172
+ var normalizedMethod = method.toLowerCase();
173
+ if (!pathObject[normalizedMethod]) {
174
+ return null;
175
+ }
176
+ return pathObject[normalizedMethod];
116
177
  }
117
- export class OpenAPIFetchError extends Error {
118
- url;
119
- name = 'OpenAPIFetchError';
120
- constructor(message, url) {
121
- super(message);
122
- this.url = url;
178
+ /**
179
+ * Flatten security objects in case they are nested.
180
+ * @example [{bearerAuth:[], basicAuth:[]}] => [{ bearerAuth: [] }, { basicAuth: [] }]
181
+ */
182
+ function flattenSecurities(security) {
183
+ if (!Array.isArray(security) || security.length === 0) {
184
+ return [];
123
185
  }
186
+ return security.flatMap(function (securityObject) {
187
+ return Object.entries(securityObject).map(function (_a) {
188
+ var _b;
189
+ var authType = _a[0], config = _a[1];
190
+ return (_b = {},
191
+ _b[authType] = config,
192
+ _b);
193
+ });
194
+ });
124
195
  }
@@ -1,4 +1,4 @@
1
- import { OpenAPIV3 } from 'openapi-types';
1
+ import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
2
  type JSONValue = string | number | boolean | null | JSONValue[] | {
3
3
  [key: string]: JSONValue;
4
4
  };
@@ -7,7 +7,7 @@ type JSONValue = string | number | boolean | null | JSONValue[] | {
7
7
  */
8
8
  export declare function generateSchemaExample(schema: OpenAPIV3.SchemaObject, options?: {
9
9
  onlyRequired?: boolean;
10
- }, ancestors?: Set<OpenAPIV3.SchemaObject>): JSONValue | undefined;
10
+ }): JSONValue | undefined;
11
11
  /**
12
12
  * Generate an example for a media type.
13
13
  */
@@ -1,115 +1,43 @@
1
1
  import { noReference } from './utils';
2
+ import { getExampleFromSchema } from '@scalar/oas-utils/spec-getters';
2
3
  /**
3
4
  * Generate a JSON example from a schema
4
5
  */
5
- export function generateSchemaExample(schema, options = {}, ancestors = new Set()) {
6
- const { onlyRequired = false } = options;
7
- if (ancestors.has(schema)) {
8
- return undefined;
9
- }
10
- if (typeof schema.example !== 'undefined') {
11
- return schema.example;
12
- }
13
- if (schema.enum && schema.enum.length > 0) {
14
- return schema.enum[0];
15
- }
16
- if (schema.type === 'string') {
17
- if (schema.default) {
18
- return schema.default;
19
- }
20
- if (schema.format === 'date-time') {
21
- return new Date().toISOString();
22
- }
23
- if (schema.format === 'date') {
24
- return new Date().toISOString().split('T')[0];
25
- }
26
- if (schema.format === 'email') {
27
- return 'name@gmail.com';
28
- }
29
- if (schema.format === 'hostname') {
30
- return 'example.com';
31
- }
32
- if (schema.format === 'ipv4') {
33
- return '0.0.0.0';
34
- }
35
- if (schema.format === 'ipv6') {
36
- return '2001:0db8:85a3:0000:0000:8a2e:0370:7334';
37
- }
38
- if (schema.format === 'uri') {
39
- return 'https://example.com';
40
- }
41
- if (schema.format === 'uuid') {
42
- return '123e4567-e89b-12d3-a456-426614174000';
43
- }
44
- if (schema.format === 'binary') {
45
- return 'binary';
46
- }
47
- if (schema.format === 'byte') {
48
- return 'Ynl0ZXM=';
49
- }
50
- if (schema.format === 'password') {
51
- return 'password';
52
- }
53
- return 'text';
54
- }
55
- if (schema.type === 'number' || schema.type === 'integer') {
56
- return schema.default || 0;
57
- }
58
- if (schema.type === 'boolean') {
59
- return schema.default || false;
60
- }
61
- if (schema.type === 'array') {
62
- if (schema.items) {
63
- const exampleValue = generateSchemaExample(noReference(schema.items), options, new Set(ancestors).add(schema));
64
- if (exampleValue !== undefined) {
65
- return [exampleValue];
66
- }
67
- return [];
68
- }
69
- return [];
70
- }
71
- if (schema.properties) {
72
- const example = {};
73
- const props = onlyRequired ? (schema.required ?? []) : Object.keys(schema.properties);
74
- for (const key of props) {
75
- const property = noReference(schema.properties[key]);
76
- if (property && (onlyRequired || !property.deprecated)) {
77
- const exampleValue = generateSchemaExample(noReference(property), options, new Set(ancestors).add(schema));
78
- if (exampleValue !== undefined) {
79
- example[key] = exampleValue;
80
- }
81
- }
82
- }
83
- return example;
84
- }
85
- if (schema.oneOf && schema.oneOf.length > 0) {
86
- return generateSchemaExample(noReference(schema.oneOf[0]), options, new Set(ancestors).add(schema));
87
- }
88
- if (schema.anyOf && schema.anyOf.length > 0) {
89
- return generateSchemaExample(noReference(schema.anyOf[0]), options, new Set(ancestors).add(schema));
90
- }
91
- if (schema.allOf && schema.allOf.length > 0) {
92
- return schema.allOf.reduce((acc, curr) => {
93
- const example = generateSchemaExample(noReference(curr), options, new Set(ancestors).add(schema));
94
- if (typeof example === 'object' && !Array.isArray(example) && example !== null) {
95
- return { ...acc, ...example };
96
- }
97
- return acc;
98
- }, {});
99
- }
100
- return undefined;
6
+ export function generateSchemaExample(schema, options) {
7
+ if (options === void 0) { options = {}; }
8
+ return getExampleFromSchema(schema, {
9
+ emptyString: 'text',
10
+ omitEmptyAndOptionalProperties: options.onlyRequired,
11
+ variables: {
12
+ 'date-time': new Date().toISOString(),
13
+ date: new Date().toISOString().split('T')[0],
14
+ email: 'name@gmail.com',
15
+ hostname: 'example.com',
16
+ ipv4: '0.0.0.0',
17
+ ipv6: '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
18
+ uri: 'https://example.com',
19
+ uuid: '123e4567-e89b-12d3-a456-426614174000',
20
+ binary: 'binary',
21
+ byte: 'Ynl0ZXM=',
22
+ password: 'password',
23
+ },
24
+ });
101
25
  }
102
26
  /**
103
27
  * Generate an example for a media type.
104
28
  */
105
- export function generateMediaTypeExample(mediaType, options = {}) {
29
+ export function generateMediaTypeExample(mediaType, options) {
30
+ if (options === void 0) { options = {}; }
106
31
  if (mediaType.example) {
107
32
  return mediaType.example;
108
33
  }
109
34
  if (mediaType.examples) {
110
- const example = mediaType.examples[Object.keys(mediaType.examples)[0]];
111
- if (example) {
112
- return noReference(example).value;
35
+ var key = Object.keys(mediaType.examples)[0];
36
+ if (key) {
37
+ var example = mediaType.examples[key];
38
+ if (example) {
39
+ return noReference(example).value;
40
+ }
113
41
  }
114
42
  }
115
43
  if (mediaType.schema) {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export * from './fetchOpenAPIOperation';
1
+ export * from './resolveOpenAPIOperation';
2
2
  export * from './OpenAPIOperation';
3
- export type { OpenAPIFetcher } from './types';
3
+ export * from './OpenAPIOperationContext';
4
+ export type { OpenAPIOperationData } from './types';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
- export * from './fetchOpenAPIOperation';
1
+ export * from './resolveOpenAPIOperation';
2
2
  export * from './OpenAPIOperation';
3
+ export * from './OpenAPIOperationContext';
@@ -0,0 +1,11 @@
1
+ import { toJSON, fromJSON } from 'flatted';
2
+ import { type OpenAPIV3xDocument, type Filesystem } from '@gitbook/openapi-parser';
3
+ import { OpenAPIOperationData } from './types';
4
+ export { toJSON, fromJSON };
5
+ /**
6
+ * Resolve an OpenAPI operation in a file and compile it to a more usable format.
7
+ */
8
+ export declare function resolveOpenAPIOperation(filesystem: Filesystem<OpenAPIV3xDocument>, operationDescriptor: {
9
+ path: string;
10
+ method: string;
11
+ }): Promise<OpenAPIOperationData | null>;