forked from pasdo501/gatsby-source-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
115 lines (102 loc) · 3.07 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
const {
processNode,
normaliseFieldName,
mapMediaToNodes,
mapProductsToCategories,
mapProductsToTags,
mapRelatedProducts,
mapGroupedProducts,
asyncGetProductVariations,
} = require("./helpers");
exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest, store, cache },
configOptions
) => {
const { createNode, touchNode } = actions;
delete configOptions.plugins;
const {
api,
https,
api_keys,
fields,
api_version = "wc/v3",
per_page,
wpAPIPrefix = null,
} = configOptions;
// set up WooCommerce node api tool
const WooCommerce = new WooCommerceRestApi({
url: `http${https ? "s" : ""}://${api}`,
consumerKey: api_keys.consumer_key,
consumerSecret: api_keys.consumer_secret,
version: api_version,
wpAPIPrefix,
});
// Fetch Node data for a given field name
const fetchNodes = async (fieldName) => {
let data_ = [];
let page = 1;
let pages;
do {
let args = per_page ? { per_page, page } : { page };
await WooCommerce.get(fieldName, args)
.then((response) => {
if (response.status === 200) {
data_ = [...data_, ...response.data];
pages = parseInt(response.headers["x-wp-totalpages"]);
page++;
} else {
console.warn(`
========== WARNING FOR FIELD ${fieldName} ===========
The following error status was produced: ${response.data}
================== END WARNING ==================
`);
return [];
}
})
.catch((error) => {
console.warn(`
========== WARNING FOR FIELD ${fieldName} ===========
The following error status was produced: ${error.response.status}
================== END WARNING ==================
`);
return [];
});
} while (page <= pages);
return data_;
};
// Loop over each field set in configOptions and process/create nodes
async function fetchNodesAndCreate(array) {
let nodes = [];
for (const field of array) {
const fieldName = normaliseFieldName(field);
let tempNodes = await fetchNodes(field);
tempNodes = tempNodes.map((node) => ({
...node,
id: createNodeId(`woocommerce-${fieldName}-${node.id}`),
wordpress_id: node.id,
__type: `wc${fieldName[0].toUpperCase() + fieldName.slice(1)}`,
}));
nodes = nodes.concat(tempNodes);
}
nodes = await asyncGetProductVariations(nodes, WooCommerce);
nodes = await mapMediaToNodes({
nodes,
store,
cache,
createNode,
createNodeId,
touchNode,
});
nodes = mapProductsToCategories(nodes);
nodes = mapProductsToTags(nodes);
nodes = mapRelatedProducts(nodes);
nodes = mapGroupedProducts(nodes);
nodes = nodes.map((node) => processNode(createContentDigest, node));
nodes.forEach((node) => {
createNode(node);
});
}
await fetchNodesAndCreate(fields);
return;
};