Skip to content

Commit

Permalink
[gatsby-source-medium] fetch users and publications (#3623)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
racedale authored and KyleAMathews committed Jan 23, 2018
1 parent 444fcbd commit f6be954
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/gatsby-source-medium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ plugins: [
},
];
```
###### Note
Remember that if you are fetching a user, prepend your username with `@`.

## How to query

Expand Down
32 changes: 23 additions & 9 deletions packages/gatsby-source-medium/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,29 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => {
const result = await fetch(username)
const json = JSON.parse(strip(result.data))

const { posts } = json.payload
const collectionKeys = Object.keys(json.payload.references.Collection)
const userKeys = Object.keys(json.payload.references.User)

const importableResources = [
userKeys.map(key => json.payload.references.User[key]),
posts,
collectionKeys.map(key => json.payload.references.Collection[key]),
]
let importableResources = []
let posts = {} // because `posts` needs to be in a scope accessible by `links` below

const users = Object.keys(json.payload.references.User)
.map(key => json.payload.references.User[key])
importableResources = importableResources.concat(users)

if (json.payload.posts) {
posts = json.payload.posts
importableResources = importableResources.concat(posts)
}

if (json.payload.references.Post) {
posts = Object.keys(json.payload.references.Post)
.map(key => json.payload.references.Post[key])
importableResources = importableResources.concat(posts)
}

if (json.payload.references.Collection) {
const collections = Object.keys(json.payload.references.Collection)
.map(key => json.payload.references.Collection[key])
importableResources = importableResources.concat(collections)
}

const resources = Array.prototype.concat(...importableResources)
resources.map(resource => {
Expand Down

0 comments on commit f6be954

Please sign in to comment.