From 4290372093733e827f16d2d1671f2e138c052605 Mon Sep 17 00:00:00 2001 From: Ben Chypak Date: Mon, 15 Oct 2018 16:59:38 -0700 Subject: [PATCH 1/8] Add new option to only use put if option is set --- CHANGELOG.md | 3 +++ package-lock.json | 2 +- src/ApiClient.js | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd3eea1..f3b3798 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## To be released +- Add `allowHttpPut` option for handling environments not allowing http put. + ## v0.1.4 (June 21, 2018) - Fix superagent `attachCookies` and `saveCookies` usage diff --git a/package-lock.json b/package-lock.json index dc01e61..5413242 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "commercecloud-ocapi-client", - "version": "0.1.1", + "version": "0.1.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/ApiClient.js b/src/ApiClient.js index 0ec6ab7..7b23b70 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -25,6 +25,7 @@ import Fault from './models/Fault' */ const defaultConfig = { + allowHttpPut: false, basePath: 'https://localhost/s/siteId/dw/shop/v17_8', defaultHeaders: {}, timeout: 60000, @@ -43,6 +44,7 @@ export default class ApiClient { constructor(config = defaultConfig) { const { + allowHttpPut, basePath, defaultHeaders, timeout, @@ -94,6 +96,17 @@ export default class ApiClient { customers_auth.password = clientPassword } + /** + * If set to false endpoints normally sent using the HTTP method `PUT` will + * be sent using `POST`, with an aditional header (x-dw-http-method-override) + * set to `PUT`. + * Please refer to the following Salesforce documentation {@link https://documentation.demandware.com/DOC1/topic/com.demandware.dochelp/OCAPI/18.8/usage/HttpMethods.html} + * for more information. + * @type {Boolean} + * @default false + */ + this.allowHttpPut = allowHttpPut + /** * The default HTTP headers to be included for all API calls. * @type {Array.} @@ -420,6 +433,12 @@ export default class ApiClient { queryParams._ = new Date().getTime() } + // emulate PUT method is they are not allowed + if (!this.allowHttpPut && httpMethod.toUpperCase() === 'PUT') { + httpMethod = 'POST' + headerParams = Object.assign(headerParams || {}, {'x-dw-http-method-override': 'PUT'}) + } + request.query(this.normalizeParams(queryParams)) // set header parameters From 7922c17f4a2f64dcf42df51f3deb2ae77324fff6 Mon Sep 17 00:00:00 2001 From: Ben Chypak Date: Tue, 16 Oct 2018 09:08:01 -0700 Subject: [PATCH 2/8] Reword comments --- src/ApiClient.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 7b23b70..000421f 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -97,9 +97,8 @@ export default class ApiClient { } /** - * If set to false endpoints normally sent using the HTTP method `PUT` will - * be sent using `POST`, with an aditional header (x-dw-http-method-override) - * set to `PUT`. + * If set to false endpoints that normally use HTTP `PUT` will + * be sent using `POST`, with an aditional header (x-dw-http-method-override: `PUT`). * Please refer to the following Salesforce documentation {@link https://documentation.demandware.com/DOC1/topic/com.demandware.dochelp/OCAPI/18.8/usage/HttpMethods.html} * for more information. * @type {Boolean} @@ -433,7 +432,7 @@ export default class ApiClient { queryParams._ = new Date().getTime() } - // emulate PUT method is they are not allowed + // emulate PUT method because they are not allowed on staging and production environments if (!this.allowHttpPut && httpMethod.toUpperCase() === 'PUT') { httpMethod = 'POST' headerParams = Object.assign(headerParams || {}, {'x-dw-http-method-override': 'PUT'}) From bbe366dacecb9db21b7b904f7eeadbe89292302e Mon Sep 17 00:00:00 2001 From: Ben Chypak Date: Tue, 16 Oct 2018 10:35:10 -0700 Subject: [PATCH 3/8] Update readme. Change option nomenclature. This involved inverting the default value and conditional logic --- README.md | 5 +++-- src/ApiClient.js | 18 +++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 1a84831..0649e0f 100644 --- a/README.md +++ b/README.md @@ -83,12 +83,13 @@ const config = { timeout: 60000, // Request timeout in milliseconds cache: true, // If set to false an additional timestamp parameter is added to all API GET calls to prevent browser caching enableCookies: false, //If set to true, the client will save the cookies from each server response, and return them in the next request. + overrideHttpPut: true // If set to true, any methods specified as using http PUT will best sent using POST along the header value 'x-dw-http-method-override' set to 'PUT'. } ShopApi.ApiClient.instance = new ShopApi.ApiClient(config) ``` - + ### 🔐 Authorization @@ -121,7 +122,7 @@ ShopApi.ApiClient.instance = new ShopApi.ApiClient(config) Because Salesforce OCAPI is not publicly available, you need to have a running instance that you can test against. In the test folder, there is a file `config.json` that has the example configuration for your environment. Simply update the file with your instance information -Example: +Example: ```json { "clientId": "5640cc6b-f5e9-466e-9134-9853e9f9db93", diff --git a/src/ApiClient.js b/src/ApiClient.js index 000421f..1fe2dde 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -25,12 +25,12 @@ import Fault from './models/Fault' */ const defaultConfig = { - allowHttpPut: false, basePath: 'https://localhost/s/siteId/dw/shop/v17_8', - defaultHeaders: {}, - timeout: 60000, cache: true, + defaultHeaders: {}, enableCookies: false, + overrideHttpPut: true, + timeout: 60000, } /** @@ -44,7 +44,6 @@ export default class ApiClient { constructor(config = defaultConfig) { const { - allowHttpPut, basePath, defaultHeaders, timeout, @@ -52,7 +51,8 @@ export default class ApiClient { enableCookies, clientUsername, clientPassword, - oauth2AccessToken + oauth2AccessToken, + overrideHttpPut } = Object.assign(defaultConfig, config) // verify the required parameter 'basepath' is set @@ -97,14 +97,14 @@ export default class ApiClient { } /** - * If set to false endpoints that normally use HTTP `PUT` will - * be sent using `POST`, with an aditional header (x-dw-http-method-override: `PUT`). + * If set to true, endpoints that normally use HTTP `PUT` will + * be sent using `POST` with an aditional header (x-dw-http-method-override: `PUT`). * Please refer to the following Salesforce documentation {@link https://documentation.demandware.com/DOC1/topic/com.demandware.dochelp/OCAPI/18.8/usage/HttpMethods.html} * for more information. * @type {Boolean} * @default false */ - this.allowHttpPut = allowHttpPut + this.overrideHttpPut = overrideHttpPut /** * The default HTTP headers to be included for all API calls. @@ -433,7 +433,7 @@ export default class ApiClient { } // emulate PUT method because they are not allowed on staging and production environments - if (!this.allowHttpPut && httpMethod.toUpperCase() === 'PUT') { + if (this.overrideHttpPut && httpMethod.toUpperCase() === 'PUT') { httpMethod = 'POST' headerParams = Object.assign(headerParams || {}, {'x-dw-http-method-override': 'PUT'}) } From 84225c8638a26e429677b4e50e8f1d8708b1261e Mon Sep 17 00:00:00 2001 From: Ben Chypak Date: Tue, 16 Oct 2018 10:37:20 -0700 Subject: [PATCH 4/8] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3b3798..a7d8673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## To be released -- Add `allowHttpPut` option for handling environments not allowing http put. +- Add `overrideHttpPut` option for handling environments not allowing http put. ## v0.1.4 (June 21, 2018) - Fix superagent `attachCookies` and `saveCookies` usage From 9f56b917c145eab54734a811640b2ab9eed837f9 Mon Sep 17 00:00:00 2001 From: Ben Chypak Date: Tue, 16 Oct 2018 10:38:02 -0700 Subject: [PATCH 5/8] Update function comments --- src/ApiClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 1fe2dde..30bc554 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -102,7 +102,7 @@ export default class ApiClient { * Please refer to the following Salesforce documentation {@link https://documentation.demandware.com/DOC1/topic/com.demandware.dochelp/OCAPI/18.8/usage/HttpMethods.html} * for more information. * @type {Boolean} - * @default false + * @default true */ this.overrideHttpPut = overrideHttpPut From 3dc7cf3ee4e81910d5ab67d76ce12126498b9f66 Mon Sep 17 00:00:00 2001 From: Ben Chypak Date: Tue, 16 Oct 2018 10:45:10 -0700 Subject: [PATCH 6/8] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0649e0f..0f28ac1 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ const config = { timeout: 60000, // Request timeout in milliseconds cache: true, // If set to false an additional timestamp parameter is added to all API GET calls to prevent browser caching enableCookies: false, //If set to true, the client will save the cookies from each server response, and return them in the next request. - overrideHttpPut: true // If set to true, any methods specified as using http PUT will best sent using POST along the header value 'x-dw-http-method-override' set to 'PUT'. + overrideHttpPut: true // If set to true, any methods specified as using http PUT will be sent using POST along the header value 'x-dw-http-method-override' set to 'PUT'. } ShopApi.ApiClient.instance = new ShopApi.ApiClient(config) From e430425d138f69d9ac4054e21d70e17be3fdd8de Mon Sep 17 00:00:00 2001 From: John Boxall Date: Tue, 16 Oct 2018 10:54:47 -0700 Subject: [PATCH 7/8] Updating package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1a85703..10382ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commercecloud-ocapi-client", - "version": "0.1.4", + "version": "0.1.5", "description": "An ES6 JavaScript Client for Salesforce Open Commerce API", "license": "SEE LICENSE IN LICENSE", "main": "dist/commercecloud-ocapi-client.cjs.js", From e88ee4b668d4491db1920b2546b4d0a281dc45ce Mon Sep 17 00:00:00 2001 From: John Boxall Date: Tue, 16 Oct 2018 10:54:50 -0700 Subject: [PATCH 8/8] Updating CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7d8673..e23b4ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## To be released +## v0.1.5 (October 16, 2018) - Add `overrideHttpPut` option for handling environments not allowing http put. ## v0.1.4 (June 21, 2018)