@gitbook/react-openapi 0.7.1 → 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 +49 -0
- package/dist/InteractiveSection.d.ts +4 -8
- package/dist/InteractiveSection.jsx +60 -0
- package/dist/Markdown.d.ts +1 -2
- package/dist/Markdown.jsx +5 -0
- package/dist/OpenAPICodeSample.d.ts +2 -4
- package/dist/OpenAPICodeSample.jsx +141 -0
- package/dist/OpenAPIDisclosure.d.ts +12 -0
- package/dist/OpenAPIDisclosure.jsx +32 -0
- package/dist/OpenAPIDisclosureGroup.d.ts +19 -0
- package/dist/OpenAPIDisclosureGroup.jsx +81 -0
- package/dist/OpenAPIOperation.d.ts +2 -4
- package/dist/OpenAPIOperation.jsx +51 -0
- package/dist/OpenAPIOperationContext.d.ts +16 -0
- package/dist/OpenAPIOperationContext.jsx +26 -0
- package/dist/OpenAPIPath.d.ts +8 -0
- package/dist/OpenAPIPath.jsx +54 -0
- package/dist/OpenAPIRequestBody.d.ts +4 -5
- package/dist/OpenAPIRequestBody.jsx +22 -0
- package/dist/OpenAPIResponse.d.ts +4 -4
- package/dist/OpenAPIResponse.jsx +39 -0
- package/dist/OpenAPIResponseExample.d.ts +2 -4
- package/dist/OpenAPIResponseExample.jsx +108 -0
- package/dist/OpenAPIResponses.d.ts +3 -4
- package/dist/OpenAPIResponses.jsx +35 -0
- package/dist/OpenAPISchema.d.ts +11 -8
- package/dist/OpenAPISchema.jsx +285 -0
- package/dist/OpenAPISchemaName.d.ts +12 -0
- package/dist/OpenAPISchemaName.jsx +15 -0
- package/dist/OpenAPISecurities.d.ts +2 -4
- package/dist/OpenAPISecurities.jsx +55 -0
- package/dist/OpenAPIServerURL.d.ts +2 -3
- package/dist/OpenAPIServerURL.jsx +67 -0
- package/dist/OpenAPIServerURLVariable.d.ts +2 -3
- package/dist/OpenAPIServerURLVariable.jsx +8 -0
- package/dist/OpenAPISpec.d.ts +3 -4
- package/dist/OpenAPISpec.jsx +91 -0
- package/dist/OpenAPITabs.d.ts +26 -0
- package/dist/OpenAPITabs.jsx +103 -0
- package/dist/ScalarApiButton.d.ts +3 -3
- package/dist/ScalarApiButton.jsx +51 -0
- package/dist/code-samples.d.ts +4 -0
- package/dist/code-samples.js +103 -38
- package/dist/generateSchemaExample.d.ts +2 -2
- package/dist/generateSchemaExample.js +29 -102
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/resolveOpenAPIOperation.d.ts +11 -0
- package/dist/resolveOpenAPIOperation.js +194 -0
- package/dist/stringifyOpenAPI.d.ts +4 -0
- package/dist/stringifyOpenAPI.js +6 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types.d.ts +11 -12
- package/dist/useSyncedTabsGlobalState.d.ts +1 -0
- package/dist/useSyncedTabsGlobalState.js +16 -0
- package/dist/utils.d.ts +6 -2
- package/dist/utils.js +13 -6
- package/package.json +12 -10
- package/src/InteractiveSection.tsx +90 -86
- package/src/Markdown.tsx +2 -3
- package/src/OpenAPICodeSample.tsx +43 -31
- package/src/OpenAPIDisclosure.tsx +50 -0
- package/src/OpenAPIDisclosureGroup.tsx +136 -0
- package/src/OpenAPIOperation.tsx +36 -42
- package/src/OpenAPIOperationContext.tsx +45 -0
- package/src/OpenAPIPath.tsx +65 -0
- package/src/OpenAPIRequestBody.tsx +10 -17
- package/src/OpenAPIResponse.tsx +27 -45
- package/src/OpenAPIResponseExample.tsx +89 -31
- package/src/OpenAPIResponses.tsx +48 -17
- package/src/OpenAPISchema.test.ts +1 -1
- package/src/OpenAPISchema.tsx +129 -108
- package/src/OpenAPISchemaName.tsx +27 -0
- package/src/OpenAPISecurities.tsx +45 -24
- package/src/OpenAPIServerURL.tsx +17 -10
- package/src/OpenAPIServerURLVariable.tsx +2 -4
- package/src/OpenAPISpec.tsx +58 -58
- package/src/OpenAPITabs.tsx +153 -0
- package/src/ScalarApiButton.tsx +84 -7
- package/src/code-samples.test.ts +51 -0
- package/src/code-samples.ts +95 -31
- package/src/generateSchemaExample.ts +26 -153
- package/src/index.ts +3 -2
- package/src/resolveOpenAPIOperation.test.ts +177 -0
- package/src/resolveOpenAPIOperation.ts +164 -0
- package/src/stringifyOpenAPI.ts +6 -0
- package/src/types.ts +17 -10
- package/src/useSyncedTabsGlobalState.ts +23 -0
- package/src/utils.ts +14 -7
- package/dist/InteractiveSection.js +0 -47
- package/dist/Markdown.js +0 -6
- package/dist/OpenAPICodeSample.js +0 -110
- package/dist/OpenAPIOperation.js +0 -38
- package/dist/OpenAPIRequestBody.js +0 -18
- package/dist/OpenAPIResponse.js +0 -32
- package/dist/OpenAPIResponseExample.js +0 -54
- package/dist/OpenAPIResponses.js +0 -18
- package/dist/OpenAPISchema.js +0 -235
- package/dist/OpenAPISchema.test.d.ts +0 -1
- package/dist/OpenAPISchema.test.js +0 -91
- package/dist/OpenAPISecurities.js +0 -42
- package/dist/OpenAPIServerURL.js +0 -51
- package/dist/OpenAPIServerURLVariable.js +0 -10
- package/dist/OpenAPISpec.js +0 -70
- package/dist/ScalarApiButton.js +0 -14
- package/dist/fetchOpenAPIOperation.d.ts +0 -72
- package/dist/fetchOpenAPIOperation.js +0 -124
- package/dist/fetchOpenAPIOperation.test.d.ts +0 -1
- package/dist/fetchOpenAPIOperation.test.js +0 -152
- package/dist/resolveOpenAPIPath.d.ts +0 -7
- package/dist/resolveOpenAPIPath.js +0 -112
- package/dist/resolveOpenAPIPath.test.d.ts +0 -1
- package/dist/resolveOpenAPIPath.test.js +0 -39
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/fetchOpenAPIOperation.test.ts +0 -185
- package/src/fetchOpenAPIOperation.ts +0 -230
- package/src/resolveOpenAPIPath.test.ts +0 -60
- package/src/resolveOpenAPIPath.ts +0 -145
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { it, expect } from 'bun:test';
|
|
2
|
-
|
|
3
|
-
import { resolveOpenAPIPath } from './resolveOpenAPIPath';
|
|
4
|
-
import { OpenAPIFetcher } from './types';
|
|
5
|
-
|
|
6
|
-
const createFetcherForSchema = (schema: any): OpenAPIFetcher => {
|
|
7
|
-
return {
|
|
8
|
-
fetch: async (url) => {
|
|
9
|
-
return schema;
|
|
10
|
-
},
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
it('should resolve a simple path through objects', async () => {
|
|
15
|
-
const resolved = await resolveOpenAPIPath(
|
|
16
|
-
'https://test.com',
|
|
17
|
-
['a', 'b', 'c'],
|
|
18
|
-
createFetcherForSchema({
|
|
19
|
-
a: {
|
|
20
|
-
b: {
|
|
21
|
-
c: 'hello',
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
}),
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
expect(resolved).toBe('hello');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should return undefined if the last part of the path does not exists', async () => {
|
|
31
|
-
const resolved = await resolveOpenAPIPath(
|
|
32
|
-
'https://test.com',
|
|
33
|
-
['a', 'b', 'c'],
|
|
34
|
-
createFetcherForSchema({
|
|
35
|
-
a: {
|
|
36
|
-
b: {
|
|
37
|
-
d: 'hello',
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
}),
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
expect(resolved).toBe(undefined);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should return undefined if a middle part of the path does not exists', async () => {
|
|
47
|
-
const resolved = await resolveOpenAPIPath(
|
|
48
|
-
'https://test.com',
|
|
49
|
-
['a', 'x', 'c'],
|
|
50
|
-
createFetcherForSchema({
|
|
51
|
-
a: {
|
|
52
|
-
b: {
|
|
53
|
-
c: 'hello',
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
}),
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
expect(resolved).toBe(undefined);
|
|
60
|
-
});
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { OpenAPIFetcher } from './types';
|
|
2
|
-
|
|
3
|
-
const SYMBOL_MARKDOWN_PARSED = '__$markdownParsed';
|
|
4
|
-
export const SYMBOL_REF_RESOLVED = '__$refResolved';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Resolve a path in a OpenAPI file.
|
|
8
|
-
* It resolves any reference needed to resolve the path, ignoring other references outside the path.
|
|
9
|
-
*/
|
|
10
|
-
export async function resolveOpenAPIPath<T>(
|
|
11
|
-
url: string,
|
|
12
|
-
dataPath: string[],
|
|
13
|
-
fetcher: OpenAPIFetcher,
|
|
14
|
-
): Promise<T | undefined> {
|
|
15
|
-
const data = await fetcher.fetch(url);
|
|
16
|
-
let value: unknown = data;
|
|
17
|
-
|
|
18
|
-
if (!value) {
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const lastKey = dataPath[dataPath.length - 1];
|
|
23
|
-
dataPath = dataPath.slice(0, -1);
|
|
24
|
-
|
|
25
|
-
for (const part of dataPath) {
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
if (isRef(value[part])) {
|
|
28
|
-
await transformAll(url, value, part, fetcher);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
value = value[part];
|
|
33
|
-
|
|
34
|
-
// If any part along the path is undefined, return undefined.
|
|
35
|
-
if (typeof value !== 'object' || value === null) {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
await transformAll(url, value, lastKey, fetcher);
|
|
41
|
-
|
|
42
|
-
// @ts-expect-error
|
|
43
|
-
return value[lastKey] as T;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Recursively process a part of the OpenAPI spec to resolve all references.
|
|
48
|
-
*/
|
|
49
|
-
async function transformAll(
|
|
50
|
-
url: string,
|
|
51
|
-
data: any,
|
|
52
|
-
key: string | number,
|
|
53
|
-
fetcher: OpenAPIFetcher,
|
|
54
|
-
): Promise<void> {
|
|
55
|
-
const value = data[key];
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
typeof value === 'string' &&
|
|
59
|
-
key === 'description' &&
|
|
60
|
-
fetcher.parseMarkdown &&
|
|
61
|
-
!data[SYMBOL_MARKDOWN_PARSED]
|
|
62
|
-
) {
|
|
63
|
-
// Parse markdown
|
|
64
|
-
data[SYMBOL_MARKDOWN_PARSED] = true;
|
|
65
|
-
data[key] = await fetcher.parseMarkdown(value);
|
|
66
|
-
} else if (
|
|
67
|
-
typeof value === 'string' ||
|
|
68
|
-
typeof value === 'number' ||
|
|
69
|
-
typeof value === 'boolean' ||
|
|
70
|
-
value === null
|
|
71
|
-
) {
|
|
72
|
-
// Primitives
|
|
73
|
-
} else if (typeof value === 'object' && value !== null && SYMBOL_REF_RESOLVED in value) {
|
|
74
|
-
// Ref was already resolved
|
|
75
|
-
} else if (isRef(value)) {
|
|
76
|
-
const ref = value.$ref;
|
|
77
|
-
|
|
78
|
-
// Delete the ref to avoid infinite loop with circular references
|
|
79
|
-
// @ts-ignore
|
|
80
|
-
delete value.$ref;
|
|
81
|
-
|
|
82
|
-
data[key] = await resolveReference(url, ref, fetcher);
|
|
83
|
-
if (data[key]) {
|
|
84
|
-
data[key][SYMBOL_REF_RESOLVED] = extractRefName(ref);
|
|
85
|
-
}
|
|
86
|
-
} else if (Array.isArray(value)) {
|
|
87
|
-
// Recursively resolve all references in the array
|
|
88
|
-
await Promise.all(value.map((item, index) => transformAll(url, value, index, fetcher)));
|
|
89
|
-
} else if (typeof value === 'object' && value !== null) {
|
|
90
|
-
// Recursively resolve all references in the object
|
|
91
|
-
const keys = Object.keys(value);
|
|
92
|
-
for (const key of keys) {
|
|
93
|
-
await transformAll(url, value, key, fetcher);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function resolveReference(
|
|
99
|
-
origin: string,
|
|
100
|
-
ref: string,
|
|
101
|
-
fetcher: OpenAPIFetcher,
|
|
102
|
-
): Promise<any> {
|
|
103
|
-
const parsed = parseReference(origin, ref);
|
|
104
|
-
return resolveOpenAPIPath(parsed.url, parsed.dataPath, fetcher);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function parseReference(origin: string, ref: string): { url: string; dataPath: string[] } {
|
|
108
|
-
if (!ref) {
|
|
109
|
-
return {
|
|
110
|
-
url: origin,
|
|
111
|
-
dataPath: [],
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (ref.startsWith('#')) {
|
|
116
|
-
// Local references
|
|
117
|
-
const dataPath = ref.split('/').filter(Boolean).slice(1);
|
|
118
|
-
return {
|
|
119
|
-
url: origin,
|
|
120
|
-
dataPath,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Absolute references
|
|
125
|
-
const url = new URL(ref, origin);
|
|
126
|
-
if (url.hash) {
|
|
127
|
-
const hash = url.hash;
|
|
128
|
-
url.hash = '';
|
|
129
|
-
return parseReference(url.toString(), hash);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
url: url.toString(),
|
|
134
|
-
dataPath: [],
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function extractRefName(ref: string): string {
|
|
139
|
-
const parts = ref.split('/');
|
|
140
|
-
return parts[parts.length - 1];
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function isRef(ref: any): ref is { $ref: string } {
|
|
144
|
-
return typeof ref === 'object' && ref !== null && '$ref' in ref && ref.$ref;
|
|
145
|
-
}
|