diff --git a/README.md b/README.md index 967e7dc..4bea281 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ Official JavaScript library for the Command API. -[Read the Documentation](https://portal.oncommand.io/docs/command-js/0.41.0/introduction) \ No newline at end of file +[Read the Documentation](http://portal.oncommand.io/docs/command-js/0.43.0/introduction) diff --git a/dist/index.min.js b/dist/index.min.js index ea8fbdb..1d08af6 100644 --- a/dist/index.min.js +++ b/dist/index.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).command=e()}(this,(function(){"use strict";const t=require("axios");return class{constructor(t,e={}){t||this._throwFormattedError("A valid API key is required."),this.apiKey=t,this.version="0.41.0",this.customerId=e.customerId||null,this.debug=e.debug||!1,this.customers={login:this._loginCustomer.bind(this),logout:this._logoutCustomer.bind(this),create:this._createCustomer.bind(this),update:this._updateCustomer.bind(this),delete:this._deleteCustomer.bind(this)}}_logDebugMessage(t){console.log("[[[ Command.js DEBUG ]]]"),console.log(t)}_throwFormattedError(t){throw new Error(`[Command] ${t} See https://portal.oncommand.io/docs/command-js/${this.version}/introduction.`)}_request(e,s,r={},o=null){return this.debug&&this._logDebugMessage({method:e,url:`https://api.oncommand.io/v1${s}`,headers:{"x-api-key":this.apiKey},data:r}),t({method:e,url:`https://api.oncommand.io/v1${s}`,headers:{"x-api-key":this.apiKey},data:r}).then(t=>(callbak&&o(t),t)).catch(t=>{if(t&&t.response){const{status:e}=t.response,s=t.response&&t.response.data&&t.response.data&&t.response.data.data&&t.response.data.data.error;console.warn(`[${e}] ${s}`),t.response.data&&console.warn(t.response.data),this.debug&&t.response.data&&t.response.data.data&&(this._logDebugMessage(t.response.data.data.error),this._logDebugMessage(t.response.data.data.validationErrors))}})}track(t,e){if(!t)throw new Error("Must pass a key to track.");const s={key:t};return this.customerId&&(s.customerId=this.customerId),e&&(s.properties=e),this._request("post","/behavior",s)}_loginCustomer(t){if(!t)throw new Error("Must pass a customerId.");return this.customerId=t,this._request("put","/customers/login",{customerId:t})}_logoutCustomer(t){if(!t&&!this.customerId)throw new Error("Must have a customerId to logout.");return this._request("put","/customers/logout",{customerId:t||this.customerId},()=>{this.customerId=null})}_createCustomer(t){if(!t)throw new Error("Must pass a customer.");return this._request("post","/customers",{...t})}_updateCustomer(t,e){if(!t)throw new Error("Must pass a customerId.");if(!e)throw new Error("Must pass an update for the customer.");return this._request("put",`/customers/${t}`,{...e})}_deleteCustomer(t){if(!t)throw new Error("Must pass a customerId.");return this._request("delete",`/customers/${t}`)}}})); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).command=e()}(this,(function(){"use strict";const t=require("axios");return class{constructor(t,e={}){t||this._throwFormattedError("A valid API key is required."),this.apiKey=t,this.version="0.43.0",this.customerId=e.customerId||null,this.debug=e.debug||!1,this.customers={login:this._loginCustomer.bind(this),logout:this._logoutCustomer.bind(this),create:this._createCustomer.bind(this),update:this._updateCustomer.bind(this),delete:this._deleteCustomer.bind(this)}}_logDebugMessage(t){console.log("[[[ Command.js DEBUG ]]]"),console.log(t)}_throwFormattedError(t){throw new Error(`[Command] ${t} See https://portal.oncommand.io/docs/command-js/${this.version}/introduction.`)}_request(e,s,r={}){return this.debug&&this._logDebugMessage({method:e,url:`https://api.oncommand.io/v1${s}`,headers:{"x-api-key":this.apiKey},data:r}),t({method:e,url:`https://api.oncommand.io/v1${s}`,headers:{"x-api-key":this.apiKey},data:r}).then(t=>t&&t.data&&t.data.data).catch(t=>{if(t&&t.response){const{status:e}=t.response,s=t.response&&t.response.data&&t.response.data&&t.response.data.data&&t.response.data.data.error;console.warn(`[${e}] ${s}`),t.response.data&&console.warn(t.response.data),this.debug&&t.response.data&&t.response.data.data&&(this._logDebugMessage(t.response.data.data.error),this._logDebugMessage(t.response.data.data.validationErrors))}})}track(t,e){if(!t)throw new Error("Must pass a key to track.");const s={key:t};return this.customerId&&(s.customerId=this.customerId),e&&(s.properties=e),this._request("post","/behavior",s)}_loginCustomer(t){if(!t)throw new Error("Must pass a customerId.");return this.customerId=t,this._request("put","/customers/login",{customerId:t})}_logoutCustomer(t){if(!t&&!this.customerId)throw new Error("Must have a customerId to logout.");return this._request("put","/customers/logout",{customerId:t||this.customerId},()=>{this.customerId=null})}_createCustomer(t){if(!t)throw new Error("Must pass a customer.");return this._request("post","/customers",{...t})}_updateCustomer(t,e){if(!t)throw new Error("Must pass a customerId.");if(!e)throw new Error("Must pass an update for the customer.");return this._request("put",`/customers/${t}`,{...e})}_deleteCustomer(t){if(!t)throw new Error("Must pass a customerId.");return this._request("delete",`/customers/${t}`)}}})); diff --git a/package-lock.json b/package-lock.json index 1c42b6a..f133876 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@oncommandio/js", - "version": "0.41.0", + "version": "0.43.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6a6c766..818550e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oncommandio/js", - "version": "0.41.0", + "version": "0.43.0", "description": "Official JavaScript library for the Command API.", "main": "dist/index.min.js", "scripts": { @@ -21,7 +21,7 @@ ], "author": "oncommandio", "license": "MIT", - "homepage": "https://portal.oncommand.io/docs/command-js/0.41.0/introduction", + "homepage": "https://portal.oncommand.io/docs/command-js/0.43.0/introduction", "dependencies": { "axios": "^0.19.0" }, diff --git a/src/index.js b/src/index.js index 98af0ae..50c6659 100644 --- a/src/index.js +++ b/src/index.js @@ -28,7 +28,7 @@ class CommandAPI { ); } - _request(method, path, data = {}, callback = null) { + _request(method, path, data = {}) { if (this.debug) { this._logDebugMessage({ method, @@ -50,8 +50,7 @@ class CommandAPI { data }) .then(response => { - if (callbak) callback(response); - return response; + return response && response.data && response.data.data; }) .catch(error => { if (error && error.response) { diff --git a/src/index.test.js b/src/index.test.js index 2e2b865..45f695e 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -30,7 +30,7 @@ describe("index.js", () => { command.track("an event"); expect(axios).toHaveBeenCalledWith({ method: "post", - url: `http://localhost:4000/api/v1/behavior`, + url: `https://api.oncommand.io/v1/behavior`, headers: { "x-api-key": "apiKey123" }, @@ -52,7 +52,7 @@ describe("index.js", () => { command.customers.login("1234"); expect(axios).toHaveBeenCalledWith({ method: "put", - url: `http://localhost:4000/api/v1/customers/login`, + url: `https://api.oncommand.io/v1/customers/login`, headers: { "x-api-key": "apiKey123" }, @@ -75,7 +75,7 @@ describe("index.js", () => { command.customers.logout(); expect(axios).toHaveBeenCalledWith({ method: "put", - url: `http://localhost:4000/api/v1/customers/logout`, + url: `https://api.oncommand.io/v1/customers/logout`, headers: { "x-api-key": "apiKey123" }, @@ -97,7 +97,7 @@ describe("index.js", () => { command.customers.create({ emailAddress: "test@test.com" }); expect(axios).toHaveBeenCalledWith({ method: "post", - url: `http://localhost:4000/api/v1/customers`, + url: `https://api.oncommand.io/v1/customers`, headers: { "x-api-key": "apiKey123" }, @@ -121,7 +121,7 @@ describe("index.js", () => { }); expect(axios).toHaveBeenCalledWith({ method: "put", - url: `http://localhost:4000/api/v1/customers/customerId123`, + url: `https://api.oncommand.io/v1/customers/customerId123`, headers: { "x-api-key": "apiKey123" }, @@ -150,7 +150,7 @@ describe("index.js", () => { command.customers.delete("customerId123"); expect(axios).toHaveBeenCalledWith({ method: "delete", - url: `http://localhost:4000/api/v1/customers/customerId123`, + url: `https://api.oncommand.io/v1/customers/customerId123`, headers: { "x-api-key": "apiKey123" },