Skip to content

Commit 7bb37c7

Browse files
authored
Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser (#3054)
1 parent 5b2bf82 commit 7bb37c7

File tree

6 files changed

+43
-36
lines changed

6 files changed

+43
-36
lines changed

.changeset/famous-points-rest.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@gitbook/openapi-parser': patch
3+
'@gitbook/react-openapi': patch
4+
---
5+
6+
Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser

packages/openapi-parser/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export type * from '@scalar/openapi-types';
66
export * from './error';
77
export * from './helpers/shouldIgnoreEntity';
88
export * from './traverse';
9+
export * from './schemas';
910
export type * from './types';
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { OpenAPIV3, OpenAPIV3_1 } from '@scalar/openapi-types';
2+
import { shouldIgnoreEntity } from './helpers/shouldIgnoreEntity';
3+
import type { OpenAPISchema } from './types';
4+
5+
/**
6+
* Extract selected schemas from the OpenAPI document.
7+
*/
8+
export function filterSelectedOpenAPISchemas(
9+
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
10+
selectedSchemas: string[]
11+
): OpenAPISchema[] {
12+
const componentsSchemas = schema.components?.schemas ?? {};
13+
14+
// Preserve the order of the selected schemas
15+
return selectedSchemas
16+
.map((name) => {
17+
const schema = componentsSchemas[name];
18+
if (schema && !shouldIgnoreEntity(schema)) {
19+
return {
20+
name,
21+
schema,
22+
};
23+
}
24+
return null;
25+
})
26+
.filter((schema): schema is OpenAPISchema => !!schema);
27+
}

packages/openapi-parser/src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ export type FilesystemEntry<T extends AnyObject> = {
7979
filename: string;
8080
specification: T;
8181
};
82+
83+
export type OpenAPISchema = {
84+
name: string;
85+
schema: OpenAPIV3.SchemaObject;
86+
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import {
2-
type Filesystem,
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';
83
import { dereferenceFilesystem } from '../dereference';
9-
import type { OpenAPISchema, OpenAPISchemasData } from '../types';
4+
import type { OpenAPISchemasData } from '../types';
105

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

@@ -32,26 +27,3 @@ export async function resolveOpenAPISchemas(
3227

3328
return { schemas };
3429
}
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-
}

packages/react-openapi/src/types.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
OpenAPICustomOperationProperties,
33
OpenAPICustomSpecProperties,
4+
OpenAPISchema,
45
OpenAPIV3,
56
} from '@gitbook/openapi-parser';
67

@@ -56,11 +57,6 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
5657
securities: [string, OpenAPIV3.SecuritySchemeObject][];
5758
}
5859

59-
export type OpenAPISchema = {
60-
name: string;
61-
schema: OpenAPIV3.SchemaObject;
62-
};
63-
6460
export interface OpenAPISchemasData {
6561
/** Components schemas to be used for schemas */
6662
schemas: OpenAPISchema[];

0 commit comments

Comments
 (0)