UNPKG

wallee

Version:
281 lines (280 loc) 12.8 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebAppService = void 0; const Promise = require("bluebird"); const axios = require("axios"); const HMACAuthentication_1 = require("../auth/HMACAuthentication"); const ObjectSerializer_1 = require("../serializers/ObjectSerializer"); const ClientError_1 = require("../models/ClientError"); const ServerError_1 = require("../models/ServerError"); class WebAppService { constructor(configuration) { this._basePath = 'https://app-wallee.com:443/api'; this._defaultHeaders = {}; this._useQuerystring = false; this._timeout = 25; this._defaultAuthentication = new HMACAuthentication_1.HMACAuthentication(configuration).apply; this._defaultHeaders = configuration.default_headers; this.setTimeout(configuration.timeout); } /** * Set timeout in seconds. Default timeout: 25 seconds * @param {number} timeout */ set timeout(timeout) { this.setTimeout(timeout); } setTimeout(timeout) { if (timeout !== undefined) { if (!Number.isInteger(timeout)) { throw new Error('Timeout value has to be integer'); } if (timeout) { this._timeout = timeout; } else { throw new Error('Timeout value has to be greater than 0'); } } } set basePath(basePath) { this._basePath = basePath; } get basePath() { return this._basePath; } setDefaultAuthentication(auth) { this._defaultAuthentication = auth; } getVersion() { if (typeof (process) !== 'undefined' && process && process.version) { return 'node ' + process.version; } else { return 'unknown'; } } /** * This operation returns true when the app is installed in given space. The web app is implied by the client ID resp. user ID that is been used to invoke this operation. * @summary Check Installation * @param spaceId This parameter identifies the space which should be checked if the web app is installed. * @param {*} [options] Override http request options. */ checkInstallation(spaceId, options = {}) { const url = '/web-app/check-installation'; let queryParams = {}; let headers = Object.assign({}, this._defaultHeaders); // verify required parameter 'spaceId' is not null or undefined if (spaceId === null || spaceId === undefined) { throw new Error('Required parameter spaceId was null or undefined when calling checkInstallation.'); } if (spaceId !== undefined) { queryParams['spaceId'] = ObjectSerializer_1.ObjectSerializer.serialize(spaceId, "number"); } headers['Content-Type'] = 'application/json'; Object.assign(headers, options.headers); let defaultHeaders = { "x-meta-sdk-version": "4.7.0", "x-meta-sdk-language": "typescript", "x-meta-sdk-provider": "wallee", "x-meta-sdk-language-version": this.getVersion(), }; Object.assign(headers, defaultHeaders); let requestConfig = { url, method: 'GET', baseURL: this._basePath, headers, params: queryParams, timeout: this._timeout * 1000, responseType: 'json', }; const axiosInstance = axios.default.create(); axiosInstance.interceptors.request.use(this._defaultAuthentication); return new Promise((resolve, reject) => { axiosInstance.request(requestConfig) .then(success => { let body; body = ObjectSerializer_1.ObjectSerializer.deserialize(success.data, "boolean"); return resolve({ response: success.request.res, body: body }); }, failure => { var _a, _b, _c, _d, _e; let errorObject; if ((_a = failure.response) === null || _a === void 0 ? void 0 : _a.status) { if (failure.response.status >= 400 && failure.response.status <= 499) { errorObject = new ClientError_1.ClientError(); } else if (failure.response.status >= 500 && failure.response.status <= 599) { errorObject = new ServerError_1.ServerError(); } else { errorObject = new Object(); } } else { errorObject = new Object(); } return reject({ errorType: errorObject.constructor.name, date: (new Date()).toDateString(), statusCode: ((_b = failure.response) === null || _b === void 0 ? void 0 : _b.status) && isNaN(failure.response.status) ? String(failure.response.status) : "Unknown", statusMessage: ((_c = failure.response) === null || _c === void 0 ? void 0 : _c.statusText) != null ? failure.response.statusText : "Unknown", body: (_d = failure.response) === null || _d === void 0 ? void 0 : _d.data, response: (_e = failure.response) === null || _e === void 0 ? void 0 : _e.request.res }); }) .catch(error => { return reject(error); }); }); } ; /** * This operation confirms the app installation. This method has to be invoked after the user returns to the web app. The request of the user will contain the code as a request parameter. The web app is implied by the client ID resp. user ID that is been used to invoke this operation. * @summary Confirm * @param request * @param {*} [options] Override http request options. */ confirm(request, options = {}) { const url = '/web-app/confirm'; let queryParams = {}; let headers = Object.assign({}, this._defaultHeaders); // verify required parameter 'request' is not null or undefined if (request === null || request === undefined) { throw new Error('Required parameter request was null or undefined when calling confirm.'); } headers['Content-Type'] = 'application/json'; Object.assign(headers, options.headers); let defaultHeaders = { "x-meta-sdk-version": "4.7.0", "x-meta-sdk-language": "typescript", "x-meta-sdk-provider": "wallee", "x-meta-sdk-language-version": this.getVersion(), }; Object.assign(headers, defaultHeaders); let requestConfig = { url, method: 'POST', baseURL: this._basePath, headers, params: queryParams, data: request, timeout: this._timeout * 1000, responseType: 'json', }; const axiosInstance = axios.default.create(); axiosInstance.interceptors.request.use(this._defaultAuthentication); return new Promise((resolve, reject) => { axiosInstance.request(requestConfig) .then(success => { let body; body = ObjectSerializer_1.ObjectSerializer.deserialize(success.data, "WebAppConfirmationResponse"); return resolve({ response: success.request.res, body: body }); }, failure => { var _a, _b, _c, _d, _e; let errorObject; if ((_a = failure.response) === null || _a === void 0 ? void 0 : _a.status) { if (failure.response.status >= 400 && failure.response.status <= 499) { errorObject = new ClientError_1.ClientError(); } else if (failure.response.status >= 500 && failure.response.status <= 599) { errorObject = new ServerError_1.ServerError(); } else { errorObject = new Object(); } } else { errorObject = new Object(); } return reject({ errorType: errorObject.constructor.name, date: (new Date()).toDateString(), statusCode: ((_b = failure.response) === null || _b === void 0 ? void 0 : _b.status) && isNaN(failure.response.status) ? String(failure.response.status) : "Unknown", statusMessage: ((_c = failure.response) === null || _c === void 0 ? void 0 : _c.statusText) != null ? failure.response.statusText : "Unknown", body: (_d = failure.response) === null || _d === void 0 ? void 0 : _d.data, response: (_e = failure.response) === null || _e === void 0 ? void 0 : _e.request.res }); }) .catch(error => { return reject(error); }); }); } ; /** * This operation uninstalls the web app from the provided space. The web app is implied by the client ID resp. user ID that is been used to invoke this operation. * @summary Uninstall * @param spaceId This parameter identifies the space within which the web app should be uninstalled. * @param {*} [options] Override http request options. */ uninstall(spaceId, options = {}) { const url = '/web-app/uninstall'; let queryParams = {}; let headers = Object.assign({}, this._defaultHeaders); // verify required parameter 'spaceId' is not null or undefined if (spaceId === null || spaceId === undefined) { throw new Error('Required parameter spaceId was null or undefined when calling uninstall.'); } if (spaceId !== undefined) { queryParams['spaceId'] = ObjectSerializer_1.ObjectSerializer.serialize(spaceId, "number"); } headers['Content-Type'] = 'application/json'; Object.assign(headers, options.headers); let defaultHeaders = { "x-meta-sdk-version": "4.7.0", "x-meta-sdk-language": "typescript", "x-meta-sdk-provider": "wallee", "x-meta-sdk-language-version": this.getVersion(), }; Object.assign(headers, defaultHeaders); let requestConfig = { url, method: 'POST', baseURL: this._basePath, headers, params: queryParams, timeout: this._timeout * 1000, responseType: 'json', }; const axiosInstance = axios.default.create(); axiosInstance.interceptors.request.use(this._defaultAuthentication); return new Promise((resolve, reject) => { axiosInstance.request(requestConfig) .then(success => { let body; return resolve({ response: success.request.res, body: body }); }, failure => { var _a, _b, _c, _d, _e; let errorObject; if ((_a = failure.response) === null || _a === void 0 ? void 0 : _a.status) { if (failure.response.status >= 400 && failure.response.status <= 499) { errorObject = new ClientError_1.ClientError(); } else if (failure.response.status >= 500 && failure.response.status <= 599) { errorObject = new ServerError_1.ServerError(); } else { errorObject = new Object(); } } else { errorObject = new Object(); } return reject({ errorType: errorObject.constructor.name, date: (new Date()).toDateString(), statusCode: ((_b = failure.response) === null || _b === void 0 ? void 0 : _b.status) && isNaN(failure.response.status) ? String(failure.response.status) : "Unknown", statusMessage: ((_c = failure.response) === null || _c === void 0 ? void 0 : _c.statusText) != null ? failure.response.statusText : "Unknown", body: (_d = failure.response) === null || _d === void 0 ? void 0 : _d.data, response: (_e = failure.response) === null || _e === void 0 ? void 0 : _e.request.res }); }) .catch(error => { return reject(error); }); }); } ; } exports.WebAppService = WebAppService;