This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.js
79 lines (70 loc) · 2.16 KB
/
Api.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
import {Toast} from "native-base";
import I18n from './i18n/i18n';
import fetch from 'react-native-fetch-polyfill';
let baseUrl = 'http://172.20.38.161:3000/';
let Api = {
get: (options = {}) => {
Api.executeRequest(options, 'GET')
},
post: (options = {}) => {
Api.executeRequest(options, 'POST')
},
executeRequest: (options, type) => {
if(!options.url){
console.log('URL is required')
return;
}
let data = {
...options.data
}
let url = options.url;
let _data = Api.prepareData(data, type)
if (type == 'GET' && Object.keys(data).length > 0) {
url = url + '?' + Object.keys(data).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`).join('&');
}
console.log(options, type)
url = baseUrl + url
return fetch(url, _data)
.then((response) => response.json())
.then((responseJson) => {
if(options.success){
options.success(responseJson)
}
if (responseJson.status == 'error'){
Toast.show({
text: responseJson.message,
buttonText: I18n.t('errorLoging')
})
}
console.log(responseJson)
})
.catch((error) => {
console.error(error);
Toast.show({
text: error,
buttonText: I18n.t('ok')
})
});
},
prepareData: (data, type) => {
let requestData = {
method: type,
headers: {
'Accept': 'application/json',
'content-type': 'application/json',
// 'Access-Control-Allow-Origin':'*'
}
// nachin da dobavim v headers kym requesta
}
if(type == 'POST') {
requestData.body=
JSON.stringify({
data
})
}
console.log(requestData)
console.log(data)
return requestData;
}
}
export default Api;