forked from KKBOX/OpenAPI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.js
More file actions
53 lines (50 loc) · 1.04 KB
/
HttpClient.js
File metadata and controls
53 lines (50 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import axios from 'axios';
import { apiError } from '../catchError';
/**
* Do request to open api server with authorization header and error catch.
*/
export default class HttpClient {
/**
* @param {string} token - Need access token to initialize.
*/
constructor(token) {
/**
* @private
* @type {string}
*/
this.token = 'Bearer ' + token;
}
/**
* Http get method.
*
* @param {string} endpoint - Uri endpoint.
* @param {object} params - Uri parameters.
* @return {Promise}
*/
get(endpoint, params = {}) {
return axios
.get(endpoint, {
params: params,
headers: {
Authorization: this.token
}
})
.catch(apiError);
}
/**
* Http post method.
*
* @param {string} endpoint - Uri endpoint.
* @param {object} data - Body json data.
* @return {Promise}
*/
post(endpoint, data = {}) {
return axios
.post(endpoint, data, {
headers: {
Authorization: this.token
}
})
.catch(apiError);
}
}