|
| 1 | +import { mkdir, readFile, remove } from 'fs-extra'; |
| 2 | +import Handlebars from 'handlebars'; |
| 3 | +import { resolve } from 'path'; |
| 4 | + |
| 5 | +import { HttpClient } from './HttpClient'; |
| 6 | +import { Indent } from './Indent'; |
| 7 | +import { parse as parseV3 } from './openApi/v3'; |
| 8 | +import { writeFile } from './utils/fileSystem'; |
| 9 | +import { formatCode } from './utils/formatCode'; |
| 10 | +import { formatIndentation } from './utils/formatIndentation'; |
| 11 | +import { getOpenApiSpec } from './utils/getOpenApiSpec'; |
| 12 | +import { postProcessClient } from './utils/postProcessClient'; |
| 13 | +import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates'; |
| 14 | +import { writeClient } from './utils/writeClient'; |
| 15 | + |
| 16 | +export const generateCustom = async (inputPath?: string, outputPath?: string, templatePath?: string) => { |
| 17 | + if (!inputPath) throw Error('No inputPath provided'); |
| 18 | + if (!outputPath) throw Error('No outputPath provided'); |
| 19 | + if (!templatePath) throw Error('No templatePath provided'); |
| 20 | + |
| 21 | + const httpClient = HttpClient.FETCH; |
| 22 | + const useUnionTypes = true; |
| 23 | + const useOptions = false; |
| 24 | + const exportCore = true; |
| 25 | + const exportServices = false; |
| 26 | + const exportModels = true; |
| 27 | + const exportSchemas = false; |
| 28 | + const indent = Indent.SPACE_2; |
| 29 | + const postfix = 'Service'; |
| 30 | + |
| 31 | + // Generate core, models (this is mostly copied from index.ts): |
| 32 | + |
| 33 | + const openApi = await getOpenApiSpec(inputPath); |
| 34 | + const templates = registerHandlebarTemplates({ |
| 35 | + httpClient, |
| 36 | + useUnionTypes, |
| 37 | + useOptions, |
| 38 | + }); |
| 39 | + |
| 40 | + const client = parseV3(openApi); |
| 41 | + const clientFinal = postProcessClient(client); |
| 42 | + |
| 43 | + await writeClient( |
| 44 | + clientFinal, |
| 45 | + templates, |
| 46 | + outputPath, |
| 47 | + httpClient, |
| 48 | + useOptions, |
| 49 | + useUnionTypes, |
| 50 | + exportCore, |
| 51 | + exportServices, |
| 52 | + exportModels, |
| 53 | + exportSchemas, |
| 54 | + indent, |
| 55 | + postfix |
| 56 | + ); |
| 57 | + |
| 58 | + // Generate services (this is custom): |
| 59 | + |
| 60 | + registerHandlebarTemplates({ |
| 61 | + httpClient, |
| 62 | + useUnionTypes, |
| 63 | + useOptions, |
| 64 | + handlebars: Handlebars, // since we're not using precompiled templates, we need a different object here |
| 65 | + }); |
| 66 | + Handlebars.registerHelper('capitalize', str => { |
| 67 | + return str.charAt(0).toUpperCase() + str.slice(1); |
| 68 | + }); |
| 69 | + |
| 70 | + const serviceTemplate = Handlebars.compile(await readFile(templatePath, 'utf8')); |
| 71 | + |
| 72 | + const servicesDir = resolve(outputPath, 'services'); |
| 73 | + await remove(servicesDir); |
| 74 | + await mkdir(servicesDir); |
| 75 | + |
| 76 | + for (const service of clientFinal.services) { |
| 77 | + const file = resolve(outputPath, `services/${service.name}${postfix}.ts`); |
| 78 | + const templateResult = serviceTemplate({ |
| 79 | + ...service, |
| 80 | + serviceBaseUrl: clientFinal.server, |
| 81 | + httpClient, |
| 82 | + useUnionTypes, |
| 83 | + useOptions, |
| 84 | + postfix, |
| 85 | + }); |
| 86 | + await writeFile(file, formatIndentation(formatCode(templateResult), indent)); |
| 87 | + } |
| 88 | +}; |
0 commit comments