UNPKG

6.09 kBJavaScriptView Raw
1function 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; }
2
3function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
5function _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; }
6
7function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
8
9function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
10
11import 'isomorphic-fetch';
12/**
13 * Return a promise that is resolved or rejected depending on the response's
14 * status code.
15 */
16
17var _checkStatus = function _checkStatus(res) {
18 if (res.status >= 400) {
19 // Need to wrap the response in an object so that it matches the same error object
20 // returned by _catchStatusError
21 return Promise.reject(res);
22 }
23
24 return Promise.resolve(res);
25};
26
27var _tryParseJSON = function _tryParseJSON(res) {
28 try {
29 return res.json() // if JSON cannot parse, it'll return a rejected promise instead
30 // of throwing an error, so we catch that rejection so that we can rejected
31 // with the response like everything else expects
32 ["catch"](function () {
33 return Promise.reject(res);
34 });
35 } catch (error) {
36 return Promise.reject(res);
37 }
38};
39/**
40 * Calls fetch on provided url with default options necessary for interacting
41 * with our JSON API. Parses the JSON, provides appropriate headers, and asserts
42 * a valid status from the server.
43 */
44
45
46var _fetchJSON = function _fetchJSON(url) {
47 var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
48
49 var _ref$headers = _ref.headers,
50 headers = _ref$headers === void 0 ? {} : _ref$headers,
51 _ref$method = _ref.method,
52 method = _ref$method === void 0 ? 'GET' : _ref$method,
53 mode = _ref.mode,
54 options = _objectWithoutProperties(_ref, ["headers", "method", "mode"]);
55
56 var fetchHeaders = headers;
57
58 if (method !== 'GET') {
59 fetchHeaders = _objectSpread({
60 'Content-Type': 'application/json'
61 }, headers);
62 }
63
64 var fetchOptions = _objectSpread({
65 method: method,
66 mode: mode,
67 headers: fetchHeaders,
68 credentials: mode === 'cors' ? 'include' : 'same-origin'
69 }, options);
70
71 return fetch(url, fetchOptions).then(_checkStatus).then(_tryParseJSON);
72};
73
74var _hasArgumentsError = function _hasArgumentsError(responseData) {
75 return !!(responseData['error_detail'] && responseData['error_detail']['ARGUMENTS_ERROR']);
76};
77/**
78 * Parse v3 errors into an array of objects representing the errors returned by
79 * the API. The format of the parsed errors looks like:
80 *
81 * {
82 * status_code: 400,
83 * error: 'ERROR_CODE',
84 * description: 'Description of the error
85 * }
86 *
87 * An ARGUMENTS_ERROR looks like:
88 *
89 * {
90 * error: 'ARGUMENTS_ERROR',
91 * description: 'Some of the fields were invalid or something',
92 * argumentErrors: {
93 * attr1: ['INVALID'],
94 * attr2: ['This field is required']
95 * }
96 * }
97 *
98 */
99
100
101var _parseError = function _parseError(responseData) {
102 if (!responseData.error) {
103 // Weird error format, return null
104 return null;
105 }
106
107 var error = {
108 error: responseData.error,
109 description: responseData['error_description']
110 };
111
112 if (_hasArgumentsError(responseData)) {
113 error = _objectSpread(_objectSpread({}, error), {}, {
114 argumentErrors: responseData['error_detail']['ARGUMENTS_ERROR']
115 });
116 }
117
118 return error;
119};
120/**
121 * Designed to work with `_checkStatus`, or any function that
122 * raises an error on an invalid status. The error raised should have a `response`
123 * property with the original response object.
124 *
125 * Example usage:
126 *
127 * _fetchJSON('/api/v3/test/path', {'body': someData})
128 * .catch(_catchStatusError)
129 * .then(doSomethingOnSuccess)
130 * .catch(({response, parsedError}) => doSomethingOnError());
131 */
132
133
134var _catchStatusError = function _catchStatusError(res) {
135 return new Promise(function (resolve, reject) {
136 _tryParseJSON(res) // handled error, so reject with parsed error data along with response
137 .then(function (responseData) {
138 return reject({
139 response: res,
140 parsedError: _parseError(responseData)
141 });
142 }) // Unhandled error
143 ["catch"](function () {
144 return reject({
145 response: res
146 });
147 });
148 });
149};
150
151/**
152 * Low-level method that makes fetch requests, returning the response formatted as JSON.
153 * It parses errors from API v3 and throws exceptions with those errors
154 */
155var jsonRequest = function jsonRequest(url, options) {
156 return _fetchJSON(url, options)["catch"](_catchStatusError);
157};
158
159export default jsonRequest;
\No newline at end of file