forked from collective/gatsby-source-plone
/
gatsby-node.js
46 lines (41 loc) · 1010 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
40
41
42
43
44
45
46
import axios from 'axios';
import crypto from 'crypto';
// Helper to create content digest
const createContentDigest = item =>
crypto
.createHash(`md5`)
.update(JSON.stringify(item))
.digest(`hex`);
// Helper to get data from url
const fetchData = async url => {
const { data } = await axios.get(url, {
headers: {
accept: 'application/json',
},
params: {
metadata_fields: '_all',
},
});
return data;
};
exports.sourceNodes = async (
{ boundActionCreators, getNode, hasNodeChanged, store, cache },
{ baseUrl }
) => {
const { createNode } = boundActionCreators;
const data = await fetchData(`${baseUrl}/@search`);
data.items.map(item => {
let node = {
...item,
internal: {
type: `Plone${item['@type'].replace(' ', '')}`,
contentDigest: createContentDigest(item),
mediaType: 'text/html',
},
};
node.id = item['@id'];
node.parent = null;
node.children = [];
createNode(node);
});
};