Skip to content

Commit 6c3f028

Browse files
committed
Merge branch 'master' of github.com:FoxComm/api-js
2 parents f39d72a + 0795b06 commit 6c3f028

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"homepage": "https://github.com/FoxComm/api-js#readme",
2929
"dependencies": {
3030
"debug": "^2.2.0",
31-
"isomorphic-fetch": "^2.2.1",
31+
"postinstall-build": "^0.2.1",
32+
"superagent": "^3.2.1",
3233
"jwt-decode": "^2.0.1"
3334
},
3435
"devDependencies": {
@@ -37,7 +38,6 @@
3738
"babel-preset-stage-1": "^6.5.0",
3839
"escape-html": "^1.0.3",
3940
"leafdoc": "github:anru/Leafdoc#v1.1",
40-
"lodash": "^4.13.1",
4141
"mkdirp": "^0.5.1",
4242
"mocha": "^2.5.2",
4343
"nock": "^8.0.0"

src/api/auth.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export default class Auth {
2929

3030
_processJWT(promise, jwt) {
3131
return promise.then(response => {
32-
jwt = response.headers.get('jwt');
33-
return response.json();
32+
jwt = response.header.jwt;
33+
34+
return response.body;
3435
})
3536
.then(data => {
3637
if (data.email) {

src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default class Api {
3333
this.api_url = args.api_url.replace(/\/?$/, ''); // ensure no trailing slash
3434
this.stripe_key = args.stripe_key;
3535

36+
// could be passed superagent or supertest instance
37+
this.agent = args.agent || require('superagent');
38+
3639
// @property addresses: Addresses
3740
// Addresses instance
3841
this.addresses = new Addresses(this);
@@ -138,6 +141,9 @@ export default class Api {
138141
...(options.headers || {}),
139142
};
140143
}
144+
145+
options.agent = this.agent;
146+
141147
return request(method, finalUrl, data, options);
142148
}
143149

src/utils/request.js

+30-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
import fetch from 'isomorphic-fetch';
1+
import superagent from 'superagent';
32
import makeDebug from 'debug';
43

54
const debug = makeDebug('foxapi');
@@ -49,61 +48,58 @@ export default function request(method, uri, data, options) {
4948
},
5049
};
5150

51+
const agent = options.agent || superagent;
52+
const requestPromise =
53+
agent[method.toLowerCase()](uri)
54+
.set(options.headers)
55+
.withCredentials();
56+
5257
if (data) {
5358
if (method.toUpperCase() === 'GET') {
5459
const queryString = serialize(data);
60+
5561
if (queryString) {
56-
uri = appendQueryString(uri, queryString);
62+
requestPromise.query(queryString);
5763
}
5864
} else {
59-
options.body = JSON.stringify(data);
65+
requestPromise.send(data);
6066
}
6167
}
6268

6369
let error = null;
6470

6571
debug(`${method.toUpperCase()} ${uri}`);
72+
6673
if (debug.enabled && data) {
6774
debug(JSON.stringify(data));
6875
}
69-
const promise = fetch(uri, options);
7076

7177
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();
9488
}
95-
}
9689

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+
9997
throw error;
10098
}
101-
102-
return json;
103-
});
99+
);
104100
}
105101

106-
return promise;
102+
return requestPromise;
107103
}
108104

109105

0 commit comments

Comments
 (0)