Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser (#3054)

This commit is contained in:
Nolann B.
2025-03-27 15:53:41 +01:00
committed by GitHub
parent 5b2bf828e3
commit 7bb37c7f0a
6 changed files with 43 additions and 36 deletions
+1
View File
@@ -6,4 +6,5 @@ export type * from '@scalar/openapi-types';
export * from './error';
export * from './helpers/shouldIgnoreEntity';
export * from './traverse';
export * from './schemas';
export type * from './types';
+27
View File
@@ -0,0 +1,27 @@
import type { OpenAPIV3, OpenAPIV3_1 } from '@scalar/openapi-types';
import { shouldIgnoreEntity } from './helpers/shouldIgnoreEntity';
import type { OpenAPISchema } from './types';
/**
* Extract selected schemas from the OpenAPI document.
*/
export function filterSelectedOpenAPISchemas(
schema: OpenAPIV3.Document | OpenAPIV3_1.Document,
selectedSchemas: string[]
): OpenAPISchema[] {
const componentsSchemas = schema.components?.schemas ?? {};
// Preserve the order of the selected schemas
return selectedSchemas
.map((name) => {
const schema = componentsSchemas[name];
if (schema && !shouldIgnoreEntity(schema)) {
return {
name,
schema,
};
}
return null;
})
.filter((schema): schema is OpenAPISchema => !!schema);
}
+5
View File
@@ -79,3 +79,8 @@ export type FilesystemEntry<T extends AnyObject> = {
filename: string;
specification: T;
};
export type OpenAPISchema = {
name: string;
schema: OpenAPIV3.SchemaObject;
};