From 7bb7f21a1e2b7190c66a3f33575b04ecaefc6657 Mon Sep 17 00:00:00 2001 From: KaKa Date: Sat, 2 Nov 2019 00:36:26 +0800 Subject: [PATCH 1/3] Set "User-Agent" in Node only --- index.mjs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/index.mjs b/index.mjs index 5513eee..e42f229 100644 --- a/index.mjs +++ b/index.mjs @@ -187,16 +187,26 @@ export default class WooCommerceRestApi { _request(method, endpoint, data, params = {}) { const url = this._getUrl(endpoint, params); + const headers = { + Accept: "application/json" + }; + // only set "User-Agent" in node environment + // the checking method is identical to upstream axios + if ( + typeof process !== "undefined" && + Object.prototype.toString.call(process) === "[object process]" + ) { + headers["User-Agent"] = + "WooCommerce REST API - JS Client/" + this.classVersion; + } + let options = { url: url, method: method, responseEncoding: this.encoding, timeout: this.timeout, responseType: "json", - headers: { - "User-Agent": "WooCommerce REST API - JS Client/" + this.classVersion, - Accept: "application/json" - } + headers }; if (this.isHttps) { From 1a1cc3e5d49cb50ef428ab650c5594b625f53a1e Mon Sep 17 00:00:00 2001 From: KaKa Date: Sat, 2 Nov 2019 00:51:43 +0800 Subject: [PATCH 2/3] update checking method As axios require "process" in the package but woocommerce-rest-api-js-lib do not. Change to use window element to check if it is node. --- index.mjs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/index.mjs b/index.mjs index e42f229..b18c331 100644 --- a/index.mjs +++ b/index.mjs @@ -191,11 +191,7 @@ export default class WooCommerceRestApi { Accept: "application/json" }; // only set "User-Agent" in node environment - // the checking method is identical to upstream axios - if ( - typeof process !== "undefined" && - Object.prototype.toString.call(process) === "[object process]" - ) { + if (typeof window === "undefined") { headers["User-Agent"] = "WooCommerce REST API - JS Client/" + this.classVersion; } From 1d42bc373d9d3b94d90fd1d5b78301aaf39eb2e4 Mon Sep 17 00:00:00 2001 From: KaKa Date: Sat, 2 Nov 2019 00:55:33 +0800 Subject: [PATCH 3/3] Revert "update checking method" This reverts commit 1a1cc3e5d49cb50ef428ab650c5594b625f53a1e. --- index.mjs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.mjs b/index.mjs index b18c331..e42f229 100644 --- a/index.mjs +++ b/index.mjs @@ -191,7 +191,11 @@ export default class WooCommerceRestApi { Accept: "application/json" }; // only set "User-Agent" in node environment - if (typeof window === "undefined") { + // the checking method is identical to upstream axios + if ( + typeof process !== "undefined" && + Object.prototype.toString.call(process) === "[object process]" + ) { headers["User-Agent"] = "WooCommerce REST API - JS Client/" + this.classVersion; }