redoc/src/utils/loadAndBundleSpec.ts

31 lines
1.0 KiB
TypeScript
Raw Normal View History

import * as JsonSchemaRefParser from 'json-schema-ref-parser';
2018-07-23 13:22:24 +03:00
/* tslint:disable-next-line:no-implicit-dependencies */
2017-11-15 00:40:38 +03:00
import { convertObj } from 'swagger2openapi';
import { OpenAPISpec } from '../types';
export async function loadAndBundleSpec(specUrlOrObject: object | string): Promise<OpenAPISpec> {
2018-01-22 21:30:53 +03:00
const parser = new JsonSchemaRefParser();
2019-07-29 18:41:57 +03:00
const spec = (await parser.bundle(specUrlOrObject, {
resolve: { http: { withCredentials: false } },
2019-07-29 18:41:57 +03:00
} as object)) as any;
if (spec.swagger !== undefined) {
return convertSwagger2OpenAPI(spec);
} else {
return spec;
}
}
2017-11-15 00:40:38 +03:00
export function convertSwagger2OpenAPI(spec: any): Promise<OpenAPISpec> {
console.warn('[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0');
2017-11-15 00:40:38 +03:00
return new Promise<OpenAPISpec>((resolve, reject) =>
2019-05-10 18:03:28 +03:00
convertObj(spec, { patch: true, warnOnly: true, text: '{}' }, (err, res) => {
2017-11-15 00:40:38 +03:00
// TODO: log any warnings
if (err) {
return reject(err);
}
resolve(res && (res.openapi as any));
}),
);
}