diff --git a/.babelrc b/.babelrc index 36ef793..1a2920f 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,4 @@ { + "plugins": ["lodash"], "presets": ["es2015", "stage-1"] } diff --git a/.gitignore b/.gitignore index 69efb3d..0f8a87f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /node_modules /.idea +/.vscode /lib *.log diff --git a/index.html b/index.html index 49d64f0..de55f8b 100644 --- a/index.html +++ b/index.html @@ -228,6 +228,11 @@

Properties

Orders Orders instance + + analytics + Analytics + Analytics instance + @@ -1461,6 +1466,103 @@

UpdateAddressPayload

+ + + +

Analytics

Accessible via analytics property of FoxApi instance.

+ +
+

Methods

+ +
+ + + +
+ + + + + + + + + + + + + +
MethodReturnsDescription
trackEvent(<Object<SphexTracker>> tracker, <Function> saltFunction?)PromiseEnables the tracking of UI specific events. The optional salt function must +be a random number generator.
+ +
+ +
+ + +
+

SphexTracker

+ +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FieldTypeRequiredExampleDescription
channelNumber +
Unique Channel ID number
subjectNumber +
Unique Subject ID number
objString +
Name of the object type you are tracking the event for
verbString +
A single word that describes the event
objIdNumber +
Unique number that can trace the object being tracked
countNumber +
+ +
@@ -1503,6 +1605,12 @@

Methods

Promse Removes JWT cookie. + + restorePassword(<String> email) + Promise + requests email instructions to reset password +creates new password + @@ -1687,11 +1795,15 @@

Methods

Adds a gift card as payment method for the cart.

Creates payment method with a given amount using store credit.

- - removeGiftCards() + + removeGiftCard(<string> giftCardCode) + Promise<FullOrder> + Removes gift card with provided code payment method from the cart. + + + removeStoreCredits() Promise<FullOrder> - Removes all gift cards payment methods of the cart. -

Removes all store credits payment methods of the cart.

+ Removes all store credits payment methods of the cart. addCoupon(<Number> code) diff --git a/package.json b/package.json index 8e13399..483d572 100644 --- a/package.json +++ b/package.json @@ -28,12 +28,14 @@ "homepage": "https://github.com/FoxComm/api-js#readme", "dependencies": { "debug": "^2.2.0", + "jwt-decode": "^2.0.1", + "lodash": "^4.17.4", "postinstall-build": "^0.2.1", - "superagent": "^3.2.1", - "jwt-decode": "^2.0.1" + "superagent": "^3.2.1" }, "devDependencies": { "babel-cli": "^6.9.0", + "babel-plugin-lodash": "^3.2.11", "babel-preset-es2015": "^6.9.0", "babel-preset-stage-1": "^6.5.0", "escape-html": "^1.0.3", diff --git a/src/api/analytics.js b/src/api/analytics.js new file mode 100644 index 0000000..2f2a0ab --- /dev/null +++ b/src/api/analytics.js @@ -0,0 +1,61 @@ +// @class Analytics +// Accessible via [analytics](#foxapi-analytics) property of [FoxApi](#foxapi) instance. + +import * as endpoints from '../endpoints'; +import querystring from 'querystring'; + +function randomSalt(maxRand = 100000) { + return Math.floor(Math.random() * maxRand); +} + +// Add a count argument +function sphexTrackerUrl(tracker, saltFunction = randomSalt) { + const { channel, subject, verb, obj, objId, count = 1 } = tracker; + + const queryParams = { + ch: channel, + sub: subject, + v: verb, + ob: obj, + id: objId, + c: count, + slt: saltFunction(), + }; + const query = querystring.stringify(queryParams); + + return `${endpoints.hal}?${query}`; +} + +export default class Analytics { + constructor(api) { + this.api = api; + } + + /** + * @method trackEvent(tracker: Object, saltFunction?: Function): Promise + * Enables the tracking of UI specific events. The optional salt function must + * be a random number generator. + */ + trackEvent(tracker, saltFunction = randomSalt) { + return this.api.get(sphexTrackerUrl(tracker, saltFunction)); + } +} + +// @miniclass SphexTracker (Analytics) +// @field channel: Number +// Unique Channel ID number +// +// @field subject: Number +// Unique Subject ID number +// +// @field obj: String +// Name of the object type you are tracking the event for +// +// @field verb: String +// A single word that describes the event +// +// @field objId: Number +// Unique number that can trace the object being tracked +// +// @field count: Number +// Quantity \ No newline at end of file diff --git a/src/api/cart.js b/src/api/cart.js index 3109865..a56ee66 100644 --- a/src/api/cart.js +++ b/src/api/cart.js @@ -187,10 +187,10 @@ export default class Cart { } /** - * @method removeGiftCards(): Promise + * @method removeStoreCredits(): Promise * Removes all store credits payment methods of the cart. */ - removeStoreCrdits() { + removeStoreCredits() { return this.api.delete(endpoints.cartPaymentStoreCredits).then(normalizeResponse); } diff --git a/src/api/credit-cards.js b/src/api/credit-cards.js index 6076e1c..3fe48f0 100644 --- a/src/api/credit-cards.js +++ b/src/api/credit-cards.js @@ -36,13 +36,21 @@ export default class CreditCards { if (response.error) { reject([response.error.message]); } else { - var payload = creditCardFromStripePayload(response, billingAddress, addressIsNew); + return createCardFromStripeToken(response, billingAddress, addressIsNew) + .then(response => resolve(response)) + .catch(err => reject(err)); + } + }); + }); + } + + createCardFromStripeToken(token, billingAddress, addressIsNew) { + return new Promise((resolve, reject) => { + var payload = creditCardFromStripePayload(token, billingAddress, addressIsNew); return this.api.post(endpoints.creditCards, payload) .then(response => resolve(response)) .catch(err => !!err.responseJson.errors ? reject(err.responseJson.errors) : reject([err.message])); - } - }); }); } diff --git a/src/endpoints.js b/src/endpoints.js index 4bfa486..5d35941 100644 --- a/src/endpoints.js +++ b/src/endpoints.js @@ -54,3 +54,6 @@ export const changePassword = '/v1/my/account/change-password'; // orders endpoints export const orders = '/v1/my/orders'; export const order = referenceNumber => `/v1/my/orders/${referenceNumber}`; + +// analytics endpoints +export const hal = '/v1/hal'; diff --git a/src/index.js b/src/index.js index 317c817..89bebe0 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,7 @@ import Cart from './api/cart'; import Account from './api/account'; import Orders from './api/orders'; import jwtDecode from 'jwt-decode'; +import Analytics from './api/analytics'; export default class Api { constructor(args) { @@ -62,6 +63,10 @@ export default class Api { // @property orders: Orders // Orders instance this.orders = new Orders(this); + + // @property analytics: Analytics + // Analytics instance + this.analytics = new Analytics(this); } // @method addAuth(jwt: String): FoxApi diff --git a/yarn.lock b/yarn.lock index 95b232e..e05b9ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -285,6 +285,13 @@ babel-plugin-check-es2015-constants@^6.3.13: dependencies: babel-runtime "^6.0.0" +babel-plugin-lodash@^3.2.11: + version "3.2.11" + resolved "https://registry.yarnpkg.com/babel-plugin-lodash/-/babel-plugin-lodash-3.2.11.tgz#21c8fdec9fe1835efaa737873e3902bdd66d5701" + dependencies: + glob "^7.1.1" + lodash "^4.17.2" + babel-plugin-syntax-async-functions@^6.8.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95" @@ -968,8 +975,8 @@ form-data@^2.1.1, form-data@~2.1.1: mime-types "^2.1.12" formidable@^1.0.17: - version "1.0.17" - resolved "https://registry.yarnpkg.com/formidable/-/formidable-1.0.17.tgz#ef5491490f9433b705faa77249c99029ae348559" + version "1.1.1" + resolved "https://registry.yarnpkg.com/formidable/-/formidable-1.1.1.tgz#96b8886f7c3c3508b932d6bd70c4d3a88f35f1a9" fs-readdir-recursive@^1.0.0: version "1.0.0" @@ -1063,7 +1070,7 @@ glob@^5.0.5: once "^1.3.0" path-is-absolute "^1.0.0" -glob@^7.0.5: +glob@^7.0.5, glob@^7.1.1: version "7.1.1" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8" dependencies: @@ -1338,7 +1345,7 @@ kind-of@^3.0.2: unicode-7.0.0 "^0.1.5" xregexp "^2.0.0" -lodash@^4.2.0: +lodash@^4.17.2, lodash@^4.17.4, lodash@^4.2.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" @@ -1847,8 +1854,8 @@ strip-json-comments@~1.0.4: resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-1.0.4.tgz#1e15fbcac97d3ee99bf2d73b4c656b082bbafb91" superagent@^3.2.1: - version "3.3.2" - resolved "https://registry.yarnpkg.com/superagent/-/superagent-3.3.2.tgz#23b4ed895911215a1f90b44d16638b6492f9c114" + version "3.4.0" + resolved "https://registry.yarnpkg.com/superagent/-/superagent-3.4.0.tgz#cf8e40b303d5b2949c0fb036ddc30927e14958c7" dependencies: component-emitter "^1.2.0" cookiejar "^2.0.6"