@emmanuel-nike/ark-notify-js 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ark Notify
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,295 @@
1
+ # ark-notify-js
2
+
3
+ JavaScript SDK for [Ark Notify](https://github.com/ark-notify/ark-notify) — real-time pub/sub, presence, SSE streaming, and platform management.
4
+
5
+ - **Core API** (`ark-notify-js`) — imperative client, WebSocket, and SSE classes for any JavaScript environment
6
+ - **React bindings** (`ark-notify-js/react`) — hooks and provider for React 18+ applications
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm install ark-notify-js
12
+ ```
13
+
14
+ For React apps, `react` 18+ is a peer dependency.
15
+
16
+ ## Quick start (React)
17
+
18
+ Wrap your app with the provider, connect a WebSocket client, and subscribe to a channel:
19
+
20
+ ```tsx
21
+ import { ArkNotifyProvider, useConnection, useChannel } from 'ark-notify-js/react'
22
+
23
+ function App() {
24
+ return (
25
+ <ArkNotifyProvider baseUrl="http://localhost:3000">
26
+ <Chat />
27
+ </ArkNotifyProvider>
28
+ )
29
+ }
30
+
31
+ function Chat() {
32
+ const { connection, state } = useConnection({
33
+ appKey: 'app_abc',
34
+ clientId: 'user-42',
35
+ // Recommended: fetch a connection token from your backend
36
+ // token: () => fetchTokenFromYourApi(),
37
+ })
38
+
39
+ const { publish, bind } = useChannel(connection, 'room-1', {
40
+ onEvent: (event, data) => console.log(event, data),
41
+ })
42
+
43
+ return (
44
+ <div>
45
+ <p>Status: {state}</p>
46
+ <button onClick={() => publish('message', { text: 'Hello!' })}>
47
+ Send
48
+ </button>
49
+ </div>
50
+ )
51
+ }
52
+ ```
53
+
54
+ ## Architecture
55
+
56
+ Ark Notify has two API planes:
57
+
58
+ | Plane | Purpose | Auth |
59
+ |-------|---------|------|
60
+ | **Control** | Login, manage applications | JWT (`Authorization: Bearer`) |
61
+ | **Data** | WebSocket/SSE clients, server-side publish | `clientId` / connection token (clients) or app key + secret (servers) |
62
+
63
+ **Never expose your app `secret` in browser code.** Issue connection tokens and private-channel auth from your backend.
64
+
65
+ ## Provider
66
+
67
+ ```tsx
68
+ import { ArkNotifyProvider } from 'ark-notify-js/react'
69
+
70
+ <ArkNotifyProvider
71
+ baseUrl="https://notify.example.com"
72
+ token={platformJwt} // optional — for admin dashboards
73
+ >
74
+ {children}
75
+ </ArkNotifyProvider>
76
+ ```
77
+
78
+ ## Platform auth (control plane)
79
+
80
+ For admin dashboards that manage applications:
81
+
82
+ ```tsx
83
+ import { usePlatformAuth, useApplications } from 'ark-notify-js/react'
84
+
85
+ function Dashboard() {
86
+ const { user, login, logout, isAuthenticated } = usePlatformAuth()
87
+ const { apps, create, remove } = useApplications()
88
+
89
+ if (!isAuthenticated) {
90
+ return (
91
+ <button onClick={() => login({ email: '...', password: '...' })}>
92
+ Log in
93
+ </button>
94
+ )
95
+ }
96
+
97
+ return (
98
+ <div>
99
+ <p>Hello, {user?.firstName}</p>
100
+ <button onClick={() => create({ name: 'My App' })}>New app</button>
101
+ {apps.map((app) => (
102
+ <div key={app.id}>{app.name} — {app.appKey}</div>
103
+ ))}
104
+ </div>
105
+ )
106
+ }
107
+ ```
108
+
109
+ ## WebSocket connection
110
+
111
+ ### `useConnection`
112
+
113
+ ```tsx
114
+ import { useConnection } from 'ark-notify-js/react'
115
+
116
+ const {
117
+ connection,
118
+ state, // 'disconnected' | 'connecting' | 'connected' | 'reconnecting' | 'failed'
119
+ connectionId,
120
+ clientId,
121
+ authenticated,
122
+ connect,
123
+ disconnect,
124
+ } = useConnection({
125
+ appKey: 'app_abc',
126
+ clientId: 'user-42', // when requireClientAuth is false
127
+ token: 'app_abc.payload.sig', // recommended — from your backend
128
+ autoReconnect: true,
129
+ onPrivateChannelAuth: async (channel, connectionId) => {
130
+ const res = await fetch('/api/channel-auth', {
131
+ method: 'POST',
132
+ body: JSON.stringify({ channel, connectionId }),
133
+ })
134
+ const { auth } = await res.json()
135
+ return auth
136
+ },
137
+ })
138
+ ```
139
+
140
+ ### `useChannel`
141
+
142
+ Auto-subscribes on mount and unsubscribes on unmount:
143
+
144
+ ```tsx
145
+ import { useChannel } from 'ark-notify-js/react'
146
+
147
+ const { subscribed, publish, unsubscribe } = useChannel(connection, 'room-1', {
148
+ history: true,
149
+ presence: true,
150
+ presence_data: { name: 'Alice' },
151
+ })
152
+
153
+ publish('typing', { typing: true })
154
+ ```
155
+
156
+ ### `usePresence`
157
+
158
+ Track channel presence members:
159
+
160
+ ```tsx
161
+ import { usePresence } from 'ark-notify-js/react'
162
+
163
+ const { members, update, leave } = usePresence(connection, 'room-1', {
164
+ initialData: { status: 'online' },
165
+ })
166
+
167
+ // members: [{ clientId, connectionId, data, updatedAt }, ...]
168
+ update({ status: 'away' })
169
+ ```
170
+
171
+ ## SSE (subscribe-only)
172
+
173
+ For read-only event streams without WebSocket:
174
+
175
+ ```tsx
176
+ import { useSSE } from 'ark-notify-js/react'
177
+
178
+ const { connected, bind } = useSSE({
179
+ appKey: 'app_abc',
180
+ channels: ['room-1', 'room-2'],
181
+ clientId: 'user-42',
182
+ history: true,
183
+ onEvent: (event, data) => console.log(event, data),
184
+ })
185
+ ```
186
+
187
+ ## Imperative API
188
+
189
+ Use core classes directly outside React or in server code:
190
+
191
+ ```ts
192
+ import {
193
+ ArkNotifyClient,
194
+ ArkNotifyConnection,
195
+ ArkNotifySSE,
196
+ fetchConnectionToken,
197
+ } from 'ark-notify-js'
198
+
199
+ // REST client
200
+ const client = new ArkNotifyClient({ baseUrl: 'http://localhost:3000' })
201
+ await client.login({ email, password })
202
+ const { app } = await client.createApplication({ name: 'My App' })
203
+
204
+ // Server-side publish (use app credentials — never in browser)
205
+ await client.publishEvent(app.appKey, { appKey, secret }, {
206
+ channel: 'room-1',
207
+ event: 'order.created',
208
+ data: { id: 123 },
209
+ })
210
+
211
+ // Fetch a connection token (server-side)
212
+ const { token } = await fetchConnectionToken({
213
+ baseUrl: 'http://localhost:3000',
214
+ appKey: app.appKey,
215
+ credentials: { appKey: app.appKey, secret: app.secret! },
216
+ client_id: 'user-42',
217
+ user_data: { name: 'Alice' },
218
+ })
219
+
220
+ // WebSocket — pass token directly
221
+ const conn = new ArkNotifyConnection({
222
+ baseUrl: 'http://localhost:3000',
223
+ appKey: 'app_abc',
224
+ token,
225
+ })
226
+ await conn.connect()
227
+
228
+ // WebSocket — auto-fetch token when credentials + clientId are provided (server-side)
229
+ const autoConn = new ArkNotifyConnection({
230
+ baseUrl: 'http://localhost:3000',
231
+ appKey: app.appKey,
232
+ clientId: 'user-42',
233
+ credentials: { appKey: app.appKey, secret: app.secret! },
234
+ user_data: { name: 'Alice' },
235
+ })
236
+ await autoConn.connect()
237
+
238
+ autoConn.on('event', (msg) => console.log(msg))
239
+ await autoConn.subscribe('private-room-1', { auth: 'app_abc:...' })
240
+ autoConn.publish('room-1', 'message', { text: 'hi' })
241
+ ```
242
+
243
+ When `token` is omitted, `ArkNotifyConnection` automatically calls `POST /api/v1/apps/:appKey/connection-token` if both `clientId` and `credentials` are set. On reconnect, a fresh token is fetched.
244
+
245
+ ## System admin
246
+
247
+ ```tsx
248
+ import { useAdminChannels } from 'ark-notify-js/react'
249
+
250
+ const { data, loading, refresh } = useAdminChannels()
251
+ // Requires SYSTEM_ADMIN JWT via ArkNotifyProvider token
252
+ ```
253
+
254
+ ## API coverage
255
+
256
+ | Feature | Hook / Class | Method |
257
+ |---------|--------------|--------|
258
+ | Health | `ArkNotifyClient` | `.health()` |
259
+ | Login / me | `usePlatformAuth`, `ArkNotifyClient` | `.login()`, `.me()` |
260
+ | Application CRUD | `useApplications`, `ArkNotifyClient` | `.listApplications()`, `.createApplication()`, … |
261
+ | Regenerate secret | `useApplications` | `.regenerateSecret()` |
262
+ | Admin channels | `useAdminChannels` | `.adminChannels()` |
263
+ | Publish (server) | `ArkNotifyClient` | `.publishEvent()` |
264
+ | Channel auth (server) | `ArkNotifyClient` | `.authorizeChannel()` |
265
+ | Connection token (server) | `ArkNotifyClient`, `fetchConnectionToken` | `.issueConnectionToken()`, `fetchConnectionToken()` |
266
+ | WebSocket connect | `useConnection`, `ArkNotifyConnection` | `.connect()` |
267
+ | Subscribe / unsubscribe | `useChannel`, `ArkNotifyConnection` | `.subscribe()`, `.unsubscribe()` |
268
+ | Publish (client) | `useChannel`, `ArkNotifyConnection` | `.publish()` |
269
+ | Presence | `usePresence`, `ArkNotifyConnection` | `.presenceEnter()`, `.presenceUpdate()`, … |
270
+ | SSE stream | `useSSE`, `ArkNotifySSE` | `.connect()` |
271
+ | Private channels | `onPrivateChannelAuth` callback | — |
272
+ | Auto-reconnect | `useConnection` | `autoReconnect: true` |
273
+ | Heartbeat | `ArkNotifyConnection` | Server ping auto-replied |
274
+
275
+ ## Error handling
276
+
277
+ All REST errors throw `ArkNotifyError` with `status`, `code`, and `message`:
278
+
279
+ ```ts
280
+ import { ArkNotifyError } from 'ark-notify-js'
281
+
282
+ try {
283
+ await client.login({ email, password })
284
+ } catch (err) {
285
+ if (err instanceof ArkNotifyError) {
286
+ console.log(err.code, err.status, err.retryAfterSec)
287
+ }
288
+ }
289
+ ```
290
+
291
+ WebSocket errors are emitted via `connection.on('error', …)` or the `useConnection` state.
292
+
293
+ ## License
294
+
295
+ MIT