-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapiHelpers.js
More file actions
32 lines (29 loc) · 835 Bytes
/
apiHelpers.js
File metadata and controls
32 lines (29 loc) · 835 Bytes
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
import AsyncStorage from '@react-native-community/async-storage'
import axios from 'axios'
const APP_API_URL = 'XXX'
export const makeUrl = (strings, ...values) => args =>
values.reduceRight((a, v, i) => `${strings[i]}${args[v]}${a}`, strings[strings.length - 1])
let accessToken = ''
export function request(method, url, isAuth = true) {
return async args => {
if (!accessToken) {
accessToken = await AsyncStorage.getItem('accessToken')
}
const actualUrl = typeof url === 'function' ? url(args) : `${url}`
let headers = {
'Content-Type': 'application/json',
}
if (isAuth) {
headers = {
...headers,
Authorization: `Bearer ${accessToken}`,
}
}
return axios({
method,
url: `${APP_API_URL}${actualUrl}`,
headers,
...args,
})
}
}