-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (35 loc) · 1.17 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
const axios = require('axios');
class HashnodeDevblogSource {
static defaultOptions() {
return {
username: '',
typeName: 'DevblogPost',
}
}
HASHNODE_API_URL = 'https://api.hashnode.com/';
async getAllPosts() {
let query = `query { user(username: "` + this.options.username + `") { publication { posts { cuid slug title type dateUpdated dateAdded contentMarkdown brief coverImage tags { name }}}}}`;
let { data } = await axios.post(this.HASHNODE_API_URL, {query: query});
let publication = data.data.user.publication;
if (!publication) {
throw new Error('No publications found for this user.');
}
return publication.posts;
}
constructor(api, options = HashnodeDevblogSource.defaultOptions()) {
this.options = options;
if (!this.options.username) {
throw new Error('Missing username option.')
}
api.loadSource(async store => {
const contentType = store.addCollection({
typeName: this.options.typeName
});
const posts = await this.getAllPosts();
for (let post of posts) {
contentType.addNode(post);
}
});
}
}
module.exports = HashnodeDevblogSource;