@mailto.foo/sdk 1.3.0 → 1.3.3

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.
package/README.md CHANGED
@@ -12,7 +12,7 @@ npm install @mailto.foo/sdk
12
12
  ```
13
13
  Or via CDN (no npm needed):
14
14
  ``` html
15
- <script src="https://cdn.mailto.foo/sdk.min.js"></script>
15
+ <script src="https://cdn.jsdelivr.net/npm/@mailto.foo/sdk/dist/sdk.min.js"></script>
16
16
  ```
17
17
 
18
18
  ## Usage
@@ -32,9 +32,9 @@ await mailto.subscribe({ email: 'user@example.com' })
32
32
  The CDN build auto-enhances any `<form>` whose `action` points at your subscribe endpoint — no JavaScript required:
33
33
 
34
34
  ```html
35
- <script src="https://cdn.jsdelivr.net/npm/@mailto.foo/sdk@1.0.0/dist/sdk.min.js"></script>
35
+ <script src="https://cdn.jsdelivr.net/npm/@mailto.foo/sdk/dist/sdk.min.js"></script>
36
36
 
37
- <form action="https://api.mailto.foo/pk_your_publish_key/subscribe">
37
+ <form action="https://api.mailto.foo/your_publish_key/subscribe">
38
38
  <input type="email" name="email" required>
39
39
  <button type="submit">Subscribe</button>
40
40
  </form>
package/dist/cdn.js CHANGED
@@ -8,16 +8,33 @@ const collectFormData = (form) => {
8
8
  }
9
9
  return data;
10
10
  };
11
+ const ERROR_CLASS = "mailto-foo-error";
12
+ const SUCCESS_CLASS = "mailto-foo-success";
13
+ const ensureMessageElement = (form, className) => {
14
+ const existing = form.querySelector(`.${className}`);
15
+ if (existing)
16
+ return existing;
17
+ const element = document.createElement("div");
18
+ element.className = className;
19
+ form.appendChild(element);
20
+ return element;
21
+ };
11
22
  const enhanceForm = (form, publishKey) => {
12
23
  const mailto = new Mailto({ publishKey });
24
+ const errorElement = ensureMessageElement(form, ERROR_CLASS);
25
+ const successElement = ensureMessageElement(form, SUCCESS_CLASS);
13
26
  form.addEventListener("submit", async (event) => {
14
27
  event.preventDefault();
15
28
  try {
16
29
  await mailto.subscribe(collectFormData(form));
30
+ errorElement.textContent = "";
31
+ successElement.textContent = "Thanks for subscribing!";
17
32
  form.reset();
18
33
  form.dispatchEvent(new CustomEvent("mailto:success", { bubbles: true }));
19
34
  }
20
35
  catch (err) {
36
+ successElement.textContent = "";
37
+ errorElement.textContent = err instanceof Error ? err.message : String(err);
21
38
  form.dispatchEvent(new CustomEvent("mailto:error", { bubbles: true, detail: { error: err } }));
22
39
  }
23
40
  });
package/dist/cdn.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cdn.js","sourceRoot":"","sources":["../src/cdn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,MAAM,mBAAmB,GAAG,iDAAiD,CAAC;AAE9E,MAAM,eAAe,GAAG,CAAC,IAAqB,EAA0B,EAAE;IACxE,MAAM,IAAI,GAA2B,EAAE,CAAC;IACxC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;QACxD,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,IAAI,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACnD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAC,IAAqB,EAAE,UAAkB,EAAE,EAAE;IAChE,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;IAE1C,IAAI,CAAC,gBAAgB,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;QAC9C,KAAK,CAAC,cAAc,EAAE,CAAC;QAEvB,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC,IAAI,CAAsB,CAAC,CAAC;YACnE,IAAI,CAAC,KAAK,EAAE,CAAC;YACb,IAAI,CAAC,aAAa,CAAC,IAAI,WAAW,C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ch(`${d}/config/${r}/config`,{signal:e.signal});if(clearTimeout(n),!t.ok)throw new Error(`Config fetch failed: ${t.status}`);return t.json()}catch(t){throw clearTimeout(n),t}},a=null,m=()=>window.turnstile?Promise.resolve():a||(a=new Promise((r,e)=>{let n=document.createElement("script");n.src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit",n.async=!0,n.defer=!0,n.onload=()=>r(),n.onerror=t=>{a=null,e(t)},document.head.appendChild(n)}),a),y=r=>new Promise((e,n)=>{let t=document.createElement("div");t.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(0,0,0,0.5)"].join(";");let o=document.createElement("div");t.appendChild(o),document.body.appendChild(t);let i,s=()=>{try{window.turnstile.remove(i)}catch{}try{document.body.removeChild(t)}catch{}};i=window.turnstile.render(o,{sitekey:r,callback:c=>{s(),e(c)},"error-callback":()=>{s(),n(new Error("Turnstile verification failed"))},"expired-callback":()=>{s(),n(new Error("Turnstile token expired"))}})}),p=async r=>{await m();let e=document.createElement("div");e.style.cssText="position:fixed;bottom:0;right:0;z-index:9999;visibility:hidden;",document.body.appendChild(e);let n,t=()=>{try{window.turnstile.remove(n)}catch{}try{document.body.removeChild(e)}catch{}};return await new Promise((i,s)=>{n=window.turnstile.render(e,{sitekey:r,appearance:"interaction-only",callback:c=>{t(),i(c)},"error-callback":()=>{t(),i(null)},"expired-callback":()=>{t(),s(new Error("Turnstile token expired"))}})})??y(r)},l=class{publishKey;constructor(e){this.publishKey=e.publishKey}async subscribe(e){let n=await b(this.publishKey),t=n.turnstileSiteKey?await p(n.turnstileSiteKey):void 0,o={data:e,config:{publishKey:this.publishKey,token:t}},i=await fetch(`${d}/subscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)});if(!i.ok){let c=(i.headers.get("content-type")??"").includes("application/json")?(await i.json()).error??"Subscription failed":await i.text();throw new Error(c)}}};var g=/\/(pk_[A-Za-z0-9_-]+)\/subscribe\/?(?:[?#].*)?$/,h=r=>{let e={};for(let[n,t]of new FormData(r).entries())typeof t=="string"&&(e[n]=t);return e},f=(r,e)=>{let n=new l({publishKey:e});r.addEventListener("submit",async t=>{t.preventDefault();try{await n.subscribe(h(r)),r.reset(),r.dispatchEvent(new CustomEvent("mailto:success",{bubbles:!0}))}catch(o){r.dispatchEvent(new CustomEvent("mailto:error",{bubbles:!0,detail:{error:o}}))}})},u=()=>{for(let r of document.querySelectorAll("form[action]")){let e=r.action.match(g);e?.[1]&&f(r,e[1])}};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",u):u();window.Mailto=l;})();
1
+ "use strict";(()=>{var u="https://api-mailto-foo.dan-7bc.workers.dev",b=async n=>{let t=new AbortController,r=setTimeout(()=>t.abort(),5e3);try{let e=await fetch(`${u}/config/${n}/config`,{signal:t.signal});if(clearTimeout(r),!e.ok)throw new Error(`Config fetch failed: ${e.status}`);return e.json()}catch(e){throw clearTimeout(r),e}},a=null,g=()=>window.turnstile?Promise.resolve():a||(a=new Promise((n,t)=>{let r=document.createElement("script");r.src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit",r.async=!0,r.defer=!0,r.onload=()=>n(),r.onerror=e=>{a=null,t(e)},document.head.appendChild(r)}),a),p=n=>new Promise((t,r)=>{let e=document.createElement("div");e.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(0,0,0,0.5)"].join(";");let s=document.createElement("div");e.appendChild(s),document.body.appendChild(e);let i,o=()=>{try{window.turnstile.remove(i)}catch{}try{document.body.removeChild(e)}catch{}};i=window.turnstile.render(s,{sitekey:n,callback:c=>{o(),t(c)},"error-callback":()=>{o(),r(new Error("Turnstile verification failed"))},"expired-callback":()=>{o(),r(new Error("Turnstile token expired"))}})}),y=async n=>{await g();let t=document.createElement("div");t.style.cssText="position:fixed;bottom:0;right:0;z-index:9999;visibility:hidden;",document.body.appendChild(t);let r,e=()=>{try{window.turnstile.remove(r)}catch{}try{document.body.removeChild(t)}catch{}};return await new Promise((i,o)=>{r=window.turnstile.render(t,{sitekey:n,appearance:"interaction-only",callback:c=>{e(),i(c)},"error-callback":()=>{e(),i(null)},"expired-callback":()=>{e(),o(new Error("Turnstile token expired"))}})})??p(n)},l=class{publishKey;constructor(t){this.publishKey=t.publishKey}async subscribe(t){let r=await b(this.publishKey),e=r.turnstileSiteKey?await y(r.turnstileSiteKey):void 0,s={data:t,config:{publishKey:this.publishKey,token:e}},i=await fetch(`${u}/subscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(!i.ok){let c=(i.headers.get("content-type")??"").includes("application/json")?(await i.json()).error??"Subscription failed":await i.text();throw new Error(c)}}};var h=/\/(pk_[A-Za-z0-9_-]+)\/subscribe\/?(?:[?#].*)?$/,f=n=>{let t={};for(let[r,e]of new FormData(n).entries())typeof e=="string"&&(t[r]=e);return t},w="mailto-foo-error",C="mailto-foo-success",d=(n,t)=>{let r=n.querySelector(`.${t}`);if(r)return r;let e=document.createElement("div");return e.className=t,n.appendChild(e),e},E=(n,t)=>{let r=new l({publishKey:t}),e=d(n,w),s=d(n,C);n.addEventListener("submit",async i=>{i.preventDefault();try{await r.subscribe(f(n)),e.textContent="",s.textContent="Thanks for subscribing!",n.reset(),n.dispatchEvent(new CustomEvent("mailto:success",{bubbles:!0}))}catch(o){s.textContent="",e.textContent=o instanceof Error?o.message:String(o),n.dispatchEvent(new CustomEvent("mailto:error",{bubbles:!0,detail:{error:o}}))}})},m=()=>{for(let n of document.querySelectorAll("form[action]")){let t=n.action.match(h);t?.[1]&&E(n,t[1])}};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m();window.Mailto=l;})();
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@mailto.foo/sdk",
3
3
  "type": "module",
4
4
  "description": "Headless email subscription and contact form SDK for mailto.foo",
5
- "version": "1.3.0",
5
+ "version": "1.3.3",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "repository": {