Skip to content

Commit 6f7aa17

Browse files
committed
feat: Add API docs hook and render list of modules for POC API page
1 parent 7d9d3c2 commit 6f7aa17

4 files changed

Lines changed: 60 additions & 15 deletions

File tree

src/hooks/index.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,2 @@
1-
import { useState, useEffect } from 'react';
2-
3-
export function useReleaseHistory() {
4-
const releasesURL = 'https://nodejs.org/dist/index.json';
5-
const [releaseHistory, setReleaseHistory] = useState([]);
6-
useEffect(() => {
7-
const fetchData = async () => {
8-
const result = await fetch(releasesURL).then(data => data.json());
9-
setReleaseHistory(result);
10-
};
11-
fetchData();
12-
}, []);
13-
14-
return releaseHistory;
15-
}
1+
export { useApiData } from './useApiDocs';
2+
export { useReleaseHistory } from './useReleaseHistory';

src/hooks/useApiDocs.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useState, useEffect } from 'react';
2+
3+
// TODO: Flesh out API types.
4+
/* tslint:disable */
5+
interface APIResponse {
6+
classes: any[];
7+
globals: any[];
8+
methods: any[];
9+
miscs: any[];
10+
modules: any[];
11+
}
12+
/* tslint:enable */
13+
14+
export function useApiData(version: string): APIResponse {
15+
const [apiData, setApiData] = useState<APIResponse>({
16+
classes: [],
17+
globals: [],
18+
methods: [],
19+
miscs: [],
20+
modules: [],
21+
});
22+
23+
useEffect(() => {
24+
const fetchData = async () => {
25+
const res = await window.fetch(
26+
`https://nodejs.org/dist/${version}/docs/api/all.json`
27+
);
28+
setApiData((await res.json()) as APIResponse);
29+
};
30+
fetchData();
31+
}, []);
32+
33+
return apiData;
34+
}

src/hooks/useReleaseHistory.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useState, useEffect } from 'react';
2+
3+
export function useReleaseHistory() {
4+
const releasesURL = 'https://nodejs.org/dist/index.json';
5+
const [releaseHistory, setReleaseHistory] = useState([]);
6+
useEffect(() => {
7+
const fetchData = async () => {
8+
const result = await fetch(releasesURL).then(data => data.json());
9+
setReleaseHistory(result);
10+
};
11+
fetchData();
12+
}, []);
13+
14+
return releaseHistory;
15+
}

src/pages/docs.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import React from 'react';
2+
import { useApiData } from '../hooks';
23
import Hero from '../components/hero';
34
import Layout from '../components/layout';
45

56
export default () => {
67
const title = 'API Docs';
78
const description = 'Come learn yourself something.';
9+
const apiData = useApiData('v12.9.1');
810

911
return (
1012
<Layout title={title} description={description}>
1113
<Hero title={title} />
1214
<article style={{ width: '100%' }} className="article-reader">
15+
<nav>
16+
<ul>
17+
{apiData.modules.map(module => (
18+
<li key={module.name}>{module.displayName}</li>
19+
))}
20+
</ul>
21+
</nav>
1322
<p>Welcome to the API Page!</p>
1423
</article>
1524
</Layout>

0 commit comments

Comments
 (0)