Skip to content

Commit f6be954

Browse files
racedaleKyleAMathews
authored andcommitted
[gatsby-source-medium] fetch users and publications (#3623)
* Add support for fetching a users payload along with publications from Medium * Update gatsby-source-medium readme, add note for @ for usernames * Undo any changes to `links` variable
1 parent 444fcbd commit f6be954

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/gatsby-source-medium/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ plugins: [
2323
},
2424
];
2525
```
26+
###### Note
27+
Remember that if you are fetching a user, prepend your username with `@`.
2628

2729
## How to query
2830

packages/gatsby-source-medium/src/gatsby-node.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,29 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => {
3232
const result = await fetch(username)
3333
const json = JSON.parse(strip(result.data))
3434

35-
const { posts } = json.payload
36-
const collectionKeys = Object.keys(json.payload.references.Collection)
37-
const userKeys = Object.keys(json.payload.references.User)
38-
39-
const importableResources = [
40-
userKeys.map(key => json.payload.references.User[key]),
41-
posts,
42-
collectionKeys.map(key => json.payload.references.Collection[key]),
43-
]
35+
let importableResources = []
36+
let posts = {} // because `posts` needs to be in a scope accessible by `links` below
37+
38+
const users = Object.keys(json.payload.references.User)
39+
.map(key => json.payload.references.User[key])
40+
importableResources = importableResources.concat(users)
41+
42+
if (json.payload.posts) {
43+
posts = json.payload.posts
44+
importableResources = importableResources.concat(posts)
45+
}
46+
47+
if (json.payload.references.Post) {
48+
posts = Object.keys(json.payload.references.Post)
49+
.map(key => json.payload.references.Post[key])
50+
importableResources = importableResources.concat(posts)
51+
}
52+
53+
if (json.payload.references.Collection) {
54+
const collections = Object.keys(json.payload.references.Collection)
55+
.map(key => json.payload.references.Collection[key])
56+
importableResources = importableResources.concat(collections)
57+
}
4458

4559
const resources = Array.prototype.concat(...importableResources)
4660
resources.map(resource => {

0 commit comments

Comments
 (0)