mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 15:45:13 +00:00
Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser (#3054)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
'@gitbook/openapi-parser': patch
|
||||||
|
'@gitbook/react-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser
|
||||||
@@ -6,4 +6,5 @@ export type * from '@scalar/openapi-types';
|
|||||||
export * from './error';
|
export * from './error';
|
||||||
export * from './helpers/shouldIgnoreEntity';
|
export * from './helpers/shouldIgnoreEntity';
|
||||||
export * from './traverse';
|
export * from './traverse';
|
||||||
|
export * from './schemas';
|
||||||
export type * from './types';
|
export type * from './types';
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -79,3 +79,8 @@ export type FilesystemEntry<T extends AnyObject> = {
|
|||||||
filename: string;
|
filename: string;
|
||||||
specification: T;
|
specification: T;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type OpenAPISchema = {
|
||||||
|
name: string;
|
||||||
|
schema: OpenAPIV3.SchemaObject;
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,12 +1,7 @@
|
|||||||
import {
|
import type { Filesystem, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
|
||||||
type Filesystem,
|
import { filterSelectedOpenAPISchemas } from '@gitbook/openapi-parser';
|
||||||
type OpenAPIV3,
|
|
||||||
type OpenAPIV3_1,
|
|
||||||
type OpenAPIV3xDocument,
|
|
||||||
shouldIgnoreEntity,
|
|
||||||
} from '@gitbook/openapi-parser';
|
|
||||||
import { dereferenceFilesystem } from '../dereference';
|
import { dereferenceFilesystem } from '../dereference';
|
||||||
import type { OpenAPISchema, OpenAPISchemasData } from '../types';
|
import type { OpenAPISchemasData } from '../types';
|
||||||
|
|
||||||
//!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
|
//!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
|
||||||
|
|
||||||
@@ -32,26 +27,3 @@ export async function resolveOpenAPISchemas(
|
|||||||
|
|
||||||
return { schemas };
|
return { schemas };
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import type {
|
import type {
|
||||||
OpenAPICustomOperationProperties,
|
OpenAPICustomOperationProperties,
|
||||||
OpenAPICustomSpecProperties,
|
OpenAPICustomSpecProperties,
|
||||||
|
OpenAPISchema,
|
||||||
OpenAPIV3,
|
OpenAPIV3,
|
||||||
} from '@gitbook/openapi-parser';
|
} from '@gitbook/openapi-parser';
|
||||||
|
|
||||||
@@ -56,11 +57,6 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
|||||||
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type OpenAPISchema = {
|
|
||||||
name: string;
|
|
||||||
schema: OpenAPIV3.SchemaObject;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface OpenAPISchemasData {
|
export interface OpenAPISchemasData {
|
||||||
/** Components schemas to be used for schemas */
|
/** Components schemas to be used for schemas */
|
||||||
schemas: OpenAPISchema[];
|
schemas: OpenAPISchema[];
|
||||||
|
|||||||
Reference in New Issue
Block a user