UNPKG

sth-ts

Version:

An SmartHoldem API wrapper, written in TypeScript to interact with SmartHoldem blockchain.

82 lines 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("rxjs/add/operator/map"); var RxRequest_1 = require("./RxRequest"); var json_typescript_mapper_1 = require("json-typescript-mapper"); /** Http calls from peer url. */ var Http = /** @class */ (function () { function Http(network) { this.network = network; var options = { json: true, }; if (network) { options['baseUrl'] = network.getPeerUrl(); options['headers'] = { nethash: network.nethash, port: network.activePeer.port, version: network.version, }; } this.baseRequest = new RxRequest_1.RxRequest(options); } Http.prototype.getNative = function (url, params, responseType) { var _this = this; if (params === void 0) { params = {}; } var r = new RxRequest_1.RxRequest({ json: true }); return r.get(url, this.formatParams(params)).map(function (data) { return _this.formatResponse(data, responseType); }); }; Http.prototype.get = function (url, params, responseType) { var _this = this; if (params === void 0) { params = {}; } return this.baseRequest.get("/api" + url, this.formatParams(params)) .map(function (data) { return _this.formatResponse(data, responseType); }); }; Http.prototype.post = function (url, body, responseType) { var _this = this; var options = { body: body, json: true, }; return this.baseRequest.post(url, options) .map(function (data) { return _this.formatResponse(data, responseType); }); }; Http.prototype.postNative = function (url, body, responseType) { var _this = this; var r = new RxRequest_1.RxRequest({ body: body, json: true, }); return r.post(url).map(function (data) { return _this.formatResponse(data, responseType); }); }; Http.prototype.put = function (url, data) { var options = { json: data, }; return this.baseRequest.put(url, options); }; /** * Convert JSON response to specific interface. */ Http.prototype.formatResponse = function (response, responseType) { try { var result = void 0; var body = typeof response === 'string' ? JSON.parse(response) : response; result = json_typescript_mapper_1.deserialize(responseType, body); return result; } catch (e) { throw new Error(e); } }; /** * Convert property from interface to JSON */ Http.prototype.formatParams = function (params) { var options = JSON.parse(JSON.stringify(params) || '{}'); return { qs: options }; }; return Http; }()); exports.default = Http; //# sourceMappingURL=Http.js.map