@gitbook/react-openapi 1.1.6 → 1.1.8
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 +25 -0
- package/dist/InteractiveSection.d.ts +0 -2
- package/dist/InteractiveSection.jsx +3 -4
- package/dist/OpenAPICodeSample.jsx +4 -4
- package/dist/OpenAPICodeSampleInteractive.d.ts +4 -3
- package/dist/OpenAPICodeSampleInteractive.jsx +22 -15
- package/dist/OpenAPICopyButton.d.ts +7 -0
- package/dist/OpenAPICopyButton.jsx +6 -6
- package/dist/OpenAPIOperation.jsx +21 -1
- package/dist/OpenAPIPath.jsx +2 -2
- package/dist/OpenAPIRequestBody.jsx +1 -1
- package/dist/OpenAPIResponse.jsx +1 -1
- package/dist/OpenAPIResponses.jsx +2 -2
- package/dist/OpenAPISchema.d.ts +5 -14
- package/dist/OpenAPISchema.jsx +79 -28
- package/dist/OpenAPISchemaName.jsx +1 -0
- package/dist/OpenAPISchemaServer.d.ts +12 -0
- package/dist/OpenAPISchemaServer.jsx +8 -0
- package/dist/OpenAPISpec.d.ts +0 -6
- package/dist/OpenAPISpec.jsx +5 -11
- package/dist/OpenAPITabs.jsx +3 -11
- package/dist/code-samples.js +44 -9
- package/dist/decycle.d.ts +2 -0
- package/dist/decycle.js +70 -0
- package/dist/schemas/OpenAPISchemas.d.ts +4 -0
- package/dist/schemas/OpenAPISchemas.jsx +6 -6
- package/dist/schemas/resolveOpenAPISchemas.d.ts +2 -6
- package/dist/schemas/resolveOpenAPISchemas.js +1 -21
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types.d.ts +2 -5
- package/package.json +1 -1
- package/src/InteractiveSection.tsx +2 -6
- package/src/OpenAPICodeSample.tsx +16 -5
- package/src/OpenAPICodeSampleInteractive.tsx +53 -28
- package/src/OpenAPICopyButton.tsx +17 -4
- package/src/OpenAPIOperation.tsx +39 -2
- package/src/OpenAPIPath.tsx +2 -2
- package/src/OpenAPIRequestBody.tsx +1 -1
- package/src/OpenAPIResponse.tsx +4 -4
- package/src/OpenAPIResponses.tsx +1 -5
- package/src/OpenAPISchema.tsx +152 -58
- package/src/OpenAPISchemaName.tsx +3 -0
- package/src/OpenAPISchemaServer.tsx +34 -0
- package/src/OpenAPISpec.tsx +13 -11
- package/src/OpenAPITabs.tsx +3 -13
- package/src/code-samples.test.ts +69 -1
- package/src/code-samples.ts +45 -9
- package/src/decycle.ts +68 -0
- package/src/schemas/OpenAPISchemas.tsx +11 -6
- package/src/schemas/resolveOpenAPISchemas.ts +3 -31
- package/src/types.ts +6 -6
package/src/decycle.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Forked from: https://github.com/YChebotaev/json-decycle/blob/master/src/index.ts
|
|
2
|
+
// Replaced `$ref` with `$reference` to avoid conflicts with OpenAPI
|
|
3
|
+
|
|
4
|
+
const isObject = (value: any): value is object =>
|
|
5
|
+
typeof value === 'object' &&
|
|
6
|
+
value != null &&
|
|
7
|
+
!(value instanceof Boolean) &&
|
|
8
|
+
!(value instanceof Date) &&
|
|
9
|
+
!(value instanceof Number) &&
|
|
10
|
+
!(value instanceof RegExp) &&
|
|
11
|
+
!(value instanceof String);
|
|
12
|
+
|
|
13
|
+
const toPointer = (parts: string[]) =>
|
|
14
|
+
`#${parts.map((part) => String(part).replace(/~/g, '~0').replace(/\//g, '~1')).join('/')}`;
|
|
15
|
+
|
|
16
|
+
export const decycle = () => {
|
|
17
|
+
const paths = new WeakMap();
|
|
18
|
+
|
|
19
|
+
return function replacer(this: any, key: string | symbol, value: any) {
|
|
20
|
+
if (key !== '$reference' && isObject(value)) {
|
|
21
|
+
const seen = paths.has(value);
|
|
22
|
+
|
|
23
|
+
if (seen) {
|
|
24
|
+
return { $reference: toPointer(paths.get(value)) };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
paths.set(value, [...(paths.get(this) ?? []), key]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function retrocycle() {
|
|
35
|
+
const parents = new WeakMap();
|
|
36
|
+
const keys = new WeakMap();
|
|
37
|
+
const refs = new Set();
|
|
38
|
+
|
|
39
|
+
function dereference(this: { [k: string]: any }, ref: { $reference: string }) {
|
|
40
|
+
const parts = ref.$reference.slice(1).split('/');
|
|
41
|
+
let key: any;
|
|
42
|
+
let value = this;
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < parts.length; i++) {
|
|
45
|
+
key = parts[i]?.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
46
|
+
value = value[key];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const parent = parents.get(ref);
|
|
50
|
+
parent[keys.get(ref)] = value;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return function reviver(this: object, key: string | symbol, value: any) {
|
|
54
|
+
if (key === '$reference') {
|
|
55
|
+
refs.add(this);
|
|
56
|
+
} else if (isObject(value)) {
|
|
57
|
+
const isRoot = key === '' && Object.keys(this).length === 1;
|
|
58
|
+
if (isRoot) {
|
|
59
|
+
refs.forEach(dereference as any, this);
|
|
60
|
+
} else {
|
|
61
|
+
parents.set(value, this);
|
|
62
|
+
keys.set(value, key);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return value;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import clsx from 'clsx';
|
|
2
2
|
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
|
|
3
|
-
import { OpenAPIRootSchema } from '../
|
|
3
|
+
import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
|
|
4
4
|
import { Section, SectionBody } from '../StaticSection';
|
|
5
5
|
import type { OpenAPIClientContext, OpenAPIContextProps, OpenAPISchemasData } from '../types';
|
|
6
6
|
|
|
@@ -16,8 +16,12 @@ export function OpenAPISchemas(props: {
|
|
|
16
16
|
className?: string;
|
|
17
17
|
data: OpenAPISchemasData;
|
|
18
18
|
context: OpenAPISchemasContextProps;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to show the schema directly if there is only one.
|
|
21
|
+
*/
|
|
22
|
+
grouped?: boolean;
|
|
19
23
|
}) {
|
|
20
|
-
const { className, data, context } = props;
|
|
24
|
+
const { className, data, context, grouped } = props;
|
|
21
25
|
const { schemas } = data;
|
|
22
26
|
|
|
23
27
|
const clientContext: OpenAPIClientContext = {
|
|
@@ -32,7 +36,7 @@ export function OpenAPISchemas(props: {
|
|
|
32
36
|
|
|
33
37
|
return (
|
|
34
38
|
<div className={clsx('openapi-schemas', className)}>
|
|
35
|
-
<OpenAPIRootSchemasSchema schemas={schemas} context={clientContext} />
|
|
39
|
+
<OpenAPIRootSchemasSchema grouped={grouped} schemas={schemas} context={clientContext} />
|
|
36
40
|
</div>
|
|
37
41
|
);
|
|
38
42
|
}
|
|
@@ -44,11 +48,12 @@ export function OpenAPISchemas(props: {
|
|
|
44
48
|
function OpenAPIRootSchemasSchema(props: {
|
|
45
49
|
schemas: OpenAPISchemasData['schemas'];
|
|
46
50
|
context: OpenAPIClientContext;
|
|
51
|
+
grouped?: boolean;
|
|
47
52
|
}) {
|
|
48
|
-
const { schemas, context } = props;
|
|
53
|
+
const { schemas, context, grouped } = props;
|
|
49
54
|
|
|
50
|
-
// If there is only one model, we show it directly.
|
|
51
|
-
if (schemas.length === 1) {
|
|
55
|
+
// If there is only one model and we are not grouping, we show it directly.
|
|
56
|
+
if (schemas.length === 1 && !grouped) {
|
|
52
57
|
const schema = schemas?.[0]?.schema;
|
|
53
58
|
|
|
54
59
|
if (!schema) {
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type OpenAPIV3,
|
|
4
|
-
type OpenAPIV3_1,
|
|
5
|
-
type OpenAPIV3xDocument,
|
|
6
|
-
shouldIgnoreEntity,
|
|
7
|
-
} from '@gitbook/openapi-parser';
|
|
1
|
+
import type { Filesystem, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
|
|
2
|
+
import { filterSelectedOpenAPISchemas } from '@gitbook/openapi-parser';
|
|
8
3
|
import { dereferenceFilesystem } from '../dereference';
|
|
9
|
-
import type {
|
|
4
|
+
import type { OpenAPISchemasData } from '../types';
|
|
10
5
|
|
|
11
6
|
//!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
|
|
12
7
|
|
|
@@ -32,26 +27,3 @@ export async function resolveOpenAPISchemas(
|
|
|
32
27
|
|
|
33
28
|
return { schemas };
|
|
34
29
|
}
|
|
35
|
-
/**
|
|
36
|
-
* Extract selected schemas from the OpenAPI document.
|
|
37
|
-
*/
|
|
38
|
-
export function filterSelectedOpenAPISchemas(
|
|
39
|
-
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
|
|
40
|
-
selectedSchemas: string[]
|
|
41
|
-
): OpenAPISchema[] {
|
|
42
|
-
const componentsSchemas = schema.components?.schemas ?? {};
|
|
43
|
-
|
|
44
|
-
// Preserve the order of the selected schemas
|
|
45
|
-
return selectedSchemas
|
|
46
|
-
.map((name) => {
|
|
47
|
-
const schema = componentsSchemas[name];
|
|
48
|
-
if (schema && !shouldIgnoreEntity(schema)) {
|
|
49
|
-
return {
|
|
50
|
-
name,
|
|
51
|
-
schema,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
return null;
|
|
55
|
-
})
|
|
56
|
-
.filter((schema): schema is OpenAPISchema => !!schema);
|
|
57
|
-
}
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
OpenAPICustomOperationProperties,
|
|
3
3
|
OpenAPICustomSpecProperties,
|
|
4
|
+
OpenAPISchema,
|
|
4
5
|
OpenAPIV3,
|
|
5
6
|
} from '@gitbook/openapi-parser';
|
|
6
7
|
|
|
@@ -12,7 +13,11 @@ export interface OpenAPIContextProps extends OpenAPIClientContext {
|
|
|
12
13
|
/**
|
|
13
14
|
* Render the heading of the operation.
|
|
14
15
|
*/
|
|
15
|
-
renderHeading: (props: {
|
|
16
|
+
renderHeading: (props: {
|
|
17
|
+
deprecated: boolean;
|
|
18
|
+
title: string;
|
|
19
|
+
stability?: string;
|
|
20
|
+
}) => React.ReactNode;
|
|
16
21
|
/**
|
|
17
22
|
* Render the document of the operation.
|
|
18
23
|
*/
|
|
@@ -56,11 +61,6 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
|
|
56
61
|
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
|
57
62
|
}
|
|
58
63
|
|
|
59
|
-
export type OpenAPISchema = {
|
|
60
|
-
name: string;
|
|
61
|
-
schema: OpenAPIV3.SchemaObject;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
64
|
export interface OpenAPISchemasData {
|
|
65
65
|
/** Components schemas to be used for schemas */
|
|
66
66
|
schemas: OpenAPISchema[];
|