forked from mlaursen/react-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
49 lines (44 loc) · 1.76 KB
/
github.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
import express from 'express';
import { RATE_LIMIT, RATE_REMAINING, RATE_RESET } from 'constants/headerKeys';
import { FORBIDDEN } from 'constants/responseCodes';
import { Version } from 'react-md';
import { fetchGithub } from 'utils/api';
const githubRouter = express.Router();
const headers = new Headers({
Accept: 'application/vnd.github.v3+json',
'User-Agent': `react-md-documentation/${Version} mlaursen`,
});
function setHeader(key, res, { headers }) {
res.set(key, headers.get(key));
}
/**
* Soo.. There's probably a better way to set up a proxy server, but that isn't
* in my knowledge set. Anyways...
*
* GitHub likes when you specify a custom User-Agent that contains your app name/username
* when making api calls to help with debugging and other things. At the time of making
* this server, the User-Agent header is considered read-only in most browser still, and breaks
* api calls. This "proxy" server was created instead to help with this.
*
* All this endpoint will do is proxy any requests from /api/github to the github api endpoint
* and then provide the reate limiting headers in the response to my documentation website.
*
* @see https://developer.github.com/v3/#user-agent-required
*/
async function githubProxy(req, res) {
const response = await fetchGithub(req.url, { headers });
setHeader(RATE_LIMIT, res, response);
setHeader(RATE_REMAINING, res, response);
setHeader(RATE_RESET, res, response);
if (response.ok) {
const json = await response.json();
res.json(json);
} else if (response.status === FORBIDDEN) {
const json = await response.json();
res.status(FORBIDDEN).json(json);
} else {
res.status(response.status).send(response.statusText);
}
}
githubRouter.get('*', githubProxy);
export default githubRouter;