1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.setFetch = setFetch;
|
7 | exports.request = request;
|
8 |
|
9 | var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
12 |
|
13 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
14 |
|
15 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
16 |
|
17 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
18 |
|
19 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
20 |
|
21 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
22 |
|
23 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
24 |
|
25 |
|
26 |
|
27 | var _fetch = typeof window === "undefined" ? function () {
|
28 | return Promise.reject();
|
29 | } : window.fetch;
|
30 |
|
31 | var _isNode = false;
|
32 |
|
33 | function setFetch(fetch, isNode) {
|
34 | _fetch = fetch;
|
35 | _isNode = !!isNode;
|
36 | }
|
37 |
|
38 | function contentType(body) {
|
39 | if (typeof body === "string") {
|
40 | if (body === "") {
|
41 | return "text/plain";
|
42 | }
|
43 |
|
44 | return "application/octet-stream";
|
45 | } else {
|
46 | return "application/json";
|
47 | }
|
48 | }
|
49 |
|
50 | function wrapBody(body) {
|
51 | if (typeof body === "string") {
|
52 | return body;
|
53 | } else {
|
54 | return JSON.stringify(body);
|
55 | }
|
56 | }
|
57 |
|
58 | function parseResult(text) {
|
59 | try {
|
60 | return JSON.parse(text);
|
61 | } catch (e) {
|
62 | return text;
|
63 | }
|
64 | }
|
65 |
|
66 | function request(_x) {
|
67 | return _request.apply(this, arguments);
|
68 | }
|
69 |
|
70 | function _request() {
|
71 | _request = _asyncToGenerator(
|
72 |
|
73 | _regenerator["default"].mark(function _callee(options) {
|
74 | var fetchOptions, res, resText, resJson;
|
75 | return _regenerator["default"].wrap(function _callee$(_context) {
|
76 | while (1) {
|
77 | switch (_context.prev = _context.next) {
|
78 | case 0:
|
79 | fetchOptions = {
|
80 | method: options.method,
|
81 | body: wrapBody(options.body),
|
82 | credentials: "same-origin",
|
83 | headers: {}
|
84 | };
|
85 |
|
86 | if (options.skipContentTypeHeader == null || options.skipContentTypeHeader === false) {
|
87 | fetchOptions.headers = _objectSpread({}, fetchOptions.headers, {
|
88 | 'Content-Type': contentType(options.body == null ? "" : options.body)
|
89 | });
|
90 | }
|
91 |
|
92 |
|
93 | if (_isNode) {
|
94 | fetchOptions.headers = _objectSpread({}, fetchOptions.headers, {
|
95 | 'Origin': "https://node.trezor.io"
|
96 | });
|
97 | }
|
98 |
|
99 | _context.next = 5;
|
100 | return _fetch(options.url, fetchOptions);
|
101 |
|
102 | case 5:
|
103 | res = _context.sent;
|
104 | _context.next = 8;
|
105 | return res.text();
|
106 |
|
107 | case 8:
|
108 | resText = _context.sent;
|
109 |
|
110 | if (!res.ok) {
|
111 | _context.next = 13;
|
112 | break;
|
113 | }
|
114 |
|
115 | return _context.abrupt("return", parseResult(resText));
|
116 |
|
117 | case 13:
|
118 | resJson = parseResult(resText);
|
119 |
|
120 | if (!(_typeof(resJson) === "object" && resJson != null && resJson.error != null)) {
|
121 | _context.next = 18;
|
122 | break;
|
123 | }
|
124 |
|
125 | throw new Error(resJson.error);
|
126 |
|
127 | case 18:
|
128 | throw new Error(resText);
|
129 |
|
130 | case 19:
|
131 | case "end":
|
132 | return _context.stop();
|
133 | }
|
134 | }
|
135 | }, _callee);
|
136 | }));
|
137 | return _request.apply(this, arguments);
|
138 | } |
\ | No newline at end of file |