-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
35 lines (30 loc) · 991 Bytes
/
server.ts
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 bodyParser from "body-parser";
import express from "express";
import fetch, { RequestInit } from "node-fetch";
const app = express();
app.use(bodyParser.json());
app.use(async (req: express.Request, res: express.Response) => {
const toggleRequestInit: RequestInit = {
headers: {
authorization: req.headers.authorization || "",
},
method: req.method,
};
if (Object.keys(req.body).length > 0) {
toggleRequestInit.body = req.body;
}
const toggleUrl = `https://track.toggl.com${req.url}`;
const togglResponse = await fetch(toggleUrl, toggleRequestInit);
try {
const togglJsonResponse = await togglResponse.json();
return res.status(togglResponse.status).json(togglJsonResponse);
} catch (err) {
return res.status(500).json({
proxyError: err,
});
}
});
const port = 5000;
app.listen(port, () => {
console.log(`Proxy server listening on port ${port}`);
});