mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 19:06:31 +00:00
Resolve any JSON/YAML OpenAPI file and resolve common parameters (#132)
* Fix parsing of OpenAPI when content-type is unknown * Resolve common parameters * Format
This commit is contained in:
@@ -49,15 +49,30 @@ export async function fetchOpenAPIOperation<Markdown>(
|
||||
): Promise<OpenAPIOperationData | null> {
|
||||
const fetcher = cacheFetcher(rawFetcher);
|
||||
|
||||
const operation = await resolveOpenAPI<OpenAPIV3.OperationObject>(
|
||||
let operation = await resolveOpenAPI<OpenAPIV3.OperationObject>(
|
||||
input.url,
|
||||
['paths', input.path, input.method],
|
||||
fetcher,
|
||||
);
|
||||
|
||||
if (!operation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Resolve common parameters
|
||||
const commonParameters = await resolveOpenAPI<OpenAPIV3.ParameterObject[]>(
|
||||
input.url,
|
||||
['paths', input.path, 'parameters'],
|
||||
fetcher,
|
||||
);
|
||||
if (commonParameters) {
|
||||
operation = {
|
||||
...operation,
|
||||
parameters: [...commonParameters, ...(operation.parameters ?? [])],
|
||||
};
|
||||
}
|
||||
|
||||
// Resolve servers
|
||||
const servers = await resolveOpenAPI<OpenAPIV3.ServerObject[]>(input.url, ['servers'], fetcher);
|
||||
|
||||
// Resolve securities
|
||||
|
||||
+12
-8
@@ -50,15 +50,19 @@ const fetcher: OpenAPIFetcher = {
|
||||
let data: unknown = null;
|
||||
|
||||
if (response.ok) {
|
||||
if (response.headers.get('content-type')?.includes('yaml')) {
|
||||
const text = await response.text();
|
||||
|
||||
// Try with JSON
|
||||
try {
|
||||
data = JSON.parse(text);
|
||||
} catch (error) {
|
||||
//
|
||||
}
|
||||
|
||||
// Try with YAML
|
||||
if (!data) {
|
||||
try {
|
||||
data = yaml.load(await response.text());
|
||||
} catch (error) {
|
||||
//
|
||||
}
|
||||
} else if (response.headers.get('content-type')?.includes('json')) {
|
||||
try {
|
||||
data = await response.json();
|
||||
data = yaml.load(text);
|
||||
} catch (error) {
|
||||
//
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user