-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapiCallHandler.js
More file actions
156 lines (135 loc) · 3.8 KB
/
Copy pathapiCallHandler.js
File metadata and controls
156 lines (135 loc) · 3.8 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import axios from "axios";
import omit from "lodash/omit";
const ApiCallHandler = ({ reactAppApiEndpoint }) => {
const host = reactAppApiEndpoint;
const get = async (url, headers) => {
return await axios.get(url, headers);
};
const post = async (url, body, headers) => {
return await axios.post(url, body, headers);
};
const put = async (url, body, headers) => {
return await axios.put(url, body, headers);
};
const headers = (accessToken) => {
let headersHash;
if (accessToken) {
headersHash = { Accept: "application/json", Authorization: accessToken };
} else {
headersHash = { Accept: "application/json" };
}
return { headers: headersHash };
};
const createOrUpdateProject = async (projectWithUserId, accessToken) => {
const project = omit(projectWithUserId, ["user_id"]);
if (!project.identifier) {
return await post(
`${host}/api/projects`,
{ project },
headers(accessToken),
);
} else {
return await put(
`${host}/api/projects/${project.identifier}`,
{ project },
headers(accessToken),
);
}
};
const deleteProject = async (identifier, accessToken) => {
return await axios.delete(
`${host}/api/projects/${identifier}`,
headers(accessToken),
);
};
const getImage = async (url) => {
return await get(url, headers());
};
const loadRemix = async (projectIdentifier, accessToken) => {
return await get(
`${host}/api/projects/${projectIdentifier}/remix`,
headers(accessToken),
);
};
const createRemix = async (project, accessToken) => {
return await post(
`${host}/api/projects/${project.identifier}/remix`,
{ project },
headers(accessToken),
);
};
const readProject = async (projectIdentifier, locale, accessToken) => {
const queryString = locale ? `?locale=${locale}` : "";
return await get(
`${host}/api/projects/${projectIdentifier}${queryString}`,
headers(accessToken),
);
};
const loadAssets = async (assetsIdentifier, locale, accessToken) => {
const queryString = locale ? `?locale=${locale}` : "";
return await get(
`${host}/api/projects/${assetsIdentifier}/images${queryString}`,
headers(accessToken),
);
};
const readProjectList = async (page, accessToken) => {
return await get(`${host}/api/projects`, {
params: { page },
...headers(accessToken),
});
};
const uploadImages = async (projectIdentifier, accessToken, images) => {
var formData = new FormData();
images.forEach((image) => {
formData.append("images[]", image, image.name);
});
return await post(
`${host}/api/projects/${projectIdentifier}/images`,
formData,
{ ...headers(accessToken), "Content-Type": "multipart/form-data" },
);
};
const updateImage = async (projectIdentifier, accessToken, image) => {
var formData = new FormData();
formData.append("image", image, image.name);
return await put(
`${host}/api/projects/${projectIdentifier}/images`,
formData,
{ ...headers(accessToken), "Content-Type": "multipart/form-data" },
);
};
const createError = async (
projectIdentifier,
userId,
error,
sendError = false,
) => {
if (!sendError) {
return;
}
const { errorMessage, errorType } = error;
return await post(`${host}/api/project_errors`, {
error: errorMessage,
error_type: errorType,
project_id: projectIdentifier,
user_id: userId,
});
};
return {
get,
post,
put,
createOrUpdateProject,
deleteProject,
getImage,
loadRemix,
createRemix,
readProject,
loadAssets,
readProjectList,
uploadImages,
updateImage,
createError,
};
};
export default ApiCallHandler;