@gitbook/react-openapi 1.0.4 → 1.1.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 (58) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/InteractiveSection.jsx +10 -9
  3. package/dist/OpenAPICodeSample.jsx +3 -3
  4. package/dist/OpenAPIDisclosure.d.ts +5 -9
  5. package/dist/OpenAPIDisclosure.jsx +25 -27
  6. package/dist/OpenAPIDisclosureGroup.d.ts +2 -2
  7. package/dist/OpenAPIDisclosureGroup.jsx +6 -5
  8. package/dist/OpenAPIPath.jsx +5 -1
  9. package/dist/OpenAPIResponseExample.jsx +8 -8
  10. package/dist/OpenAPIResponses.jsx +3 -3
  11. package/dist/OpenAPISchema.d.ts +3 -26
  12. package/dist/OpenAPISchema.jsx +80 -131
  13. package/dist/OpenAPISpec.jsx +3 -4
  14. package/dist/OpenAPITabs.jsx +51 -47
  15. package/dist/ScalarApiButton.d.ts +3 -2
  16. package/dist/ScalarApiButton.jsx +22 -18
  17. package/dist/StaticSection.d.ts +10 -0
  18. package/dist/StaticSection.jsx +23 -0
  19. package/dist/dereference.d.ts +5 -0
  20. package/dist/dereference.js +68 -0
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.js +2 -1
  23. package/dist/models/OpenAPIModels.d.ts +9 -0
  24. package/dist/models/OpenAPIModels.jsx +62 -0
  25. package/dist/models/index.d.ts +2 -0
  26. package/dist/models/index.js +2 -0
  27. package/dist/models/resolveOpenAPIModels.d.ts +7 -0
  28. package/dist/models/resolveOpenAPIModels.js +73 -0
  29. package/dist/resolveOpenAPIOperation.d.ts +2 -2
  30. package/dist/resolveOpenAPIOperation.js +3 -34
  31. package/dist/tsconfig.build.tsbuildinfo +1 -1
  32. package/dist/types.d.ts +8 -0
  33. package/dist/useSyncedTabsGlobalState.d.ts +10 -1
  34. package/dist/useSyncedTabsGlobalState.js +19 -15
  35. package/dist/utils.js +42 -3
  36. package/package.json +3 -3
  37. package/src/InteractiveSection.tsx +10 -18
  38. package/src/OpenAPICodeSample.tsx +3 -3
  39. package/src/OpenAPIDisclosure.tsx +35 -42
  40. package/src/OpenAPIDisclosureGroup.tsx +13 -11
  41. package/src/OpenAPIPath.tsx +7 -1
  42. package/src/OpenAPIResponseExample.tsx +8 -15
  43. package/src/OpenAPIResponses.tsx +3 -3
  44. package/src/OpenAPISchema.test.ts +26 -35
  45. package/src/OpenAPISchema.tsx +138 -227
  46. package/src/OpenAPISpec.tsx +3 -5
  47. package/src/OpenAPITabs.tsx +52 -63
  48. package/src/ScalarApiButton.tsx +26 -28
  49. package/src/StaticSection.tsx +59 -0
  50. package/src/dereference.ts +29 -0
  51. package/src/index.ts +3 -2
  52. package/src/models/OpenAPIModels.tsx +89 -0
  53. package/src/models/index.ts +2 -0
  54. package/src/models/resolveOpenAPIModels.ts +35 -0
  55. package/src/resolveOpenAPIOperation.ts +8 -36
  56. package/src/types.ts +10 -0
  57. package/src/useSyncedTabsGlobalState.ts +33 -21
  58. package/src/utils.ts +51 -3
@@ -0,0 +1,68 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { dereference } from '@gitbook/openapi-parser';
38
+ var dereferenceCache = new WeakMap();
39
+ /**
40
+ * Memoized version of `dereferenceSchema`.
41
+ */
42
+ export function dereferenceFilesystem(filesystem) {
43
+ if (dereferenceCache.has(filesystem)) {
44
+ return dereferenceCache.get(filesystem);
45
+ }
46
+ var promise = baseDereferenceFilesystem(filesystem);
47
+ dereferenceCache.set(filesystem, promise);
48
+ return promise;
49
+ }
50
+ /**
51
+ * Dereference an OpenAPI schema.
52
+ */
53
+ function baseDereferenceFilesystem(filesystem) {
54
+ return __awaiter(this, void 0, void 0, function () {
55
+ var result;
56
+ return __generator(this, function (_a) {
57
+ switch (_a.label) {
58
+ case 0: return [4 /*yield*/, dereference(filesystem)];
59
+ case 1:
60
+ result = _a.sent();
61
+ if (!result.schema) {
62
+ throw new Error('Failed to dereference OpenAPI document');
63
+ }
64
+ return [2 /*return*/, result.schema];
65
+ }
66
+ });
67
+ });
68
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export * from './resolveOpenAPIOperation';
1
+ export * from './models';
2
2
  export * from './OpenAPIOperation';
3
3
  export * from './OpenAPIOperationContext';
4
- export type { OpenAPIOperationData } from './types';
4
+ export * from './resolveOpenAPIOperation';
5
+ export type { OpenAPIModelsData, OpenAPIOperationData } from './types';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
- export * from './resolveOpenAPIOperation';
1
+ export * from './models';
2
2
  export * from './OpenAPIOperation';
3
3
  export * from './OpenAPIOperationContext';
4
+ export * from './resolveOpenAPIOperation';
@@ -0,0 +1,9 @@
1
+ import type { OpenAPIContextProps, OpenAPIModelsData } from '../types';
2
+ /**
3
+ * Display OpenAPI Models.
4
+ */
5
+ export declare function OpenAPIModels(props: {
6
+ className?: string;
7
+ data: OpenAPIModelsData;
8
+ context: OpenAPIContextProps;
9
+ }): import("react").JSX.Element | null;
@@ -0,0 +1,62 @@
1
+ import clsx from 'clsx';
2
+ import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
3
+ import { OpenAPIRootSchema } from '../OpenAPISchema';
4
+ import { Section, SectionBody } from '../StaticSection';
5
+ /**
6
+ * Display OpenAPI Models.
7
+ */
8
+ export function OpenAPIModels(props) {
9
+ var className = props.className, data = props.data, context = props.context;
10
+ var models = data.models;
11
+ var clientContext = {
12
+ defaultInteractiveOpened: context.defaultInteractiveOpened,
13
+ icons: context.icons,
14
+ blockKey: context.blockKey,
15
+ };
16
+ if (!models.length) {
17
+ return null;
18
+ }
19
+ return (<div className={clsx('openapi-models', className)}>
20
+ <OpenAPIRootModelsSchema models={models} context={clientContext}/>
21
+ </div>);
22
+ }
23
+ /**
24
+ * Root schema for OpenAPI models.
25
+ * It displays a single model or a disclosure group for multiple models.
26
+ */
27
+ function OpenAPIRootModelsSchema(props) {
28
+ var _a;
29
+ var models = props.models, context = props.context;
30
+ // If there is only one model, we show it directly.
31
+ if (models.length === 1) {
32
+ var schema = (_a = models === null || models === void 0 ? void 0 : models[0]) === null || _a === void 0 ? void 0 : _a.schema;
33
+ if (!schema) {
34
+ return null;
35
+ }
36
+ return (<Section>
37
+ <SectionBody>
38
+ <OpenAPIRootSchema schema={schema} context={context}/>
39
+ </SectionBody>
40
+ </Section>);
41
+ }
42
+ // If there are multiple models, we use a disclosure group to show them all.
43
+ return (<OpenAPIDisclosureGroup allowsMultipleExpanded icon={context.icons.chevronRight} groups={models.map(function (_a) {
44
+ var name = _a.name, schema = _a.schema;
45
+ return ({
46
+ id: name,
47
+ label: (<div className="openapi-response-tab-content" key={"model-".concat(name)}>
48
+ <span className="openapi-response-statuscode">{name}</span>
49
+ </div>),
50
+ tabs: [
51
+ {
52
+ id: 'model',
53
+ body: (<Section className="openapi-section-models">
54
+ <SectionBody>
55
+ <OpenAPIRootSchema schema={schema} context={context}/>
56
+ </SectionBody>
57
+ </Section>),
58
+ },
59
+ ],
60
+ });
61
+ })}/>);
62
+ }
@@ -0,0 +1,2 @@
1
+ export * from './OpenAPIModels';
2
+ export * from './resolveOpenAPIModels';
@@ -0,0 +1,2 @@
1
+ export * from './OpenAPIModels';
2
+ export * from './resolveOpenAPIModels';
@@ -0,0 +1,7 @@
1
+ import { type Filesystem, type OpenAPIV3xDocument } from '@gitbook/openapi-parser';
2
+ import type { OpenAPIModelsData } from '../types';
3
+ /**
4
+ * Resolve an OpenAPI models from a file and compile it to a more usable format.
5
+ * Models are extracted from the OpenAPI components.schemas
6
+ */
7
+ export declare function resolveOpenAPIModels(filesystem: Filesystem<OpenAPIV3xDocument>): Promise<OpenAPIModelsData | null>;
@@ -0,0 +1,73 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { shouldIgnoreEntity, } from '@gitbook/openapi-parser';
38
+ import { dereferenceFilesystem } from '../dereference';
39
+ //!!TODO: We should return only the models that are used in the block. Still a WIP awaiting future work.
40
+ /**
41
+ * Resolve an OpenAPI models from a file and compile it to a more usable format.
42
+ * Models are extracted from the OpenAPI components.schemas
43
+ */
44
+ export function resolveOpenAPIModels(filesystem) {
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ var schema, models;
47
+ return __generator(this, function (_a) {
48
+ switch (_a.label) {
49
+ case 0: return [4 /*yield*/, dereferenceFilesystem(filesystem)];
50
+ case 1:
51
+ schema = _a.sent();
52
+ models = getOpenAPIComponents(schema);
53
+ return [2 /*return*/, { models: models }];
54
+ }
55
+ });
56
+ });
57
+ }
58
+ /**
59
+ * Get OpenAPI components.schemas that are not ignored.
60
+ */
61
+ function getOpenAPIComponents(schema) {
62
+ var _a, _b;
63
+ var schemas = (_b = (_a = schema.components) === null || _a === void 0 ? void 0 : _a.schemas) !== null && _b !== void 0 ? _b : {};
64
+ return Object.entries(schemas)
65
+ .filter(function (_a) {
66
+ var schema = _a[1];
67
+ return !shouldIgnoreEntity(schema);
68
+ })
69
+ .map(function (_a) {
70
+ var key = _a[0], schema = _a[1];
71
+ return ({ name: key, schema: schema });
72
+ });
73
+ }
@@ -1,7 +1,7 @@
1
1
  import { fromJSON, toJSON } from 'flatted';
2
- import { type Filesystem, type OpenAPIV3xDocument } from '@gitbook/openapi-parser';
2
+ import type { Filesystem, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
3
3
  import type { OpenAPIOperationData } from './types';
4
- export { toJSON, fromJSON };
4
+ export { fromJSON, toJSON };
5
5
  /**
6
6
  * Resolve an OpenAPI operation in a file and compile it to a more usable format.
7
7
  */
@@ -55,9 +55,9 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
55
  return to.concat(ar || Array.prototype.slice.call(from));
56
56
  };
57
57
  import { fromJSON, toJSON } from 'flatted';
58
- import { dereference, } from '@gitbook/openapi-parser';
58
+ import { dereferenceFilesystem } from './dereference';
59
59
  import { checkIsReference } from './utils';
60
- export { toJSON, fromJSON };
60
+ export { fromJSON, toJSON };
61
61
  /**
62
62
  * Resolve an OpenAPI operation in a file and compile it to a more usable format.
63
63
  */
@@ -69,7 +69,7 @@ export function resolveOpenAPIOperation(filesystem, operationDescriptor) {
69
69
  switch (_g.label) {
70
70
  case 0:
71
71
  path = operationDescriptor.path, method = operationDescriptor.method;
72
- return [4 /*yield*/, memoDereferenceFilesystem(filesystem)];
72
+ return [4 /*yield*/, dereferenceFilesystem(filesystem)];
73
73
  case 1:
74
74
  schema = _g.sent();
75
75
  operation = getOperationByPathAndMethod(schema, path, method);
@@ -108,37 +108,6 @@ export function resolveOpenAPIOperation(filesystem, operationDescriptor) {
108
108
  });
109
109
  });
110
110
  }
111
- var dereferenceCache = new WeakMap();
112
- /**
113
- * Memoized version of `dereferenceSchema`.
114
- */
115
- function memoDereferenceFilesystem(filesystem) {
116
- if (dereferenceCache.has(filesystem)) {
117
- return dereferenceCache.get(filesystem);
118
- }
119
- var promise = dereferenceFilesystem(filesystem);
120
- dereferenceCache.set(filesystem, promise);
121
- return promise;
122
- }
123
- /**
124
- * Dereference an OpenAPI schema.
125
- */
126
- function dereferenceFilesystem(filesystem) {
127
- return __awaiter(this, void 0, void 0, function () {
128
- var result;
129
- return __generator(this, function (_a) {
130
- switch (_a.label) {
131
- case 0: return [4 /*yield*/, dereference(filesystem)];
132
- case 1:
133
- result = _a.sent();
134
- if (!result.schema) {
135
- throw new Error('Failed to dereference OpenAPI document');
136
- }
137
- return [2 /*return*/, result.schema];
138
- }
139
- });
140
- });
141
- }
142
111
  /**
143
112
  * Get a path object from its path.
144
113
  */