-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiCaller.tsx
More file actions
111 lines (96 loc) · 3.05 KB
/
apiCaller.tsx
File metadata and controls
111 lines (96 loc) · 3.05 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import fetch from 'isomorphic-fetch';
import {
IListItem, IItem, IList, IUser,
} from './interfaces/api';
const apiUrl = 'http://x2021oxygene667208093000.francecentral.cloudapp.azure.com:5555/';
// const apiUrl = 'http://192.168.1.23:5555/';
interface IRequestOptions {
method: string;
headers: {};
body?: string;
}
function fetchApi<T>(path: string, method: string, headers = {}, data = {}) {
const requestOption: IRequestOptions = {
method,
headers: {
Accept: '*/*',
'Content-Type': 'application/json;multipart/form-data;',
...headers,
},
};
if (method !== 'GET' && method !== 'HEAD') {
requestOption.body = JSON.stringify(data);
}
return new Promise<T>((resolve, reject) => {
fetch(apiUrl + path, requestOption)
.then((response) => {
if (response.ok) {
response.json().then((json) => { resolve(json); });
} else {
response.json().then((json: Object) => {
let errors: string[] = [];
Object.values(json).forEach((e) => {
errors = errors.concat(e);
});
reject(errors);
});
}
});
});
}
function signUp(username: string, password: string): Promise<IUser> {
return fetchApi('sign-up/', 'POST', {}, {
username,
password,
});
}
function signIn(username: string, password: string): Promise<IUser> {
return fetchApi('sign-in/', 'POST', {}, {
username,
password,
});
}
function createItem(itemName: string, token: string): Promise<IItem> {
return fetchApi('create-item/', 'POST', { token }, { name: itemName });
}
function getItems(token: string) {
return fetchApi('get-items/', 'GET', { token });
}
function createShoppingList(shoppingListName: string, token: string): Promise<IList> {
return fetchApi('create-shopping-list/', 'POST', { token }, { name: shoppingListName });
}
function getShoppingList(token: string): Promise<IList[]> {
return fetchApi('get-shopping-lists/', 'GET', { token });
}
function putItemInShoppingList(shoppingListId: string,
itemId: string, token: string): Promise<IListItem> {
return fetchApi('put-item-in-shopping-list/', 'POST', { token }, {
shopping_list: shoppingListId,
item: itemId,
});
}
function getShoppingListItems(listId: string, token: string): Promise<IListItem[]> {
return fetchApi(`get-items-from-shopping-list/${listId}/`, 'GET', { token });
}
function checkShippingListItem(shoppingListItemId: string, token: string): Promise<IListItem> {
return fetchApi(`check-shopping-list-item/${shoppingListItemId}/`, 'GET', { token });
}
function deleteShoppingList(shoppingListId: string, token: string) {
return fetchApi(`delete-shopping-list/${shoppingListId}/`, 'GET', { token });
}
function uploadProfilePicture(picture: string, token: string) {
return fetchApi('upload-profile-picture/', 'POST', { token }, picture);
}
export {
signUp,
signIn,
createItem,
getItems,
createShoppingList,
getShoppingList,
putItemInShoppingList,
getShoppingListItems,
checkShippingListItem,
deleteShoppingList,
uploadProfilePicture,
};