-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.js
37 lines (33 loc) · 998 Bytes
/
response.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
import provider from './provider';
export const parseResponse = (obj) => {
return obj.map((item) => parseBill(item));
};
export const parseBill = (obj) => {
const key = `${new Date().getTime()}-${Math.random()}`;
const { time, type, category, amount } = obj;
return {
time: Number(time),
type: Number(type),
category: String(category),
amount: Number(amount),
key
};
};
const response = () =>
new Promise((resolve, reject) => {
provider()
.then((response) => {
if (response.status === 200) {
try {
const result = parseResponse(response.data);
resolve(result);
} catch (e) {
reject(e);
}
} else {
reject(response);
}
})
.catch((error) => reject(error));
});
export default response;