@trycourier/courier-js 1.0.0 → 1.2.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 ADDED
@@ -0,0 +1,45 @@
1
+ [![Courier: Your Complete Communication Stack](https://www.courier.com/_next/image/?url=https%3A%2F%2Fimages.ctfassets.net%2Fz7iqk1q8njt4%2F1PZo9WNTdmoDoYH3yulXa0%2Fb10830f7bfb09af5e644a39ac3d20c41%2FCourierJS_header_alt2.png&w=1920&q=75)](https://courier.com)
2
+
3
+ ## Requirements
4
+
5
+ Sign up to [Courier](https://app.courier.com/signup) if you do not have an account with us and get your clientKey from [here](https://app.courier.com/settings/api-keys)
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ # npm
11
+ npm install @trycourier/courier-js
12
+ # yarn
13
+ yarn add @trycourier/courier-js
14
+ # pnpm
15
+ pnpm add @trycourier/courier-js
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```ts
21
+ import courier from "@trycourier/courier-js";
22
+
23
+ courier.init({
24
+ clientKey: "<REPLACE_WITH_YOUR_CLIENT_KEY>",
25
+ debug: true,
26
+ });
27
+
28
+ /*
29
+ Upon initialization, you can use the SDK. All the methods are async and return a Promise `user | identify` means that you are identifying a user with a unique id in Courier and optionally passing in some user attributes like email, phone, etc. so that you can reach out to your users on right channels of their choice.
30
+ */
31
+ courier.identify("<your_user_id>", {
32
+ email: "suhas+from+ui@courier.com",
33
+ });
34
+ ```
35
+
36
+ ## Send User to Preference Center
37
+
38
+ This method generates a URL that you can use to send your users to the Courier Preference Center to let them manage their notification preferences. You can use this URL in your application to send your users to the Preference Center.
39
+
40
+ ```ts
41
+ const prefCenterLink = courier.generatePreferencesUrl("<your_user_id>", {
42
+ // optional
43
+ brandId: "<your_brand_id>",
44
+ });
45
+ ```
package/dist/index.d.ts CHANGED
@@ -6,10 +6,13 @@ type CourierOptions = {
6
6
  userId?: string;
7
7
  userSignature?: string;
8
8
  };
9
+ type PreferenceLinkOptions = {
10
+ brandId?: string;
11
+ };
9
12
  declare class Courier {
10
13
  private authorization?;
11
14
  private baseUrl;
12
- private clientKey?;
15
+ private clientKey;
13
16
  private debug?;
14
17
  private userId?;
15
18
  private userSignature?;
@@ -18,6 +21,7 @@ declare class Courier {
18
21
  post<T>(path: string, body: T): Promise<void>;
19
22
  put<T>(path: string, body?: T): Promise<void>;
20
23
  delete(path: string): Promise<void>;
24
+ generatePreferencesUrl(userId: string, options?: PreferenceLinkOptions): string;
21
25
  }
22
26
 
23
27
  declare const client: {
@@ -27,6 +31,7 @@ declare const client: {
27
31
  identify(userId: string, payload: Record<string, unknown>): Promise<void>;
28
32
  subscribe(userId: string, listId: string): Promise<void>;
29
33
  unsubscribe(userId: string, listId: string): Promise<void>;
34
+ generatePreferencesUrl(userId: string, options?: PreferenceLinkOptions): string;
30
35
  };
31
36
 
32
37
  export { client as default };
package/dist/index.js CHANGED
@@ -25,7 +25,24 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // package.json
28
- var version = "1.0.0";
28
+ var version = "1.2.0";
29
+
30
+ // src/helpers/decode.ts
31
+ function decode(clientKey) {
32
+ const binaryString = atob(clientKey);
33
+ const bytes = new Uint8Array(binaryString.length);
34
+ for (let i = 0; i < binaryString.length; i++) {
35
+ bytes[i] = binaryString.charCodeAt(i);
36
+ }
37
+ return String.fromCharCode(...bytes);
38
+ }
39
+ function encode(key) {
40
+ const bytes = new Uint8Array(key.length);
41
+ for (let i = 0; i < key.length; i++) {
42
+ bytes[i] = key.charCodeAt(i);
43
+ }
44
+ return btoa(String.fromCharCode(...bytes));
45
+ }
29
46
 
30
47
  // src/helpers/client.ts
31
48
  async function tryCatch(fn, debug = true) {
@@ -46,7 +63,7 @@ var Courier = class {
46
63
  userId,
47
64
  userSignature
48
65
  }) {
49
- if (!clientKey || authorization) {
66
+ if (!clientKey) {
50
67
  throw new Error("Courier client key is required");
51
68
  }
52
69
  this.authorization = authorization;
@@ -98,6 +115,16 @@ var Courier = class {
98
115
  };
99
116
  await tryCatch(deleteFn, this.debug);
100
117
  }
118
+ generatePreferencesUrl(userId, options) {
119
+ var _a;
120
+ if (!userId) {
121
+ throw new Error("User ID is required to generate preferences URL");
122
+ }
123
+ const id = decode(this.clientKey);
124
+ return `https://view.notificationcenter.app/p/${encode(
125
+ `${id}#${(_a = options == null ? void 0 : options.brandId) != null ? _a : ""}#${userId}#${false}`
126
+ )}`;
127
+ }
101
128
  };
102
129
 
103
130
  // src/index.ts
@@ -116,7 +143,11 @@ var client = {
116
143
  if (!userId) {
117
144
  throw new Error("userId is required");
118
145
  }
119
- await this.instance.post(`identify/${userId}`, payload);
146
+ await this.instance.post(`identify/${userId}`, {
147
+ profile: {
148
+ ...payload
149
+ }
150
+ });
120
151
  },
121
152
  async subscribe(userId, listId) {
122
153
  if (!userId || !listId) {
@@ -129,6 +160,9 @@ var client = {
129
160
  throw new Error("userId is required");
130
161
  }
131
162
  this.instance.delete(`lists/${listId}/unsubscribe/${userId}`);
163
+ },
164
+ generatePreferencesUrl(userId, options) {
165
+ return this.instance.generatePreferencesUrl(userId, options);
132
166
  }
133
167
  };
134
168
  var src_default = client;
package/dist/index.mjs CHANGED
@@ -1,5 +1,22 @@
1
1
  // package.json
2
- var version = "1.0.0";
2
+ var version = "1.2.0";
3
+
4
+ // src/helpers/decode.ts
5
+ function decode(clientKey) {
6
+ const binaryString = atob(clientKey);
7
+ const bytes = new Uint8Array(binaryString.length);
8
+ for (let i = 0; i < binaryString.length; i++) {
9
+ bytes[i] = binaryString.charCodeAt(i);
10
+ }
11
+ return String.fromCharCode(...bytes);
12
+ }
13
+ function encode(key) {
14
+ const bytes = new Uint8Array(key.length);
15
+ for (let i = 0; i < key.length; i++) {
16
+ bytes[i] = key.charCodeAt(i);
17
+ }
18
+ return btoa(String.fromCharCode(...bytes));
19
+ }
3
20
 
4
21
  // src/helpers/client.ts
5
22
  async function tryCatch(fn, debug = true) {
@@ -20,7 +37,7 @@ var Courier = class {
20
37
  userId,
21
38
  userSignature
22
39
  }) {
23
- if (!clientKey || authorization) {
40
+ if (!clientKey) {
24
41
  throw new Error("Courier client key is required");
25
42
  }
26
43
  this.authorization = authorization;
@@ -72,6 +89,16 @@ var Courier = class {
72
89
  };
73
90
  await tryCatch(deleteFn, this.debug);
74
91
  }
92
+ generatePreferencesUrl(userId, options) {
93
+ var _a;
94
+ if (!userId) {
95
+ throw new Error("User ID is required to generate preferences URL");
96
+ }
97
+ const id = decode(this.clientKey);
98
+ return `https://view.notificationcenter.app/p/${encode(
99
+ `${id}#${(_a = options == null ? void 0 : options.brandId) != null ? _a : ""}#${userId}#${false}`
100
+ )}`;
101
+ }
75
102
  };
76
103
 
77
104
  // src/index.ts
@@ -90,7 +117,11 @@ var client = {
90
117
  if (!userId) {
91
118
  throw new Error("userId is required");
92
119
  }
93
- await this.instance.post(`identify/${userId}`, payload);
120
+ await this.instance.post(`identify/${userId}`, {
121
+ profile: {
122
+ ...payload
123
+ }
124
+ });
94
125
  },
95
126
  async subscribe(userId, listId) {
96
127
  if (!userId || !listId) {
@@ -103,6 +134,9 @@ var client = {
103
134
  throw new Error("userId is required");
104
135
  }
105
136
  this.instance.delete(`lists/${listId}/unsubscribe/${userId}`);
137
+ },
138
+ generatePreferencesUrl(userId, options) {
139
+ return this.instance.generatePreferencesUrl(userId, options);
106
140
  }
107
141
  };
108
142
  var src_default = client;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/courier-js",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",