-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
56 lines (54 loc) · 1.74 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const fetch = require("node-fetch")
const queryString = require("query-string")
exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
const { name, ...apiOptions } = configOptions
// Gatsby adds a configOption that's not need for this plugin, delete it
delete configOptions.plugins
// Helper function that processes a object to match Gatsby's node structure
const processObject = object => {
const nodeId = createNodeId(`mmf-object-${object.id}`)
const nodeContent = JSON.stringify(object)
const nodeData = Object.assign({}, object, {
id: nodeId,
parent: null,
children: [],
internal: {
type: name,
content: nodeContent,
contentDigest: createContentDigest(object),
}
})
return nodeData
}
// Convert the options object into a query string
const {url, ...query} = apiOptions
const urlOptions = queryString.stringify(query)
// Join apiOptions with the MyMiniFactory API URL
const apiUrl = `${url}?${urlOptions}`
// Gatsby expects sourceNodes to return a promise
return (
// Parse a response from the apiUrl
fetch(apiUrl)
// Parse the response as JSON
.then(response => {
if (response.ok) {
return response.json()
}
throw new Error('Network response was not ok.')
})
// Process the JSON data into a node
.then(data => {
// For each query result
data.items.forEach(item => {
// Process the mmf data to match the structure of Gatsby node
const nodeData = processObject(item)
// Use Gatsby's createNode helper to create a node from the node data
createNode(nodeData)
})
}).catch( error => console.log('There has been a problem with fetch operation', error.message))
)
}