UNPKG

@elasticemail/elasticemail-client

Version:

Official ElasticEmail SDK. This API is based on the REST API architecture, allowing the user to easily manage their data with this resource-based approach.

701 lines (664 loc) 26.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _superagent = _interopRequireDefault(require("superagent")); var _querystring = _interopRequireDefault(require("querystring")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Elastic Email REST API * This API is based on the REST API architecture, allowing the user to easily manage their data with this resource-based approach. Every API call is established on which specific request type (GET, POST, PUT, DELETE) will be used. The API has a limit of 20 concurrent connections and a hard timeout of 600 seconds per request. To start using this API, you will need your Access Token (available <a target=\"_blank\" href=\"https://app.elasticemail.com/marketing/settings/new/manage-api\">here</a>). Remember to keep it safe. Required access levels are listed in the given request’s description. Downloadable library clients can be found in our Github repository <a target=\"_blank\" href=\"https://github.com/ElasticEmail?tab=repositories&q=%22rest+api%22+in%3Areadme\">here</a> * * The version of the OpenAPI document: 4.0.0 * Contact: support@elasticemail.com * * 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 4.0.26 */ /** * 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 () { /** * The base URL against which to resolve every API call's (relative) path. * Overrides the default value set in spec file if present * @param {String} basePath */ function ApiClient() { var basePath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'https://api.elasticemail.com/v4'; _classCallCheck(this, ApiClient); /** * The base URL against which to resolve every API call's (relative) path. * @type {String} * @default https://api.elasticemail.com/v4 */ this.basePath = basePath.replace(/\/+$/, ''); /** * The authentication methods to be included for all API calls. * @type {Array.<String>} */ this.authentications = { 'apikey': { type: 'apiKey', 'in': 'header', name: 'X-ElasticEmail-ApiKey' } }; /** * The default HTTP headers to be included for all API calls. * @type {Array.<String>} * @default {} */ this.defaultHeaders = { 'User-Agent': 'OpenAPI-Generator/4.0.26/Javascript' }; /** * 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>. */ return _createClass(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, this).join(','); case 'ssv': return param.map(this.paramToString, this).join(' '); case 'tsv': return param.map(this.paramToString, this).join('\t'); case 'pipes': return param.map(this.paramToString, this).join('|'); case 'multi': //return the array directly as SuperAgent will handle it as expected return param.map(this.paramToString, this); case 'passthrough': return param; 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) { var localVarBearerToken = typeof auth.accessToken === 'function' ? auth.accessToken() : auth.accessToken; request.set({ 'Authorization': 'Bearer ' + localVarBearerToken }); } 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 || _typeof(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); } /** * Callback function to receive the result of the operation. * @callback module:ApiClient~callApiCallback * @param {String} error Error message, if any. * @param data The data returned by the service call. * @param {String} response The complete HTTP response. */ /** * 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 {module:ApiClient~callApiCallback} callback The callback function. * @returns {Object} The SuperAgent request object. */ }, { key: "callApi", value: function callApi(path, httpMethod, pathParams, queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, apiBasePath, callback) { var _this3 = this; var url = this.buildUrl(path, pathParams, apiBasePath); var request = (0, _superagent["default"])(httpMethod, url); if (this.plugins !== null) { for (var 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); var 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') { var _formParams = this.normalizeParams(formParams); for (var key in _formParams) { if (_formParams.hasOwnProperty(key)) { var _formParamsValue = _formParams[key]; if (this.isFileParam(_formParamsValue)) { // file field request.attach(key, _formParamsValue); } else if (Array.isArray(_formParamsValue) && _formParamsValue.length && this.isFileParam(_formParamsValue[0])) { // multiple files _formParamsValue.forEach(function (file) { return request.attach(key, file); }); } else { request.field(key, _formParamsValue); } } } } else if (bodyParam !== null && bodyParam !== undefined) { if (!request.header['Content-Type']) { request.type('application/json'); } request.send(bodyParam); } var accept = this.jsonPreferredMime(accepts); if (accept) { request.accept(accept); } if (returnType === 'Blob') { request.responseType('blob'); } else if (returnType === 'String') { request.responseType('text'); } // Attach previously saved cookies, if enabled if (this.enableCookies) { if (typeof window === 'undefined') { this.agent._attachCookies(request); } else { request.withCredentials(); } } request.end(function (error, response) { if (callback) { var data = null; if (!error) { try { data = _this3.deserialize(response, returnType); if (_this3.enableCookies && typeof window === 'undefined') { _this3.agent._saveCookies(response); } } catch (err) { error = err; } } callback(error, data, response); } }); return request; } /** * 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: "hostSettings", value: /** * Gets an array of host settings * @returns An array of host settings */ function hostSettings() { return [{ 'url': "https://api.elasticemail.com/v4", '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' && _typeof(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.replace(/(\d)(T)(\d)/i, '$1 $3')); } 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': 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 (_typeof(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); } } } }]); }(); /** * 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' }; /** * The default API client implementation. * @type {module:ApiClient} */ ApiClient.instance = new ApiClient(); var _default = exports["default"] = ApiClient;