-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
62 lines (56 loc) · 1.47 KB
/
proxy.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
import token from './token';
export default async function proxy(urlStr, method, headers, fetch) {
const url = new URL(urlStr);
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,OPTIONS',
'Access-Control-Max-Age': '86400',
};
const API_URL = 'https://developer.sepush.co.za/business/2.0';
async function handleRequest() {
let response = await fetch(API_URL + url.pathname + url.search, { headers: { Token: token } });
return {
status: response.status,
body: response.body,
headers: {
'Access-Control-Allow-Origin': '*',
Vary: 'Origin',
'Cache-Control': 'max-age=7200',
},
};
}
function handleOptions() {
if (headers['Origin'] !== null && headers['Access-Control-Request-Method'] !== null && headers['Access-Control-Request-Headers'] !== null) {
// Handle CORS preflight requests.
return {
body: null,
headers: {
...corsHeaders,
'Access-Control-Allow-Headers': headers['Access-Control-Request-Headers'],
},
};
} else {
// Handle standard OPTIONS request.
return {
body: null,
headers: {
Allow: 'GET, OPTIONS',
},
};
}
}
if (url.pathname == '/') {
return { status: 404 };
} else if (method === 'OPTIONS') {
// Handle CORS preflight requests
return handleOptions();
} else if (method === 'GET') {
// Handle requests to the API server
return handleRequest();
} else {
return {
status: 405,
statusText: 'Method Not Allowed',
};
}
}