-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpAgent.js
88 lines (79 loc) · 2.41 KB
/
httpAgent.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import axios from 'axios';
import qs from 'qs';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { acc } from 'react-native-reanimated';
class Agent {
// TODO: maybe package into small helper superagent auth lib.
constructor(
API_ROOT = '/',
tokenKeyName = '@secureJwt',
setTokenInStorage = true,
token = '',
) {
console.log(API_ROOT);
this._responseBody = this._responseBody.bind(this);
this.API_ROOT = API_ROOT;
this.axios = axios;
this.tokenKeyName = tokenKeyName;
this.setTokenInStorage = setTokenInStorage;
this.token = token;
this.axios.interceptors.request.use(
async function (config) {
const token = this.setTokenInStorage
? await Agent.getToken(this.tokenKeyName)
: this.token;
config.headers.Authorization = `Token ${token}`;
return config;
}.bind(this),
);
}
static getToken(tokenKeyName) {
// gets the token from local-storage
return AsyncStorage.getItem(tokenKeyName);
}
static setToken(token, tokenKeyName) {
// sets token in local-storage
return AsyncStorage.setItem(tokenKeyName, token);
}
async _tokenPlugin(req) {
/**
* plugin superagent uses before each request,
* using static method getToken to get token
* and set in it the header
* */
try {
this.token = await Agent.getToken(this.tokenKeyName);
req.set('Authorization', `Token ${this.token || ''}`);
return req;
} catch (error) {
console.log('Error while fetching token from storage.', error);
return req;
}
}
async _responseBody(res) {
const {accessToken} = res.data.user || {};
console.log('Did we get a token?!', accessToken);
if (accessToken) {
// set in local-storage, so that on next request it's
// attached in header
await Agent.setToken(accessToken, this.tokenKeyName);
}
return res.data;
}
_get(url, fullPath = false, query = {}) {
return axios
.get(fullPath ? url : `${this.API_ROOT}${url}`, qs.stringify(query))
.then(this._responseBody);
}
_post(url, body, fullPath = false) {
return axios
.post(fullPath ? url : `${this.API_ROOT}${url}`, body)
.then(this._responseBody);
}
_put(url, body, fullPath = false) {
return axios
.put(fullPath ? url : `${this.API_ROOT}${url}`, body)
.then(this._responseBody);
}
}
export default Agent;