Skip to content

Commit

Permalink
Merge pull request #33 from LIN3S/feature/cart-token-by-parameters
Browse files Browse the repository at this point in the history
Allow cart token's cookie name to be passed in config parameters
  • Loading branch information
gorkalaucirica authored Nov 26, 2018
2 parents 47a8f42 + 301592e commit 0b60e0f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const api = createSyliusApiClient({
locale: 'en_US',
cartCookieExpiration: 604800000,
userCookieExpiration: 2592000000,
cartTokenCookie: 'cart-token',
})
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
122 changes: 63 additions & 59 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 0b60e0f

Please sign in to comment.