Skip to content

Commit 5282f73

Browse files
committed
fix: refactor according to changed hashnode api
1 parent 7c8c30a commit 5282f73

File tree

3 files changed

+10
-46
lines changed

3 files changed

+10
-46
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<h1 align="center">gridsome-source-hashnode-devblog</h1>
77
<p align="center">
88
Gridsome plugin to retrieve blog posts from your <a href = "https://hashnode.com/devblog">devblog</a> on <a href = "https://hashnode.com/">hashnode</a>. </p>
9-
<p align="center"><a href="https://npmjs.com/package/gridsome-source-hashnode-devblog"><img src="https://badge.fury.io/js/gridsome-source-hashnode-devblog.svg" alt="npm version"></a><img alt="npm" src="https://img.shields.io/npm/dt/gridsome-source-hashnode-devblog"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square"><img src="https://badgen.net/github/license/nishantwrp/gridsome-source-hashnode-devblog"><img src="https://img.shields.io/david/nishantwrp/gridsome-source-hashnode-devblog"></p>
9+
<p align="center">
10+
<a href="https://npmjs.com/package/gridsome-source-hashnode-devblog"><img src="https://badge.fury.io/js/gridsome-source-hashnode-devblog.svg" alt="npm version"></a>
11+
<img alt="npm" src="https://img.shields.io/npm/dt/gridsome-source-hashnode-devblog">
12+
<img src="https://img.shields.io/david/nishantwrp/gridsome-source-hashnode-devblog"></p>
1013
</p>
1114

1215
## Installation
@@ -28,7 +31,7 @@ module.exports = {
2831
{
2932
use: 'gridsome-source-hashnode-devblog',
3033
options: {
31-
username: '', // Your username on hashnode
34+
username: '', // Your username on hashnode without `@`.
3235
typeName: 'DevblogPost' // Optional
3336
}
3437
}
@@ -48,7 +51,6 @@ query {
4851
type
4952
dateAdded
5053
contentMarkdown
51-
content
5254
brief
5355
coverImage
5456
tags {

index.js

+4-42
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,16 @@ class HashnodeDevblogSource {
1010

1111
HASHNODE_API_URL = 'https://api.hashnode.com/';
1212

13-
async getCuidsForAllPosts() {
14-
let query = `query{ user(username: "` + this.options.username + `") {publication {posts { cuid }}}}`;
13+
async getAllPosts() {
14+
let query = `query { user(username: "` + this.options.username + `") { publication { posts { cuid slug title type dateUpdated dateAdded contentMarkdown brief coverImage tags { name }}}}}`;
1515
let { data } = await axios.post(this.HASHNODE_API_URL, {query: query});
1616
let publication = data.data.user.publication;
1717

1818
if (!publication) {
1919
throw new Error('No publications found for this user.');
2020
}
2121

22-
let posts = publication.posts;
23-
24-
let allCuids = []
25-
for (let post of posts) {
26-
allCuids.push(post.cuid);
27-
}
28-
29-
return allCuids;
30-
}
31-
32-
getQueryForSinglePostDetail(cuid) {
33-
return cuid + `:` + `post(cuid: "` + cuid + `") { slug title type dateUpdated dateAdded contentMarkdown content brief coverImage tags { name }}`;
34-
}
35-
36-
async getAllPostDetails(allCuids) {
37-
if (!allCuids.length) {
38-
console.warn('No posts found in the devblog.');
39-
return [];
40-
}
41-
42-
let query = `query{`;
43-
44-
for (let cuid of allCuids) {
45-
query += this.getQueryForSinglePostDetail(cuid);
46-
}
47-
48-
query += '}';
49-
50-
let { data } = await axios.post(this.HASHNODE_API_URL, {query: query});
51-
data = data.data;
52-
53-
let posts = [];
54-
55-
for (let postCuid in data) {
56-
posts.push(data[postCuid]);
57-
}
58-
59-
return posts;
22+
return publication.posts;
6023
}
6124

6225
constructor(api, options = HashnodeDevblogSource.defaultOptions()) {
@@ -71,8 +34,7 @@ class HashnodeDevblogSource {
7134
typeName: this.options.typeName
7235
});
7336

74-
const allCuids = await this.getCuidsForAllPosts();
75-
const posts = await this.getAllPostDetails(allCuids);
37+
const posts = await this.getAllPosts();
7638

7739
for (let post of posts) {
7840
contentType.addNode(post);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
},
2323
"homepage": "https://github.com/nishantwrp/gridsome-source-hashnode-devblog#readme",
2424
"dependencies": {
25-
"axios": "^0.19.2"
25+
"axios": "^0.21.1"
2626
}
2727
}

0 commit comments

Comments
 (0)