|
| 1 | +import { join, resolve } from 'path'; |
| 2 | + |
| 3 | +import type { Service } from '../client/interfaces/Service'; |
| 4 | +import type { HttpClient } from '../HttpClient'; |
| 5 | +import type { Indent } from '../Indent'; |
| 6 | +import { mkdir, writeFile } from './fileSystem'; |
| 7 | +import { formatCode as f } from './formatCode'; |
| 8 | +import { formatIndentation as i } from './formatIndentation'; |
| 9 | +import { isDefined } from './isDefined'; |
| 10 | +import type { Templates } from './registerHandlebarTemplates'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Generate Services using the Handlebar template and write to disk. |
| 14 | + * @param services Array of Services to write |
| 15 | + * @param templates The loaded handlebar templates |
| 16 | + * @param outputPath Directory to write the generated files to |
| 17 | + * @param httpClient The selected httpClient (fetch, xhr, node or axios) |
| 18 | + * @param useUnionTypes Use union types instead of enums |
| 19 | + * @param useOptions Use options or arguments functions |
| 20 | + * @param indent Indentation options (4, 2 or tab) |
| 21 | + * @param postfix Service name postfix |
| 22 | + * @param clientName Custom client class name |
| 23 | + */ |
| 24 | +export const writeClientSamples = async ( |
| 25 | + services: Service[], |
| 26 | + templates: Templates, |
| 27 | + outputPath: string, |
| 28 | + httpClient: HttpClient, |
| 29 | + useUnionTypes: boolean, |
| 30 | + useOptions: boolean, |
| 31 | + indent: Indent, |
| 32 | + postfix: string, |
| 33 | + clientName?: string |
| 34 | +): Promise<void> => { |
| 35 | + for (const service of services) { |
| 36 | + await mkdir(join(outputPath, service.name)); |
| 37 | + for (const operation of service.operations) { |
| 38 | + const file = resolve(outputPath, service.name, `${operation.name}${postfix}.ts`); |
| 39 | + const templateResult = templates.exports.sample({ |
| 40 | + ...operation, |
| 41 | + serviceName: service.name, |
| 42 | + httpClient, |
| 43 | + useUnionTypes, |
| 44 | + useOptions, |
| 45 | + postfix, |
| 46 | + exportClient: isDefined(clientName), |
| 47 | + }); |
| 48 | + await writeFile(file, i(f(templateResult), indent)); |
| 49 | + } |
| 50 | + } |
| 51 | +}; |
0 commit comments