File: /var/www/quadcode.com/node_modules/pipedrive/dist/ApiClient.js
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _superagent = _interopRequireDefault(require("superagent"));
var _querystring = _interopRequireDefault(require("querystring"));
var _lodash = require("lodash");
var _UnauthorizedException = _interopRequireDefault(require("./exceptions/UnauthorizedException"));
var _OAuthProviderException = _interopRequireDefault(require("./exceptions/OAuthProviderException"));
var _NotFoundException = _interopRequireDefault(require("./exceptions/NotFoundException"));
var _FailResponseException = _interopRequireDefault(require("./exceptions/FailResponseException"));
/**
* Pipedrive API v1
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/
/**
* @module ApiClient
* @version 1.0.0
*/
/**
* Manages low level client-server communications, parameter marshalling, etc. There should not be any need for an
* application to use this class directly - the *Api and model classes provide the public API for the service. The
* contents of this file should be regarded as internal but are documented for completeness.
* @alias module:ApiClient
* @class
*/
var ApiClient = /*#__PURE__*/function () {
function ApiClient() {
(0, _classCallCheck2["default"])(this, ApiClient);
/**
* The base URL against which to resolve every API call's (relative) path.
* @type {String}
* @default https://api.pipedrive.com/v1
*/
this.basePath = 'https://api.pipedrive.com/v1'.replace(/\/+$/, '');
/**
* The authentication methods to be included for all API calls.
* @type {Object}
*/
this.authentications = {
'api_key': {
type: 'apiKey',
"in": 'query',
name: 'api_token',
apiKey: ''
},
'basic_authentication': {
type: 'basic'
},
'oauth2': {
type: 'oauth2',
host: 'https://oauth.pipedrive.com',
accessToken: '',
refreshToken: '',
// The access token expiration value in seconds sent by the oauth server.
expiresIn: 0,
// The access token expiration time as number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
expiresAt: 0,
scope: '',
clientId: '',
clientSecret: '',
redirectUri: '',
tokenUpdateCallback: null
}
};
/**
* The default HTTP headers to be included for all API calls.
* @type {Object}
* @default {}
*/
this.defaultHeaders = {
'User-Agent': this.getUserAgent()
};
/**
* The default HTTP timeout for all API calls.
* @type {Number}
* @default 60000
*/
this.timeout = 60000;
/**
* If set to false an additional timestamp parameter is added to all API GET calls to
* prevent browser caching
* @type {Boolean}
* @default true
*/
this.cache = true;
/**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false
*/
this.enableCookies = false;
/*
* Used to save and return cookies in a node.js (non-browser) setting,
* if this.enableCookies is set to true.
*/
if (typeof window === 'undefined') {
this.agent = new _superagent["default"].agent();
}
/*
* Allow user to override superagent agent
*/
this.requestAgent = null;
/*
* Allow user to add superagent plugins
*/
this.plugins = null;
}
/**
* Returns a string representation for an actual parameter.
* @param param The actual parameter.
* @returns {String} The string representation of <code>param</code>.
*/
(0, _createClass2["default"])(ApiClient, [{
key: "paramToString",
value: function paramToString(param) {
if (param == undefined || param == null) {
return '';
}
if (param instanceof Date) {
return param.toJSON();
}
if (ApiClient.canBeJsonified(param)) {
return JSON.stringify(param);
}
return param.toString();
}
/**
* Returns a boolean indicating if the parameter could be JSON.stringified
* @param param The actual parameter
* @returns {Boolean} Flag indicating if <code>param</code> can be JSON.stringified
*/
}, {
key: "buildUrl",
value:
/**
* Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
* NOTE: query parameters are not handled here.
* @param {String} path The path to append to the base URL.
* @param {Object} pathParams The parameter values to append.
* @param {String} apiBasePath Base path defined in the path, operation level to override the default one
* @returns {String} The encoded path with parameter values substituted.
*/
function buildUrl(path, pathParams, apiBasePath) {
var _this = this;
if (!path.match(/^\//)) {
path = '/' + path;
}
var url = this.basePath + path;
// use API (operation, path) base path if defined
if (apiBasePath !== null && apiBasePath !== undefined) {
url = apiBasePath + path;
}
url = url.replace(/\{([\w-]+)\}/g, function (fullMatch, key) {
var value;
if (pathParams.hasOwnProperty(key)) {
value = _this.paramToString(pathParams[key]);
} else {
value = fullMatch;
}
return encodeURIComponent(value);
});
return url;
}
/**
* Checks whether the given content type represents JSON.<br>
* JSON content type examples:<br>
* <ul>
* <li>application/json</li>
* <li>application/json; charset=UTF8</li>
* <li>APPLICATION/JSON</li>
* </ul>
* @param {String} contentType The MIME content type to check.
* @returns {Boolean} <code>true</code> if <code>contentType</code> represents JSON, otherwise <code>false</code>.
*/
}, {
key: "isJsonMime",
value: function isJsonMime(contentType) {
return Boolean(contentType != null && contentType.match(/^application\/json(;.*)?$/i));
}
/**
* Chooses a content type from the given array, with JSON preferred; i.e. return JSON if included, otherwise return the first.
* @param {Array.<String>} contentTypes
* @returns {String} The chosen content type, preferring JSON.
*/
}, {
key: "jsonPreferredMime",
value: function jsonPreferredMime(contentTypes) {
for (var i = 0; i < contentTypes.length; i++) {
if (this.isJsonMime(contentTypes[i])) {
return contentTypes[i];
}
}
return contentTypes[0];
}
/**
* Checks whether the given parameter value represents file-like content.
* @param param The parameter to check.
* @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
*/
}, {
key: "isFileParam",
value: function isFileParam(param) {
// fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
if (typeof require === 'function') {
var fs;
try {
fs = require('fs');
} catch (err) {}
if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
return true;
}
}
// Buffer in Node.js
if (typeof Buffer === 'function' && param instanceof Buffer) {
return true;
}
// Blob in browser
if (typeof Blob === 'function' && param instanceof Blob) {
return true;
}
// File in browser (it seems File object is also instance of Blob, but keep this for safe)
if (typeof File === 'function' && param instanceof File) {
return true;
}
return false;
}
/**
* Normalizes parameter values:
* <ul>
* <li>remove nils</li>
* <li>keep files and arrays</li>
* <li>format to string with `paramToString` for other cases</li>
* </ul>
* @param {Object.<String, Object>} params The parameters as object properties.
* @returns {Object.<String, Object>} normalized parameters.
*/
}, {
key: "normalizeParams",
value: function normalizeParams(params) {
var newParams = {};
for (var key in params) {
if (params.hasOwnProperty(key) && params[key] != undefined && params[key] != null) {
var value = params[key];
if (this.isFileParam(value) || Array.isArray(value)) {
newParams[key] = value;
} else {
newParams[key] = this.paramToString(value);
}
}
}
return newParams;
}
/**
* Builds a string representation of an array-type actual parameter, according to the given collection format.
* @param {Array} param An array parameter.
* @param {module:ApiClient.CollectionFormatEnum} collectionFormat The array element separator strategy.
* @returns {String|Array} A string representation of the supplied collection, using the specified delimiter. Returns
* <code>param</code> as is if <code>collectionFormat</code> is <code>multi</code>.
*/
}, {
key: "buildCollectionParam",
value: function buildCollectionParam(param, collectionFormat) {
if (param == null) {
return null;
}
switch (collectionFormat) {
case 'csv':
return param.map(this.paramToString).join(',');
case 'ssv':
return param.map(this.paramToString).join(' ');
case 'tsv':
return param.map(this.paramToString).join('\t');
case 'pipes':
return param.map(this.paramToString).join('|');
case 'multi':
//return the array directly as SuperAgent will handle it as expected
return param.map(this.paramToString);
default:
throw new Error('Unknown collection format: ' + collectionFormat);
}
}
/**
* Applies authentication headers to the request.
* @param {Object} request The request object created by a <code>superagent()</code> call.
* @param {Array.<String>} authNames An array of authentication method names.
*/
}, {
key: "applyAuthToRequest",
value: function applyAuthToRequest(request, authNames) {
var _this2 = this;
authNames.forEach(function (authName) {
var auth = _this2.authentications[authName];
switch (auth.type) {
case 'basic':
if (auth.username || auth.password) {
request.auth(auth.username || '', auth.password || '');
}
break;
case 'bearer':
if (auth.accessToken) {
request.set({
'Authorization': 'Bearer ' + auth.accessToken
});
}
break;
case 'apiKey':
if (auth.apiKey) {
var data = {};
if (auth.apiKeyPrefix) {
data[auth.name] = auth.apiKeyPrefix + ' ' + auth.apiKey;
} else {
data[auth.name] = auth.apiKey;
}
if (auth['in'] === 'header') {
request.set(data);
} else {
request.query(data);
}
}
break;
case 'oauth2':
if (auth.accessToken) {
request.set({
'Authorization': 'Bearer ' + auth.accessToken
});
}
break;
default:
throw new Error('Unknown authentication type: ' + auth.type);
}
});
}
/**
* Deserializes an HTTP response body into a value of the specified type.
* @param {Object} response A SuperAgent response object.
* @param {(String|Array.<String>|Object.<String, Object>|Function)} returnType The type to return. Pass a string for simple types
* or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
* return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
* all properties on <code>data<code> will be converted to this type.
* @returns A value of the specified type.
*/
}, {
key: "deserialize",
value: function deserialize(response, returnType) {
if (response == null || returnType == null || response.status == 204) {
return null;
}
// Rely on SuperAgent for parsing response body.
// See http://visionmedia.github.io/superagent/#parsing-response-bodies
var data = response.body;
if (data == null || (0, _typeof2["default"])(data) === 'object' && typeof data.length === 'undefined' && !Object.keys(data).length) {
// SuperAgent does not always produce a body; use the unparsed response as a fallback
data = response.text;
}
return ApiClient.convertToType(data, returnType);
}
/**
* Invokes the REST service using the supplied settings and parameters.
* @param {String} path The base URL to invoke.
* @param {String} httpMethod The HTTP method to use.
* @param {Object.<String, String>} pathParams A map of path parameters and their values.
* @param {Object.<String, Object>} queryParams A map of query parameters and their values.
* @param {Object.<String, Object>} headerParams A map of header parameters and their values.
* @param {Object.<String, Object>} formParams A map of form parameters and their values.
* @param {Object} bodyParam The value to pass as the request body.
* @param {Array.<String>} authNames An array of authentication type names.
* @param {Array.<String>} contentTypes An array of request MIME types.
* @param {Array.<String>} accepts An array of acceptable response MIME types.
* @param {(String|Array|ObjectFunction)} returnType The required type to return; can be a string for simple types or the
* constructor for a complex type.
* @param {String} apiBasePath base path defined in the operation/path level to override the default one
* @param {Boolean} secondRequest Indicates whether this api call is done for the first or second time (this can happen after automatic access token refresh).
* @returns {Promise} A {@link https://www.promisejs.org/|Promise} object.
*/
}, {
key: "callApi",
value: (function () {
var _callApi = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, apiBasePath) {
var secondRequest,
url,
request,
index,
contentType,
_formParams,
key,
normalizeBodyParams,
accept,
response,
shouldRefreshTokenAndRetry,
_exception,
_exception2,
exception,
exceptionMessage,
data,
_args = arguments;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
secondRequest = _args.length > 12 && _args[12] !== undefined ? _args[12] : false;
url = this.buildUrl(path, pathParams, apiBasePath);
request = (0, _superagent["default"])(httpMethod, url);
if (this.plugins !== null) {
for (index in this.plugins) {
if (this.plugins.hasOwnProperty(index)) {
request.use(this.plugins[index]);
}
}
}
// apply authentications
this.applyAuthToRequest(request, authNames);
// set query parameters
if (httpMethod.toUpperCase() === 'GET' && this.cache === false) {
queryParams['_'] = new Date().getTime();
}
request.query(this.normalizeParams(queryParams));
// set header parameters
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
// set requestAgent if it is set by user
if (this.requestAgent) {
request.agent(this.requestAgent);
}
// set request timeout
request.timeout(this.timeout);
contentType = this.jsonPreferredMime(contentTypes);
if (contentType) {
// Issue with superagent and multipart/form-data (https://github.com/visionmedia/superagent/issues/746)
if (contentType !== 'multipart/form-data') {
request.type(contentType);
}
}
if (contentType === 'application/x-www-form-urlencoded') {
request.send(_querystring["default"].stringify(this.normalizeParams(formParams)));
} else if (contentType === 'multipart/form-data') {
_formParams = this.normalizeParams(formParams);
for (key in _formParams) {
if (_formParams.hasOwnProperty(key)) {
if (key === 'file' || this.isFileParam(_formParams[key])) {
// file field
request.attach(key, _formParams[key]);
} else {
request.field(key, _formParams[key]);
}
}
}
} else if (bodyParam !== null && bodyParam !== undefined) {
if (!request.header['Content-Type']) {
request.type('application/json');
}
normalizeBodyParams = this.replaceCamelCaseObj(bodyParam);
request.send(normalizeBodyParams);
}
accept = this.jsonPreferredMime(accepts);
if (accept) {
request.accept(accept);
}
if (returnType === 'Blob') {
request.responseType('blob');
} else if (returnType === 'String') {
request.responseType('string');
}
// Attach previously saved cookies, if enabled
if (this.enableCookies) {
if (typeof window === 'undefined') {
this.agent._attachCookies(request);
} else {
request.withCredentials();
}
}
if (!this.shouldRefreshToken()) {
_context.next = 20;
break;
}
_context.next = 20;
return this.refreshToken();
case 20:
_context.prev = 20;
_context.next = 23;
return request;
case 23:
response = _context.sent;
_context.next = 52;
break;
case 26:
_context.prev = 26;
_context.t0 = _context["catch"](20);
shouldRefreshTokenAndRetry = !!(!this.isApiTokenSet() && !secondRequest && _context.t0.status === 401 && this.isOauth2Supported());
if (!shouldRefreshTokenAndRetry) {
_context.next = 35;
break;
}
_context.next = 32;
return this.refreshToken();
case 32:
_context.next = 34;
return this.callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, apiBasePath, true);
case 34:
return _context.abrupt("return", _context.sent);
case 35:
if (!(_context.t0.status === 401)) {
_context.next = 40;
break;
}
_exception = new _UnauthorizedException["default"]();
if (_context.t0.response.body.error_info) {
_exception.errorInfo = _context.t0.response.body.error_info;
}
_exception.context = _context.t0.response;
throw _exception;
case 40:
if (!(_context.t0.status === 404)) {
_context.next = 45;
break;
}
_exception2 = new _NotFoundException["default"]();
if (_context.t0.response.body.error_info) {
_exception2.errorInfo = _context.t0.response.body.error_info;
}
_exception2.context = _context.t0.response;
throw _exception2;
case 45:
exception = new _FailResponseException["default"]();
exceptionMessage = _context.t0.response && _context.t0.response.res.statusMessage || _context.t0.message;
exception.message = exceptionMessage;
exception.errorCode = _context.t0.status;
if (_context.t0.response && _context.t0.response.body.error_info) {
exception.errorInfo = _context.t0.response.body.error_info;
}
exception.context = _context.t0.response;
throw exception;
case 52:
data = this.deserialize(response, returnType);
if (this.enableCookies && typeof window === 'undefined') {
this.agent._saveCookies(response);
}
return _context.abrupt("return", data);
case 55:
case "end":
return _context.stop();
}
}
}, _callee, this, [[20, 26]]);
}));
function callApi(_x, _x2, _x3, _x4, _x5, _x6, _x7, _x8, _x9, _x10, _x11, _x12) {
return _callApi.apply(this, arguments);
}
return callApi;
}()
/**
* Converts CamelCase attributes of an object to snake_case and returns it
*/
)
}, {
key: "replaceCamelCaseObj",
value: function replaceCamelCaseObj(obj) {
var _this3 = this;
var snakeCased = {};
for (var key in obj) {
var keyValue = obj[key];
var isArray = Array.isArray(keyValue);
var isNull = keyValue === null;
var isObject = (0, _typeof2["default"])(keyValue) === 'object' && !isArray && !isNull;
var isHash = /^[a-f0-9]{40}$|^[a-f0-9]{40}_[a-z0-9]+$/i.test(key);
if (isArray) keyValue = keyValue.map(function (kv) {
return (0, _typeof2["default"])(kv) === 'object' ? _this3.replaceCamelCaseObj(kv) : kv;
});
if (isHash) {
snakeCased[key] = isObject ? key : keyValue;
} else {
snakeCased[(0, _lodash.snakeCase)(key)] = isObject ? this.replaceCamelCaseObj(keyValue) : keyValue;
}
}
return snakeCased;
}
/**
* Checks whether the API supports oauth2 type authorization.
* @returns {Boolean} Whether oauth2 type authorization is supported by the API.
*/
}, {
key: "isOauth2Supported",
value: function isOauth2Supported() {
return !!(this.authentications && this.authentications.oauth2);
}
/**
* Checks whether the API token is set for the API calls.
* @returns {Boolean} Whether API token is set for authorization.
*/
}, {
key: "isApiTokenSet",
value: function isApiTokenSet() {
return !!(this.authentications && this.authentications.api_key && this.authentications.api_key.apiKey);
}
/**
* Checks whether the oauth2 type authorizations is set for the API calls and if the access token is expired.
* @returns {Boolean} Whether the OAuth access token is expired.
*/
}, {
key: "shouldRefreshToken",
value: function shouldRefreshToken() {
return !this.isApiTokenSet() && this.isOauth2Supported() && !!this.authentications.oauth2.expiresAt && Date.now() > this.authentications.oauth2.expiresAt;
}
/**
* Checks if the given property is set in the this.authentications.oauth2 object.
* Error is thrown if the property is not set.
* @param {String} property The OAuth 2 property to receive.
* @returns The value of the given property.
*/
}, {
key: "getOAuth2Property",
value: function getOAuth2Property(property) {
var value = this.authentications.oauth2[property];
if (!value) {
throw new Error("OAuth 2 property ".concat(property, " is not set."));
}
return value;
}
/**
* Creates the url for authorizing the client.
* @returns {String} The authorization url.
*/
}, {
key: "buildAuthorizationUrl",
value: function buildAuthorizationUrl() {
if (!this.isOauth2Supported()) {
throw new Error('Could not create authorization url. OAuth 2 is not supported.');
}
var host = this.getOAuth2Property('host');
var clientId = this.getOAuth2Property('clientId');
var redirectUri = this.getOAuth2Property('redirectUri');
return "".concat(host, "/oauth/authorize?client_id=").concat(clientId, "&redirect_uri=").concat(encodeURIComponent(redirectUri));
}
/**
* Authorizes the authorization code sent by the server and returns OAuth 2 token.
* @param {String} code The authorization code sent by the oauth server.
* @returns {Object} The OAuth 2 token.
*/
}, {
key: "authorize",
value: (function () {
var _authorize = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(code) {
var clientId, clientSecret, redirectUri, host, authorizationUrl, clientIdAndSecretInBase64, response, exception;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.isOauth2Supported()) {
_context2.next = 2;
break;
}
throw new Error('Could not authorize the client. OAuth 2 is not supported.');
case 2:
if (code) {
_context2.next = 4;
break;
}
throw new Error('Authorization failed. Authorization code is not set.');
case 4:
clientId = this.getOAuth2Property('clientId');
clientSecret = this.getOAuth2Property('clientSecret');
redirectUri = this.getOAuth2Property('redirectUri');
host = this.getOAuth2Property('host');
authorizationUrl = "".concat(host, "/oauth/token");
clientIdAndSecretInBase64 = Buffer.from("".concat(clientId, ":").concat(clientSecret)).toString('base64');
_context2.prev = 10;
_context2.next = 13;
return _superagent["default"].post(authorizationUrl).set('User-Agent', this.getUserAgent()).set('Authorization', "Basic ".concat(clientIdAndSecretInBase64)).send("code=".concat(code)).send("redirect_uri=".concat(redirectUri)).send('grant_type=authorization_code');
case 13:
response = _context2.sent;
this.updateToken(response.body);
return _context2.abrupt("return", response.body);
case 18:
_context2.prev = 18;
_context2.t0 = _context2["catch"](10);
exception = new _OAuthProviderException["default"]();
exception.message = _context2.t0.response.res.statusMessage;
exception.errorCode = _context2.t0.status;
exception.context = _context2.t0.response;
throw exception;
case 25:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[10, 18]]);
}));
function authorize(_x13) {
return _authorize.apply(this, arguments);
}
return authorize;
}()
/**
* Refreshes the OAuth 2 access token.
* @returns {Object} The OAuth 2 token.
*/
)
}, {
key: "refreshToken",
value: (function () {
var _refreshToken = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
var refreshToken, clientId, clientSecret, host, refreshUrl, clientIdAndSecretInBase64, response, exception;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (this.isOauth2Supported()) {
_context3.next = 2;
break;
}
throw new Error('Could not refresh the token. OAuth 2 is not supported.');
case 2:
refreshToken = this.getOAuth2Property('refreshToken');
clientId = this.getOAuth2Property('clientId');
clientSecret = this.getOAuth2Property('clientSecret');
host = this.getOAuth2Property('host');
refreshUrl = "".concat(host, "/oauth/token");
clientIdAndSecretInBase64 = Buffer.from("".concat(clientId, ":").concat(clientSecret)).toString('base64');
_context3.prev = 8;
_context3.next = 11;
return _superagent["default"].post(refreshUrl).set('User-Agent', this.getUserAgent()).set('Authorization', "Basic ".concat(clientIdAndSecretInBase64)).send("refresh_token=".concat(refreshToken)).send('grant_type=refresh_token');
case 11:
response = _context3.sent;
this.updateToken(response.body);
return _context3.abrupt("return", response.body);
case 16:
_context3.prev = 16;
_context3.t0 = _context3["catch"](8);
exception = new _OAuthProviderException["default"]();
exception.message = _context3.t0.response.res.statusMessage;
exception.errorCode = _context3.t0.status;
exception.context = _context3.t0.response;
throw exception;
case 23:
case "end":
return _context3.stop();
}
}
}, _callee3, this, [[8, 16]]);
}));
function refreshToken() {
return _refreshToken.apply(this, arguments);
}
return refreshToken;
}()
/**
* Updates the ApiClient oauth2 authentication properties and invokes the token update callback if it is set.
* Besides extracting all the values from the provided OAuth 2 token,
* the expiration time of access token is calculated and set as expiresAt property.
* @param {Object} token The OAuth 2 token got from the oauth server.
*/
)
}, {
key: "updateToken",
value: function updateToken(token) {
if (!this.isOauth2Supported()) {
throw new Error('Could not update the token. OAuth 2 is not supported.');
}
var oauth2 = this.authentications.oauth2;
if (!!token.access_token) {
oauth2.accessToken = token.access_token;
}
if (!!token.refresh_token) {
oauth2.refreshToken = token.refresh_token;
}
if (!!token.expires_in) {
oauth2.expiresIn = token.expires_in;
oauth2.expiresAt = Date.now() + token.expires_in * 1000;
}
if (!!token.scope) {
oauth2.scope = token.scope;
}
if (!!token.api_domain) {
this.basePath = "".concat(token.api_domain, "/api/v1");
}
if (typeof oauth2.tokenUpdateCallback === 'function') {
oauth2.tokenUpdateCallback(token);
}
}
/**
* Revoke Refresh Token aka marking an app uninstalled or revoke the Access Token.
* @param {Object} optional opts object with tokenTypeHint param, values can be: 'access_token' or 'refresh_token'.
*/
}, {
key: "revokeToken",
value: (function () {
var _revokeToken = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
var opts,
tokenTypeHint,
token,
clientId,
clientSecret,
host,
revokeUrl,
clientIdAndSecretInBase64,
request,
response,
exception,
_args4 = arguments;
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
opts = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : {};
tokenTypeHint = opts.tokenTypeHint;
if (this.isOauth2Supported()) {
_context4.next = 4;
break;
}
throw new Error('Could not revoke the token. OAuth 2 is not supported.');
case 4:
token = tokenTypeHint === 'refresh_token' ? this.getOAuth2Property('refreshToken') : encodeURIComponent(this.getOAuth2Property('accessToken'));
clientId = this.getOAuth2Property('clientId');
clientSecret = this.getOAuth2Property('clientSecret');
host = this.getOAuth2Property('host');
revokeUrl = "".concat(host, "/oauth/revoke");
clientIdAndSecretInBase64 = Buffer.from("".concat(clientId, ":").concat(clientSecret)).toString('base64');
_context4.prev = 10;
request = _superagent["default"].post(revokeUrl).set('User-Agent', this.getUserAgent()).set('Authorization', "Basic ".concat(clientIdAndSecretInBase64)).send("token=".concat(token));
if (tokenTypeHint) {
request = request.send("token_type_hint=".concat(tokenTypeHint));
}
_context4.next = 15;
return request;
case 15:
response = _context4.sent;
return _context4.abrupt("return", response.body);
case 19:
_context4.prev = 19;
_context4.t0 = _context4["catch"](10);
exception = new _OAuthProviderException["default"]();
exception.message = _context4.t0.response.res.statusMessage;
exception.errorCode = _context4.t0.status;
exception.context = _context4.t0.response;
throw exception;
case 26:
case "end":
return _context4.stop();
}
}
}, _callee4, this, [[10, 19]]);
}));
function revokeToken() {
return _revokeToken.apply(this, arguments);
}
return revokeToken;
}()
/**
* Parses an ISO-8601 string representation or epoch representation of a date value.
* @param {String} str The date value as a string.
* @returns {Date} The parsed date object.
*/
)
}, {
key: "getUserAgent",
value:
/**
* Gets the string to be used for User-Agent request header
* @returns User-Agent request header value
*/
function getUserAgent() {
var version = require('../package.json').version;
return "Pipedrive-SDK-Javascript-".concat(version);
}
/**
* Gets an array of host settings
* @returns An array of host settings
*/
}, {
key: "hostSettings",
value: function hostSettings() {
return [{
'url': "https://api.pipedrive.com/v1",
'description': "No description provided"
}];
}
}, {
key: "getBasePathFromSettings",
value: function getBasePathFromSettings(index) {
var variables = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var servers = this.hostSettings();
// check array index out of bound
if (index < 0 || index >= servers.length) {
throw new Error("Invalid index " + index + " when selecting the host settings. Must be less than " + servers.length);
}
var server = servers[index];
var url = server['url'];
// go through variable and assign a value
for (var variable_name in server['variables']) {
if (variable_name in variables) {
var variable = server['variables'][variable_name];
if (!('enum_values' in variable) || variable['enum_values'].includes(variables[variable_name])) {
url = url.replace("{" + variable_name + "}", variables[variable_name]);
} else {
throw new Error("The variable `" + variable_name + "` in the host URL has invalid value " + variables[variable_name] + ". Must be " + server['variables'][variable_name]['enum_values'] + ".");
}
} else {
// use default value
url = url.replace("{" + variable_name + "}", server['variables'][variable_name]['default_value']);
}
}
return url;
}
/**
* Constructs a new map or array model from REST data.
* @param data {Object|Array} The REST data.
* @param obj {Object|Array} The target object or array.
*/
}], [{
key: "canBeJsonified",
value: function canBeJsonified(str) {
if (typeof str !== 'string' && (0, _typeof2["default"])(str) !== 'object') return false;
try {
var type = str.toString();
return type === '[object Object]' || type === '[object Array]';
} catch (err) {
return false;
}
}
}, {
key: "parseDate",
value: function parseDate(str) {
if (isNaN(str)) {
return new Date(str);
}
return new Date(+str);
}
/**
* Converts a value to the specified type.
* @param {(String|Object)} data The data to convert, as a string or object.
* @param {(String|Array.<String>|Object.<String, Object>|Function)} type The type to return. Pass a string for simple types
* or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
* return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
* all properties on <code>data<code> will be converted to this type.
* @returns An instance of the specified type or null or undefined if data is null or undefined.
*/
}, {
key: "convertToType",
value: function convertToType(data, type) {
if (data === null || data === undefined) return data;
switch (type) {
case 'Boolean':
return Boolean(data);
case 'Integer':
return parseInt(data, 10);
case 'Number':
return parseFloat(data);
case 'String':
return String(data);
case 'Date':
if (typeof data === 'string') {
return String(data);
} else {
return ApiClient.parseDate(String(data));
}
case 'Blob':
return data;
default:
if (type === Object) {
// generic object, return directly
return data;
} else if (typeof type.constructFromObject === 'function') {
// for model type like User and enum class
return type.constructFromObject(data);
} else if (Array.isArray(type)) {
// for array type like: ['String']
var itemType = type[0];
return data.map(function (item) {
return ApiClient.convertToType(item, itemType);
});
} else if ((0, _typeof2["default"])(type) === 'object') {
// for plain object type like: {'String': 'Integer'}
var keyType, valueType;
for (var k in type) {
if (type.hasOwnProperty(k)) {
keyType = k;
valueType = type[k];
break;
}
}
var result = {};
for (var k in data) {
if (data.hasOwnProperty(k)) {
var key = ApiClient.convertToType(k, keyType);
var value = ApiClient.convertToType(data[k], valueType);
result[key] = value;
}
}
return result;
} else {
// for unknown type, return the data directly
return data;
}
}
}
}, {
key: "constructFromObject",
value: function constructFromObject(data, obj, itemType) {
if (Array.isArray(data)) {
for (var i = 0; i < data.length; i++) {
if (data.hasOwnProperty(i)) obj[i] = ApiClient.convertToType(data[i], itemType);
}
} else {
for (var k in data) {
if (data.hasOwnProperty(k)) obj[k] = ApiClient.convertToType(data[k], itemType);
}
}
}
}]);
return ApiClient;
}();
/**
* Enumeration of collection format separator strategies.
* @enum {String}
* @readonly
*/
ApiClient.CollectionFormatEnum = {
/**
* Comma-separated values. Value: <code>csv</code>
* @const
*/
CSV: ',',
/**
* Space-separated values. Value: <code>ssv</code>
* @const
*/
SSV: ' ',
/**
* Tab-separated values. Value: <code>tsv</code>
* @const
*/
TSV: '\t',
/**
* Pipe(|)-separated values. Value: <code>pipes</code>
* @const
*/
PIPES: '|',
/**
* Native array. Value: <code>multi</code>
* @const
*/
MULTI: 'multi'
};
var _default = ApiClient;
exports["default"] = _default;