Skip to content

Commit b66044f

Browse files
committed
Fix API issues by adding cache
- Also go back to using raw endpoint
1 parent 0ea3861 commit b66044f

2 files changed

Lines changed: 75 additions & 33 deletions

File tree

‎pages/index.js‎

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import PropTypes from 'prop-types';
2-
import Marked from 'marked';
3-
import Fetch from 'isomorphic-unfetch';
42
import Layout from '../components/Layout';
53
import Content from '../components/Content';
4+
import { getPage, getDirectory } from '../utilities/GithubAPI';
65

76
function Index(props) {
87
const { content, paths } = props;
@@ -20,37 +19,6 @@ Index.propTypes = {
2019
paths: PropTypes.arrayOf(PropTypes.string).isRequired,
2120
};
2221

23-
async function getPage(path) {
24-
const uri = `https://api.github.com/repos/codebru/markdown-test-repo/contents/${path}`;
25-
const res = await Fetch(uri);
26-
const data = await res.json();
27-
if (!Object.prototype.hasOwnProperty.call(data, 'content')) {
28-
if (Object.prototype.hasOwnProperty.call(data, 'message')) {
29-
// Error possibly
30-
return data.message;
31-
}
32-
return false;
33-
}
34-
const contentMd = Buffer.from(data.content, data.encoding).toString();
35-
const contentHTML = Marked(contentMd);
36-
return contentHTML;
37-
}
38-
39-
async function getDirectory(path) {
40-
try {
41-
const uri = `https://api.github.com/repos/codebru/markdown-test-repo/contents/${path}`;
42-
const res = await Fetch(uri);
43-
const data = await res.json();
44-
if (!data || !Array.isArray(data) || !Object.prototype.hasOwnProperty.call(data[0], 'path')) {
45-
throw new Error('Failed to fetch tree.');
46-
}
47-
const pathArray = data.map(entry => entry.path);
48-
return pathArray;
49-
} catch (e) {
50-
return `Unable to get data: ${e}`;
51-
}
52-
}
53-
5422
Index.getInitialProps = async ({ query }) => {
5523
let pathArray = [];
5624
let contentHTML = null;

‎utilities/GithubAPI.js‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Marked from 'marked';
2+
import Fetch from 'isomorphic-unfetch';
3+
4+
const HOUR = 1000 * 60 * 60;
5+
const dirCache = [];
6+
const pageCache = [];
7+
8+
function checkCache(cache, path) {
9+
let entry = null;
10+
let index = 0;
11+
let foundIndex = null;
12+
let found = false;
13+
14+
for (index = 0, found = false; index < cache.length && found === false; index += 1) {
15+
if (cache[index].path === path) {
16+
entry = cache[index];
17+
found = true;
18+
foundIndex = index;
19+
}
20+
}
21+
22+
if (!entry) {
23+
return false;
24+
}
25+
if (entry.timestamp + HOUR < Date.now()) {
26+
cache.splice(foundIndex, 1); // Remove entry
27+
return false;
28+
}
29+
return entry.data;
30+
}
31+
32+
function addCache(cache, path, data) {
33+
cache.push({ path, data, timestamp: Date.now() });
34+
}
35+
36+
async function getPage(path) {
37+
const cacheResponse = checkCache(pageCache, path);
38+
if (cacheResponse) {
39+
return cacheResponse;
40+
}
41+
// Using the raw access rather than API due to the rate limiting
42+
const uri = `https://raw.githubusercontent.com/codebru/markdown-test-repo/master/${path}`;
43+
const res = await Fetch(uri);
44+
const data = await res.text();
45+
if (String(data) === '404: Not Found\n') {
46+
return false;
47+
}
48+
const contentMd = data;
49+
const contentHTML = Marked(contentMd);
50+
addCache(pageCache, path, contentHTML);
51+
return contentHTML;
52+
}
53+
54+
async function getDirectory(path) {
55+
try {
56+
const cacheResponse = checkCache(dirCache, path);
57+
if (cacheResponse) {
58+
return cacheResponse;
59+
}
60+
const uri = `https://api.github.com/repos/codebru/markdown-test-repo/contents/${path}`;
61+
const res = await Fetch(uri);
62+
const data = await res.json();
63+
if (!data || !Array.isArray(data) || !Object.prototype.hasOwnProperty.call(data[0], 'path')) {
64+
throw new Error('Failed to fetch tree.');
65+
}
66+
const pathArray = data.map(entry => entry.path);
67+
addCache(dirCache, path, pathArray);
68+
return pathArray;
69+
} catch (e) {
70+
return `Unable to get data: ${e}`;
71+
}
72+
}
73+
74+
export { getPage, getDirectory };

0 commit comments

Comments
 (0)