UNPKG

yaas-sdk

Version:
77 lines (68 loc) 3.08 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var YAAS_Client = exports.YAAS_Client = function () { function YAAS_Client(token) { _classCallCheck(this, YAAS_Client); this.token = token; } _createClass(YAAS_Client, [{ key: 'ws', value: function ws(path, method, query, body) { return YAAS_Client.ws(path, method, query, body, this.token); } }], [{ key: 'ws', value: function ws(path, method, query, body, token) { var url = path; var opts = { method: (method || 'GET').toUpperCase(), mode: 'cors', credentials: 'omit', headers: {} }; if (query) { var qs = []; for (var i in query) { qs.push(encodeURIComponent(i) + '=' + encodeURIComponent(query[i])); } if (qs.length) { url += '?' + qs.join('&'); } } if (typeof body === 'string') { opts.headers['Content-Type'] = 'application/x-www-form-urlencoded'; opts.body = body; } else if ((typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object') { opts.headers['Content-Type'] = 'application/json'; opts.body = JSON.stringify(body); } if (token) { opts.headers['Authorization'] = 'Bearer ' + token; } return fetch(url, opts).then(function (response) { if (response.ok) { if (response.headers.get('Content-Type') === 'application/json') { return response.json(); } else { return response.text(); } } else { throw response.status; } }); } }, { key: 'getAccessTokenFromCredentials', value: function getAccessTokenFromCredentials(ep, clientId, clientSecret, scope) { var body = ["grant_type=client_credentials", "scope=" + (scope && scope.join ? scope.join(' ') : scope || ''), "client_id=" + clientId, "client_secret=" + clientSecret].join('&'); return YAAS_Client.ws(ep, 'POST', null, body); } }]); return YAAS_Client; }(); //# sourceMappingURL=client.js.map