UNPKG

sws-frontend

Version:

sws frontend project

88 lines 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var oboe = require("oboe"); var helpers_1 = require("../commons/helpers"); var errormodal_1 = require("../components/errormodal"); var baseapi_1 = require("./baseapi"); exports.writeopts = function (body) { var json = { headers: { 'Content-Type': 'application/json', 'Accept': ['application/octet-stream', 'application/json'] } }; return body ? helpers_1.merge(json, { body: JSON.stringify(body) }) : json; }; /* gorups all call types for export. */ exports.channelapi = function (basePath) { /* PUT call factory. */ var putopts = function (body) { return helpers_1.merge(exports.writeopts(body), { method: 'PUT' }); }; var put = function (url, handlers, body, opts) { return channel(url, handlers, helpers_1.merge(putopts(body), opts)); }; /* POST call factory. */ var postopts = function (body) { return helpers_1.merge(exports.writeopts(body), { method: 'POST' }); }; var post = function (url, handlers, body, opts) { return channel(url, handlers, helpers_1.merge(postopts(body), opts)); }; /* DELETE call factory. */ var delopts = function (body) { return helpers_1.merge(exports.writeopts(body), { method: 'DELETE' }); }; var del = function (url, handlers, body, opts) { return channel(url, handlers, helpers_1.merge(delopts(body), opts)); }; var chopts = function (url, opts) { if (opts === void 0) { opts = {}; } return helpers_1.merge(opts, { 'url': url ? basePath + "/" + url : basePath }); }; // opts={ // method: String, // optional // headers: Object, // optional // body: String|Object, // optional // cached: Boolean, // optional // withCredentials: Boolean // optional, browser only // } var channel = function (url, handlers, opts) { if (opts === void 0) { opts = {}; } var start = handlers.start, end = handlers.end, result = handlers.result, log = handlers.log, fail = handlers.fail, error = handlers.error; var _opts = chopts(url, helpers_1.merge(baseapi_1.baseopts, opts)); return new Promise(function () { return oboe(_opts) .start(function (o) { if (start) start(o); }).done(function (o) { if (end) end(o); }).node('!', function (o) { // This callback will be called everytime a new object is found var type = o.type, content = o.content; switch (type) { case "log": if (log) log(o); break; case "result": if (result) result(o); break; case "error": this.abort(); if (error) { error(helpers_1.merge(errormodal_1.errorBuilder("Unable to obtain a response"), errormodal_1.makeError(content))); break; } else { throw helpers_1.merge(errormodal_1.errorBuilder("Unable to obtain a response"), errormodal_1.makeError(content)); } } return oboe.drop; }).fail(function (e) { if (fail) fail(helpers_1.merge(errormodal_1.errorBuilder("Unable to obtain a response"), errormodal_1.makeError(e.jsonBody))); else throw e; }); }); }; return { get: channel, post: post, put: put, del: del }; }; exports.default = exports.channelapi; //# sourceMappingURL=channel.js.map