UNPKG

ionic-coreo

Version:

Ionic2 module for integration with Coreo

139 lines 5.28 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var core_1 = require('@angular/core'); var Observable_1 = require('rxjs/Observable'); var http_1 = require('@angular/http'); var token_context_1 = require('./token-context'); var config_1 = require('./config'); var CoreoClientError = (function (_super) { __extends(CoreoClientError, _super); function CoreoClientError(m, response) { _super.call(this, m); this.m = m; this.response = response; Object.setPrototypeOf(this, CoreoClientError.prototype); } Object.defineProperty(CoreoClientError.prototype, "statusCode", { get: function () { return this.response && this.response.status; }, enumerable: true, configurable: true }); Object.defineProperty(CoreoClientError.prototype, "message", { get: function () { return this.m; }, enumerable: true, configurable: true }); return CoreoClientError; }(Error)); exports.CoreoClientError = CoreoClientError; var CoreoClient = (function () { function CoreoClient(http, config, tokenContext) { this.http = http; this.config = config; this.tokenContext = tokenContext; } CoreoClient.prototype.getAuthorizationHeader = function (token) { return 'JWT ' + token.token; }; CoreoClient.prototype.post = function (path, body, options) { if (options === void 0) { options = {}; } if (body instanceof FormData) { options.body = body; options.headers = options.headers || {}; options.headers['enctype'] = 'multipart/form-data'; } else if (body) { options.body = JSON.stringify(body); } options.method = 'post'; return this.request(path, options); }; CoreoClient.prototype.postRecord = function (record) { var _this = this; return Observable_1.Observable.fromPromise(record.toFormData()).switchMap(function (formData) { return _this.post('/records', formData); }); }; CoreoClient.prototype.request = function (path, options) { if (options === void 0) { options = {}; } var hdrs = options.headers || {}; if (!(options.body && options.body instanceof FormData)) { hdrs['Content-Type'] = 'application/json'; } if (typeof options.authentication !== 'undefined') { if (options.authentication) { hdrs.Authorization = this.getAuthorizationHeader(options.authentication); } } else if (this.tokenContext.token) { hdrs.Authorization = this.getAuthorizationHeader(this.tokenContext.token); } var headers = new http_1.Headers(hdrs); var url = this.config.baseUrl + path; var requestOptions = Object.assign({}, { url: url, method: 'get', body: null }, options); requestOptions.headers = headers; var request = new http_1.Request(requestOptions); return this.http.request(request) .map(function (res) { if (res.status === 204) { return {}; } else { return res.json(); } }) .catch(this.handleError); }; CoreoClient.prototype.graphqlQuery = function (query, variables, options) { if (variables === void 0) { variables = {}; } if (options === void 0) { options = {}; } var queryEncoded = encodeURI(query.replace(/\s/g, '')); return this.request("/graphql?query=" + queryEncoded, options); }; CoreoClient.prototype.handleError = function (error) { // In a real world app, we might use a remote logging infrastructure var errMsg; if (error instanceof http_1.Response) { var body = error.json() || ''; var err = body.error || JSON.stringify(body); switch (error.status) { case 400: errMsg = body && body.message || "Bad Request"; break; case 401: errMsg = body && body.message || "Unauthorized"; break; default: errMsg = error.status + " - " + (error.statusText || '') + " " + err; } } else { errMsg = error.message ? error.message : error.toString(); } return Observable_1.Observable.throw(new CoreoClientError(errMsg, error)); }; CoreoClient.decorators = [ { type: core_1.Injectable }, ]; /** @nocollapse */ CoreoClient.ctorParameters = function () { return [ { type: http_1.Http, }, { type: config_1.CoreoConfig, }, { type: token_context_1.CoreoTokenContext, }, ]; }; return CoreoClient; }()); exports.CoreoClient = CoreoClient; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = CoreoClient; //# sourceMappingURL=client.js.map