forked from ronnieduke/gatsby-source-mura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
63 lines (55 loc) · 1.86 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
57
58
59
60
61
62
63
const fetch = require('node-fetch')
const queryString = require('query-string')
const crypto = require('crypto')
exports.sourceNodes = (
{ boundActionCreators, createNodeId },
configOptions
) => {
const { createNode } = boundActionCreators
// Gatsby adds a configOption that's not needed for this plugin, delete it
// delete configOptions.plugins
// Helper function that processes Mura Content to match Gatsby's node structure
const processItem = item => {
const nodeId = createNodeId(`${item.contentid}`)
const nodeContent = JSON.stringify(item)
const nodeContentDigest = crypto
.createHash('md5')
.update(nodeContent)
.digest('hex')
const nodeData = Object.assign({}, item, {
id: nodeId,
parent: null,
children: [],
internal: {
type: `MuraNode`,
content: nodeContent,
contentDigest: nodeContentDigest,
},
})
return nodeData
}
// Convert the options object into a query string
const apiOptions = queryString.stringify(configOptions)
const baseURL = "https://it-demo.muraxp.com"
const siteID = "headless-demo"
// Join apiOptions with the Pixabay API URL
const apiUrl = `${baseURL}/index.cfm/_api/json/v1/${siteID}/content/?${apiOptions}`
console.log(apiUrl)
// Gatsby expects sourceNodes to return a promise
return (
// Fetch a response from the apiUrl
fetch(apiUrl)
// Parse the response as JSON
.then(response => response.json())
// Process the JSON data into a node
.then(data => {
// For each query result (or 'hit')
data.data.items.forEach(item => {
// Process the item data to match the structure of a Gatsby node
const nodeData = processItem(item)
// Use Gatsby's createNode helper to create a node from the node data
createNode(nodeData)
})
})
)
}