gong-api-client
Version:
A Node.js library that automatically generates a TypeScript client for the Gong API from the OpenAPI specification
74 lines • 2.54 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadGongSpec = downloadGongSpec;
exports.saveSpec = saveSpec;
exports.downloadAndSaveGongSpec = downloadAndSaveGongSpec;
const axios_1 = __importDefault(require("axios"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Downloads the Gong API specification from the Gong API.
* @returns The API specification as a JSON object.
*/
async function downloadGongSpec() {
const url = 'https://gong.app.gong.io/ajax/settings/api/documentation/specs?version=';
try {
const response = await axios_1.default.get(url, {
headers: {
'Accept': 'application/json'
}
});
return response.data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
throw new Error(`Error downloading API spec: ${error.message}`);
}
throw error;
}
}
/**
* Saves the API specification to a file.
* @param spec The API specification object.
* @param outputFile Path to save the file.
*/
function saveSpec(spec, outputFile) {
try {
const outputDir = path_1.default.dirname(outputFile);
if (!fs_1.default.existsSync(outputDir)) {
fs_1.default.mkdirSync(outputDir, { recursive: true });
}
fs_1.default.writeFileSync(outputFile, JSON.stringify(spec, null, 2));
console.log(`Saved API specification to ${outputFile}`);
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Error saving API spec: ${error.message}`);
}
throw error;
}
}
/**
* Main function to download and save the Gong API specification.
* @param outputFile Path to save the API specification.
*/
async function downloadAndSaveGongSpec(outputFile = 'gong-openapi.json') {
console.log('Downloading Gong API specification...');
const spec = await downloadGongSpec();
saveSpec(spec, outputFile);
return outputFile;
}
// If this file is run directly
if (require.main === module) {
const outputFile = process.argv[2] || 'gong-openapi.json';
downloadAndSaveGongSpec(outputFile)
.then(() => console.log('Done!'))
.catch(error => {
console.error(error.message);
process.exit(1);
});
}
//# sourceMappingURL=downloadGongApiSpec.js.map