Skip to content

Add option to pass scalar plugins #3027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-glasses-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/openapi-parser': patch
---

Add option to pass scalar plugins
23 changes: 9 additions & 14 deletions packages/openapi-parser/src/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
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';

/**
* 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;
Expand Down
8 changes: 7 additions & 1 deletion packages/openapi-parser/src/parse.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,6 +16,12 @@ export interface ParseOpenAPIInput {
* Trust the input. This will skip advanced validation.
*/
trust?: boolean;
/**
* Options for the parser.
*/
options?: {
plugins?: LoadPlugin[];
};
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/openapi-parser/src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down