Add option to pass scalar plugins (#3027)

This commit is contained in:
Nolann B.
2025-03-24 14:11:14 +01:00
committed by GitHub
parent bba2e52e24
commit 48c18c03b9
4 changed files with 27 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/openapi-parser': patch
---
Add option to pass scalar plugins
+9 -14
View File
@@ -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<Filesystem> {
const { filesystem } = await load(input.value, {
plugins: [fetchURLs({ rootURL: input.rootURL })],
export async function createFileSystem(
input: Pick<ParseOpenAPIInput, 'value' | 'rootURL' | 'options'>
): Promise<Filesystem> {
const { value, rootURL, options } = input;
const { filesystem } = await load(value, {
plugins: [fetchURLs({ rootURL }), ...(options?.plugins || [])],
});
return filesystem;
+7 -1
View File
@@ -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[];
};
}
/**
+6 -2
View File
@@ -12,12 +12,16 @@ import type { Filesystem, OpenAPIV3xDocument } from './types';
export async function parseOpenAPIV3(
input: ParseOpenAPIInput
): Promise<Filesystem<OpenAPIV3xDocument>> {
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;
}