openapi-typescript-codegen/src/utils/getOpenApiVersion.ts
Ferdi Koomen 30e26621a5 - Added core template files
- Working on v2 parsing
2019-11-07 01:17:39 +01:00

23 lines
802 B
TypeScript

export enum OpenApiVersion {
V2 = 2,
V3 = 3,
}
/**
* Get the Open API specification version (V2 or V3). This generator only supports
* version 2 and 3 of the specification, so we will alert the user if we encounter
* an incompatible type. Or if the type is missing...
* @param openApi The loaded spec (can be any object)
*/
export function getOpenApiVersion(openApi: any): OpenApiVersion | undefined {
const info: any = openApi.swagger || openApi.openapi;
if (info && typeof info === 'string') {
const c: string = info.charAt(0);
const v: number = Number.parseInt(c);
if (v === OpenApiVersion.V2 || v === OpenApiVersion.V3) {
return v as OpenApiVersion;
}
}
throw new Error(`Unsupported Open API version: "${String(info)}"`);
}