Skip to content

Commit c516864

Browse files
committed
Fixes
1 parent 934aa48 commit c516864

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

packages/gitbook/src/lib/openapi/fetch.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,29 @@ export type AnyOpenAPIOperationBlock = DocumentBlockOpenAPI | DocumentBlockOpenA
1919

2020
export type OpenAPIBlockType = 'operation' | 'models';
2121

22-
export type ResolveOpenAPIOperationBlockResult = {
23-
error: undefined;
22+
export type OpenAPIOperationResult = {
2423
data: OpenAPIOperationData | null;
24+
error: undefined;
2525
specUrl: string | null;
2626
};
2727

28-
export type ResolveOpenAPIModelsBlockResult = {
29-
error: undefined;
28+
export type OpenAPIModelsResult = {
3029
data: OpenAPIModelsData | null;
30+
error: undefined;
3131
specUrl: string | null;
3232
};
3333

34-
export type ResolveOpenAPIBlockResult<T extends OpenAPIBlockType = OpenAPIBlockType> =
35-
| (T extends 'operation' ? ResolveOpenAPIOperationBlockResult : ResolveOpenAPIModelsBlockResult)
36-
| ResolveOpenAPIBlockError;
34+
export type OpenAPIErrorResult = {
35+
data?: undefined;
36+
specUrl?: undefined;
37+
error: OpenAPIParseError;
38+
};
3739

38-
type ResolveOpenAPIBlockError = { error: OpenAPIParseError; data?: undefined; specUrl?: undefined };
40+
export type OpenAPIBlockResult<T extends OpenAPIBlockType = 'operation'> =
41+
| (T extends 'operation' ? OpenAPIOperationResult : OpenAPIModelsResult)
42+
| OpenAPIErrorResult;
3943

40-
type ResolveOpenAPIBlockArgs<T extends OpenAPIBlockType = OpenAPIBlockType> = {
44+
type ResolveOpenAPIBlockArgs<T = OpenAPIBlockType> = {
4145
block: AnyOpenAPIOperationBlock;
4246
context: GitBookAnyContext;
4347
/**
@@ -48,21 +52,21 @@ type ResolveOpenAPIBlockArgs<T extends OpenAPIBlockType = OpenAPIBlockType> = {
4852
type?: T | OpenAPIBlockType;
4953
};
5054

51-
const weakmap = new WeakMap<AnyOpenAPIOperationBlock, Promise<ResolveOpenAPIBlockResult>>();
55+
const weakmap = new WeakMap<AnyOpenAPIOperationBlock, Promise<OpenAPIBlockResult>>();
5256

5357
/**
5458
* Cache the result of resolving an OpenAPI block.
5559
* It is important because the resolve is called in sections and in the block itself.
5660
*/
5761
export function resolveOpenAPIBlock<T extends OpenAPIBlockType = 'operation'>(
5862
args: ResolveOpenAPIBlockArgs<T>
59-
): Promise<ResolveOpenAPIBlockResult<T>> {
63+
): Promise<OpenAPIBlockResult<T>> {
6064
if (weakmap.has(args.block)) {
61-
return weakmap.get(args.block) as Promise<ResolveOpenAPIBlockResult<T>>;
65+
return weakmap.get(args.block);
6266
}
6367

6468
const result = baseResolveOpenAPIBlock(args);
65-
weakmap.set(args.block, result as Promise<ResolveOpenAPIBlockResult<T>>);
69+
weakmap.set(args.block, result as Promise<OpenAPIBlockResult>);
6670
return result;
6771
}
6872

@@ -71,17 +75,17 @@ export function resolveOpenAPIBlock<T extends OpenAPIBlockType = 'operation'>(
7175
*/
7276
async function baseResolveOpenAPIBlock<T extends OpenAPIBlockType = 'operation'>(
7377
args: ResolveOpenAPIBlockArgs<T>
74-
): Promise<ResolveOpenAPIBlockResult<T>> {
78+
): Promise<OpenAPIBlockResult<T>> {
7579
const { context, block, type = 'operation' } = args;
7680
if (!block.data.path || !block.data.method) {
77-
return { data: null, specUrl: null } as ResolveOpenAPIBlockResult<T>;
81+
return createResults(null);
7882
}
7983

8084
const ref = block.data.ref;
8185
const resolved = ref ? await resolveContentRef(ref, context) : null;
8286

8387
if (!resolved) {
84-
return { data: null, specUrl: null } as ResolveOpenAPIBlockResult<T>;
88+
return createResults(null);
8589
}
8690

8791
try {
@@ -93,17 +97,17 @@ async function baseResolveOpenAPIBlock<T extends OpenAPIBlockType = 'operation'>
9397
return fetchFilesystem(resolved.href);
9498
})();
9599

100+
let data: OpenAPIOperationData | OpenAPIModelsData | null = null;
96101
if (type === 'models') {
97-
const data = await resolveOpenAPIModels(filesystem);
98-
return { data: data, specUrl: resolved.href } as ResolveOpenAPIBlockResult<T>;
102+
data = await resolveOpenAPIModels(filesystem);
103+
} else {
104+
data = await resolveOpenAPIOperation(filesystem, {
105+
path: block.data.path,
106+
method: block.data.method,
107+
});
99108
}
100109

101-
const data = await resolveOpenAPIOperation(filesystem, {
102-
path: block.data.path,
103-
method: block.data.method,
104-
});
105-
106-
return { data, specUrl: resolved.href } as ResolveOpenAPIBlockResult<T>;
110+
return createResults(data, resolved.href);
107111
} catch (error) {
108112
if (error instanceof OpenAPIParseError) {
109113
return { error };
@@ -175,3 +179,17 @@ async function fetchFilesystemUncached(
175179

176180
return richFilesystem;
177181
}
182+
183+
/**
184+
* Create a result for OpenAPI based on the type.
185+
*/
186+
function createResults<T extends OpenAPIOperationData | OpenAPIModelsData>(
187+
data: T | null,
188+
specUrl?: string
189+
): OpenAPIBlockResult<T> {
190+
return {
191+
error: undefined,
192+
data,
193+
specUrl,
194+
};
195+
}

packages/react-openapi/src/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export function createStateKey(key: string, scope?: string) {
1111
return scope ? `${scope}_${key}` : key;
1212
}
1313

14+
/**
15+
* Check if an object has a description. Either at the root level or in items.
16+
*/
1417
function hasDescription(object: AnyObject) {
1518
if (Array.isArray(object)) {
1619
return object.some(hasDescription);
@@ -23,6 +26,7 @@ function hasDescription(object: AnyObject) {
2326
* Resolve the description of an object.
2427
*/
2528
export function resolveDescription(object: OpenAPIV3.SchemaObject | AnyObject) {
29+
// If the object has items and has a description, we resolve the description from items
2630
if ('items' in object && typeof object.items === 'object' && hasDescription(object.items)) {
2731
if (Array.isArray(object.items)) {
2832
return resolveDescription(object.items[0]);
@@ -69,7 +73,7 @@ export function resolveFirstExample(object: AnyObject) {
6973
return formatExample(object.example);
7074
}
7175

72-
// Resolve example from items if any
76+
// Resolve example from items if it exists
7377
if (object.items && typeof object.items === 'object') {
7478
if (Array.isArray(object.items)) {
7579
return formatExample(object.items[0].example);
@@ -139,6 +143,9 @@ function formatExample(example: unknown): string {
139143
return stringifyOpenAPI(example);
140144
}
141145

146+
/**
147+
* Check if an example should be displayed.
148+
*/
142149
function shouldDisplayExample(schema: OpenAPIV3.SchemaObject): boolean {
143150
return (
144151
(typeof schema.example === 'string' && !!schema.example) ||

0 commit comments

Comments
 (0)