@notifizz/nodejs 1.0.0 → 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 +40 -30
- package/dist/NotifizzClient.d.ts +2 -2
- package/dist/NotifizzClient.js +10 -6
- package/dist/NotifizzClient.js.map +1 -1
- package/package.json +14 -5
- package/src/NotifizzClient.ts +0 -175
- package/src/index.ts +0 -1
- package/tsconfig.json +0 -14
package/README.md
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @notifizz/nodejs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
It allows you to:
|
|
3
|
+
JavaScript/TypeScript client for sending and tracking events with Notifizz. Use it to track events, run workflows, generate hashed user tokens and send notifications to the Notification Center.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
- Configure enrichment functions
|
|
8
|
-
- Handle authentication via `sdkSecretKey` and `authSecretKey`
|
|
9
|
-
- Send notifications directly to the **Notification Center**
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
## 🚀 Installation
|
|
5
|
+
## Installation
|
|
14
6
|
|
|
15
7
|
```bash
|
|
16
|
-
npm install notifizz
|
|
8
|
+
npm install @notifizz/nodejs
|
|
17
9
|
# or
|
|
18
|
-
yarn add notifizz
|
|
10
|
+
yarn add @notifizz/nodejs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
19
14
|
|
|
20
|
-
|
|
15
|
+
### Initialize the client
|
|
21
16
|
|
|
22
|
-
|
|
17
|
+
```javascript
|
|
18
|
+
const { NotifizzClient } = require("@notifizz/nodejs");
|
|
19
|
+
// or ESM: import { NotifizzClient } from "@notifizz/nodejs";
|
|
23
20
|
|
|
24
21
|
const client = new NotifizzClient(
|
|
25
|
-
"AUTH_SECRET_KEY",
|
|
26
|
-
"SDK_SECRET_KEY"
|
|
22
|
+
"AUTH_SECRET_KEY", // provided by Notifizz
|
|
23
|
+
"SDK_SECRET_KEY" // provided by Notifizz
|
|
27
24
|
);
|
|
25
|
+
```
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
### Track events with workflows
|
|
30
28
|
|
|
29
|
+
```javascript
|
|
31
30
|
await client
|
|
32
31
|
.track({
|
|
33
32
|
eventName: "user_signed_up",
|
|
@@ -38,28 +37,39 @@ await client
|
|
|
38
37
|
},
|
|
39
38
|
})
|
|
40
39
|
.workflow("campaign_123", [
|
|
41
|
-
{ id: "user_1", email: "
|
|
42
|
-
{ id: "user_2", email: "
|
|
40
|
+
{ id: "user_1", email: "user1@example.com" },
|
|
41
|
+
{ id: "user_2", email: "user2@example.com" },
|
|
43
42
|
]);
|
|
43
|
+
```
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const token = client.generateHashedToken("user_123");
|
|
45
|
+
You can chain multiple `.workflow(campaignId, recipients)` calls before it sends.
|
|
48
46
|
|
|
47
|
+
### Generate a hashed user token
|
|
49
48
|
|
|
50
|
-
|
|
49
|
+
Use this for backend authentication (e.g. Notification Center).
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}));
|
|
51
|
+
```javascript
|
|
52
|
+
const token = client.generateHashedToken("user_123");
|
|
53
|
+
```
|
|
56
54
|
|
|
57
|
-
|
|
55
|
+
### Send a notification to the Notification Center
|
|
58
56
|
|
|
57
|
+
```javascript
|
|
59
58
|
await client.send({
|
|
60
59
|
notifId: "notif_123",
|
|
61
60
|
properties: {
|
|
62
|
-
recipients: [{ id: "user_1", email: "
|
|
61
|
+
recipients: [{ id: "user_1", email: "user@example.com" }],
|
|
63
62
|
message: "Hello world",
|
|
64
63
|
},
|
|
65
64
|
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API summary| Method | Description |
|
|
68
|
+
|--------|-------------|
|
|
69
|
+
| `new NotifizzClient(authSecretKey, sdkSecretKey)` | Create a client. |
|
|
70
|
+
| `client.track({ eventName, sdkSecretKey, properties })` | Start tracking an event; returns a context. |
|
|
71
|
+
| `context.workflow(campaignId, recipients)` | Attach a workflow and recipients (chainable). |
|
|
72
|
+
| `client.generateHashedToken(userId)` | Generate a hashed token for the user. |
|
|
73
|
+
| `NotifizzClient.configureEnrich(workflowIds, fn)` | Register an enrichment function for workflow(s). |
|
|
74
|
+
| `client.send({ notifId, properties })` | Send a notification to the Notification Center. |
|
|
75
|
+
| `client.config({ autoSendDelayMs })` | Configure options (e.g. auto-send delay in ms). |
|
package/dist/NotifizzClient.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type TrackProps = {
|
|
|
7
7
|
};
|
|
8
8
|
type NotifizzOptions = {
|
|
9
9
|
autoSendDelayMs?: number;
|
|
10
|
+
baseUrl?: string;
|
|
10
11
|
};
|
|
11
12
|
type EnrichFunction = (properties: {
|
|
12
13
|
[k: string]: any;
|
|
@@ -33,7 +34,7 @@ declare class TrackContext {
|
|
|
33
34
|
private readonly options;
|
|
34
35
|
private readonly sdkSecretKey;
|
|
35
36
|
private readonly baseUrl;
|
|
36
|
-
constructor(event: TrackProps, options: NotifizzOptions, sdkSecretKey: string);
|
|
37
|
+
constructor(event: TrackProps, options: NotifizzOptions, sdkSecretKey: string, baseUrl: string);
|
|
37
38
|
workflow(campaignId: string, recipients: {
|
|
38
39
|
id: string;
|
|
39
40
|
email: string;
|
|
@@ -45,7 +46,6 @@ declare class TrackContext {
|
|
|
45
46
|
export declare class NotifizzClient {
|
|
46
47
|
private readonly authSecretKey;
|
|
47
48
|
private readonly sdkSecretKey;
|
|
48
|
-
private readonly baseUrl;
|
|
49
49
|
private readonly algorithm;
|
|
50
50
|
private readonly encoding;
|
|
51
51
|
private options;
|
package/dist/NotifizzClient.js
CHANGED
|
@@ -26,18 +26,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.NotifizzClient = void 0;
|
|
27
27
|
const axios_1 = __importDefault(require("axios"));
|
|
28
28
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
29
|
+
const DEFAULT_BASE_URL = "https://eu.api.notifizz.com/v1";
|
|
29
30
|
const defaultOptions = {
|
|
30
31
|
autoSendDelayMs: 1000,
|
|
31
32
|
};
|
|
32
33
|
class TrackContext {
|
|
33
|
-
constructor(event, options, sdkSecretKey) {
|
|
34
|
+
constructor(event, options, sdkSecretKey, baseUrl) {
|
|
34
35
|
this.workflows = [];
|
|
35
36
|
this.hasSent = false;
|
|
36
37
|
this.autoSendTimer = null;
|
|
37
|
-
this.baseUrl = "http://localhost:6001/v1";
|
|
38
38
|
this.event = event;
|
|
39
39
|
this.options = options;
|
|
40
40
|
this.sdkSecretKey = sdkSecretKey;
|
|
41
|
+
this.baseUrl = baseUrl;
|
|
41
42
|
if (options.autoSendDelayMs !== undefined) {
|
|
42
43
|
this.autoSendTimer = setTimeout(() => {
|
|
43
44
|
if (!this.hasSent) {
|
|
@@ -83,7 +84,6 @@ class NotifizzClient {
|
|
|
83
84
|
return node_crypto_1.default.createHash(this.algorithm).update(textToHash).digest(this.encoding);
|
|
84
85
|
}
|
|
85
86
|
constructor(authSecretKey, sdkSecretKey) {
|
|
86
|
-
this.baseUrl = "http://localhost:6001/v1";
|
|
87
87
|
this.algorithm = 'sha256';
|
|
88
88
|
this.encoding = 'hex';
|
|
89
89
|
this.options = Object.assign({}, defaultOptions);
|
|
@@ -113,16 +113,20 @@ class NotifizzClient {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
track(props) {
|
|
116
|
-
|
|
116
|
+
var _a;
|
|
117
|
+
const baseUrl = (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : DEFAULT_BASE_URL;
|
|
118
|
+
return new TrackContext(props, this.options, this.sdkSecretKey, baseUrl);
|
|
117
119
|
}
|
|
118
120
|
config(opts) {
|
|
119
121
|
this.options = Object.assign(Object.assign({}, this.options), opts);
|
|
120
122
|
}
|
|
121
123
|
send(request) {
|
|
122
124
|
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
var _a;
|
|
123
126
|
try {
|
|
124
|
-
const _a =
|
|
125
|
-
const
|
|
127
|
+
const baseUrl = (_a = this.options.baseUrl) !== null && _a !== void 0 ? _a : DEFAULT_BASE_URL;
|
|
128
|
+
const _b = request.properties, { recipients } = _b, eventProperties = __rest(_b, ["recipients"]);
|
|
129
|
+
const { data } = yield axios_1.default.post(`${baseUrl}/notification/channel/notificationcenter/config/${request.notifId}/track`, { properties: eventProperties, recipients: recipients }, {
|
|
126
130
|
headers: {
|
|
127
131
|
Authorization: `Bearer ${this.sdkSecretKey}`
|
|
128
132
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotifizzClient.js","sourceRoot":"","sources":["../src/NotifizzClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,8DAAiC;
|
|
1
|
+
{"version":3,"file":"NotifizzClient.js","sourceRoot":"","sources":["../src/NotifizzClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA0B;AAC1B,8DAAiC;AAyBjC,MAAM,gBAAgB,GAAG,gCAAgC,CAAC;AAE1D,MAAM,cAAc,GAAoB;IACtC,eAAe,EAAE,IAAI;CACtB,CAAC;AAeF,MAAM,YAAY;IAShB,YAAY,KAAiB,EAAE,OAAwB,EAAE,YAAoB,EAAE,OAAe;QAP7E,cAAS,GAAsB,EAAE,CAAC;QAC3C,YAAO,GAAG,KAAK,CAAC;QACP,kBAAa,GAA0B,IAAI,CAAC;QAM3D,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,IAAI,OAAO,CAAC,eAAe,KAAK,SAAS,EAAE,CAAC;YAC1C,IAAI,CAAC,aAAa,GAAG,UAAU,CAAC,GAAG,EAAE;gBACn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
"
|
|
7
|
-
"build": "tsc"
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public",
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
8
7
|
},
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"start": "node dist/index.js"
|
|
17
|
+
},
|
|
9
18
|
"keywords": [],
|
|
10
19
|
"author": "damiencosset",
|
|
11
20
|
"license": "ISC",
|
package/src/NotifizzClient.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import crypto from "node:crypto";
|
|
3
|
-
|
|
4
|
-
export type TrackProps = {
|
|
5
|
-
eventName: string;
|
|
6
|
-
sdkSecretKey: string;
|
|
7
|
-
properties: {
|
|
8
|
-
[k: string]: any;
|
|
9
|
-
},
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
type WorkflowContext = {
|
|
13
|
-
campaignId: string;
|
|
14
|
-
recipients: { id: string; email: string; [k: string]: any; }[];
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
type NotifizzOptions = {
|
|
18
|
-
autoSendDelayMs?: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
type EnrichFunction = (properties: {[k: string]: any}) => {
|
|
22
|
-
recipients: [EventRecipient];
|
|
23
|
-
[k: string]: any;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const defaultOptions: NotifizzOptions = {
|
|
27
|
-
autoSendDelayMs: 1000,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
type EventProperties = {
|
|
31
|
-
notifId: string;
|
|
32
|
-
properties: {
|
|
33
|
-
recipients: [EventRecipient];
|
|
34
|
-
[k: string]: any;
|
|
35
|
-
},
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type EventRecipient = {
|
|
39
|
-
id: string,
|
|
40
|
-
email: string,
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
class TrackContext {
|
|
44
|
-
private readonly event: TrackProps;
|
|
45
|
-
private readonly workflows: WorkflowContext[] = [];
|
|
46
|
-
private hasSent = false;
|
|
47
|
-
private readonly autoSendTimer: NodeJS.Timeout | null = null;
|
|
48
|
-
private readonly options: NotifizzOptions;
|
|
49
|
-
private readonly sdkSecretKey: string;
|
|
50
|
-
|
|
51
|
-
private readonly baseUrl: string = "http://localhost:6001/v1";
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
constructor(event: TrackProps, options: NotifizzOptions, sdkSecretKey: string) {
|
|
55
|
-
this.event = event;
|
|
56
|
-
this.options = options;
|
|
57
|
-
this.sdkSecretKey = sdkSecretKey;
|
|
58
|
-
|
|
59
|
-
if (options.autoSendDelayMs !== undefined) {
|
|
60
|
-
this.autoSendTimer = setTimeout(() => {
|
|
61
|
-
if (!this.hasSent) {
|
|
62
|
-
this.send();
|
|
63
|
-
}
|
|
64
|
-
}, options.autoSendDelayMs);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
workflow(campaignId: string, recipients: { id: string; email: string; [k: string]: any; }[]) {
|
|
69
|
-
if (this.hasSent) throw new Error("Cannot add workflows after sending the event.");
|
|
70
|
-
this.workflows.push({ campaignId, recipients });
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private async send() {
|
|
75
|
-
if (this.hasSent) return;
|
|
76
|
-
if (this.autoSendTimer) clearTimeout(this.autoSendTimer);
|
|
77
|
-
|
|
78
|
-
this.hasSent = true;
|
|
79
|
-
|
|
80
|
-
const payload = {
|
|
81
|
-
...this.event,
|
|
82
|
-
workflows: this.workflows,
|
|
83
|
-
sdkSecretKey: this.sdkSecretKey,
|
|
84
|
-
};
|
|
85
|
-
try {
|
|
86
|
-
await axios.post(
|
|
87
|
-
`${this.baseUrl}/events/track`,payload,
|
|
88
|
-
{
|
|
89
|
-
headers: {
|
|
90
|
-
Authorization: `Bearer ${this.sdkSecretKey}`
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
);
|
|
94
|
-
}catch(e) {
|
|
95
|
-
console.log('Error', e);
|
|
96
|
-
throw e;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Optional for promise-like behavior
|
|
103
|
-
then(resolve: any, reject: any) {
|
|
104
|
-
return this.send().then(resolve, reject);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export class NotifizzClient {
|
|
109
|
-
private readonly authSecretKey: string;
|
|
110
|
-
private readonly sdkSecretKey: string;
|
|
111
|
-
private readonly baseUrl: string = "http://localhost:6001/v1";
|
|
112
|
-
private readonly algorithm: string = 'sha256';
|
|
113
|
-
private readonly encoding: crypto.BinaryToTextEncoding = 'hex';
|
|
114
|
-
private options: NotifizzOptions = { ...defaultOptions };
|
|
115
|
-
static enrichFunctions: Map<string, EnrichFunction[]> = new Map();
|
|
116
|
-
|
|
117
|
-
private sha256(textToHash: string): string {
|
|
118
|
-
return crypto.createHash(this.algorithm).update(textToHash).digest(this.encoding);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
constructor(authSecretKey: string, sdkSecretKey: string) {
|
|
122
|
-
this.authSecretKey = authSecretKey;
|
|
123
|
-
this.sdkSecretKey = sdkSecretKey;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
generateHashedToken(userId: string): string{
|
|
127
|
-
return this.sha256(userId + this.authSecretKey)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
static configureEnrich(workflowIds: string | string[], enrichFn: EnrichFunction) {
|
|
131
|
-
if (typeof workflowIds === "string") {
|
|
132
|
-
this.addEnrichmentFunction(workflowIds, enrichFn);
|
|
133
|
-
} else {
|
|
134
|
-
workflowIds.forEach(workflowId => {
|
|
135
|
-
this.addEnrichmentFunction(workflowId, enrichFn);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private static addEnrichmentFunction(workflowId: string, enrichFn: EnrichFunction) {
|
|
141
|
-
if (this.enrichFunctions.has(workflowId)) {
|
|
142
|
-
this.enrichFunctions.get(workflowId)?.push(enrichFn);
|
|
143
|
-
} else {
|
|
144
|
-
this.enrichFunctions.set(workflowId, [enrichFn]);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
track(props: TrackProps): TrackContext {
|
|
149
|
-
return new TrackContext(props, this.options, this.sdkSecretKey);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
config(opts: Partial<NotifizzOptions>) {
|
|
153
|
-
this.options = { ...this.options, ...opts };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
async send(request: EventProperties) {
|
|
157
|
-
try {
|
|
158
|
-
const { recipients, ...eventProperties } = request.properties
|
|
159
|
-
|
|
160
|
-
const { data } = await axios.post(
|
|
161
|
-
`${this.baseUrl}/notification/channel/notificationcenter/config/${request.notifId}/track`,
|
|
162
|
-
{properties: eventProperties, recipients: recipients},
|
|
163
|
-
{
|
|
164
|
-
headers: {
|
|
165
|
-
Authorization: `Bearer ${this.sdkSecretKey}`
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
);
|
|
169
|
-
return data;
|
|
170
|
-
} catch (e) {
|
|
171
|
-
console.log('Error', e);
|
|
172
|
-
throw e;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {NotifizzClient} from "./NotifizzClient"
|
package/tsconfig.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"outDir": "dist",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"target": "ES6",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"sourceMap": true,
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true
|
|
11
|
-
},
|
|
12
|
-
"include": ["src"],
|
|
13
|
-
"exclude": ["node_modules", "dist"]
|
|
14
|
-
}
|