MCPcopy Index your code
hub / github.com/GitbookIO/gitbook / parseOpenAPIV3

Function parseOpenAPIV3

packages/openapi-parser/src/v3.ts:11–59  ·  view source on GitHub ↗
(input: ParseOpenAPIInput)

Source from the content-addressed store, hash-verified

9 * It can throw an `OpenAPIFetchError` if the document is invalid.
10 */
11export async function parseOpenAPIV3(input: ParseOpenAPIInput): Promise<ParseOpenAPIResult> {
12 const { value, rootURL, options = {} } = input;
13
14 const result = await validate(value).catch((error) => {
15 throw new OpenAPIParseError('Invalid OpenAPI document', {
16 code: 'invalid',
17 rootURL,
18 cause: error,
19 });
20 });
21
22 // If there is no version, we consider it invalid instantely.
23 if (!result.version) {
24 throw new OpenAPIParseError(
25 'Can’t find supported Swagger/OpenAPI version in the provided document, version must be a string.',
26 {
27 code: 'invalid',
28 rootURL,
29 errors: result.errors,
30 }
31 );
32 }
33
34 // If the version is 2.0, we throw an error to trigger the upgrade.
35 if (result.version === '2.0') {
36 throw new OpenAPIParseError('Only OpenAPI v3 is supported', {
37 code: 'parse-v2-in-v3',
38 rootURL,
39 });
40 }
41
42 // We don't rely on `result.invalid` because it's too strict.
43 // If we succeed in parsing a schema, then we consider it valid.
44 if (!result.specification) {
45 throw new OpenAPIParseError('Invalid OpenAPI document', {
46 code: 'invalid',
47 rootURL,
48 errors: result.errors,
49 });
50 }
51
52 const filesystem = await createFileSystem({
53 value: result.specification,
54 rootURL,
55 options,
56 });
57
58 return { filesystem, errors: result.errors ?? [] };
59}

Callers 2

parseOpenAPIFunction · 0.90

Calls 2

validateFunction · 0.90
createFileSystemFunction · 0.90

Tested by

no test coverage detected