Skip to content

Commit acc529f

Browse files
committed
Refactor before review
1 parent 53de768 commit acc529f

File tree

6 files changed

+46
-44
lines changed

6 files changed

+46
-44
lines changed

packages/react-openapi/src/OpenAPISchema.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ function OpenAPISchemaAlternative(props: {
166166
/**
167167
* Render a circular reference to a schema.
168168
*/
169-
export function OpenAPISchemaCircularRef(props: { id: string; schema: OpenAPIV3.SchemaObject }) {
169+
function OpenAPISchemaCircularRef(props: { id: string; schema: OpenAPIV3.SchemaObject }) {
170170
const { id, schema } = props;
171171

172172
return (
@@ -180,7 +180,7 @@ export function OpenAPISchemaCircularRef(props: { id: string; schema: OpenAPIV3.
180180
/**
181181
* Render the enum value for a schema.
182182
*/
183-
export function OpenAPISchemaEnum(props: { enumValues: any[] }) {
183+
function OpenAPISchemaEnum(props: { enumValues: any[] }) {
184184
const { enumValues } = props;
185185

186186
return (
@@ -198,7 +198,10 @@ export function OpenAPISchemaEnum(props: { enumValues: any[] }) {
198198
);
199199
}
200200

201-
export function OpenAPISchemaPresentation(props: { property: OpenAPISchemaPropertyEntry }) {
201+
/**
202+
* Render the top row of a schema. e.g: name, type, and required status.
203+
*/
204+
function OpenAPISchemaPresentation(props: { property: OpenAPISchemaPropertyEntry }) {
202205
const {
203206
property: { schema, propertyName, required },
204207
} = props;
@@ -245,9 +248,7 @@ export function OpenAPISchemaPresentation(props: { property: OpenAPISchemaProper
245248
/**
246249
* Get the sub-properties of a schema.
247250
*/
248-
export function getSchemaProperties(
249-
schema: OpenAPIV3.SchemaObject
250-
): null | OpenAPISchemaPropertyEntry[] {
251+
function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISchemaPropertyEntry[] {
251252
// check array AND schema.items as this is sometimes null despite what the type indicates
252253
if (schema.type === 'array' && schema.items && !checkIsReference(schema.items)) {
253254
const items = schema.items;
@@ -360,7 +361,7 @@ function flattenAlternatives(
360361
}, []);
361362
}
362363

363-
export function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
364+
function getSchemaTitle(schema: OpenAPIV3.SchemaObject): string {
364365
// Otherwise try to infer a nice title
365366
let type = 'any';
366367

packages/react-openapi/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './models';
22
export * from './OpenAPIOperation';
33
export * from './OpenAPIOperationContext';
4-
export { resolveOpenAPIOperation } from './resolveOpenAPIOperation';
4+
export * from './resolveOpenAPIOperation';
55
export type { OpenAPIModelsData, OpenAPIOperationData } from './types';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { type Filesystem, type OpenAPIV3xDocument, dereference } from '@gitbook/openapi-parser';
2+
3+
const dereferenceCache = new WeakMap<Filesystem, Promise<OpenAPIV3xDocument>>();
4+
5+
/**
6+
* Memoized version of `dereferenceSchema`.
7+
*/
8+
export function memoDereferenceFilesystem(filesystem: Filesystem): Promise<OpenAPIV3xDocument> {
9+
if (dereferenceCache.has(filesystem)) {
10+
return dereferenceCache.get(filesystem) as Promise<OpenAPIV3xDocument>;
11+
}
12+
13+
const promise = dereferenceFilesystem(filesystem);
14+
dereferenceCache.set(filesystem, promise);
15+
return promise;
16+
}
17+
18+
/**
19+
* Dereference an OpenAPI schema.
20+
*/
21+
async function dereferenceFilesystem(filesystem: Filesystem): Promise<OpenAPIV3xDocument> {
22+
const result = await dereference(filesystem);
23+
24+
if (!result.schema) {
25+
throw new Error('Failed to dereference OpenAPI document');
26+
}
27+
28+
return result.schema as OpenAPIV3xDocument;
29+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { OpenAPIModels } from './OpenAPIModels';
1+
export * from './OpenAPIModels';
22
export * from './resolveOpenAPIModels';

packages/react-openapi/src/models/resolveOpenAPIModels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
type OpenAPIV3xDocument,
66
shouldIgnoreEntity,
77
} from '@gitbook/openapi-parser';
8-
import { memoDereferenceFilesystem } from '../resolveOpenAPIOperation';
8+
import { memoDereferenceFilesystem } from '../memoDereferenceFilesystem';
99
import type { OpenAPIModelsData } from '../types';
1010

1111
//!!TODO: We should return only the models that are used in the block. Still a WIP awaiting future work.

packages/react-openapi/src/resolveOpenAPIOperation.ts

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { fromJSON, toJSON } from 'flatted';
22

3-
import {
4-
type Filesystem,
5-
type OpenAPIV3,
6-
type OpenAPIV3_1,
7-
type OpenAPIV3xDocument,
8-
dereference,
3+
import type {
4+
Filesystem,
5+
OpenAPIV3,
6+
OpenAPIV3_1,
7+
OpenAPIV3xDocument,
98
} from '@gitbook/openapi-parser';
9+
import { memoDereferenceFilesystem } from './memoDereferenceFilesystem';
1010
import type { OpenAPIOperationData } from './types';
1111
import { checkIsReference } from './utils';
1212

@@ -69,34 +69,6 @@ export async function resolveOpenAPIOperation(
6969
};
7070
}
7171

72-
const dereferenceCache = new WeakMap<Filesystem, Promise<OpenAPIV3xDocument>>();
73-
74-
/**
75-
* Memoized version of `dereferenceSchema`.
76-
*/
77-
export function memoDereferenceFilesystem(filesystem: Filesystem): Promise<OpenAPIV3xDocument> {
78-
if (dereferenceCache.has(filesystem)) {
79-
return dereferenceCache.get(filesystem) as Promise<OpenAPIV3xDocument>;
80-
}
81-
82-
const promise = dereferenceFilesystem(filesystem);
83-
dereferenceCache.set(filesystem, promise);
84-
return promise;
85-
}
86-
87-
/**
88-
* Dereference an OpenAPI schema.
89-
*/
90-
async function dereferenceFilesystem(filesystem: Filesystem): Promise<OpenAPIV3xDocument> {
91-
const result = await dereference(filesystem);
92-
93-
if (!result.schema) {
94-
throw new Error('Failed to dereference OpenAPI document');
95-
}
96-
97-
return result.schema as OpenAPIV3xDocument;
98-
}
99-
10072
/**
10173
* Get a path object from its path.
10274
*/

0 commit comments

Comments
 (0)