mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-27 10:43:32 +00:00
Add a fetch option to parseOpenAPI (#4753)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@gitbook/openapi-parser": minor
|
||||
---
|
||||
|
||||
Add a `fetch` option to `parseOpenAPI` to customize how external references are loaded.
|
||||
@@ -14,14 +14,15 @@ export async function createFileSystem(
|
||||
input: Pick<ParseOpenAPIInput, 'value' | 'rootURL' | 'options'>
|
||||
): Promise<Filesystem> {
|
||||
const { value, rootURL, options } = input;
|
||||
const fetcher = options?.fetch ?? fetch;
|
||||
|
||||
const result = await bundle(value, {
|
||||
treeShake: false,
|
||||
plugins: [
|
||||
fetchURL(),
|
||||
fetchURL(fetcher),
|
||||
parseYaml(),
|
||||
parseJson(),
|
||||
fetchURLs({ rootURL }),
|
||||
fetchURLs({ rootURL, fetch: fetcher }),
|
||||
...(options?.plugins || []),
|
||||
],
|
||||
});
|
||||
|
||||
@@ -84,6 +84,35 @@ describe('#parseOpenAPI', () => {
|
||||
"must have required property 'in'"
|
||||
);
|
||||
});
|
||||
|
||||
it('loads external references with the provided fetch', async () => {
|
||||
const urls: string[] = [];
|
||||
await parseOpenAPI({
|
||||
value: {
|
||||
openapi: '3.0.3',
|
||||
info: { title: 'Test', version: '1.0.0' },
|
||||
paths: {},
|
||||
components: {
|
||||
schemas: {
|
||||
Absolute: { $ref: 'https://example.com/absolute.json' },
|
||||
Relative: { $ref: './relative.json' },
|
||||
},
|
||||
},
|
||||
},
|
||||
rootURL: 'https://example.com/openapi.json',
|
||||
options: {
|
||||
fetch: async (url) => {
|
||||
urls.push(url);
|
||||
return new Response('{"type":"object"}');
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(urls.sort()).toEqual([
|
||||
'https://example.com/absolute.json',
|
||||
'https://example.com/relative.json',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
function specV32(
|
||||
|
||||
@@ -20,6 +20,10 @@ export interface ParseOpenAPIInput {
|
||||
*/
|
||||
options?: {
|
||||
plugins?: Plugin[];
|
||||
/**
|
||||
* Fetch used to load external references. Defaults to the global `fetch`.
|
||||
*/
|
||||
fetch?: (url: string) => Promise<Response>;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,14 @@ export const fetchUrlsDefaultConfiguration = {
|
||||
limit: 40,
|
||||
};
|
||||
|
||||
export const fetchURL = (): Plugin => ({
|
||||
export const fetchURL = (fetcher: (url: string) => Promise<Response> = fetch): Plugin => ({
|
||||
type: 'loader',
|
||||
validate(value) {
|
||||
return URL.canParse(value);
|
||||
},
|
||||
async exec(value) {
|
||||
try {
|
||||
const response = await fetch(value);
|
||||
const response = await fetcher(value);
|
||||
if (!response.ok) {
|
||||
return { ok: false };
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@ export const fetchURLs = (customConfiguration: {
|
||||
* Limit the number of requests. Set to `false` to disable the limit.
|
||||
*/
|
||||
limit?: number | false;
|
||||
|
||||
/**
|
||||
* Fetch used to load the URLs. Defaults to the global `fetch`.
|
||||
*/
|
||||
fetch?: (url: string) => Promise<Response>;
|
||||
}): Plugin => {
|
||||
// State
|
||||
let numberOfRequests = 0;
|
||||
@@ -53,7 +58,7 @@ export const fetchURLs = (customConfiguration: {
|
||||
try {
|
||||
numberOfRequests++;
|
||||
const url = getReferenceUrl({ value, rootURL: configuration.rootURL });
|
||||
const response = await fetch(url);
|
||||
const response = await (configuration.fetch ?? fetch)(url);
|
||||
if (!response.ok) {
|
||||
return { ok: false };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user