From 286d7ec509d532db489cf895b256a7ba5a5aca39 Mon Sep 17 00:00:00 2001 From: Ander Rodriguez Date: Mon, 26 Nov 2018 10:34:07 +0100 Subject: [PATCH 1/2] Allow cart token's cookie name to be passed in config parameters --- CHANGELOG.md | 2 + README.md | 1 + src/session.js | 122 +++++++++++++++++++++++++------------------------ 3 files changed, 66 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6572975..1656a86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ To get the diff between two versions, go to https://github.com/LIN3S/SyliusShopA * 0.7.7 * [FEATURE] Add locale params to checkout complete method +* 0.7.6 + * [FEATURE] Config will accept a new parameters `cartTokenCookie` that will be cart cookie's name if any * 0.7.6 * [FIX] Add error control to checkout complete method * 0.7.5 diff --git a/README.md b/README.md index 9a36e0d..47eaee4 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ const api = createSyliusApiClient({ locale: 'en_US', cartCookieExpiration: 604800000, userCookieExpiration: 2592000000, + cartTokenCookie: 'cart-token', }) ``` diff --git a/src/session.js b/src/session.js index 0242e52..d0614c9 100644 --- a/src/session.js +++ b/src/session.js @@ -27,68 +27,72 @@ export class CartDoesNotExist { message = 'Cart has not been initialized'; } -const session = config => ({ - Cart: { - id: () => { - const id = getCookie(CART_TOKEN_COOKIE); - - if (typeof id === 'undefined') { - throw new CartDoesNotExist(); - } - - return id; - }, - generateId: () => { - const newSessionId = uuid(); - const params = { - name: CART_TOKEN_COOKIE, - value: newSessionId, - expiration: config.cartCookieExpiration || 604800000 // 7 days - }; - - if (config.cookieDomain) { - params.domain = config.cookieDomain; - } - - setCookie(params); - - return newSessionId; - }, - remove: () => { - const params = {}; - - if (config.cookieDomain) { - params.domain = config.cookieDomain; +const session = config => { + const cartTokenCookie = config.cartTokenCookie || CART_TOKEN_COOKIE; + + return ({ + Cart: { + id: () => { + const id = getCookie(cartTokenCookie); + + if (typeof id === 'undefined') { + throw new CartDoesNotExist(); + } + + return id; + }, + generateId: () => { + const newSessionId = uuid(); + const params = { + name: cartTokenCookie, + value: newSessionId, + expiration: config.cartCookieExpiration || 604800000 // 7 days + }; + + if (config.cookieDomain) { + params.domain = config.cookieDomain; + } + + setCookie(params); + + return newSessionId; + }, + remove: () => { + const params = {}; + + if (config.cookieDomain) { + params.domain = config.cookieDomain; + } + + return removeCookie(cartTokenCookie, params); } - - return removeCookie(CART_TOKEN_COOKIE, params); - } - }, - User: { - token: () => getCookie(USER_TOKEN_COOKIE), - set: (token) => { - const params = { - name: USER_TOKEN_COOKIE, - value: token, - expiration: config.userCookieExpiration || 2592000000 // 1 month - }; - - if (config.cookieDomain) { - params.domain = config.cookieDomain; - } - - setCookie(params); }, - remove: () => { - const params = {}; - - if (config.cookieDomain) { - params.domain = config.cookieDomain; + User: { + token: () => getCookie(USER_TOKEN_COOKIE), + set: (token) => { + const params = { + name: USER_TOKEN_COOKIE, + value: token, + expiration: config.userCookieExpiration || 2592000000 // 1 month + }; + + if (config.cookieDomain) { + params.domain = config.cookieDomain; + } + + setCookie(params); + }, + remove: () => { + const params = {}; + + if (config.cookieDomain) { + params.domain = config.cookieDomain; + } + + return removeCookie(USER_TOKEN_COOKIE, params); } - - return removeCookie(USER_TOKEN_COOKIE, params); } - } -}); + }); +}; export default session; From 301592efadff509c770fe78b19043b8fc1384b23 Mon Sep 17 00:00:00 2001 From: Ander Rodriguez Date: Mon, 26 Nov 2018 10:41:11 +0100 Subject: [PATCH 2/2] Minor version change, new feature --- CHANGELOG.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1656a86..d84f645 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,10 @@ This changelog references the relevant changes done between versions. To get the diff for a specific change, go to https://github.com/LIN3S/SyliusShopApiClient/commit/XXX where XXX is the change hash To get the diff between two versions, go to https://github.com/LIN3S/SyliusShopApiClient/compare/v0.2.0...v0.3.0 +* 0.8.0 + * [FEATURE] Config will accept a new parameters `cartTokenCookie` that will be cart cookie's name if any * 0.7.7 * [FEATURE] Add locale params to checkout complete method -* 0.7.6 - * [FEATURE] Config will accept a new parameters `cartTokenCookie` that will be cart cookie's name if any * 0.7.6 * [FIX] Add error control to checkout complete method * 0.7.5 diff --git a/package.json b/package.json index df0318c..c059417 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lin3s-sylius-shop-api-client", - "version": "0.7.7", + "version": "0.8.0", "author": "LIN3S", "license": "MIT", "description": "JavaScript client on top of SyliusShopApiPlugin to build integrations with ease.",