@dropins/tools 0.27.0-alpha1 → 0.27.0-alpha2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/recaptcha.js +2 -2
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name": "@dropins/tools", "version": "0.27.0-alpha1"}
1
+ {"name": "@dropins/tools", "version": "0.27.0-alpha2"}
package/recaptcha.js CHANGED
@@ -1,4 +1,4 @@
1
- import{FetchGraphQL as p}from"./fetch-graphql.js";const o={failedFetch:"Failed to fetch config from backend with status:",failedSetStorageConfig:"Failed to set storage config",failedGetStorageConfig:"Configuration could not be loaded.",failedExecutionRecaptcha:"Recaptcha execution failed",failedInitializing:"An error occurred while initializing ReCaptcha:"},u={PLACE_ORDER:"placeOrder",CONTACT:"contactUs",CUSTOMER_LOGIN:"generateCustomerToken",CUSTOMER_FORGOT_PASSWORD:"requestPasswordResetEmail",CUSTOMER_CREATE:"createCustomerV2",CUSTOMER_EDIT:"updateCustomerV2",NEWSLETTER:"subscribeEmailToNewsletter",PRODUCT_REVIEW:"createProductReview",SENDFRIEND:"SENDFRIEND",BRAINTREE:"BRAINTREE"},C=(r,e)=>{if(r&&r.forms){const t=r.forms.concat(e).map(a=>typeof a!="string"?{...a,enabledBadgePlace:!1}:{badgeId:u[a],enabledBadgePlace:!1});return{...r,forms:[...new Set(t)]}}return{}},g=async(r,e=1,t=1e3)=>{const a=sessionStorage.getItem(r);return a!==null?JSON.parse(a):e>0?(await new Promise(i=>setTimeout(i,t)),g(r,e-1,t)):null},d=(r,e)=>{if(!r||!e.website_key)return null;try{sessionStorage.setItem(r,JSON.stringify(e))}catch(t){return console.error(o.failedSetStorageConfig,t),null}},{failedExecutionRecaptcha:h}=o,_=async r=>{if(!window.grecaptcha)return Promise.reject(h);try{return await window.grecaptcha.execute(r,{action:"click"})}catch(e){return Promise.reject(`${h} : ${e}`)}},b=()=>new Promise(r=>{const e=new MutationObserver((a,i)=>{window.grecaptcha&&(i.disconnect(),r(!0))}),t={childList:!0,subtree:!0,attributes:!0};e.observe(document.body,t)}),m=async(r,e)=>(window.grecaptcha||await b(),grecaptcha.ready(()=>{const t=document.querySelectorAll(`#${r}`);if(!t.length)return null;t.forEach((a,i)=>a.id=`${a.id}_${i+1}`),t.forEach(a=>{if(a.innerHTML==="")try{grecaptcha.render(a.id,{sitekey:e.website_key,badge:e.badge_position,size:"invisible"})}catch(i){console.error(i)}})})),R=`query {
1
+ import{FetchGraphQL as p}from"./fetch-graphql.js";const o={failedFetch:"Failed to fetch config from backend with status:",failedSetStorageConfig:"Failed to set storage config",failedGetStorageConfig:"Configuration could not be loaded.",failedExecutionRecaptcha:"Recaptcha execution failed",failedInitializing:"An error occurred while initializing ReCaptcha:"},u={PLACE_ORDER:"placeOrder",CONTACT:"contactUs",CUSTOMER_LOGIN:"generateCustomerToken",CUSTOMER_FORGOT_PASSWORD:"requestPasswordResetEmail",CUSTOMER_CREATE:"createCustomerV2",CUSTOMER_EDIT:"updateCustomerV2",NEWSLETTER:"subscribeEmailToNewsletter",PRODUCT_REVIEW:"createProductReview",SENDFRIEND:"SENDFRIEND",BRAINTREE:"BRAINTREE"},C=(r,e)=>{if(r&&r.forms){const t=r.forms.concat(e).map(a=>typeof a!="string"?{...a,enabledBadgePlace:!1}:{badgeId:u[a],enabledBadgePlace:!1});return{...r,forms:[...new Set(t)]}}return{}},g=async(r,e=1,t=1e3)=>{const a=sessionStorage.getItem(r);return a!==null?JSON.parse(a):e>0?(await new Promise(i=>setTimeout(i,t)),g(r,e-1,t)):null},d=(r,e)=>{if(!r||!e.website_key)return null;try{sessionStorage.setItem(r,JSON.stringify(e))}catch(t){return console.error(o.failedSetStorageConfig,t),null}},{failedExecutionRecaptcha:h}=o,_=async r=>{if(!window.grecaptcha)return Promise.reject(h);try{return await window.grecaptcha.execute(r,{action:"click"})}catch(e){return Promise.reject(`${h} : ${e}`)}},b=()=>new Promise(r=>{const e=new MutationObserver((a,i)=>{window.grecaptcha&&(i.disconnect(),r(!0))}),t={childList:!0,subtree:!0,attributes:!0};e.observe(document.body,t)}),m=async(r,e)=>(window.grecaptcha||await b(),grecaptcha.ready(()=>{const t=document.querySelectorAll(`#${r}`);if(!t.length)return null;t.forEach(a=>a.id=`${a.id}_${Math.random().toString(36)}`),t.forEach(a=>{if(a.innerHTML==="")try{grecaptcha.render(a.id,{sitekey:e.website_key,badge:e.badge_position,size:"invisible"})}catch(i){console.error(i)}})})),R=`query {
2
2
  recaptchaV3Config {
3
3
  is_enabled
4
4
  website_key
@@ -8,4 +8,4 @@ import{FetchGraphQL as p}from"./fetch-graphql.js";const o={failedFetch:"Failed t
8
8
  failure_message
9
9
  forms
10
10
  }
11
- }`,n=new p().getMethods();class w{constructor(){var e;this._enableReCAPTCHA=!1,this._recaptchaBackendEndpoint=((e=n.getConfig())==null?void 0:e.endpoint)||"",this._recaptchaScriptUrl="https://www.google.com/recaptcha/api.js",this._configStorageKey="recaptchaConfig"}async _updateBadgePosition(e,t){var c;if(!t)return null;const{badgeId:a,enabledBadgePlace:i}=e;if(t.badge_position!=="inline"||i)return null;await m(a,t);const l={...t,forms:((c=t.forms)==null?void 0:c.map(s=>{const f=s.badgeId===a;return{...s,enabledBadgePlace:f}}))??[]};d(this._configStorageKey,l)}async _addRecaptchaScript(){const e=await this._loadConfig();if(!document.getElementById("recaptchaId")&&e){const t=e.website_key,a=e.badge_position==="inline";if(!t)return;const i=document.createElement("script");i.setAttribute("id","recaptchaId"),i.src=a?`${this._recaptchaScriptUrl}?render=${t}&badge=none`:`${this._recaptchaScriptUrl}?render=${t}&badge=${e.badge_position}`,document.head.appendChild(i)}}async _fetchStoreConfig(){var e;try{const t=await n.fetchGraphQl(R,{method:"GET",cache:"force-cache"});if((e=t==null?void 0:t.errors)!=null&&e.length){console.error(t.errors[0].message);return}return t}catch(t){console.error(`${o.failedFetch}:`,t);return}}async _loadConfig(){const e=await g(this._configStorageKey);return e?(this._enableReCAPTCHA=!!e.is_enabled,e):(console.error(o.failedGetStorageConfig),null)}setEndpoint(e){e&&(this._recaptchaBackendEndpoint=e,n.setEndpoint(e))}async setConfig(e){var t;try{const a=await this._fetchStoreConfig();if(!((t=a==null?void 0:a.data)!=null&&t.recaptchaV3Config)){sessionStorage.removeItem(this._configStorageKey);return}const i=C(a.data.recaptchaV3Config,e);d(this._configStorageKey,i)}catch(a){console.error(o.failedSetStorageConfig,a),sessionStorage.removeItem(this._configStorageKey)}}async initReCaptcha(){try{const e=await this._loadConfig();if(!(e!=null&&e.forms)||!e.is_enabled)return;await this._addRecaptchaScript(),e.forms.forEach(t=>{this._updateBadgePosition(t,e)})}catch(e){console.error(o.failedInitializing,e)}}async verifyReCaptcha(){try{const e=await this._loadConfig();return!(e!=null&&e.forms)||!e.website_key||!e.is_enabled?void 0:await _(e.website_key)}catch(e){console.error(e)}}getMethods(){return{setEndpoint:this.setEndpoint.bind(this),setConfig:this.setConfig.bind(this),initReCaptcha:this.initReCaptcha.bind(this),verifyReCaptcha:this.verifyReCaptcha.bind(this)}}}const y=new w,{initReCaptcha:S,verifyReCaptcha:T,setEndpoint:I,setConfig:P}=y.getMethods();export{w as RecaptchaModule,S as initReCaptcha,n as recaptchaFetchApi,P as setConfig,I as setEndpoint,T as verifyReCaptcha};
11
+ }`,n=new p().getMethods();class w{constructor(){var e;this._enableReCAPTCHA=!1,this._recaptchaBackendEndpoint=((e=n.getConfig())==null?void 0:e.endpoint)||"",this._recaptchaScriptUrl="https://www.google.com/recaptcha/api.js",this._configStorageKey="recaptchaConfig"}async _updateBadgePosition(e,t){var c;if(!t)return null;const{badgeId:a,enabledBadgePlace:i}=e;if(t.badge_position!=="inline"||i)return null;await m(a,t);const l={...t,forms:((c=t.forms)==null?void 0:c.map(s=>{const f=s.badgeId===a;return{...s,enabledBadgePlace:f}}))??[]};d(this._configStorageKey,l)}async _addRecaptchaScript(){const e=await this._loadConfig();if(!document.getElementById("recaptchaId")&&e){const t=e.website_key,a=e.badge_position==="inline";if(!t)return;const i=document.createElement("script");i.setAttribute("id","recaptchaId"),i.src=a?`${this._recaptchaScriptUrl}?render=${t}&badge=none`:`${this._recaptchaScriptUrl}?render=${t}&badge=${e.badge_position}`,document.head.appendChild(i)}}async _fetchStoreConfig(){var e;try{const t=await n.fetchGraphQl(R,{method:"GET",cache:"force-cache"});if((e=t==null?void 0:t.errors)!=null&&e.length){console.error(t.errors[0].message);return}return t}catch(t){console.error(`${o.failedFetch}:`,t);return}}async _loadConfig(){const e=await g(this._configStorageKey);return e?(this._enableReCAPTCHA=!!e.is_enabled,e):(console.error(o.failedGetStorageConfig),null)}setEndpoint(e){e&&(this._recaptchaBackendEndpoint=e,n.setEndpoint(e))}async setConfig(e){var t;try{const a=await this._fetchStoreConfig();if(!((t=a==null?void 0:a.data)!=null&&t.recaptchaV3Config)){sessionStorage.removeItem(this._configStorageKey);return}const i=C(a.data.recaptchaV3Config,e);d(this._configStorageKey,i)}catch(a){console.error(o.failedSetStorageConfig,a),sessionStorage.removeItem(this._configStorageKey)}}async initReCaptcha(){try{const e=await this._loadConfig();if(!(e!=null&&e.forms)||!e.is_enabled)return;await this._addRecaptchaScript(),e.forms.forEach(t=>{this._updateBadgePosition(t,e)})}catch(e){console.error(o.failedInitializing,e)}}async verifyReCaptcha(){try{const e=await this._loadConfig();return!(e!=null&&e.forms)||!e.website_key||!e.is_enabled?void 0:await _(e.website_key)}catch(e){console.error(e)}}getMethods(){return{setEndpoint:this.setEndpoint.bind(this),setConfig:this.setConfig.bind(this),initReCaptcha:this.initReCaptcha.bind(this),verifyReCaptcha:this.verifyReCaptcha.bind(this)}}}const y=new w,{initReCaptcha:E,verifyReCaptcha:T,setEndpoint:I,setConfig:P}=y.getMethods();export{w as RecaptchaModule,E as initReCaptcha,n as recaptchaFetchApi,P as setConfig,I as setEndpoint,T as verifyReCaptcha};