From 7c9f70e300005f2985ccf89a9ecb51083dedbdf4 Mon Sep 17 00:00:00 2001 From: Nolann Biron Date: Mon, 24 Mar 2025 09:36:16 +0100 Subject: [PATCH] Add option to pass scalar plugins --- .changeset/bright-glasses-peel.md | 5 +++++ packages/openapi-parser/src/filesystem.ts | 23 +++++++++-------------- packages/openapi-parser/src/parse.ts | 8 +++++++- packages/openapi-parser/src/v3.ts | 8 ++++++-- 4 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 .changeset/bright-glasses-peel.md diff --git a/.changeset/bright-glasses-peel.md b/.changeset/bright-glasses-peel.md new file mode 100644 index 0000000000..b7b7049945 --- /dev/null +++ b/.changeset/bright-glasses-peel.md @@ -0,0 +1,5 @@ +--- +'@gitbook/openapi-parser': patch +--- + +Add option to pass scalar plugins diff --git a/packages/openapi-parser/src/filesystem.ts b/packages/openapi-parser/src/filesystem.ts index c256034e91..bde8be855f 100644 --- a/packages/openapi-parser/src/filesystem.ts +++ b/packages/openapi-parser/src/filesystem.ts @@ -1,4 +1,5 @@ -import { type AnyApiDefinitionFormat, load } from '@scalar/openapi-parser'; +import { load } from '@scalar/openapi-parser'; +import type { ParseOpenAPIInput } from './parse'; import { fetchURLs } from './scalar-plugins/fetchURLs'; import type { Filesystem } from './types'; @@ -6,19 +7,13 @@ import type { Filesystem } from './types'; * Create a filesystem from an OpenAPI document. * Fetches all the URLs specified in references and builds a filesystem. */ -export async function createFileSystem(input: { - /** - * The OpenAPI document to create the filesystem from. - */ - value: AnyApiDefinitionFormat; - /** - * The root URL of the specified OpenAPI document. - * Used to resolve relative URLs. - */ - rootURL: string | null; -}): Promise { - const { filesystem } = await load(input.value, { - plugins: [fetchURLs({ rootURL: input.rootURL })], +export async function createFileSystem( + input: Pick +): Promise { + const { value, rootURL, options } = input; + + const { filesystem } = await load(value, { + plugins: [fetchURLs({ rootURL }), ...(options?.plugins || [])], }); return filesystem; diff --git a/packages/openapi-parser/src/parse.ts b/packages/openapi-parser/src/parse.ts index f8832a0340..dd3819f76e 100644 --- a/packages/openapi-parser/src/parse.ts +++ b/packages/openapi-parser/src/parse.ts @@ -1,4 +1,4 @@ -import type { AnyApiDefinitionFormat } from '@scalar/openapi-parser'; +import type { AnyApiDefinitionFormat, LoadPlugin } from '@scalar/openapi-parser'; import { OpenAPIParseError } from './error'; import { convertOpenAPIV2ToOpenAPIV3 } from './v2'; import { parseOpenAPIV3 } from './v3'; @@ -16,6 +16,12 @@ export interface ParseOpenAPIInput { * Trust the input. This will skip advanced validation. */ trust?: boolean; + /** + * Options for the parser. + */ + options?: { + plugins?: LoadPlugin[]; + }; } /** diff --git a/packages/openapi-parser/src/v3.ts b/packages/openapi-parser/src/v3.ts index 583a0cac66..14a890769a 100644 --- a/packages/openapi-parser/src/v3.ts +++ b/packages/openapi-parser/src/v3.ts @@ -12,12 +12,16 @@ import type { Filesystem, OpenAPIV3xDocument } from './types'; export async function parseOpenAPIV3( input: ParseOpenAPIInput ): Promise> { - const { value, rootURL, trust } = input; + const { value, rootURL, trust, options = {} } = input; const specification = trust ? await trustedValidate({ value, rootURL }) : await untrustedValidate({ value, rootURL }); - const filesystem = await createFileSystem({ value: specification, rootURL }); + const filesystem = await createFileSystem({ + value: specification, + rootURL, + options, + }); return filesystem; }