@mailto.foo/sdk 1.0.3 → 1.1.0

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
@@ -16,23 +16,40 @@ Or via CDN (no npm needed):
16
16
  ```
17
17
 
18
18
  ## Usage
19
+
19
20
  ### npm / ESM
20
21
 
21
- ``` js
22
- import { MailtoWidget } from '@mailto.foo/sdk'
22
+ ```js
23
+ import { Mailto } from '@mailto.foo/sdk'
24
+
25
+ const mailto = new Mailto({ publishKey: 'pk_your_publish_key' })
23
26
 
24
- MailtoWidget.init('pk_your_publish_key')
27
+ await mailto.subscribe({ email: 'user@example.com' })
25
28
  ```
26
29
 
27
30
  ### Script tag
28
31
 
32
+ ```html
33
+ <script src="https://cdn.mailto.foo/sdk.js"></script>
29
34
  ```
30
- <script
31
- src="https://cdn.mailto.foo/sdk.js"
32
- data-key="pk_your_publish_key"
33
- ></script>
35
+
36
+ ```js
37
+ const mailto = new Mailto({ publishKey: 'pk_your_publish_key' })
38
+
39
+ await mailto.subscribe({ email: 'user@example.com' })
40
+ ```
41
+
42
+ ### Error handling
43
+
44
+ `subscribe` throws an `Error` with the message from the API on failure, so you can catch it directly:
45
+
46
+ ```js
47
+ try {
48
+ await mailto.subscribe({ email: 'user@example.com' })
49
+ } catch (err) {
50
+ console.error(err.message) // e.g. "Email already subscribed"
51
+ }
34
52
  ```
35
- That's it. The widget mounts, renders the bot protection challenge, and submits emails to your dashboard automatically.
36
53
 
37
54
  ### Publish Key
38
55
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,13 @@
1
- export declare const Mailto: {
2
- subscribe: (email: string) => Promise<void>;
3
- };
1
+ interface SubscribeData {
2
+ email: string;
3
+ }
4
+ interface SubscribeConfig {
5
+ publishKey: string;
6
+ token: string;
7
+ }
8
+ export declare class Mailto {
9
+ private publishKey;
10
+ constructor(config: SubscribeConfig);
11
+ subscribe(data: SubscribeData): Promise<void>;
12
+ }
13
+ export {};
package/dist/index.js CHANGED
@@ -12,17 +12,29 @@ const getToken = () => {
12
12
  .catch(reject);
13
13
  });
14
14
  };
15
- export const Mailto = {
16
- subscribe: async (email) => {
15
+ export class Mailto {
16
+ publishKey;
17
+ constructor(config) {
18
+ this.publishKey = config.publishKey;
19
+ }
20
+ async subscribe(data) {
17
21
  const token = await getToken();
22
+ const body = {
23
+ data,
24
+ config: { publishKey: this.publishKey, token },
25
+ };
18
26
  const res = await fetch("https://api-mailto-foo.dan-7bc.workers.dev/subscribe", {
19
27
  method: "POST",
20
28
  headers: { "Content-Type": "application/json" },
21
- body: JSON.stringify({ email, token }),
29
+ body: JSON.stringify(body),
22
30
  });
23
31
  if (!res.ok) {
24
- throw new Error("Subscription failed");
32
+ const contentType = res.headers.get("content-type") ?? "";
33
+ const body = contentType.includes("application/json")
34
+ ? (await res.json()).error ?? "Subscription failed"
35
+ : await res.text();
36
+ throw new Error(body);
25
37
  }
26
38
  }
27
- };
39
+ }
28
40
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,gBAAgB,CAAC;AAEjC,MAAM,QAAQ,GAAG,GAAoB,EAAE;IACrC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,GAAG,GAAG,IAAK,GAAW,CAAC;YAC3B,WAAW,EAAE,iCAAiC;SAC/C,CAAC,CAAC;QAEH,GAAG,CAAC,KAAK,EAAE;aACR,IAAI,CAAC,CAAC,MAAW,EAAE,EAAE;YACpB,GAAG,CAAC,KAAK,EAAE,CAAC;YACZ,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACxB,CAAC,CAAC;aACD,KAAK,CAAC,MAAM,CAAC,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,MAAM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/B,MAAM,IAAI,GAAkB;YAC1B,IAAI;YACJ,MAAM,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,KAAK,EAAE;SAC/C,CAAC;QAEF,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,sDAAsD,EAAE;YAC9E,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC;QAEH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACZ,MAAM,WAAW,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;YAC1D,MAAM,IAAI,GAAG,WAAW,CAAC,QAAQ,CAAC,kBAAkB,CAAC;gBACnD,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,EAAyB,CAAA,CAAC,KAAK,IAAI,qBAAqB;gBACzE,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;CACF"}
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.0.3",
5
+ "version": "1.1.0",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "repository": {