gong-api-client
Version:
A Node.js library that automatically generates a TypeScript client for the Gong API from the OpenAPI specification
96 lines • 4.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateGongApiClient = exports.downloadAndSaveGongSpec = void 0;
exports.initGongApiClient = initGongApiClient;
const downloadGongApiSpec_1 = require("./downloadGongApiSpec");
Object.defineProperty(exports, "downloadAndSaveGongSpec", { enumerable: true, get: function () { return downloadGongApiSpec_1.downloadAndSaveGongSpec; } });
const generateGongApiClient_1 = require("./generateGongApiClient");
Object.defineProperty(exports, "generateGongApiClient", { enumerable: true, get: function () { return generateGongApiClient_1.generateGongApiClient; } });
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
/**
* Initializes the Gong API client.
* @param options Configuration options for the Gong API client.
* @returns A promise that resolves when the client is initialized.
*/
async function initGongApiClient(options = {}) {
const { baseUrl = process.env.GONG_BASE_URL || 'https://api.gong.io', apiKey = process.env.GONG_API_KEY, accessKey = process.env.GONG_ACCESS_KEY, specFile = process.env.GONG_SPEC_FILE || 'gong-openapi.json', outputDir = process.env.GONG_OUTPUT_DIR || 'src/generated', regenerate = false } = options;
// Check if the client code already exists
const generatedIndexPath = path_1.default.join(outputDir, 'index.ts');
const clientExists = fs_1.default.existsSync(generatedIndexPath);
// Regenerate the client code if requested or if it doesn't exist
if (regenerate || !clientExists) {
// Download the API specification if it doesn't exist or regenerate is true
if (regenerate || !fs_1.default.existsSync(specFile)) {
await (0, downloadGongApiSpec_1.downloadAndSaveGongSpec)(specFile);
}
// Generate the client code
await (0, generateGongApiClient_1.generateGongApiClient)(specFile, outputDir);
}
// Import and configure the generated client
try {
// Dynamically import the generated client
const generatedClient = await Promise.resolve(`${path_1.default.join(process.cwd(), outputDir)}`).then(s => __importStar(require(s)));
// Configure the client with the provided options
if (generatedClient.OpenAPI) {
generatedClient.OpenAPI.BASE = baseUrl;
if (apiKey && accessKey) {
generatedClient.OpenAPI.WITH_CREDENTIALS = true;
generatedClient.OpenAPI.CREDENTIALS = 'include';
generatedClient.OpenAPI.TOKEN = async () => {
return `Bearer ${apiKey}:${accessKey}`;
};
}
}
return generatedClient;
}
catch (error) {
if (error instanceof Error) {
throw new Error(`Error importing generated client: ${error.message}`);
}
throw error;
}
}
// Export a default object with all the functionality
exports.default = {
initGongApiClient,
downloadAndSaveGongSpec: downloadGongApiSpec_1.downloadAndSaveGongSpec,
generateGongApiClient: generateGongApiClient_1.generateGongApiClient
};
//# sourceMappingURL=index.js.map