Skip to content

Commit 4f5cbfe

Browse files
authored
Upgrade @scalar/openapi-parser (#3442)
1 parent afba8fc commit 4f5cbfe

File tree

8 files changed

+77
-29
lines changed

8 files changed

+77
-29
lines changed

.changeset/tall-buckets-clean.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+
Upgrade Scalar parser

bun.lock

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
"name": "@gitbook/openapi-parser",
170170
"version": "2.1.4",
171171
"dependencies": {
172-
"@scalar/openapi-parser": "^0.10.10",
172+
"@scalar/openapi-parser": "^0.18.0",
173173
"@scalar/openapi-types": "^0.1.9",
174174
},
175175
"devDependencies": {
@@ -1100,7 +1100,7 @@
11001100

11011101
"@scalar/object-utils": ["@scalar/object-utils@1.1.13", "", { "dependencies": { "flatted": "^3.3.1", "just-clone": "^6.2.0", "ts-deepmerge": "^7.0.1" } }, "sha512-311eTykIXgOtjCs4VTELj9UMT97jHTWc5qkGNoIzZ5nxjCcvOVe7kDQobIkE8dGT+ybOgHz5qly02Eu7nVHeZQ=="],
11021102

1103-
"@scalar/openapi-parser": ["@scalar/openapi-parser@0.10.14", "", { "dependencies": { "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", "ajv-formats": "^3.0.1", "jsonpointer": "^5.0.1", "leven": "^4.0.0", "yaml": "^2.4.5" } }, "sha512-VXr979NMx6wZ+kpFKor2eyCJZOjyMwcBRc6c4Gc92ZMOC7ZNYqjwbw+Ubh2ELJyP5cWAjOFSrNwtylema0pw5w=="],
1103+
"@scalar/openapi-parser": ["@scalar/openapi-parser@0.18.0", "", { "dependencies": { "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", "ajv-formats": "^3.0.1", "jsonpointer": "^5.0.1", "leven": "^4.0.0", "yaml": "^2.4.5" } }, "sha512-qAWEfJR5f/LwSFULFeSkD56LHgU9ovyj+e1uqy9XaW9Hb9P0ewIGKi+Gd4wTyxfFkdPLXTFYXApjviP6jV+new=="],
11041104

11051105
"@scalar/openapi-types": ["@scalar/openapi-types@0.1.9", "", {}, "sha512-HQQudOSQBU7ewzfnBW9LhDmBE2XOJgSfwrh5PlUB7zJup/kaRkBGNgV2wMjNz9Af/uztiU/xNrO179FysmUT+g=="],
11061106

@@ -3768,8 +3768,6 @@
37683768

37693769
"@scalar/api-client/@scalar/object-utils": ["@scalar/object-utils@1.2.1", "", { "dependencies": { "flatted": "^3.3.1", "just-clone": "^6.2.0", "ts-deepmerge": "^7.0.1", "type-fest": "^4.20.0" } }, "sha512-2P/0In6XSrV1Ye8yU3LyOWJKWkzXpxf0thHmTfJriqGBZz41s06td2KKqoqHSEEi+FAsl3O1ngmOOzLM3QUkkw=="],
37703770

3771-
"@scalar/api-client/@scalar/openapi-parser": ["@scalar/openapi-parser@0.18.0", "", { "dependencies": { "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", "ajv-formats": "^3.0.1", "jsonpointer": "^5.0.1", "leven": "^4.0.0", "yaml": "^2.4.5" } }, "sha512-qAWEfJR5f/LwSFULFeSkD56LHgU9ovyj+e1uqy9XaW9Hb9P0ewIGKi+Gd4wTyxfFkdPLXTFYXApjviP6jV+new=="],
3772-
37733771
"@scalar/api-client/@scalar/openapi-types": ["@scalar/openapi-types@0.3.3", "", { "dependencies": { "zod": "3.24.1" } }, "sha512-mr3OvbCyvuzSDgv9gR9D3pJAsl82BRNBp1P8ldgRgKF4Wiswfa7nQeKibpX0kHSGXLgiyvxWJULUFwgdWMXNNA=="],
37743772

37753773
"@scalar/api-client/@scalar/themes": ["@scalar/themes@0.13.4", "", { "dependencies": { "@scalar/types": "0.2.3", "nanoid": "^5.1.5" } }, "sha512-PMiSqX+MZF29u32ogEjTVA5c/LBd5cE/QPF7lK0EJeemXRy2r/nWbvrXtqD9Y7Sw54g1caVNjK8yJcCYJdB6hw=="],
@@ -3782,8 +3780,6 @@
37823780

37833781
"@scalar/components/@scalar/themes": ["@scalar/themes@0.13.4", "", { "dependencies": { "@scalar/types": "0.2.3", "nanoid": "^5.1.5" } }, "sha512-PMiSqX+MZF29u32ogEjTVA5c/LBd5cE/QPF7lK0EJeemXRy2r/nWbvrXtqD9Y7Sw54g1caVNjK8yJcCYJdB6hw=="],
37843782

3785-
"@scalar/import/@scalar/openapi-parser": ["@scalar/openapi-parser@0.18.0", "", { "dependencies": { "ajv": "^8.17.1", "ajv-draft-04": "^1.0.0", "ajv-formats": "^3.0.1", "jsonpointer": "^5.0.1", "leven": "^4.0.0", "yaml": "^2.4.5" } }, "sha512-qAWEfJR5f/LwSFULFeSkD56LHgU9ovyj+e1uqy9XaW9Hb9P0ewIGKi+Gd4wTyxfFkdPLXTFYXApjviP6jV+new=="],
3786-
37873783
"@scalar/oas-utils/@scalar/openapi-types": ["@scalar/openapi-types@0.2.0", "", { "dependencies": { "zod": "^3.23.8" } }, "sha512-waiKk12cRCqyUCWTOX0K1WEVX46+hVUK+zRPzAahDJ7G0TApvbNkuy5wx7aoUyEk++HHde0XuQnshXnt8jsddA=="],
37883784

37893785
"@scalar/oas-utils/@scalar/types": ["@scalar/types@0.1.7", "", { "dependencies": { "@scalar/openapi-types": "0.2.0", "@unhead/schema": "^1.11.11", "nanoid": "^5.1.5", "type-fest": "^4.20.0", "zod": "^3.23.8" } }, "sha512-irIDYzTQG2KLvFbuTI8k2Pz/R4JR+zUUSykVTbEMatkzMmVFnn1VzNSMlODbadycwZunbnL2tA27AXed9URVjw=="],

packages/openapi-parser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"version": "2.1.4",
1313
"sideEffects": false,
1414
"dependencies": {
15-
"@scalar/openapi-parser": "^0.10.10",
15+
"@scalar/openapi-parser": "^0.18.0",
1616
"@scalar/openapi-types": "^0.1.9"
1717
},
1818
"devDependencies": {

packages/openapi-parser/src/filesystem.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ describe('#createFileSystem', () => {
3939
value: url,
4040
rootURL: url,
4141
});
42-
expect(filesystem).toHaveLength(4);
42+
expect(filesystem).toHaveLength(1);
4343
expect(filesystem[0]?.isEntrypoint).toBe(true);
44-
expect(filesystem[1]?.isEntrypoint).toBe(false);
45-
expect(filesystem[1]?.filename).toBe('user.yaml');
46-
expect(filesystem[2]?.filename).toBe('../tag.yaml');
47-
expect(filesystem[3]?.filename).toBe('http://localhost:3020/root/pet.yaml');
44+
expect(filesystem[0]?.filename).toBe('openapi.json');
4845
});
4946
});

packages/openapi-parser/src/filesystem.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { load } from '@scalar/openapi-parser';
1+
import { bundle } from '@scalar/openapi-parser';
2+
import { parseJson, parseYaml } from '@scalar/openapi-parser/plugins-browser';
23
import type { ParseOpenAPIInput } from './parse';
4+
import { fetchURL } from './scalar-plugins/fetchURL';
35
import { fetchURLs } from './scalar-plugins/fetchURLs';
46
import type { Filesystem } from './types';
57

@@ -12,9 +14,24 @@ export async function createFileSystem(
1214
): Promise<Filesystem> {
1315
const { value, rootURL, options } = input;
1416

15-
const { filesystem } = await load(value, {
16-
plugins: [fetchURLs({ rootURL }), ...(options?.plugins || [])],
17+
const result = await bundle(value, {
18+
treeShake: false,
19+
plugins: [
20+
fetchURL(),
21+
parseYaml(),
22+
parseJson(),
23+
fetchURLs({ rootURL }),
24+
...(options?.plugins || []),
25+
],
1726
});
1827

19-
return filesystem;
28+
return [
29+
{
30+
dir: '.',
31+
isEntrypoint: true,
32+
references: [],
33+
filename: 'openapi.json',
34+
specification: result,
35+
},
36+
];
2037
}

packages/openapi-parser/src/parse.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import type { AnyApiDefinitionFormat, LoadPlugin } from '@scalar/openapi-parser';
1+
import type { AnyObject, bundle } from '@scalar/openapi-parser';
22
import { OpenAPIParseError } from './error';
33
import { convertOpenAPIV2ToOpenAPIV3 } from './v2';
44
import { parseOpenAPIV3 } from './v3';
55

6+
export type ParsePlugin = Parameters<typeof bundle>[1]['plugins'][number];
7+
68
export interface ParseOpenAPIInput {
79
/**
810
* The API definition to parse.
911
*/
10-
value: AnyApiDefinitionFormat;
12+
value: string | AnyObject;
1113
/**
1214
* The root URL of the specified OpenAPI document.
1315
*/
@@ -20,7 +22,7 @@ export interface ParseOpenAPIInput {
2022
* Options for the parser.
2123
*/
2224
options?: {
23-
plugins?: LoadPlugin[];
25+
plugins?: ParsePlugin[];
2426
};
2527
}
2628

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { normalize } from '@scalar/openapi-parser';
2+
import type { ParsePlugin } from '../parse';
3+
4+
export const fetchUrlsDefaultConfiguration = {
5+
limit: 40,
6+
};
7+
8+
export const fetchURL = (): ParsePlugin => ({
9+
validate(value) {
10+
return URL.canParse(value);
11+
},
12+
async exec(value) {
13+
try {
14+
const response = await fetch(value);
15+
if (!response.ok) {
16+
return { ok: false };
17+
}
18+
const text = await response.text();
19+
return {
20+
ok: true,
21+
data: normalize(text),
22+
};
23+
} catch {
24+
return { ok: false };
25+
}
26+
},
27+
});

packages/openapi-parser/src/scalar-plugins/fetchURLs.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { type LoadPlugin, normalize } from '@scalar/openapi-parser';
1+
import { normalize } from '@scalar/openapi-parser';
2+
import type { ParsePlugin } from '../parse';
23

34
export const fetchUrlsDefaultConfiguration = {
45
limit: 40,
56
};
67

7-
export const fetchURLs: (customConfiguration: {
8+
export const fetchURLs = (customConfiguration: {
89
/**
910
* Root URL to resolve relative URLs.
1011
*/
@@ -14,7 +15,7 @@ export const fetchURLs: (customConfiguration: {
1415
* Limit the number of requests. Set to `false` to disable the limit.
1516
*/
1617
limit?: number | false;
17-
}) => LoadPlugin = (customConfiguration) => {
18+
}): ParsePlugin => {
1819
// State
1920
let numberOfRequests = 0;
2021

@@ -25,7 +26,7 @@ export const fetchURLs: (customConfiguration: {
2526
};
2627

2728
return {
28-
check(value?: any) {
29+
validate(value) {
2930
// Not a string
3031
if (typeof value !== 'string') {
3132
return false;
@@ -42,25 +43,28 @@ export const fetchURLs: (customConfiguration: {
4243

4344
return true;
4445
},
45-
async get(value?: any) {
46+
async exec(value) {
4647
// Limit the number of requests
4748
if (configuration?.limit !== false && numberOfRequests >= configuration?.limit) {
48-
return undefined;
49+
return { ok: false };
4950
}
5051

5152
try {
5253
numberOfRequests++;
5354
const url = getReferenceUrl({ value, rootURL: configuration.rootURL });
5455
const response = await fetch(url);
5556
if (!response.ok) {
56-
return undefined;
57+
return { ok: false };
5758
}
5859
const text = await response.text();
5960
// Try to normalize the text to be sure it's a valid JSON or YAML.
6061
await normalize(text);
61-
return text;
62-
} catch (_error: any) {
63-
return undefined;
62+
return {
63+
ok: true,
64+
data: text,
65+
};
66+
} catch {
67+
return { ok: false };
6468
}
6569
},
6670
};

0 commit comments

Comments
 (0)