@gitbook/react-openapi 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/InteractiveSection.d.ts +0 -2
- package/dist/InteractiveSection.jsx +8 -44
- package/dist/OpenAPICodeSample.jsx +9 -11
- package/dist/OpenAPIRequestBody.d.ts +2 -2
- package/dist/OpenAPIRequestBody.jsx +5 -2
- package/dist/OpenAPIResponse.jsx +4 -14
- package/dist/OpenAPIResponseExample.jsx +5 -5
- package/dist/OpenAPIResponses.jsx +2 -3
- package/dist/OpenAPISchema.jsx +15 -25
- package/dist/OpenAPISpec.jsx +4 -4
- package/dist/OpenAPITabs.d.ts +1 -0
- package/dist/OpenAPITabs.jsx +52 -16
- package/dist/generateSchemaExample.js +2 -3
- package/dist/resolveOpenAPIOperation.js +4 -4
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/useSyncedTabsGlobalState.d.ts +1 -0
- package/dist/useSyncedTabsGlobalState.js +16 -0
- package/dist/utils.d.ts +0 -1
- package/dist/utils.js +0 -6
- package/package.json +3 -2
- package/src/InteractiveSection.tsx +4 -41
- package/src/OpenAPICodeSample.tsx +9 -11
- package/src/OpenAPIRequestBody.tsx +7 -3
- package/src/OpenAPIResponse.tsx +4 -18
- package/src/OpenAPIResponseExample.tsx +5 -5
- package/src/OpenAPIResponses.tsx +2 -7
- package/src/OpenAPISchema.tsx +16 -27
- package/src/OpenAPISpec.tsx +4 -7
- package/src/OpenAPITabs.tsx +63 -23
- package/src/generateSchemaExample.ts +2 -3
- package/src/resolveOpenAPIOperation.ts +8 -7
- package/src/useSyncedTabsGlobalState.ts +23 -0
- package/src/utils.ts +0 -8
- package/dist/fetchOpenAPIOperation.d.ts +0 -27
- package/dist/fetchOpenAPIOperation.js +0 -195
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -7,8 +7,8 @@ import {
|
|
|
7
7
|
type OpenAPIV3_1,
|
|
8
8
|
dereference,
|
|
9
9
|
} from '@gitbook/openapi-parser';
|
|
10
|
-
import { noReference } from './utils';
|
|
11
10
|
import { OpenAPIOperationData } from './types';
|
|
11
|
+
import { checkIsReference } from './utils';
|
|
12
12
|
|
|
13
13
|
export { toJSON, fromJSON };
|
|
14
14
|
|
|
@@ -48,8 +48,8 @@ export async function resolveOpenAPIOperation(
|
|
|
48
48
|
const securityKey = Object.keys(entry)[0];
|
|
49
49
|
if (securityKey) {
|
|
50
50
|
const securityScheme = schema.components?.securitySchemes?.[securityKey];
|
|
51
|
-
if (securityScheme) {
|
|
52
|
-
securities.push([securityKey,
|
|
51
|
+
if (securityScheme && !checkIsReference(securityScheme)) {
|
|
52
|
+
securities.push([securityKey, securityScheme]);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -116,12 +116,13 @@ function getPathObject(
|
|
|
116
116
|
function getPathObjectParameter(
|
|
117
117
|
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
|
|
118
118
|
path: string,
|
|
119
|
-
):
|
|
119
|
+
):
|
|
120
|
+
| (OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject)[]
|
|
121
|
+
| (OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject)[]
|
|
122
|
+
| null {
|
|
120
123
|
const pathObject = getPathObject(schema, path);
|
|
121
124
|
if (pathObject?.parameters) {
|
|
122
|
-
return pathObject.parameters
|
|
123
|
-
| OpenAPIV3.ParameterObject[]
|
|
124
|
-
| OpenAPIV3_1.ParameterObject[];
|
|
125
|
+
return pathObject.parameters;
|
|
125
126
|
}
|
|
126
127
|
return null;
|
|
127
128
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { create } from 'zustand';
|
|
4
|
+
|
|
5
|
+
interface SyncedTabsState<T> {
|
|
6
|
+
tabs: Map<string, T>;
|
|
7
|
+
setTabs: (updater: (tabs: Map<string, T>) => Map<string, T>) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const useSyncedTabsStore = create<SyncedTabsState<any>>()((set) => ({
|
|
11
|
+
tabs: new Map<string, any>(),
|
|
12
|
+
setTabs: (updater) =>
|
|
13
|
+
set((state) => ({
|
|
14
|
+
tabs: updater(new Map(state.tabs)), // Ensure a new Map is created for reactivity
|
|
15
|
+
})),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
// Selector for better performance - only re-renders when tabs change
|
|
19
|
+
export function useSyncedTabsGlobalState<T>() {
|
|
20
|
+
const tabs = useSyncedTabsStore((state) => state.tabs as Map<string, T>);
|
|
21
|
+
const setTabs = useSyncedTabsStore((state) => state.setTabs as SyncedTabsState<T>['setTabs']);
|
|
22
|
+
return [tabs, setTabs] as const;
|
|
23
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import type { AnyObject, OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
2
2
|
|
|
3
|
-
export function noReference<T>(input: T | OpenAPIV3.ReferenceObject): T {
|
|
4
|
-
if (checkIsReference(input)) {
|
|
5
|
-
throw new Error('Reference found');
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return input;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
3
|
export function checkIsReference(input: unknown): input is OpenAPIV3.ReferenceObject {
|
|
12
4
|
return typeof input === 'object' && !!input && '$ref' in input;
|
|
13
5
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { toJSON, fromJSON } from 'flatted';
|
|
2
|
-
import { type OpenAPICustomOperationProperties, type OpenAPICustomSpecProperties, type OpenAPIV3xDocument, type Filesystem, type OpenAPIV3 } from '@gitbook/openapi-parser';
|
|
3
|
-
export interface OpenAPIFetcher {
|
|
4
|
-
/**
|
|
5
|
-
* Fetch an OpenAPI file by its URL. It should return a fully parsed OpenAPI v3 document.
|
|
6
|
-
*/
|
|
7
|
-
fetch: (url: string) => Promise<Filesystem<OpenAPIV3xDocument>>;
|
|
8
|
-
}
|
|
9
|
-
export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
|
10
|
-
path: string;
|
|
11
|
-
method: string;
|
|
12
|
-
/** Servers to be used for this operation */
|
|
13
|
-
servers: OpenAPIV3.ServerObject[];
|
|
14
|
-
/** Spec of the operation */
|
|
15
|
-
operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
|
|
16
|
-
/** Securities that should be used for this operation */
|
|
17
|
-
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
|
18
|
-
}
|
|
19
|
-
export { toJSON, fromJSON };
|
|
20
|
-
/**
|
|
21
|
-
* Resolve an OpenAPI operation in a file and compile it to a more usable format.
|
|
22
|
-
*/
|
|
23
|
-
export declare function fetchOpenAPIOperation(input: {
|
|
24
|
-
url: string;
|
|
25
|
-
path: string;
|
|
26
|
-
method: string;
|
|
27
|
-
}, fetcher: OpenAPIFetcher): Promise<OpenAPIOperationData | null>;
|
|
@@ -1,195 +0,0 @@
|
|
|
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
|
-
};
|
|
57
|
-
import { toJSON, fromJSON } from 'flatted';
|
|
58
|
-
import { OpenAPIParseError, dereference, } from '@gitbook/openapi-parser';
|
|
59
|
-
import { noReference } from './utils';
|
|
60
|
-
export { toJSON, fromJSON };
|
|
61
|
-
/**
|
|
62
|
-
* Resolve an OpenAPI operation in a file and compile it to a more usable format.
|
|
63
|
-
*/
|
|
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);
|
|
119
|
-
}
|
|
120
|
-
var promise = dereferenceFilesystem(filesystem, url);
|
|
121
|
-
dereferenceCache.set(filesystem, promise);
|
|
122
|
-
return promise;
|
|
123
|
-
}
|
|
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];
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Get a path object from its path.
|
|
145
|
-
*/
|
|
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];
|
|
150
|
-
}
|
|
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);
|
|
160
|
-
}
|
|
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;
|
|
171
|
-
}
|
|
172
|
-
var normalizedMethod = method.toLowerCase();
|
|
173
|
-
if (!pathObject[normalizedMethod]) {
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
return pathObject[normalizedMethod];
|
|
177
|
-
}
|
|
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 [];
|
|
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
|
-
});
|
|
195
|
-
}
|