forked from amodm/api-covid19-in
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
32 lines (29 loc) · 1.12 KB
/
api.js
File metadata and controls
32 lines (29 loc) · 1.12 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
/**
* Returns the raw response without any modification, treating @data as the body
*/
export function rawResponse(data) {
return new Response(typeof data === 'string' ? data : JSON.stringify(data), { headers: standardHeaders });
}
export function successResponse(data) {
const output = {
'success': true,
'data': data,
'lastRefreshed': new Date().toISOString(),
'lastOriginUpdate': new Date().toISOString()
};
return new Response(JSON.stringify(output), { headers: standardHeaders });
}
export async function errorResponse(details, timestampsPromise, status = 500) {
const timestamps = timestampsPromise ? await timestampsPromise : ["0", "0"];
const output = {
'success': false,
'error': details,
'lastRefreshed': new Date(parseInt(timestamps[0])).toISOString(),
'lastOriginUpdate': new Date(parseInt(timestamps[1])).toISOString()
};
return new Response(JSON.stringify(output), { headers: standardHeaders, status });
}
const standardHeaders = {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*'
};