Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to provide custom setCookie / getCookie methods #82

Closed
stafyniaksacha opened this issue Jan 17, 2023 · 2 comments · Fixed by #115
Closed

Allow to provide custom setCookie / getCookie methods #82

stafyniaksacha opened this issue Jan 17, 2023 · 2 comments · Fixed by #115

Comments

@stafyniaksacha
Copy link
Contributor

stafyniaksacha commented Jan 17, 2023

Hi there!

It would be great to have a way to provide our own setCookie / getCookie method, so we can customize domain, expiration, or handle SSR on our own.

Here would be the usage:

import swell from 'swell-js'

swell.init('storeId', 'publicKey', {
  // ...
  setCookie: (name: string, value: string) => {
    // ...
  },
  getCookie: (name: string) => {
    // ...
    return 'value'
  },
})

I've made it working using pnpm (with patch/patch-commit) and swell-js@3.18.3 with this patch:

diff --git a/dist/api-bb3bfec8.js b/dist/api-bb3bfec8.js
index f5c5925e8fb76d2e8232a97ae3507ad6ae467649..0acb726cefb7f9fe0c6ad742bc0a8fc2188f2e86 100644
--- a/dist/api-bb3bfec8.js
+++ b/dist/api-bb3bfec8.js
@@ -43,6 +43,8 @@ const api = {
     options.locale = opt.locale;
     options.currency = opt.currency;
     options.api = api;
+    options.getCookie = opt.getCookie || getCookie;
+    options.setCookie = opt.setCookie || setCookie;
     setOptions(options);
   },
 
@@ -108,9 +110,9 @@ async function request(
     ...opt,
   };
 
-  const session = allOptions.session || getCookie('swell-session');
-  const locale = allOptions.locale || getCookie('swell-locale');
-  const currency = allOptions.currency || getCookie('swell-currency');
+  const session = allOptions.session || allOptions.getCookie('swell-session');
+  const locale = allOptions.locale || allOptions.getCookie('swell-locale');
+  const currency = allOptions.currency || allOptions.getCookie('swell-currency');
 
   const baseUrl = `${allOptions.url}${allOptions.base || ''}/api`;
   const reqMethod = String(method).toLowerCase();
@@ -167,7 +169,7 @@ async function request(
   const responseSession = response.headers.get('X-Session');
 
   if (typeof responseSession === 'string' && session !== responseSession) {
-    setCookie('swell-session', responseSession);
+    allOptions.setCookie('swell-session', responseSession);
   }
 
   const result = await response.json();
diff --git a/dist/currency-85151e0d.js b/dist/currency-85151e0d.js
index a738ee61537a2c8c297479da8104b95714bc1e46..48b0006bf8afbc70bc3045fd093c6f7b46d36af7 100644
--- a/dist/currency-85151e0d.js
+++ b/dist/currency-85151e0d.js
@@ -2,7 +2,6 @@ import 'qs';
 import { f as find, g as get, r as round } from './index-bee7164f.js';
 import 'deepmerge';
 import 'fast-case';
-import { g as getCookie, s as setCookie } from './cookie-dff5d694.js';
 
 const FORMATTERS = {};
 
@@ -25,7 +24,7 @@ function methods(request, opt) {
     selected() {
       if (!this.code) {
         this.set(
-          getCookie('swell-currency') || opt.api.settings.get('store.currency'),
+          opt.getCookie('swell-currency') || opt.api.settings.get('store.currency'),
         );
       }
 
@@ -53,7 +52,7 @@ function methods(request, opt) {
         ),
       );
 
-      setCookie('swell-currency', code);
+      opt.setCookie('swell-currency', code);
 
       return this.state;
     },
diff --git a/dist/locale-abdc14e0.js b/dist/locale-abdc14e0.js
index 2eee80646b6979c33193a87c495e7b59598b15c3..7cd4b4209a92a5b23b7dadd9d93153c96488871b 100644
--- a/dist/locale-abdc14e0.js
+++ b/dist/locale-abdc14e0.js
@@ -2,7 +2,6 @@ import 'qs';
 import { f as find } from './index-bee7164f.js';
 import 'deepmerge';
 import 'fast-case';
-import { s as setCookie, g as getCookie } from './cookie-dff5d694.js';
 
 function methods(request, opt) {
   return {
@@ -15,7 +14,7 @@ function methods(request, opt) {
 
     async select(locale) {
       this.set(locale);
-      setCookie('swell-locale', locale);
+      opt.setCookie('swell-locale', locale);
       opt.api.settings.locale = locale;
       return await request('put', '/session', { locale });
     },
@@ -25,7 +24,7 @@ function methods(request, opt) {
         return this.code;
       }
       const storeLocale = opt.api.settings.getStoreLocale();
-      const cookieLocale = getCookie('swell-locale');
+      const cookieLocale = opt.getCookie('swell-locale');
       opt.api.settings.locale = cookieLocale || storeLocale;
       return cookieLocale || storeLocale;
     },
@stafyniaksacha
Copy link
Contributor Author

@swellmike Is there any change to have this implemented?

@swellmike
Copy link
Contributor

@stafyniaksacha Yes, this is a nice change. Feel free to open a PR against master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants