From dba585241ffffec61f03443cce0999628bd567c1 Mon Sep 17 00:00:00 2001 From: pasdo501 Date: Sat, 9 Mar 2019 11:55:34 +1300 Subject: [PATCH 1/5] Allow nested fields, very basic error handling - Allows access to 'nested' fields, e.g. products/categories - Works for field names following the specs of the API docs (https://woocommerce.github.io/woocommerce-rest-api-docs/) - Does NOT work for endpoints containing a wild card, e.g. products/id:/reviews - Some very basic error handling - if the route is not available, a warning message is logged to the console during develop/build - In this case, an empty array is returning, effectively skipping this field - Additional minor change: Added a 'wordpress_id' field to nodes -> this corresponds to the int ID saved in the wordpress DB (e.g. post_id, tax_id, etc) --- gatsby-node.js | 15 ++++++++++++--- helpers/index.js | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index d1e6df6..b9073b5 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,5 +1,5 @@ const WooCommerceAPI = require('woocommerce-api'); -const { processNode } = require('./helpers'); +const { processNode, normaliseFieldName } = require('./helpers'); exports.sourceNodes = async ( { boundActionCreators, createNodeId }, @@ -22,14 +22,23 @@ exports.sourceNodes = async ( // Fetch Node and turn our response to JSON const fetchNodes = async (fieldName) => { const res = await WooCommerce.getAsync(fieldName); - return JSON.parse(res.toJSON().body); + const json = res.toJSON(); + if(json.statusCode !== 200) { + console.warn(` + \n========== WARNING FOR FIELD ${fieldName} ==========\n`); + console.warn(`The following error message was produced: ${json.body}`); + console.warn(`\n========== END WARNING ==========\n`); + return []; + } + return JSON.parse(json.body); }; // Loop over each field set in configOptions and process/create nodes async function fetchNodesAndCreate (array) { for (const field of array) { const nodes = await fetchNodes(field); - nodes.forEach(n=>createNode(processNode(createNodeId, n, field))); + const fieldName = normaliseFieldName(field); + nodes.forEach(n=>createNode(processNode(createNodeId, n, fieldName))); } } diff --git a/helpers/index.js b/helpers/index.js index 806cc5b..d43dcc4 100644 --- a/helpers/index.js +++ b/helpers/index.js @@ -10,6 +10,7 @@ const processNode = (createNodeId, node, fieldName) => { const nodeData = Object.assign({}, node, { id: nodeId, + wordpress_id: node['id'], parent: null, children: [], internal: { @@ -21,7 +22,19 @@ const processNode = (createNodeId, node, fieldName) => { return nodeData }; -module.exports = { processNode }; +// Turn multi part endpoints into camelCase +// e.g. products/categories becomes productsCategories +const normaliseFieldName = (name) => { + const parts = name.split('/'); + return parts.reduce((whole, partial) => { + if(whole === '') { + return whole.concat(partial); + } + return whole.concat(partial[0].toUpperCase() + partial.slice(1)); + }, '') +} + +module.exports = { processNode, normaliseFieldName }; // Helper Helpers function capitalize(s){ From d7489cf39e1bc5882fc31f327699415d014f6d61 Mon Sep 17 00:00:00 2001 From: pasdo501 Date: Sat, 9 Mar 2019 12:31:08 +1300 Subject: [PATCH 2/5] Make API version configurable - Allows the API version being used to be changed. - Defaults to 'wc/v1', the version currently being used. - If an incorrect version is entered in the config file, warnings will be logged for each field being fetched, but nothing specific about the API veresion will be pinpointed, so it's up to people to enter a valid version. - Updated README to reflect this change, as well as a short addition to the supported field types section. --- README.md | 7 +++++++ gatsby-node.js | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ba7d7d6..ff026c4 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ plugins:[ consumer_key: , consumer_secret: , }, + // Version of the woocommerce API to use + // OPTIONAL: defaults to 'wc/v1' + api_version: ['wc/v3'] // Array of strings with fields you'd like to create nodes for... fields: ['products'] } @@ -36,3 +39,7 @@ plugins:[ - Orders - Reports - Coupons + +**Note**: If following the endpoint layout from the [WooCommerce REST API docs](https://woocommerce.github.io/woocommerce-rest-api-docs/?php#introduction), all fields that do not contain a wildcard *should* be supported. + +For example, to get product categories: including 'products/categories' in fields will show up as allWcProductsCategories / wcProductsCategories \ No newline at end of file diff --git a/gatsby-node.js b/gatsby-node.js index b9073b5..ebc2894 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -8,7 +8,7 @@ exports.sourceNodes = async ( const { createNode } = boundActionCreators; delete configOptions.plugins; - const { api, https, api_keys, fields } = configOptions; + const { api, https, api_keys, fields, api_version } = configOptions; // set up WooCommerce node api tool const WooCommerce = new WooCommerceAPI({ @@ -16,7 +16,7 @@ exports.sourceNodes = async ( consumerKey: api_keys.consumer_key, consumerSecret: api_keys.consumer_secret, wpAPI: true, - version: 'wc/v1' + version: api_version || 'wc/v1' }); // Fetch Node and turn our response to JSON From 744775c3fa8dced3a67770af6bfc6dd919eaabbc Mon Sep 17 00:00:00 2001 From: pasdo501 Date: Sat, 9 Mar 2019 15:17:01 +1300 Subject: [PATCH 3/5] Add per_page config option - Allows the number of items to retrieve from the API request to be configurable (defaults to 10 otherwise) - Updated README; also moved optional config options to the end --- README.md | 8 +++++--- gatsby-node.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ff026c4..9168c2a 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,13 @@ plugins:[ consumer_key: , consumer_secret: , }, + // Array of strings with fields you'd like to create nodes for... + fields: ['products'], // Version of the woocommerce API to use // OPTIONAL: defaults to 'wc/v1' - api_version: ['wc/v3'] - // Array of strings with fields you'd like to create nodes for... - fields: ['products'] + api_version: ['wc/v3'], + // OPTIONAL: How many results to retrieve + per_page: 100 } } ] diff --git a/gatsby-node.js b/gatsby-node.js index ebc2894..cf8dc68 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -8,7 +8,7 @@ exports.sourceNodes = async ( const { createNode } = boundActionCreators; delete configOptions.plugins; - const { api, https, api_keys, fields, api_version } = configOptions; + const { api, https, api_keys, fields, api_version, per_page } = configOptions; // set up WooCommerce node api tool const WooCommerce = new WooCommerceAPI({ @@ -21,11 +21,16 @@ exports.sourceNodes = async ( // Fetch Node and turn our response to JSON const fetchNodes = async (fieldName) => { - const res = await WooCommerce.getAsync(fieldName); + const endpoint = per_page + ? fieldName + `?per_page=${per_page}` + : fieldName; + + const res = await WooCommerce.getAsync(endpoint); const json = res.toJSON(); if(json.statusCode !== 200) { console.warn(` - \n========== WARNING FOR FIELD ${fieldName} ==========\n`); + \n========== WARNING FOR FIELD ${fieldName} ==========\n` + ); console.warn(`The following error message was produced: ${json.body}`); console.warn(`\n========== END WARNING ==========\n`); return []; From d4704ae0a600ff2f5c03109d336cb9a3874d14cd Mon Sep 17 00:00:00 2001 From: Travis Reynolds Date: Thu, 4 Apr 2019 12:10:34 +0100 Subject: [PATCH 4/5] Remove array from api_version --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9168c2a..963bf39 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ plugins:[ fields: ['products'], // Version of the woocommerce API to use // OPTIONAL: defaults to 'wc/v1' - api_version: ['wc/v3'], + api_version: 'wc/v3', // OPTIONAL: How many results to retrieve per_page: 100 } @@ -44,4 +44,4 @@ plugins:[ **Note**: If following the endpoint layout from the [WooCommerce REST API docs](https://woocommerce.github.io/woocommerce-rest-api-docs/?php#introduction), all fields that do not contain a wildcard *should* be supported. -For example, to get product categories: including 'products/categories' in fields will show up as allWcProductsCategories / wcProductsCategories \ No newline at end of file +For example, to get product categories: including 'products/categories' in fields will show up as allWcProductsCategories / wcProductsCategories From 42cb1e9a4939c2ccd89bb032f5ccc4f4a79fdb95 Mon Sep 17 00:00:00 2001 From: Travis Reynolds Date: Thu, 4 Apr 2019 12:11:46 +0100 Subject: [PATCH 5/5] Specify default option in object spread. --- gatsby-node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index cf8dc68..803c148 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -8,7 +8,7 @@ exports.sourceNodes = async ( const { createNode } = boundActionCreators; delete configOptions.plugins; - const { api, https, api_keys, fields, api_version, per_page } = configOptions; + const { api, https, api_keys, fields, api_version = 'wc/v1', per_page } = configOptions; // set up WooCommerce node api tool const WooCommerce = new WooCommerceAPI({ @@ -16,7 +16,7 @@ exports.sourceNodes = async ( consumerKey: api_keys.consumer_key, consumerSecret: api_keys.consumer_secret, wpAPI: true, - version: api_version || 'wc/v1' + version: api_version }); // Fetch Node and turn our response to JSON