-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgetDocs.ts
46 lines (41 loc) · 1.41 KB
/
getDocs.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
36
37
38
39
40
41
42
43
44
45
46
import config from 'config';
import { Headers } from 'node-fetch';
import fetch, { cleanHeaders, handleRes } from './fetch';
import getCategories from './getCategories';
interface Document {
_id: string;
title: string;
slug: string;
order: number;
hidden: boolean;
children: Document[];
}
function flatten(data: Document[][]): Document[] {
return [].concat(...data);
}
async function getCategoryDocs(key: string, selectedVersion: string, category: string): Promise<Document[]> {
return fetch(`${config.get('host')}/api/v1/categories/${category}/docs`, {
method: 'get',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
'Content-Type': 'application/json',
})
),
}).then(handleRes);
}
/**
* Retrieve the docs under all categories
*
* @param {String} key the project API key
* @param {String} selectedVersion the project version
* @returns {Promise<Array<Document>>} an array containing the docs
*/
export default async function getDocs(key: string, selectedVersion: string): Promise<Document[]> {
return getCategories(key, selectedVersion)
.then(categories => categories.filter(({ type }: { type: string }) => type === 'guide'))
.then(categories => categories.map(({ slug }: { slug: string }) => getCategoryDocs(key, selectedVersion, slug)))
.then(categoryDocsPromises => Promise.all(categoryDocsPromises))
.then(flatten);
}