-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
61 lines (55 loc) · 1.26 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
// Hacker News Icon by Freepik <http://www.flaticon.com/free-icon/hacker-news-logo_51785>
const got = require('got');
const OPTS_DEFAULT = {
json: true,
};
const getItemData = id => got(
`https://hacker-news.firebaseio.com/v0/item/${id}.json`,
OPTS_DEFAULT
);
const processNews = ({ body }) => {
const top = body
.filter((x, y) => y < 10)
.map(getItemData);
return Promise.all(top)
.then(topResults => {
const items = topResults.map(({ body: item }) => ({
title: item.title,
subtitle: item.by,
arg: item.url,
icon: {
path: './icon.png',
},
}));
return items;
});
};
module.exports = {
keyword: 'hn',
action: 'openurl',
helper: {
title: 'Get stories on Hacker News',
subtitle: 'Available Options: new, top, best',
icon: {
path: './icon.png',
},
},
query: q => new Promise(resolve => {
switch (q) {
case 'new':
case 'top':
case 'best':
got(
`https://hacker-news.firebaseio.com/v0/${q}stories.json`,
OPTS_DEFAULT
)
.then(processNews)
.then((items) => resolve({ items }));
return;
default:
// do nothing...
break;
}
resolve({ items: [] });
}),
};