notifykitdev 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.
- package/README.md +250 -0
- package/dist/index.d.mts +136 -0
- package/dist/index.d.ts +136 -0
- package/dist/index.js +156 -0
- package/dist/index.mjs +135 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# NotifyKit JavaScript SDK
|
|
2
|
+
|
|
3
|
+
Official NotifyKit SDK for JavaScript. Send notifications from your apps with a simple, non-blocking API.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Works everywhere: Node.js 18+, Bun, Deno, browsers, React, Vue, Next.js, Express
|
|
8
|
+
- TypeScript-first with full type definitions included
|
|
9
|
+
- Zero dependencies (~2KB minified)
|
|
10
|
+
- Async/non-blocking - won't slow down your app
|
|
11
|
+
- Silent failures - logs errors but never crashes your app
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# npm
|
|
17
|
+
npm install notifykitdev
|
|
18
|
+
|
|
19
|
+
# yarn
|
|
20
|
+
yarn add notifykitdev
|
|
21
|
+
|
|
22
|
+
# pnpm
|
|
23
|
+
pnpm add notifykitdev
|
|
24
|
+
|
|
25
|
+
# bun
|
|
26
|
+
bun add notifykitdev
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import NotifyKit from 'notifykitdev';
|
|
33
|
+
|
|
34
|
+
// Initialize once at startup
|
|
35
|
+
NotifyKit.init('nsk_your_api_key');
|
|
36
|
+
|
|
37
|
+
// Send notifications anywhere in your app
|
|
38
|
+
NotifyKit.notify('User signed up!');
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API Reference
|
|
42
|
+
|
|
43
|
+
### `NotifyKit.init(apiKey)`
|
|
44
|
+
|
|
45
|
+
Initialize the SDK with your API key. Call this once at application startup.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Simple initialization
|
|
49
|
+
NotifyKit.init('nsk_your_api_key');
|
|
50
|
+
|
|
51
|
+
// With options
|
|
52
|
+
NotifyKit.init({
|
|
53
|
+
apiKey: 'nsk_your_api_key',
|
|
54
|
+
baseUrl: 'https://api.notifykit.dev', // optional, custom API URL
|
|
55
|
+
debug: true, // optional, enable debug logging
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `NotifyKit.notify(message, options?)`
|
|
60
|
+
|
|
61
|
+
Send a notification. This is fire-and-forget: it returns immediately and won't throw errors.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// Simple message
|
|
65
|
+
NotifyKit.notify('Hello world!');
|
|
66
|
+
|
|
67
|
+
// With topic for categorization
|
|
68
|
+
NotifyKit.notify('New order received', { topic: 'orders' });
|
|
69
|
+
|
|
70
|
+
// With idempotency key to prevent duplicates
|
|
71
|
+
NotifyKit.notify('Welcome email sent', {
|
|
72
|
+
topic: 'onboarding',
|
|
73
|
+
idempotencyKey: `welcome-${userId}`,
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Options:**
|
|
78
|
+
|
|
79
|
+
| Option | Type | Description |
|
|
80
|
+
|--------|------|-------------|
|
|
81
|
+
| `topic` | `string` | Categorize notifications for filtering |
|
|
82
|
+
| `idempotencyKey` | `string` | Prevent duplicate notifications |
|
|
83
|
+
|
|
84
|
+
### `NotifyKit.isInitialized()`
|
|
85
|
+
|
|
86
|
+
Check if the SDK has been initialized.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
if (!NotifyKit.isInitialized()) {
|
|
90
|
+
NotifyKit.init('nsk_your_api_key');
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Framework Examples
|
|
95
|
+
|
|
96
|
+
### Express.js
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import express from 'express';
|
|
100
|
+
import NotifyKit from 'notifykitdev';
|
|
101
|
+
|
|
102
|
+
const app = express();
|
|
103
|
+
|
|
104
|
+
// Initialize once at startup
|
|
105
|
+
NotifyKit.init(process.env.NOTIFYKIT_API_KEY!);
|
|
106
|
+
|
|
107
|
+
app.post('/api/orders', async (req, res) => {
|
|
108
|
+
const order = await createOrder(req.body);
|
|
109
|
+
|
|
110
|
+
// Fire-and-forget notification
|
|
111
|
+
NotifyKit.notify(`New order #${order.id} for $${order.total}`, {
|
|
112
|
+
topic: 'orders',
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
res.json(order);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
app.listen(3000);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Next.js (App Router)
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// app/api/signup/route.ts
|
|
125
|
+
import NotifyKit from 'notifykitdev';
|
|
126
|
+
|
|
127
|
+
// Initialize (safe to call multiple times)
|
|
128
|
+
NotifyKit.init(process.env.NOTIFYKIT_API_KEY!);
|
|
129
|
+
|
|
130
|
+
export async function POST(request: Request) {
|
|
131
|
+
const { email } = await request.json();
|
|
132
|
+
const user = await createUser(email);
|
|
133
|
+
|
|
134
|
+
NotifyKit.notify(`New signup: ${email}`, {
|
|
135
|
+
topic: 'signups',
|
|
136
|
+
idempotencyKey: `signup-${user.id}`,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return Response.json({ success: true });
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Next.js (Client-side)
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// components/feedback-form.tsx
|
|
147
|
+
'use client';
|
|
148
|
+
|
|
149
|
+
import NotifyKit from 'notifykitdev';
|
|
150
|
+
|
|
151
|
+
// Initialize with your write-only API key (safe for frontend)
|
|
152
|
+
NotifyKit.init('nsk_your_api_key');
|
|
153
|
+
|
|
154
|
+
export function FeedbackForm() {
|
|
155
|
+
const handleSubmit = async (e: React.FormEvent) => {
|
|
156
|
+
e.preventDefault();
|
|
157
|
+
const feedback = new FormData(e.target as HTMLFormElement).get('feedback');
|
|
158
|
+
|
|
159
|
+
NotifyKit.notify(`New feedback: ${feedback}`, { topic: 'feedback' });
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<form onSubmit={handleSubmit}>
|
|
164
|
+
<textarea name="feedback" required />
|
|
165
|
+
<button type="submit">Send Feedback</button>
|
|
166
|
+
</form>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### React
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import { useEffect } from 'react';
|
|
175
|
+
import NotifyKit from 'notifykitdev';
|
|
176
|
+
|
|
177
|
+
// Initialize outside component (runs once)
|
|
178
|
+
NotifyKit.init('nsk_your_api_key');
|
|
179
|
+
|
|
180
|
+
function App() {
|
|
181
|
+
const handleClick = () => {
|
|
182
|
+
NotifyKit.notify('Button clicked!', { topic: 'events' });
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return <button onClick={handleClick}>Click me</button>;
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Deno
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import NotifyKit from 'npm:notifykitdev';
|
|
193
|
+
|
|
194
|
+
NotifyKit.init(Deno.env.get('NOTIFYKIT_API_KEY')!);
|
|
195
|
+
|
|
196
|
+
Deno.serve(async (req) => {
|
|
197
|
+
NotifyKit.notify('Request received', { topic: 'requests' });
|
|
198
|
+
return new Response('OK');
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Bun
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import NotifyKit from 'notifykitdev';
|
|
206
|
+
|
|
207
|
+
NotifyKit.init(Bun.env.NOTIFYKIT_API_KEY!);
|
|
208
|
+
|
|
209
|
+
Bun.serve({
|
|
210
|
+
port: 3000,
|
|
211
|
+
fetch(req) {
|
|
212
|
+
NotifyKit.notify('Request received');
|
|
213
|
+
return new Response('OK');
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Error Handling
|
|
219
|
+
|
|
220
|
+
The SDK is designed to never throw errors or crash your app. All errors are logged to the console but won't interrupt your code:
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
// This won't throw even if the API is down
|
|
224
|
+
NotifyKit.notify('Hello world!');
|
|
225
|
+
|
|
226
|
+
// Enable debug mode for more detailed logging
|
|
227
|
+
NotifyKit.init({
|
|
228
|
+
apiKey: 'nsk_your_api_key',
|
|
229
|
+
debug: true,
|
|
230
|
+
});
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## TypeScript
|
|
234
|
+
|
|
235
|
+
Full TypeScript support is included. Import types directly:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
import NotifyKit, { type NotifyOptions, type NotifyKitConfig } from 'notifykitdev';
|
|
239
|
+
|
|
240
|
+
const options: NotifyOptions = {
|
|
241
|
+
topic: 'alerts',
|
|
242
|
+
idempotencyKey: 'unique-123',
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
NotifyKit.notify('Alert!', options);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NotifyKit SDK TypeScript type definitions.
|
|
3
|
+
* @module notifykit/types
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Options for configuring a notification.
|
|
7
|
+
*/
|
|
8
|
+
interface NotifyOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Topic to categorize the notification.
|
|
11
|
+
* Used for filtering and routing notifications.
|
|
12
|
+
*/
|
|
13
|
+
topic?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Unique key to prevent duplicate notifications.
|
|
16
|
+
* If a notification with the same idempotency key was already sent,
|
|
17
|
+
* the duplicate will be ignored.
|
|
18
|
+
*/
|
|
19
|
+
idempotencyKey?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Configuration options for initializing NotifyKit.
|
|
23
|
+
*/
|
|
24
|
+
interface NotifyKitConfig {
|
|
25
|
+
/**
|
|
26
|
+
* Your NotifyKit API key (starts with 'nsk_').
|
|
27
|
+
*/
|
|
28
|
+
apiKey: string;
|
|
29
|
+
/**
|
|
30
|
+
* Custom API base URL.
|
|
31
|
+
* Defaults to 'https://api.notifykit.dev'.
|
|
32
|
+
*/
|
|
33
|
+
baseUrl?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Enable debug logging.
|
|
36
|
+
* @default false
|
|
37
|
+
*/
|
|
38
|
+
debug?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Response from the notify API.
|
|
42
|
+
*/
|
|
43
|
+
interface NotifyResponse {
|
|
44
|
+
/**
|
|
45
|
+
* Unique identifier for the notification event.
|
|
46
|
+
*/
|
|
47
|
+
eventId: string;
|
|
48
|
+
/**
|
|
49
|
+
* Status of the notification request.
|
|
50
|
+
*/
|
|
51
|
+
status: "accepted" | "rejected";
|
|
52
|
+
/**
|
|
53
|
+
* Error message if the notification was rejected.
|
|
54
|
+
*/
|
|
55
|
+
error?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* NotifyKit SDK for JavaScript.
|
|
60
|
+
* Send notifications from your apps with a simple API.
|
|
61
|
+
* @module notifykit
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* NotifyKit SDK client.
|
|
66
|
+
* Initialize once with your API key, then send notifications from anywhere.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import NotifyKit from 'notifykit';
|
|
71
|
+
*
|
|
72
|
+
* // Initialize once at startup
|
|
73
|
+
* NotifyKit.init('nsk_your_api_key');
|
|
74
|
+
*
|
|
75
|
+
* // Send notifications anywhere in your app
|
|
76
|
+
* NotifyKit.notify('User signed up!');
|
|
77
|
+
* NotifyKit.notify('Payment received', { topic: 'payments' });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
declare const NotifyKit: {
|
|
81
|
+
/**
|
|
82
|
+
* Initializes the NotifyKit SDK with your API key.
|
|
83
|
+
* Call this once at application startup.
|
|
84
|
+
*
|
|
85
|
+
* @param apiKeyOrConfig - Your API key string or full configuration object
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* // Simple initialization
|
|
90
|
+
* NotifyKit.init('nsk_your_api_key');
|
|
91
|
+
*
|
|
92
|
+
* // With custom options
|
|
93
|
+
* NotifyKit.init({
|
|
94
|
+
* apiKey: 'nsk_your_api_key',
|
|
95
|
+
* debug: true,
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
init(apiKeyOrConfig: string | NotifyKitConfig): void;
|
|
100
|
+
/**
|
|
101
|
+
* Sends a notification asynchronously.
|
|
102
|
+
* This method is fire-and-forget: it won't block your app or throw errors.
|
|
103
|
+
* Errors are logged to the console but won't crash your application.
|
|
104
|
+
*
|
|
105
|
+
* @param message - The notification message to send
|
|
106
|
+
* @param options - Optional configuration for the notification
|
|
107
|
+
* @returns A promise that resolves when the notification is sent (or fails silently)
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // Simple notification
|
|
112
|
+
* NotifyKit.notify('Hello world!');
|
|
113
|
+
*
|
|
114
|
+
* // With topic for categorization
|
|
115
|
+
* NotifyKit.notify('New order received', { topic: 'orders' });
|
|
116
|
+
*
|
|
117
|
+
* // With idempotency key to prevent duplicates
|
|
118
|
+
* NotifyKit.notify('Welcome!', {
|
|
119
|
+
* topic: 'onboarding',
|
|
120
|
+
* idempotencyKey: `welcome-${userId}`,
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
notify(message: string, options?: NotifyOptions): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Checks if the SDK has been initialized.
|
|
127
|
+
* @returns True if init() has been called with an API key
|
|
128
|
+
*/
|
|
129
|
+
isInitialized(): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Resets the SDK state. Useful for testing.
|
|
132
|
+
*/
|
|
133
|
+
reset(): void;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export { type NotifyKitConfig, type NotifyOptions, type NotifyResponse, NotifyKit as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NotifyKit SDK TypeScript type definitions.
|
|
3
|
+
* @module notifykit/types
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Options for configuring a notification.
|
|
7
|
+
*/
|
|
8
|
+
interface NotifyOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Topic to categorize the notification.
|
|
11
|
+
* Used for filtering and routing notifications.
|
|
12
|
+
*/
|
|
13
|
+
topic?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Unique key to prevent duplicate notifications.
|
|
16
|
+
* If a notification with the same idempotency key was already sent,
|
|
17
|
+
* the duplicate will be ignored.
|
|
18
|
+
*/
|
|
19
|
+
idempotencyKey?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Configuration options for initializing NotifyKit.
|
|
23
|
+
*/
|
|
24
|
+
interface NotifyKitConfig {
|
|
25
|
+
/**
|
|
26
|
+
* Your NotifyKit API key (starts with 'nsk_').
|
|
27
|
+
*/
|
|
28
|
+
apiKey: string;
|
|
29
|
+
/**
|
|
30
|
+
* Custom API base URL.
|
|
31
|
+
* Defaults to 'https://api.notifykit.dev'.
|
|
32
|
+
*/
|
|
33
|
+
baseUrl?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Enable debug logging.
|
|
36
|
+
* @default false
|
|
37
|
+
*/
|
|
38
|
+
debug?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Response from the notify API.
|
|
42
|
+
*/
|
|
43
|
+
interface NotifyResponse {
|
|
44
|
+
/**
|
|
45
|
+
* Unique identifier for the notification event.
|
|
46
|
+
*/
|
|
47
|
+
eventId: string;
|
|
48
|
+
/**
|
|
49
|
+
* Status of the notification request.
|
|
50
|
+
*/
|
|
51
|
+
status: "accepted" | "rejected";
|
|
52
|
+
/**
|
|
53
|
+
* Error message if the notification was rejected.
|
|
54
|
+
*/
|
|
55
|
+
error?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* NotifyKit SDK for JavaScript.
|
|
60
|
+
* Send notifications from your apps with a simple API.
|
|
61
|
+
* @module notifykit
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* NotifyKit SDK client.
|
|
66
|
+
* Initialize once with your API key, then send notifications from anywhere.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import NotifyKit from 'notifykit';
|
|
71
|
+
*
|
|
72
|
+
* // Initialize once at startup
|
|
73
|
+
* NotifyKit.init('nsk_your_api_key');
|
|
74
|
+
*
|
|
75
|
+
* // Send notifications anywhere in your app
|
|
76
|
+
* NotifyKit.notify('User signed up!');
|
|
77
|
+
* NotifyKit.notify('Payment received', { topic: 'payments' });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
declare const NotifyKit: {
|
|
81
|
+
/**
|
|
82
|
+
* Initializes the NotifyKit SDK with your API key.
|
|
83
|
+
* Call this once at application startup.
|
|
84
|
+
*
|
|
85
|
+
* @param apiKeyOrConfig - Your API key string or full configuration object
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* // Simple initialization
|
|
90
|
+
* NotifyKit.init('nsk_your_api_key');
|
|
91
|
+
*
|
|
92
|
+
* // With custom options
|
|
93
|
+
* NotifyKit.init({
|
|
94
|
+
* apiKey: 'nsk_your_api_key',
|
|
95
|
+
* debug: true,
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
init(apiKeyOrConfig: string | NotifyKitConfig): void;
|
|
100
|
+
/**
|
|
101
|
+
* Sends a notification asynchronously.
|
|
102
|
+
* This method is fire-and-forget: it won't block your app or throw errors.
|
|
103
|
+
* Errors are logged to the console but won't crash your application.
|
|
104
|
+
*
|
|
105
|
+
* @param message - The notification message to send
|
|
106
|
+
* @param options - Optional configuration for the notification
|
|
107
|
+
* @returns A promise that resolves when the notification is sent (or fails silently)
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // Simple notification
|
|
112
|
+
* NotifyKit.notify('Hello world!');
|
|
113
|
+
*
|
|
114
|
+
* // With topic for categorization
|
|
115
|
+
* NotifyKit.notify('New order received', { topic: 'orders' });
|
|
116
|
+
*
|
|
117
|
+
* // With idempotency key to prevent duplicates
|
|
118
|
+
* NotifyKit.notify('Welcome!', {
|
|
119
|
+
* topic: 'onboarding',
|
|
120
|
+
* idempotencyKey: `welcome-${userId}`,
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
notify(message: string, options?: NotifyOptions): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Checks if the SDK has been initialized.
|
|
127
|
+
* @returns True if init() has been called with an API key
|
|
128
|
+
*/
|
|
129
|
+
isInitialized(): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Resets the SDK state. Useful for testing.
|
|
132
|
+
*/
|
|
133
|
+
reset(): void;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export { type NotifyKitConfig, type NotifyOptions, type NotifyResponse, NotifyKit as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
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 index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var DEFAULT_BASE_URL = "https://api.notifykit.dev";
|
|
27
|
+
var state = {
|
|
28
|
+
apiKey: null,
|
|
29
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
30
|
+
debug: false,
|
|
31
|
+
initialized: false
|
|
32
|
+
};
|
|
33
|
+
function debugLog(...args) {
|
|
34
|
+
if (state.debug) {
|
|
35
|
+
console.log("[NotifyKit]", ...args);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function errorLog(...args) {
|
|
39
|
+
console.error("[NotifyKit]", ...args);
|
|
40
|
+
}
|
|
41
|
+
var NotifyKit = {
|
|
42
|
+
/**
|
|
43
|
+
* Initializes the NotifyKit SDK with your API key.
|
|
44
|
+
* Call this once at application startup.
|
|
45
|
+
*
|
|
46
|
+
* @param apiKeyOrConfig - Your API key string or full configuration object
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* // Simple initialization
|
|
51
|
+
* NotifyKit.init('nsk_your_api_key');
|
|
52
|
+
*
|
|
53
|
+
* // With custom options
|
|
54
|
+
* NotifyKit.init({
|
|
55
|
+
* apiKey: 'nsk_your_api_key',
|
|
56
|
+
* debug: true,
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
init(apiKeyOrConfig) {
|
|
61
|
+
if (typeof apiKeyOrConfig === "string") {
|
|
62
|
+
state.apiKey = apiKeyOrConfig;
|
|
63
|
+
state.baseUrl = DEFAULT_BASE_URL;
|
|
64
|
+
state.debug = false;
|
|
65
|
+
} else {
|
|
66
|
+
state.apiKey = apiKeyOrConfig.apiKey;
|
|
67
|
+
state.baseUrl = apiKeyOrConfig.baseUrl || DEFAULT_BASE_URL;
|
|
68
|
+
state.debug = apiKeyOrConfig.debug || false;
|
|
69
|
+
}
|
|
70
|
+
state.initialized = true;
|
|
71
|
+
debugLog("Initialized with base URL:", state.baseUrl);
|
|
72
|
+
},
|
|
73
|
+
/**
|
|
74
|
+
* Sends a notification asynchronously.
|
|
75
|
+
* This method is fire-and-forget: it won't block your app or throw errors.
|
|
76
|
+
* Errors are logged to the console but won't crash your application.
|
|
77
|
+
*
|
|
78
|
+
* @param message - The notification message to send
|
|
79
|
+
* @param options - Optional configuration for the notification
|
|
80
|
+
* @returns A promise that resolves when the notification is sent (or fails silently)
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // Simple notification
|
|
85
|
+
* NotifyKit.notify('Hello world!');
|
|
86
|
+
*
|
|
87
|
+
* // With topic for categorization
|
|
88
|
+
* NotifyKit.notify('New order received', { topic: 'orders' });
|
|
89
|
+
*
|
|
90
|
+
* // With idempotency key to prevent duplicates
|
|
91
|
+
* NotifyKit.notify('Welcome!', {
|
|
92
|
+
* topic: 'onboarding',
|
|
93
|
+
* idempotencyKey: `welcome-${userId}`,
|
|
94
|
+
* });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
async notify(message, options) {
|
|
98
|
+
if (!state.initialized || !state.apiKey) {
|
|
99
|
+
errorLog(
|
|
100
|
+
"NotifyKit not initialized. Call NotifyKit.init() with your API key first."
|
|
101
|
+
);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
debugLog("Sending notification:", message, options);
|
|
106
|
+
const headers = {
|
|
107
|
+
"Content-Type": "application/json",
|
|
108
|
+
"X-API-Key": state.apiKey
|
|
109
|
+
};
|
|
110
|
+
if (options?.idempotencyKey) {
|
|
111
|
+
headers["Idempotency-Key"] = options.idempotencyKey;
|
|
112
|
+
}
|
|
113
|
+
const body = {
|
|
114
|
+
message
|
|
115
|
+
};
|
|
116
|
+
if (options?.topic) {
|
|
117
|
+
body.topic = options.topic;
|
|
118
|
+
}
|
|
119
|
+
const response = await fetch(`${state.baseUrl}/v1/notify`, {
|
|
120
|
+
method: "POST",
|
|
121
|
+
headers,
|
|
122
|
+
body: JSON.stringify(body)
|
|
123
|
+
});
|
|
124
|
+
if (!response.ok) {
|
|
125
|
+
const errorData = await response.json().catch(() => ({}));
|
|
126
|
+
errorLog(
|
|
127
|
+
"Failed to send notification:",
|
|
128
|
+
response.status,
|
|
129
|
+
errorData.error || response.statusText
|
|
130
|
+
);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const data = await response.json();
|
|
134
|
+
debugLog("Notification sent successfully:", data.eventId);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
errorLog("Failed to send notification:", error);
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
/**
|
|
140
|
+
* Checks if the SDK has been initialized.
|
|
141
|
+
* @returns True if init() has been called with an API key
|
|
142
|
+
*/
|
|
143
|
+
isInitialized() {
|
|
144
|
+
return state.initialized && state.apiKey !== null;
|
|
145
|
+
},
|
|
146
|
+
/**
|
|
147
|
+
* Resets the SDK state. Useful for testing.
|
|
148
|
+
*/
|
|
149
|
+
reset() {
|
|
150
|
+
state.apiKey = null;
|
|
151
|
+
state.baseUrl = DEFAULT_BASE_URL;
|
|
152
|
+
state.debug = false;
|
|
153
|
+
state.initialized = false;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
var index_default = NotifyKit;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://api.notifykit.dev";
|
|
3
|
+
var state = {
|
|
4
|
+
apiKey: null,
|
|
5
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
6
|
+
debug: false,
|
|
7
|
+
initialized: false
|
|
8
|
+
};
|
|
9
|
+
function debugLog(...args) {
|
|
10
|
+
if (state.debug) {
|
|
11
|
+
console.log("[NotifyKit]", ...args);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function errorLog(...args) {
|
|
15
|
+
console.error("[NotifyKit]", ...args);
|
|
16
|
+
}
|
|
17
|
+
var NotifyKit = {
|
|
18
|
+
/**
|
|
19
|
+
* Initializes the NotifyKit SDK with your API key.
|
|
20
|
+
* Call this once at application startup.
|
|
21
|
+
*
|
|
22
|
+
* @param apiKeyOrConfig - Your API key string or full configuration object
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Simple initialization
|
|
27
|
+
* NotifyKit.init('nsk_your_api_key');
|
|
28
|
+
*
|
|
29
|
+
* // With custom options
|
|
30
|
+
* NotifyKit.init({
|
|
31
|
+
* apiKey: 'nsk_your_api_key',
|
|
32
|
+
* debug: true,
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
init(apiKeyOrConfig) {
|
|
37
|
+
if (typeof apiKeyOrConfig === "string") {
|
|
38
|
+
state.apiKey = apiKeyOrConfig;
|
|
39
|
+
state.baseUrl = DEFAULT_BASE_URL;
|
|
40
|
+
state.debug = false;
|
|
41
|
+
} else {
|
|
42
|
+
state.apiKey = apiKeyOrConfig.apiKey;
|
|
43
|
+
state.baseUrl = apiKeyOrConfig.baseUrl || DEFAULT_BASE_URL;
|
|
44
|
+
state.debug = apiKeyOrConfig.debug || false;
|
|
45
|
+
}
|
|
46
|
+
state.initialized = true;
|
|
47
|
+
debugLog("Initialized with base URL:", state.baseUrl);
|
|
48
|
+
},
|
|
49
|
+
/**
|
|
50
|
+
* Sends a notification asynchronously.
|
|
51
|
+
* This method is fire-and-forget: it won't block your app or throw errors.
|
|
52
|
+
* Errors are logged to the console but won't crash your application.
|
|
53
|
+
*
|
|
54
|
+
* @param message - The notification message to send
|
|
55
|
+
* @param options - Optional configuration for the notification
|
|
56
|
+
* @returns A promise that resolves when the notification is sent (or fails silently)
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* // Simple notification
|
|
61
|
+
* NotifyKit.notify('Hello world!');
|
|
62
|
+
*
|
|
63
|
+
* // With topic for categorization
|
|
64
|
+
* NotifyKit.notify('New order received', { topic: 'orders' });
|
|
65
|
+
*
|
|
66
|
+
* // With idempotency key to prevent duplicates
|
|
67
|
+
* NotifyKit.notify('Welcome!', {
|
|
68
|
+
* topic: 'onboarding',
|
|
69
|
+
* idempotencyKey: `welcome-${userId}`,
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
async notify(message, options) {
|
|
74
|
+
if (!state.initialized || !state.apiKey) {
|
|
75
|
+
errorLog(
|
|
76
|
+
"NotifyKit not initialized. Call NotifyKit.init() with your API key first."
|
|
77
|
+
);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
debugLog("Sending notification:", message, options);
|
|
82
|
+
const headers = {
|
|
83
|
+
"Content-Type": "application/json",
|
|
84
|
+
"X-API-Key": state.apiKey
|
|
85
|
+
};
|
|
86
|
+
if (options?.idempotencyKey) {
|
|
87
|
+
headers["Idempotency-Key"] = options.idempotencyKey;
|
|
88
|
+
}
|
|
89
|
+
const body = {
|
|
90
|
+
message
|
|
91
|
+
};
|
|
92
|
+
if (options?.topic) {
|
|
93
|
+
body.topic = options.topic;
|
|
94
|
+
}
|
|
95
|
+
const response = await fetch(`${state.baseUrl}/v1/notify`, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers,
|
|
98
|
+
body: JSON.stringify(body)
|
|
99
|
+
});
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
const errorData = await response.json().catch(() => ({}));
|
|
102
|
+
errorLog(
|
|
103
|
+
"Failed to send notification:",
|
|
104
|
+
response.status,
|
|
105
|
+
errorData.error || response.statusText
|
|
106
|
+
);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const data = await response.json();
|
|
110
|
+
debugLog("Notification sent successfully:", data.eventId);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
errorLog("Failed to send notification:", error);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
/**
|
|
116
|
+
* Checks if the SDK has been initialized.
|
|
117
|
+
* @returns True if init() has been called with an API key
|
|
118
|
+
*/
|
|
119
|
+
isInitialized() {
|
|
120
|
+
return state.initialized && state.apiKey !== null;
|
|
121
|
+
},
|
|
122
|
+
/**
|
|
123
|
+
* Resets the SDK state. Useful for testing.
|
|
124
|
+
*/
|
|
125
|
+
reset() {
|
|
126
|
+
state.apiKey = null;
|
|
127
|
+
state.baseUrl = DEFAULT_BASE_URL;
|
|
128
|
+
state.debug = false;
|
|
129
|
+
state.initialized = false;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
var index_default = NotifyKit;
|
|
133
|
+
export {
|
|
134
|
+
index_default as default
|
|
135
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "notifykitdev",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official NotifyKit SDK for JavaScript - Send notifications from your apps",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
25
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"notifications",
|
|
31
|
+
"notifykit",
|
|
32
|
+
"push",
|
|
33
|
+
"telegram",
|
|
34
|
+
"alerts",
|
|
35
|
+
"sdk"
|
|
36
|
+
],
|
|
37
|
+
"author": "NotifyKit",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/notifykit/notifykit-js"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://notifykit.dev",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"tsup": "^8.0.0",
|
|
49
|
+
"typescript": "^5.3.0"
|
|
50
|
+
}
|
|
51
|
+
}
|