@trycourier/courier-js 1.0.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.
@@ -0,0 +1,32 @@
1
+ type CourierOptions = {
2
+ authorization?: string;
3
+ baseUrl?: string;
4
+ clientKey?: string;
5
+ debug?: boolean;
6
+ userId?: string;
7
+ userSignature?: string;
8
+ };
9
+ declare class Courier {
10
+ private authorization?;
11
+ private baseUrl;
12
+ private clientKey?;
13
+ private debug?;
14
+ private userId?;
15
+ private userSignature?;
16
+ constructor({ authorization, baseUrl, clientKey, debug, userId, userSignature, }: CourierOptions);
17
+ private getHeaders;
18
+ post<T>(path: string, body: T): Promise<void>;
19
+ put<T>(path: string, body?: T): Promise<void>;
20
+ delete(path: string): Promise<void>;
21
+ }
22
+
23
+ declare const client: {
24
+ __instance: Courier | null;
25
+ init(options: CourierOptions): void;
26
+ readonly instance: Courier;
27
+ identify(userId: string, payload: Record<string, unknown>): Promise<void>;
28
+ subscribe(userId: string, listId: string): Promise<void>;
29
+ unsubscribe(userId: string, listId: string): Promise<void>;
30
+ };
31
+
32
+ export { client as default };
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ default: () => src_default
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // package.json
28
+ var version = "1.0.0";
29
+
30
+ // src/helpers/client.ts
31
+ async function tryCatch(fn, debug = true) {
32
+ var _a, _b;
33
+ const response = await fn();
34
+ if (!response.ok && debug) {
35
+ console.error(
36
+ `Error invoking ${response.url}: ${(_a = response.status) != null ? _a : 404} because ${JSON.stringify((_b = await response.json()) == null ? void 0 : _b.message)}`
37
+ );
38
+ }
39
+ }
40
+ var Courier = class {
41
+ constructor({
42
+ authorization,
43
+ baseUrl,
44
+ clientKey,
45
+ debug = false,
46
+ userId,
47
+ userSignature
48
+ }) {
49
+ if (!clientKey || authorization) {
50
+ throw new Error("Courier client key is required");
51
+ }
52
+ this.authorization = authorization;
53
+ this.baseUrl = `${baseUrl != null ? baseUrl : "https://api.courier.com"}/client/`;
54
+ this.clientKey = clientKey;
55
+ this.debug = debug;
56
+ this.userId = userId;
57
+ this.userSignature = userSignature;
58
+ }
59
+ getHeaders() {
60
+ return new Headers({
61
+ "Content-Type": "application/json",
62
+ "x-courier-client-version": `courier-js@${version}`,
63
+ "Access-Control-Allow-Origin": "*",
64
+ ...this.authorization && { Authorization: this.authorization },
65
+ ...this.userId && { "x-courier-user-id": this.userId },
66
+ ...this.userSignature && {
67
+ "x-courier-user-signature": this.userSignature
68
+ },
69
+ ...this.clientKey && { "x-courier-client-key": this.clientKey }
70
+ });
71
+ }
72
+ async post(path, body) {
73
+ const postFn = () => {
74
+ return fetch(`${this.baseUrl}${path}`, {
75
+ body: JSON.stringify(body),
76
+ headers: this.getHeaders(),
77
+ method: "POST"
78
+ });
79
+ };
80
+ await tryCatch(postFn, this.debug);
81
+ }
82
+ async put(path, body) {
83
+ const putFn = () => {
84
+ return fetch(`${this.baseUrl}${path}`, {
85
+ ...body ? { body: JSON.stringify(body) } : {},
86
+ headers: this.getHeaders(),
87
+ method: "PUT"
88
+ });
89
+ };
90
+ await tryCatch(putFn, this.debug);
91
+ }
92
+ async delete(path) {
93
+ const deleteFn = () => {
94
+ return fetch(`${this.baseUrl}${path}`, {
95
+ headers: this.getHeaders(),
96
+ method: "DELETE"
97
+ });
98
+ };
99
+ await tryCatch(deleteFn, this.debug);
100
+ }
101
+ };
102
+
103
+ // src/index.ts
104
+ var client = {
105
+ __instance: null,
106
+ init(options) {
107
+ this.__instance = new Courier(options);
108
+ },
109
+ get instance() {
110
+ if (!this.__instance) {
111
+ throw new Error("Courier instance not initialized");
112
+ }
113
+ return this.__instance;
114
+ },
115
+ async identify(userId, payload) {
116
+ if (!userId) {
117
+ throw new Error("userId is required");
118
+ }
119
+ await this.instance.post(`identify/${userId}`, payload);
120
+ },
121
+ async subscribe(userId, listId) {
122
+ if (!userId || !listId) {
123
+ throw new Error("userId is required");
124
+ }
125
+ await this.instance.put(`lists/${listId}/subscribe/${userId}`);
126
+ },
127
+ async unsubscribe(userId, listId) {
128
+ if (!userId || !listId) {
129
+ throw new Error("userId is required");
130
+ }
131
+ this.instance.delete(`lists/${listId}/unsubscribe/${userId}`);
132
+ }
133
+ };
134
+ var src_default = client;
135
+ // Annotate the CommonJS export names for ESM import in node:
136
+ 0 && (module.exports = {});
package/dist/index.mjs ADDED
@@ -0,0 +1,111 @@
1
+ // package.json
2
+ var version = "1.0.0";
3
+
4
+ // src/helpers/client.ts
5
+ async function tryCatch(fn, debug = true) {
6
+ var _a, _b;
7
+ const response = await fn();
8
+ if (!response.ok && debug) {
9
+ console.error(
10
+ `Error invoking ${response.url}: ${(_a = response.status) != null ? _a : 404} because ${JSON.stringify((_b = await response.json()) == null ? void 0 : _b.message)}`
11
+ );
12
+ }
13
+ }
14
+ var Courier = class {
15
+ constructor({
16
+ authorization,
17
+ baseUrl,
18
+ clientKey,
19
+ debug = false,
20
+ userId,
21
+ userSignature
22
+ }) {
23
+ if (!clientKey || authorization) {
24
+ throw new Error("Courier client key is required");
25
+ }
26
+ this.authorization = authorization;
27
+ this.baseUrl = `${baseUrl != null ? baseUrl : "https://api.courier.com"}/client/`;
28
+ this.clientKey = clientKey;
29
+ this.debug = debug;
30
+ this.userId = userId;
31
+ this.userSignature = userSignature;
32
+ }
33
+ getHeaders() {
34
+ return new Headers({
35
+ "Content-Type": "application/json",
36
+ "x-courier-client-version": `courier-js@${version}`,
37
+ "Access-Control-Allow-Origin": "*",
38
+ ...this.authorization && { Authorization: this.authorization },
39
+ ...this.userId && { "x-courier-user-id": this.userId },
40
+ ...this.userSignature && {
41
+ "x-courier-user-signature": this.userSignature
42
+ },
43
+ ...this.clientKey && { "x-courier-client-key": this.clientKey }
44
+ });
45
+ }
46
+ async post(path, body) {
47
+ const postFn = () => {
48
+ return fetch(`${this.baseUrl}${path}`, {
49
+ body: JSON.stringify(body),
50
+ headers: this.getHeaders(),
51
+ method: "POST"
52
+ });
53
+ };
54
+ await tryCatch(postFn, this.debug);
55
+ }
56
+ async put(path, body) {
57
+ const putFn = () => {
58
+ return fetch(`${this.baseUrl}${path}`, {
59
+ ...body ? { body: JSON.stringify(body) } : {},
60
+ headers: this.getHeaders(),
61
+ method: "PUT"
62
+ });
63
+ };
64
+ await tryCatch(putFn, this.debug);
65
+ }
66
+ async delete(path) {
67
+ const deleteFn = () => {
68
+ return fetch(`${this.baseUrl}${path}`, {
69
+ headers: this.getHeaders(),
70
+ method: "DELETE"
71
+ });
72
+ };
73
+ await tryCatch(deleteFn, this.debug);
74
+ }
75
+ };
76
+
77
+ // src/index.ts
78
+ var client = {
79
+ __instance: null,
80
+ init(options) {
81
+ this.__instance = new Courier(options);
82
+ },
83
+ get instance() {
84
+ if (!this.__instance) {
85
+ throw new Error("Courier instance not initialized");
86
+ }
87
+ return this.__instance;
88
+ },
89
+ async identify(userId, payload) {
90
+ if (!userId) {
91
+ throw new Error("userId is required");
92
+ }
93
+ await this.instance.post(`identify/${userId}`, payload);
94
+ },
95
+ async subscribe(userId, listId) {
96
+ if (!userId || !listId) {
97
+ throw new Error("userId is required");
98
+ }
99
+ await this.instance.put(`lists/${listId}/subscribe/${userId}`);
100
+ },
101
+ async unsubscribe(userId, listId) {
102
+ if (!userId || !listId) {
103
+ throw new Error("userId is required");
104
+ }
105
+ this.instance.delete(`lists/${listId}/unsubscribe/${userId}`);
106
+ }
107
+ };
108
+ var src_default = client;
109
+ export {
110
+ src_default as default
111
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@trycourier/courier-js",
3
+ "version": "1.0.0",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "sideEffects": false,
8
+ "license": "MIT",
9
+ "files": [
10
+ "dist/**"
11
+ ],
12
+ "devDependencies": {
13
+ "eslint": "^7.32.0",
14
+ "tsup": "^5.10.1",
15
+ "typescript": "^4.5.3",
16
+ "@trycourier/tsconfig": "1.0.0",
17
+ "eslint-config-courier": "1.0.0"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "scripts": {
23
+ "build": "tsup src/index.ts --format esm,cjs --dts",
24
+ "dev": "tsup src/index.ts --format esm,cjs --watch --dts",
25
+ "lint": "eslint \"src/**/*.ts*\"",
26
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
27
+ }
28
+ }