-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
39 lines (34 loc) · 981 Bytes
/
gatsby-node.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
const fetch = require('node-fetch')
const queryString = require('query-string')
exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
delete configOptions.plugins
const processGif = gif => {
const nodeId = createNodeId(`giphy-tending-gif-${gif.id}`)
const nodeContent = JSON.stringify(gif)
const nodeData = Object.assign({}, gif, {
id: nodeId,
parent: null,
children: [],
internal: {
type: `GiphyGif`,
content: nodeContent,
contentDigest: createContentDigest(gif)
}
})
return nodeData
}
const apiOptions = queryString.stringify(configOptions)
const apiUrl = `https://api.giphy.com/v1/gifs/trending?${apiOptions}`
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
data.data.forEach(gif => {
const nodeData = processGif(gif)
createNode(nodeData)
})
})
}