diff --git a/CHANGELOG.md b/CHANGELOG.md index cd3eea1..e23b4ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v0.1.5 (October 16, 2018) +- Add `overrideHttpPut` option for handling environments not allowing http put. + ## v0.1.4 (June 21, 2018) - Fix superagent `attachCookies` and `saveCookies` usage diff --git a/README.md b/README.md index 1a84831..0f28ac1 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 be 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/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/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", diff --git a/src/ApiClient.js b/src/ApiClient.js index 0ec6ab7..30bc554 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -26,10 +26,11 @@ import Fault from './models/Fault' const defaultConfig = { basePath: 'https://localhost/s/siteId/dw/shop/v17_8', - defaultHeaders: {}, - timeout: 60000, cache: true, + defaultHeaders: {}, enableCookies: false, + overrideHttpPut: true, + timeout: 60000, } /** @@ -50,7 +51,8 @@ export default class ApiClient { enableCookies, clientUsername, clientPassword, - oauth2AccessToken + oauth2AccessToken, + overrideHttpPut } = Object.assign(defaultConfig, config) // verify the required parameter 'basepath' is set @@ -94,6 +96,16 @@ export default class ApiClient { customers_auth.password = clientPassword } + /** + * 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 true + */ + this.overrideHttpPut = overrideHttpPut + /** * The default HTTP headers to be included for all API calls. * @type {Array.} @@ -420,6 +432,12 @@ export default class ApiClient { queryParams._ = new Date().getTime() } + // emulate PUT method because they are not allowed on staging and production environments + if (this.overrideHttpPut && httpMethod.toUpperCase() === 'PUT') { + httpMethod = 'POST' + headerParams = Object.assign(headerParams || {}, {'x-dw-http-method-override': 'PUT'}) + } + request.query(this.normalizeParams(queryParams)) // set header parameters