-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
35 lines (31 loc) · 944 Bytes
/
proxy.ts
File metadata and controls
35 lines (31 loc) · 944 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
import { Handler } from '@netlify/functions';
import { RestManager } from 'studo.js';
export const handler: Handler = async (event) => {
const endpoint = event.path.substr('/api/proxy/'.length);
if (!endpoint || !/^\w+(\/\w+)*\/?$/.test(endpoint)) {
return {
statusCode: 400,
body: JSON.stringify({ proxy_error: 'invalid endpoint' }),
headers: { 'Content-type': 'application/json' },
};
}
try {
const response = await RestManager.request(
event.httpMethod,
endpoint,
{ body: event.body },
event.headers['session-token']
);
return {
statusCode: response.status,
body: await response.text(),
headers: { 'Content-type': response.headers.get('content-type') },
};
} catch (error) {
return {
statusCode: 502,
body: JSON.stringify({ proxy_error: error.toString() }),
headers: { 'Content-type': 'application/json' },
};
}
};