mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
31 lines
1.2 KiB
TypeScript
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));
|
|
}
|
|
}
|