-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserService.js
49 lines (42 loc) · 1.13 KB
/
UserService.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
import axios from 'axios'
import { Config } from 'App/Config'
import { is, curryN, gte } from 'ramda'
const isWithin = curryN(3, (min, max, value) => {
const isNumber = is(Number)
return isNumber(min) && isNumber(max) && isNumber(value) && gte(value, min) && gte(max, value)
})
const in200s = isWithin(200, 299)
/**
* This is an example of a service that connects to a 3rd party API.
*
* Feel free to remove this example from your application.
*/
const userApiClient = axios.create({
/**
* Import the config from the App/Config/index.js file
*/
baseURL: Config.API_URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
timeout: 3000,
})
function fetchUser() {
// Simulate an error 50% of the time just for testing purposes
if (Math.random() > 0.5) {
return new Promise(function(resolve, reject) {
resolve(null)
})
}
let number = Math.floor(Math.random() / 0.1) + 1
return userApiClient.get(number.toString()).then((response) => {
if (in200s(response.status)) {
return response.data
}
return null
})
}
export const userService = {
fetchUser,
}