-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhackerNewsApi.js
24 lines (18 loc) · 950 Bytes
/
hackerNewsApi.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
import ApiService from './Api';
const JSON_QUERY = '.json?print=pretty';
const BASE_URL = 'http://hn.algolia.com/api/v1';
const client = new ApiService({ baseURL: BASE_URL });
const hackerNewsApi = {};
const PAGE_LIMIT = 20;
const getPageSlice = (limit, page = 0) => ({ begin: page * limit, end: (page + 1) * limit });
const getPageValues = ({ begin, end, items }) => items.slice(begin, end);
hackerNewsApi.getTopStoryIds = () => client.get(`/topstories${JSON_QUERY}`);
hackerNewsApi.getStory = id => client.get(`/item/${id}${JSON_QUERY}`);
hackerNewsApi.getStoriesByPage = (ids, page) => {
const { begin, end } = getPageSlice(PAGE_LIMIT, page);
const activeIds = getPageValues({ begin, end, items: ids });
const storyPromises = activeIds.map(id => hackerNewsApi.getStory(id));
return Promise.all(storyPromises);
};
hackerNewsApi.getFromPage = page => client.get(`/search?page=${page}&tags=(story,poll)`);
export default hackerNewsApi;