-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
67 lines (54 loc) · 1.42 KB
/
util.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
62
63
64
65
66
67
import fetch from 'node-fetch';
export async function gql(query) {
const res = await fetch('https://api.hashnode.com/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({query}),
});
const json = await res.json();
return json.data;
}
export const USER_QUERY = username => `{
user(username: "${username}") {
publicationDomain
numFollowers
numFollowing
numPosts
numReactions
}
}`;
export const getUser = username => {
return gql(USER_QUERY(username));
}
export const POSTS_QUERY = username => page => `{
user(username: "${username}") {
publication {
posts(page: ${page}) {
popularity
totalReactions
}
}
}
}`;
export async function getAllPosts(username, page = 0) {
const posts = (await gql(POSTS_QUERY(username)(page))).user.publication.posts;
if (posts.length > 0) {
return posts.concat(await getAllPosts(username, page + 1));
}
return posts;
}
export function parse(data) {
const posts = data.posts;
delete data.posts;
data.stats = {
followers: data.user.numFollowers,
following: data.user.numFollowing,
posts: data.user.numPosts,
reactions: data.user.numReactions,
popularity: posts.map(post => post.popularity).reduce(sum),
};
return data;
}
export const sum = (a, b) => a + b;