import { HttpClient } from './HttpClient'; import { parse as parseV2 } from './openApi/v2'; import { parse as parseV3 } from './openApi/v3'; import { getOpenApiSpec } from './utils/getOpenApiSpec'; import { getOpenApiVersion, OpenApiVersion } from './utils/getOpenApiVersion'; import { isString } from './utils/isString'; import { postProcessClient } from './utils/postProcessClient'; import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates'; import { writeClient } from './utils/writeClient'; export { HttpClient } from './HttpClient'; export type Options = { input: string | Record; output: string; httpClient?: HttpClient; useOptions?: boolean; useUnionTypes?: boolean; exportCore?: boolean; exportServices?: boolean; exportModels?: boolean; exportSchemas?: boolean; request?: string; write?: boolean; }; /** * Generate the OpenAPI client. This method will read the OpenAPI specification and based on the * given language it will generate the client, including the typed models, validation schemas, * service layer, etc. * @param input The relative location of the OpenAPI spec * @param output The relative location of the output directory * @param httpClient The selected httpClient (fetch, xhr, node or axios) * @param useOptions Use options or arguments functions * @param useUnionTypes Use union types instead of enums * @param exportCore: Generate core client classes * @param exportServices: Generate services * @param exportModels: Generate models * @param exportSchemas: Generate schemas * @param request: Path to custom request file * @param write Write the files to disk (true or false) */ export async function generate({ input, output, httpClient = HttpClient.FETCH, useOptions = false, useUnionTypes = false, exportCore = true, exportServices = true, exportModels = true, exportSchemas = false, request, write = true, }: Options): Promise { const openApi = isString(input) ? await getOpenApiSpec(input) : input; const openApiVersion = getOpenApiVersion(openApi); const templates = registerHandlebarTemplates({ httpClient, useUnionTypes, useOptions, }); switch (openApiVersion) { case OpenApiVersion.V2: { const client = parseV2(openApi); const clientFinal = postProcessClient(client); if (!write) break; await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request); break; } case OpenApiVersion.V3: { const client = parseV3(openApi); const clientFinal = postProcessClient(client); if (!write) break; await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request); break; } } }