-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
47 lines (44 loc) · 1.21 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
const fetch = require("node-fetch")
const queryString = require("query-string")
exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
const { name, url } = configOptions
delete configOptions.plugins
const processObject = (object, index, type) => {
const nodeId = createNodeId(`mmf-object-map-${type}-${index}`)
const nodeContent = JSON.stringify(object)
const nodeData = Object.assign({}, object, {
id: nodeId,
parent: null,
children: [],
internal: {
type: name + type,
content: nodeContent,
contentDigest: createContentDigest(object),
}
})
return nodeData
}
return (
fetch(url)
.then(response => {
if (response.ok) {
return response.json()
}
throw new Error('Network response was not ok.')
})
.then(data => {
data.places.forEach((place, i) => {
const nodeData = processObject(place, i, 'Place')
createNode(nodeData)
})
data.objects.forEach((object, i) => {
const nodeData = processObject(object, i, 'Object')
createNode(nodeData)
})
}).catch( error => console.log('There has been a problem with fetch operation', error.message))
)
}