-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgridsome.server.js
40 lines (32 loc) · 1.07 KB
/
gridsome.server.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
const WPService = require('./services/WPService');
const { normalizeFields } = require('./utils/normalizer');
const getPosts = async actions => {
const { data } = await WPService.getPosts();
const collection = actions.addCollection('Post');
for (const post of data) {
collection.addNode(normalizeFields(post));
}
};
const getProducts = async actions => {
const { data } = await WPService.getProducts();
const productsCollection = actions.addCollection('Product');
const normalizedData = [];
for (const product of data) {
normalizedData.push(normalizeFields(product));
productsCollection.addNode(normalizeFields(product));
}
const brandsCollection = actions.addCollection('Brand');
const brands = [
...new Set(normalizedData.map(item => item.brand && item.brand.name)),
];
for (let index = 0; index < brands.length; index++) {
brandsCollection.addNode({ name: brands[index] });
}
};
module.exports = api => {
api.loadSource(async actions => {
await getPosts(actions);
await getProducts(actions);
});
api.createPages(() => {});
};