diff --git a/.changeset/famous-points-rest.md b/.changeset/famous-points-rest.md new file mode 100644 index 000000000..cbfa96cac --- /dev/null +++ b/.changeset/famous-points-rest.md @@ -0,0 +1,6 @@ +--- +'@gitbook/openapi-parser': patch +'@gitbook/react-openapi': patch +--- + +Move filterSelectedOpenAPISchemas to @gitbook/openapi-parser diff --git a/packages/openapi-parser/src/index.ts b/packages/openapi-parser/src/index.ts index 8b99bb9cb..8028a6e72 100644 --- a/packages/openapi-parser/src/index.ts +++ b/packages/openapi-parser/src/index.ts @@ -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'; diff --git a/packages/openapi-parser/src/schemas.ts b/packages/openapi-parser/src/schemas.ts new file mode 100644 index 000000000..b753cceee --- /dev/null +++ b/packages/openapi-parser/src/schemas.ts @@ -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); +} diff --git a/packages/openapi-parser/src/types.ts b/packages/openapi-parser/src/types.ts index 696cc02a2..d94436a89 100644 --- a/packages/openapi-parser/src/types.ts +++ b/packages/openapi-parser/src/types.ts @@ -79,3 +79,8 @@ export type FilesystemEntry = { filename: string; specification: T; }; + +export type OpenAPISchema = { + name: string; + schema: OpenAPIV3.SchemaObject; +}; diff --git a/packages/react-openapi/src/schemas/resolveOpenAPISchemas.ts b/packages/react-openapi/src/schemas/resolveOpenAPISchemas.ts index d061f0a8d..2e03c1de5 100644 --- a/packages/react-openapi/src/schemas/resolveOpenAPISchemas.ts +++ b/packages/react-openapi/src/schemas/resolveOpenAPISchemas.ts @@ -1,12 +1,7 @@ -import { - type Filesystem, - type OpenAPIV3, - type OpenAPIV3_1, - type OpenAPIV3xDocument, - shouldIgnoreEntity, -} from '@gitbook/openapi-parser'; +import type { Filesystem, OpenAPIV3xDocument } from '@gitbook/openapi-parser'; +import { filterSelectedOpenAPISchemas } from '@gitbook/openapi-parser'; 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. @@ -32,26 +27,3 @@ export async function resolveOpenAPISchemas( 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); -} diff --git a/packages/react-openapi/src/types.ts b/packages/react-openapi/src/types.ts index 9f7829055..6557fb3e4 100644 --- a/packages/react-openapi/src/types.ts +++ b/packages/react-openapi/src/types.ts @@ -1,6 +1,7 @@ import type { OpenAPICustomOperationProperties, OpenAPICustomSpecProperties, + OpenAPISchema, OpenAPIV3, } from '@gitbook/openapi-parser'; @@ -56,11 +57,6 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties { securities: [string, OpenAPIV3.SecuritySchemeObject][]; } -export type OpenAPISchema = { - name: string; - schema: OpenAPIV3.SchemaObject; -}; - export interface OpenAPISchemasData { /** Components schemas to be used for schemas */ schemas: OpenAPISchema[];