UNPKG

cobinhood-rx

Version:

CobinhoodRx is a rxjs node wrapper for the CobinhoodRx Api.

69 lines 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const rxjs_1 = require("rxjs"); const Utilities_1 = require("./Utilities"); const NounceManager_1 = require("./NounceManager"); const HttpMethod_1 = require("../Enum/HttpMethod"); const json2typescript_1 = require("json2typescript"); class TransportManager { constructor(config, http) { this.config = config; this.http = http; this.jsc = new json2typescript_1.JsonConvert(); this.transportOptions = { method: 'GET', agent: false, headers: {}, }; } publicRequest(httpMethod, url, requestOptions = {}, useCredentials = false) { return this.prepareRequest(httpMethod, url, requestOptions, useCredentials); } privateRequest(httpMethod, url, requestOptions = {}, useCredentials = true) { return this.prepareRequest(httpMethod, url, requestOptions, useCredentials); } prepareRequest(httpMethod, url, requestOptions = {}, useCredentials = false) { if (this.config.token === null && useCredentials) { return rxjs_1.Observable.throw(new Error('No API Token Found!')); } let opts = Object.assign({}, this.transportOptions); opts.method = httpMethod; let requestObject = requestOptions; requestObject = Utilities_1.Utilities.removeUndefined(Object.assign({}, requestOptions)); if (httpMethod === HttpMethod_1.HttpMethod.POST || httpMethod === HttpMethod_1.HttpMethod.PUT) { const body = requestObject; opts = Object.assign({ body: JSON.stringify(body) }, opts); } if (useCredentials) { opts.headers.authorization = this.config.token; if (httpMethod !== HttpMethod_1.HttpMethod.GET) { opts.headers.nonce = NounceManager_1.default.getNonce(); } } if (requestObject && httpMethod === HttpMethod_1.HttpMethod.GET) { url = `${url}${Utilities_1.Utilities.generateQuerySting(requestObject)}`; } return this.http.request(url, opts); } processResponse(res, classType, dataKey) { if (res.success) { let result = (dataKey) ? res.result[dataKey] : res.result; result = (result) ? result : res.result; if (res.result === null) { return res.success; } if (classType) { return this.jsc.deserialize(result, classType); } else if (!classType) { return result; } } else { throw res.error.error_code; } } } exports.TransportManager = TransportManager; //# sourceMappingURL=TransportManager.js.map