|
1 | | - |
2 | | -import fetch from 'isomorphic-fetch'; |
| 1 | +import superagent from 'superagent'; |
3 | 2 | import makeDebug from 'debug'; |
4 | 3 |
|
5 | 4 | const debug = makeDebug('foxapi'); |
@@ -49,61 +48,58 @@ export default function request(method, uri, data, options) { |
49 | 48 | }, |
50 | 49 | }; |
51 | 50 |
|
| 51 | + const agent = options.agent || superagent; |
| 52 | + const requestPromise = |
| 53 | + agent[method.toLowerCase()](uri) |
| 54 | + .set(options.headers) |
| 55 | + .withCredentials(); |
| 56 | + |
52 | 57 | if (data) { |
53 | 58 | if (method.toUpperCase() === 'GET') { |
54 | 59 | const queryString = serialize(data); |
| 60 | + |
55 | 61 | if (queryString) { |
56 | | - uri = appendQueryString(uri, queryString); |
| 62 | + requestPromise.query(queryString); |
57 | 63 | } |
58 | 64 | } else { |
59 | | - options.body = JSON.stringify(data); |
| 65 | + requestPromise.send(data); |
60 | 66 | } |
61 | 67 | } |
62 | 68 |
|
63 | 69 | let error = null; |
64 | 70 |
|
65 | 71 | debug(`${method.toUpperCase()} ${uri}`); |
| 72 | + |
66 | 73 | if (debug.enabled && data) { |
67 | 74 | debug(JSON.stringify(data)); |
68 | 75 | } |
69 | | - const promise = fetch(uri, options); |
70 | 76 |
|
71 | 77 | if (options.handleResponse !== false) { |
72 | | - return promise |
73 | | - .then(response => { |
74 | | - debug(`${response.status} ${method.toUpperCase()} ${uri}`); |
75 | | - if (response.status == 401) { |
76 | | - options.unauthorizedHandler(response); |
77 | | - } |
78 | | - if (response.status < 200 || response.status >= 300) { |
79 | | - const message = `${method.toUpperCase()} ${uri} responded with ${response.statusText}`; |
80 | | - error = new Error(message); |
81 | | - error.response = response; |
82 | | - } |
83 | | - |
84 | | - return response; |
85 | | - }) |
86 | | - .then(response => response.text()) |
87 | | - .then(responseText => { |
88 | | - let json = null; |
89 | | - if (responseText) { |
90 | | - try { |
91 | | - json = JSON.parse(responseText); |
92 | | - } catch (ex) { |
93 | | - // invalid json |
| 78 | + return requestPromise |
| 79 | + .then( |
| 80 | + response => { |
| 81 | + debug(`${response.status} ${method.toUpperCase()} ${uri}`); |
| 82 | + |
| 83 | + return response.body; |
| 84 | + }, |
| 85 | + err => { |
| 86 | + if (err.statusCode === 401) { |
| 87 | + options.unauthorizedHandler(); |
94 | 88 | } |
95 | | - } |
96 | 89 |
|
97 | | - if (error) { |
98 | | - error.responseJson = json; |
| 90 | + error = new Error(err.message || String(err)); |
| 91 | + error.response = err.response; |
| 92 | + error.responseJson = err.response; |
| 93 | + |
| 94 | + const message = `${method.toUpperCase()} ${uri} responded with ${err.statusCode}`; |
| 95 | + debug(message); |
| 96 | + |
99 | 97 | throw error; |
100 | 98 | } |
101 | | - |
102 | | - return json; |
103 | | - }); |
| 99 | + ); |
104 | 100 | } |
105 | 101 |
|
106 | | - return promise; |
| 102 | + return requestPromise; |
107 | 103 | } |
108 | 104 |
|
109 | 105 |
|
0 commit comments