forked from goosedefi/goose-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse-lib.js
More file actions
35 lines (31 loc) · 804 Bytes
/
response-lib.js
File metadata and controls
35 lines (31 loc) · 804 Bytes
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
export function success(body) {
if(typeof body === "string"){
return buildResponse(200, body);
}else{
return buildResponse(200, JSON.stringify(body));
}
}
export function badRequest(message) {
return buildResponse(400, JSON.stringify({message: message}));
}
export function exception(ex) {
return buildResponse(500, JSON.stringify({message: ex.toString()}));
}
export function failure(body) {
console.log(body);
if(body.statusCode) {
return body;
}
return buildResponse(500, JSON.stringify({message: body.toString()}));
}
export function buildResponse(statusCode, body, headers) {
return {
statusCode: statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
...headers
},
body: body
};
}