UNPKG

chargebee-typescript

Version:

A library in typescript for integrating with Chargebee.

71 lines (70 loc) 2.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestWrapper = void 0; const util_1 = require("./util"); const core_1 = require("./core"); const idempotency_constants_1 = require("./idempotency_constants"); class RequestWrapper { constructor(args, apiCall, envArg) { this.args = args; this.apiCall = apiCall; this.envArg = envArg; this.httpHeaders = {}; if (this.apiCall.hasIdInUrl) { RequestWrapper.validateIdParam(this.args[0]); } } static validateIdParam(idParam) { if (!idParam || idParam.trim().length < 1) { throw new Error('the required id parameter missing or wrong'); } return idParam; } headers(headers) { Object.assign(this.httpHeaders, headers); return this; } setIdempotencyKey(idempotencyKey) { this.headers({ [idempotency_constants_1.IdempotencyConstants.IDEMPOTENCY_HEADER]: idempotencyKey }); return this; } param(custom_param) { if (this.apiCall.hasIdInUrl) { this.args[1] = Object.assign(Object.assign({}, this.args[1]), custom_param); } else { this.args[0] = Object.assign(Object.assign({}, this.args[0]), custom_param); } return this; } request(callBack = undefined, envOptions) { let env = {}; let jsonConstructor = {}.constructor; util_1.Util.extend(true, env, this.envArg); if (typeof envOptions !== 'undefined') { util_1.Util.extend(true, env, envOptions); } else if (typeof callBack !== 'undefined' && callBack.constructor === jsonConstructor && !util_1.Util.isFunction(callBack)) { util_1.Util.extend(true, env, callBack); callBack = undefined; } let deferred = util_1.Util.createDeferred(callBack); let urlIdParam = this.apiCall.hasIdInUrl ? this.args[0] : null; let params = this.apiCall.hasIdInUrl ? this.args[1] : this.args[0]; if (typeof callBack !== 'undefined' && !util_1.Util.isFunction(callBack)) { throw new Error('The callback parameter passed is incorrect.'); } function callBackWrapper(err, response) { if (err) { deferred.reject(err); } else { deferred.resolve(response); } } core_1.Core.makeApiRequest(env, callBackWrapper, this.apiCall.httpMethod, this.apiCall.urlPrefix, this.apiCall.urlSuffix, urlIdParam, params, this.httpHeaders, this.apiCall.isListReq, this.apiCall.subDomain, this.apiCall.isOperationNeedsJsonInput, this.apiCall.jsonKeys); return deferred.promise; } ; } exports.RequestWrapper = RequestWrapper;