UNPKG

gong-api-client

Version:

A Node.js library that automatically generates a TypeScript client for the Gong API from the OpenAPI specification

96 lines 3.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateGongApiClient = generateGongApiClient; const openapi_typescript_codegen_1 = require("openapi-typescript-codegen"); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const downloadGongApiSpec_1 = require("./downloadGongApiSpec"); /** * Generates TypeScript client code from the Gong API specification. * @param specFile Path to the API specification file. * @param outputDir Directory to output the generated code. */ async function generateGongApiClient(specFile = 'gong-openapi.json', outputDir = 'src/generated') { try { // Ensure the spec file exists if (!fs_1.default.existsSync(specFile)) { console.log(`Spec file ${specFile} not found. Downloading...`); await (0, downloadGongApiSpec_1.downloadAndSaveGongSpec)(specFile); } // Ensure the output directory exists if (!fs_1.default.existsSync(outputDir)) { fs_1.default.mkdirSync(outputDir, { recursive: true }); } // Read the spec file const spec = JSON.parse(fs_1.default.readFileSync(specFile, 'utf-8')); // Generate the client code console.log(`Generating TypeScript client from ${specFile} to ${outputDir}...`); // Generate the client code await (0, openapi_typescript_codegen_1.generate)({ input: spec, output: outputDir, exportCore: true, exportServices: true, exportModels: true, exportSchemas: true, indent: '2', useUnionTypes: true, useOptions: true, httpClient: 'axios', }); // Create a simple index file that exports everything createIndexFile(outputDir); console.log(`Generated TypeScript client in ${outputDir}`); } catch (error) { if (error instanceof Error) { throw new Error(`Error generating API client: ${error.message}`); } throw error; } } /** * Creates a simple index file that exports everything. * @param outputDir The output directory. */ function createIndexFile(outputDir) { // Export the OpenAPI core let indexContent = `export * from './core/OpenAPI';\n`; // Export all services const servicesDir = path_1.default.join(outputDir, 'services'); if (fs_1.default.existsSync(servicesDir)) { const serviceFiles = fs_1.default.readdirSync(servicesDir) .filter(file => file.endsWith('.ts')); for (const file of serviceFiles) { const serviceName = file.replace(/\.ts$/, ''); indexContent += `export * from './services/${serviceName}';\n`; } } // Export all models const modelsDir = path_1.default.join(outputDir, 'models'); if (fs_1.default.existsSync(modelsDir)) { const modelFiles = fs_1.default.readdirSync(modelsDir) .filter(file => file.endsWith('.ts')); for (const file of modelFiles) { const modelName = file.replace(/\.ts$/, ''); indexContent += `export * from './models/${modelName}';\n`; } } // Write the index file fs_1.default.writeFileSync(path_1.default.join(outputDir, 'index.ts'), indexContent); } // If this file is run directly if (require.main === module) { const specFile = process.argv[2] || 'gong-openapi.json'; const outputDir = process.argv[3] || 'src/generated'; generateGongApiClient(specFile, outputDir) .then(() => console.log('Done!')) .catch(error => { console.error(error.message); process.exit(1); }); } //# sourceMappingURL=generateGongApiClient.js.map