From 4e4a52b32fe45781741c027ab7818a8e14cd1323 Mon Sep 17 00:00:00 2001 From: Dale Race Date: Fri, 19 Jan 2018 21:51:48 -0600 Subject: [PATCH 1/3] Add support for fetching a users payload along with publications from Medium --- .../gatsby-source-medium/src/gatsby-node.js | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/gatsby-source-medium/src/gatsby-node.js b/packages/gatsby-source-medium/src/gatsby-node.js index 7d94e52c31093..6c9fd77a9e5b4 100644 --- a/packages/gatsby-source-medium/src/gatsby-node.js +++ b/packages/gatsby-source-medium/src/gatsby-node.js @@ -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) + let importableResources = [] + let posts = {} // because `posts` needs to be in a scope accessible by `links` below - const importableResources = [ - userKeys.map(key => json.payload.references.User[key]), - posts, - collectionKeys.map(key => json.payload.references.Collection[key]), - ] + 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 => { @@ -52,7 +66,7 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => { .digest(`hex`) const links = - resource.type === `Post` + resource.type === `Post` || resource.type === `Collection` ? { author___NODE: resource.creatorId, } From 0b33b89647a24aeed73d2581050d76448c4e4557 Mon Sep 17 00:00:00 2001 From: Dale Race Date: Fri, 19 Jan 2018 22:02:19 -0600 Subject: [PATCH 2/3] Update gatsby-source-medium readme, add note for @ for usernames --- packages/gatsby-source-medium/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/gatsby-source-medium/README.md b/packages/gatsby-source-medium/README.md index e15fed0489ff0..5e83f681eb4ea 100644 --- a/packages/gatsby-source-medium/README.md +++ b/packages/gatsby-source-medium/README.md @@ -23,6 +23,8 @@ plugins: [ }, ]; ``` +###### Note +Remember that if you are fetching a user, prepend your username with `@`. ## How to query From 65072992910e37e22e54d06858a707cfbb5ffe83 Mon Sep 17 00:00:00 2001 From: Dale Race Date: Fri, 19 Jan 2018 22:08:30 -0600 Subject: [PATCH 3/3] Undo any changes to `links` variable --- packages/gatsby-source-medium/src/gatsby-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-source-medium/src/gatsby-node.js b/packages/gatsby-source-medium/src/gatsby-node.js index 6c9fd77a9e5b4..99703f51c4e5b 100644 --- a/packages/gatsby-source-medium/src/gatsby-node.js +++ b/packages/gatsby-source-medium/src/gatsby-node.js @@ -66,7 +66,7 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => { .digest(`hex`) const links = - resource.type === `Post` || resource.type === `Collection` + resource.type === `Post` ? { author___NODE: resource.creatorId, }