|
1 | | -const WooCommerceAPI = require('woocommerce-api'); |
2 | | -const { processNode, normaliseFieldName } = require('./helpers'); |
| 1 | +const WooCommerceAPI = require("woocommerce-api") |
| 2 | +const { |
| 3 | + processNode, |
| 4 | + normaliseFieldName, |
| 5 | + mapMediaToNodes, |
| 6 | + mapProductsToCategories, |
| 7 | +} = require("./helpers") |
3 | 8 |
|
4 | 9 | exports.sourceNodes = async ( |
5 | | - { boundActionCreators, createNodeId }, |
| 10 | + { actions, createNodeId, createContentDigest, store, cache }, |
6 | 11 | configOptions |
7 | 12 | ) => { |
8 | | - const { createNode } = boundActionCreators; |
9 | | - delete configOptions.plugins; |
| 13 | + const { createNode, touchNode } = actions |
| 14 | + delete configOptions.plugins |
10 | 15 |
|
11 | 16 | const { api, https, api_keys, fields, api_version = 'wc/v1', per_page } = configOptions; |
12 | 17 |
|
13 | 18 | // set up WooCommerce node api tool |
14 | 19 | const WooCommerce = new WooCommerceAPI({ |
15 | | - url: `http${https?'s':''}://${api}`, |
| 20 | + url: `http${https ? "s" : ""}://${api}`, |
16 | 21 | consumerKey: api_keys.consumer_key, |
17 | 22 | consumerSecret: api_keys.consumer_secret, |
18 | 23 | wpAPI: true, |
19 | 24 | version: api_version |
20 | 25 | }); |
21 | 26 |
|
22 | 27 | // Fetch Node and turn our response to JSON |
23 | | - const fetchNodes = async (fieldName) => { |
24 | | - const endpoint = per_page |
25 | | - ? fieldName + `?per_page=${per_page}` |
26 | | - : fieldName; |
27 | | - |
28 | | - const res = await WooCommerce.getAsync(endpoint); |
29 | | - const json = res.toJSON(); |
30 | | - if(json.statusCode !== 200) { |
| 28 | + const fetchNodes = async fieldName => { |
| 29 | + const endpoint = per_page ? fieldName + `?per_page=${per_page}` : fieldName |
| 30 | + |
| 31 | + const res = await WooCommerce.getAsync(endpoint) |
| 32 | + const json = res.toJSON() |
| 33 | + if (json.statusCode !== 200) { |
31 | 34 | console.warn(` |
32 | | - \n========== WARNING FOR FIELD ${fieldName} ==========\n` |
33 | | - ); |
34 | | - console.warn(`The following error message was produced: ${json.body}`); |
35 | | - console.warn(`\n========== END WARNING ==========\n`); |
36 | | - return []; |
| 35 | + \n========== WARNING FOR FIELD ${fieldName} ==========\n`) |
| 36 | + console.warn(`The following error message was produced: ${json.body}`) |
| 37 | + console.warn(`\n========== END WARNING ==========\n`) |
| 38 | + return [] |
37 | 39 | } |
38 | | - return JSON.parse(json.body); |
39 | | - }; |
| 40 | + return JSON.parse(json.body) |
| 41 | + } |
40 | 42 |
|
41 | 43 | // Loop over each field set in configOptions and process/create nodes |
42 | | - async function fetchNodesAndCreate (array) { |
| 44 | + async function fetchNodesAndCreate(array) { |
| 45 | + let nodes = [] |
43 | 46 | for (const field of array) { |
44 | | - const nodes = await fetchNodes(field); |
45 | | - const fieldName = normaliseFieldName(field); |
46 | | - nodes.forEach(n=>createNode(processNode(createNodeId, n, fieldName))); |
| 47 | + const fieldName = normaliseFieldName(field) |
| 48 | + let tempNodes = await fetchNodes(field) |
| 49 | + tempNodes = tempNodes.map(node => { |
| 50 | + return { |
| 51 | + fieldName, |
| 52 | + ...node, |
| 53 | + } |
| 54 | + }) |
| 55 | + nodes = nodes.concat(tempNodes) |
47 | 56 | } |
| 57 | + |
| 58 | + nodes = await mapMediaToNodes({ |
| 59 | + nodes, |
| 60 | + store, |
| 61 | + cache, |
| 62 | + createNode, |
| 63 | + createNodeId, |
| 64 | + touchNode, |
| 65 | + }) |
| 66 | + |
| 67 | + nodes = nodes.map(node => |
| 68 | + processNode(createNodeId, createContentDigest, node) |
| 69 | + ) |
| 70 | + nodes = mapProductsToCategories(nodes) |
| 71 | + |
| 72 | + nodes.forEach(node => createNode(node)) |
48 | 73 | } |
49 | | - |
| 74 | + |
50 | 75 | // Leh go... |
51 | | - await fetchNodesAndCreate(fields); |
52 | | - return; |
53 | | -}; |
| 76 | + await fetchNodesAndCreate(fields) |
| 77 | + return |
| 78 | +} |
0 commit comments