openapi-typescript-codegen/src/utils/writeClientServices.ts
Ferdi Koomen fd988e834c - Woking on babel support
- Working on test server
- Embedding all templates
2020-09-24 16:10:00 +02:00

31 lines
1.2 KiB
TypeScript

import { resolve } from 'path';
import { Service } from '../client/interfaces/Service';
import { writeFile } from './fileSystem';
import { format } from './format';
import { Templates } from './registerHandlebarTemplates';
const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';
/**
* Generate Services using the Handlebar template and write to disk.
* @param services Array of Services to write.
* @param templates The loaded handlebar templates.
* @param outputPath Directory to write the generated files to.
* @param useOptions Use options or arguments functions.
*/
export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, useOptions: boolean): Promise<void> {
for (const service of services) {
const file = resolve(outputPath, `${service.name}.ts`);
const hasApiErrors = service.operations.some(operation => operation.errors.length);
const hasApiVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));
const templateResult = templates.exports.service({
...service,
hasApiErrors,
hasApiVersion,
useOptions,
});
await writeFile(file, format(templateResult));
}
}