Skip to content

Commit 48c18c0

Browse files
authored
Add option to pass scalar plugins (#3027)
1 parent bba2e52 commit 48c18c0

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

.changeset/bright-glasses-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/openapi-parser': patch
3+
---
4+
5+
Add option to pass scalar plugins

packages/openapi-parser/src/filesystem.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import { type AnyApiDefinitionFormat, load } from '@scalar/openapi-parser';
1+
import { load } from '@scalar/openapi-parser';
2+
import type { ParseOpenAPIInput } from './parse';
23
import { fetchURLs } from './scalar-plugins/fetchURLs';
34
import type { Filesystem } from './types';
45

56
/**
67
* Create a filesystem from an OpenAPI document.
78
* Fetches all the URLs specified in references and builds a filesystem.
89
*/
9-
export async function createFileSystem(input: {
10-
/**
11-
* The OpenAPI document to create the filesystem from.
12-
*/
13-
value: AnyApiDefinitionFormat;
14-
/**
15-
* The root URL of the specified OpenAPI document.
16-
* Used to resolve relative URLs.
17-
*/
18-
rootURL: string | null;
19-
}): Promise<Filesystem> {
20-
const { filesystem } = await load(input.value, {
21-
plugins: [fetchURLs({ rootURL: input.rootURL })],
10+
export async function createFileSystem(
11+
input: Pick<ParseOpenAPIInput, 'value' | 'rootURL' | 'options'>
12+
): Promise<Filesystem> {
13+
const { value, rootURL, options } = input;
14+
15+
const { filesystem } = await load(value, {
16+
plugins: [fetchURLs({ rootURL }), ...(options?.plugins || [])],
2217
});
2318

2419
return filesystem;

packages/openapi-parser/src/parse.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AnyApiDefinitionFormat } from '@scalar/openapi-parser';
1+
import type { AnyApiDefinitionFormat, LoadPlugin } from '@scalar/openapi-parser';
22
import { OpenAPIParseError } from './error';
33
import { convertOpenAPIV2ToOpenAPIV3 } from './v2';
44
import { parseOpenAPIV3 } from './v3';
@@ -16,6 +16,12 @@ export interface ParseOpenAPIInput {
1616
* Trust the input. This will skip advanced validation.
1717
*/
1818
trust?: boolean;
19+
/**
20+
* Options for the parser.
21+
*/
22+
options?: {
23+
plugins?: LoadPlugin[];
24+
};
1925
}
2026

2127
/**

packages/openapi-parser/src/v3.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import type { Filesystem, OpenAPIV3xDocument } from './types';
1212
export async function parseOpenAPIV3(
1313
input: ParseOpenAPIInput
1414
): Promise<Filesystem<OpenAPIV3xDocument>> {
15-
const { value, rootURL, trust } = input;
15+
const { value, rootURL, trust, options = {} } = input;
1616
const specification = trust
1717
? await trustedValidate({ value, rootURL })
1818
: await untrustedValidate({ value, rootURL });
1919

20-
const filesystem = await createFileSystem({ value: specification, rootURL });
20+
const filesystem = await createFileSystem({
21+
value: specification,
22+
rootURL,
23+
options,
24+
});
2125

2226
return filesystem;
2327
}

0 commit comments

Comments
 (0)