Skip to content

Commit 04465e2

Browse files
committed
Use fetch to replace axios
1 parent 6ff98b2 commit 04465e2

File tree

4 files changed

+296
-169
lines changed

4 files changed

+296
-169
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"parser": "babel-eslint",
33
"extends": "airbnb/base",
44
"env": {
5-
"mocha": true
5+
"mocha": true,
6+
"browser": true
67
}
78
}

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
"url": "https://github.com/detectlanguage/detectlanguage-node/issues"
3636
},
3737
"homepage": "https://github.com/detectlanguage/detectlanguage-node",
38-
"dependencies": {
39-
"axios": "^0.21.1"
40-
},
4138
"devDependencies": {
4239
"@babel/cli": "^7.11.6",
4340
"@babel/core": "^7.11.6",

src/client.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import axios from 'axios';
21
import * as defaults from './defaults';
32
import { handleError } from './error';
43

@@ -11,28 +10,42 @@ export default class Client {
1110
Authorization: `Bearer ${apiKey}`,
1211
};
1312

14-
this.connection = axios.create({
15-
headers,
16-
baseURL: `${config.protocol}://${config.host}/${config.apiVersion}/`,
17-
timeout: config.timeout * 1000,
18-
});
13+
this.baseURL = `${config.protocol}://${config.host}/${config.apiVersion}/`;
14+
this.timeout = config.timeout * 1000;
15+
this.headers = headers;
1916
}
2017

2118
async get(path) {
2219
try {
23-
const response = await this.connection.get(path);
20+
const response = await fetch(this.baseURL + path, {
21+
headers: this.headers,
22+
timeout: this.timeout,
23+
});
2424

25-
return response.data;
25+
if (!response.ok) {
26+
throw new Error(await response.text());
27+
}
28+
29+
return await response.json();
2630
} catch (e) {
2731
return handleError(e);
2832
}
2933
}
3034

3135
async post(path, data) {
3236
try {
33-
const response = await this.connection.post(path, data);
37+
const response = await fetch(this.baseURL + path, {
38+
method: 'POST',
39+
headers: Object.assign(this.headers, { 'Content-Type': 'application/json' }),
40+
body: JSON.stringify(data),
41+
timeout: this.timeout,
42+
});
43+
44+
if (!response.ok) {
45+
throw new Error(await response.text());
46+
}
3447

35-
return response.data;
48+
return await response.json();
3649
} catch (e) {
3750
return handleError(e);
3851
}

0 commit comments

Comments
 (0)