arifa-client 1.0.1 → 1.0.3
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 +139 -2
- package/dist/ArifaClient.d.ts +5 -1
- package/dist/ArifaClient.js +15 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,9 +3,146 @@
|
|
|
3
3
|
JavaScript/TypeScript client SDK for **Arifa Realtime Notification Service**.
|
|
4
4
|
Allows you to connect via WebSocket, listen to events, and send notifications using HTTP.
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
## Installation
|
|
8
7
|
|
|
9
8
|
```bash
|
|
10
9
|
npm install arifa-client
|
|
11
|
-
```
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
or using Yarn:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
yarn add arifa-client
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
#### Import and Initialize
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { ArifaClient } from "arifa-client";
|
|
24
|
+
|
|
25
|
+
const client = new ArifaClient({
|
|
26
|
+
apiKey: "arifa_test:YOUR_API_KEY",
|
|
27
|
+
client: "web", // "web" or "mobile"
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### Listen to Connection State
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
client.onConnectionChange((state) => {
|
|
35
|
+
console.log("Connection state:", state); // "connected" or "disconnected"
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Subscribe Recipient
|
|
40
|
+
|
|
41
|
+
Note: `Recipient must be UUID`
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const sub = client.subscribe("RECIPIENT_UUID");
|
|
45
|
+
|
|
46
|
+
// Listen for events
|
|
47
|
+
sub.listen((event) => {
|
|
48
|
+
console.log("Received event:", event);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Unsubscribe when done
|
|
52
|
+
sub.unsubscribe();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Send a Event/Notification/Data
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const res = await client.notify({
|
|
59
|
+
recipient: "RECIPIENT_UUID",
|
|
60
|
+
payload: { title: "Hello", message: "Welcome to Arifa!" }, // Can be any JSON data
|
|
61
|
+
});
|
|
62
|
+
console.log(res);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`recipient` → UUID of the user
|
|
66
|
+
`payload`→ Any JSON object
|
|
67
|
+
`client` → Optional, "web" or "mobile"
|
|
68
|
+
|
|
69
|
+
**Note**: The notify method returns a result object with the notification status.
|
|
70
|
+
Example responses:
|
|
71
|
+
|
|
72
|
+
- "sent" → Notification successfully sent to a connected user
|
|
73
|
+
|
|
74
|
+
- "User offline, notification saved" → User is offline; the message was stored for later delivery
|
|
75
|
+
|
|
76
|
+
- "Failed to send/save notification" → There was an error sending or saving the notification
|
|
77
|
+
|
|
78
|
+
#### Disconnect
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
client.disconnect();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### Check Connection Status
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
if (client.connected()) {
|
|
88
|
+
console.log("WebSocket is connected");
|
|
89
|
+
} else {
|
|
90
|
+
console.log("WebSocket is disconnected");
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## ## ArifaClient Usage Example
|
|
95
|
+
|
|
96
|
+
```jsx
|
|
97
|
+
import { ArifaClient } from "arifa-client";
|
|
98
|
+
|
|
99
|
+
// Initialize client
|
|
100
|
+
const client = new ArifaClient({
|
|
101
|
+
apiKey: "arifa_test:YOUR_API_KEY",
|
|
102
|
+
client: "web", // "web" or "mobile"
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Listen to connection state
|
|
106
|
+
client.onConnectionChange((state) => {
|
|
107
|
+
console.log("Connection state:", state); // "connected" or "disconnected"
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Subscribe to a recipient (UUID required)
|
|
111
|
+
const sub = client.subscribe("RECIPIENT_UUID");
|
|
112
|
+
|
|
113
|
+
// Listen for incoming events
|
|
114
|
+
sub.listen((event) => {
|
|
115
|
+
console.log("Received event:", event);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Example: Send a notification/event
|
|
119
|
+
const sendNotification = async () => {
|
|
120
|
+
const res = await client.notify({
|
|
121
|
+
recipient: "RECIPIENT_UUID",
|
|
122
|
+
payload: { title: "Hello", message: "Welcome to Arifa!" }, // Any JSON data
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log(res);
|
|
126
|
+
/*
|
|
127
|
+
Possible responses:
|
|
128
|
+
"sent" → Notification successfully sent to a connected user
|
|
129
|
+
"User offline, notification saved" → User offline; message stored for later delivery
|
|
130
|
+
"Failed to send/save notification" → Error sending or saving notification
|
|
131
|
+
*/
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
sendNotification();
|
|
135
|
+
|
|
136
|
+
// Unsubscribe when done
|
|
137
|
+
// sub.unsubscribe();
|
|
138
|
+
|
|
139
|
+
// Disconnect client when done
|
|
140
|
+
// client.disconnect();
|
|
141
|
+
|
|
142
|
+
// Check connection status
|
|
143
|
+
if (client.connected()) {
|
|
144
|
+
console.log("WebSocket is connected");
|
|
145
|
+
} else {
|
|
146
|
+
console.log("WebSocket is disconnected");
|
|
147
|
+
}
|
|
148
|
+
```
|
package/dist/ArifaClient.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ interface NotifyPayload {
|
|
|
11
11
|
payload: Record<string, any>;
|
|
12
12
|
client?: "web" | "mobile";
|
|
13
13
|
}
|
|
14
|
+
interface NotifyResponse {
|
|
15
|
+
success: boolean;
|
|
16
|
+
message: string;
|
|
17
|
+
}
|
|
14
18
|
type ConnectionCallback = (state: ConnectionState) => void;
|
|
15
19
|
export declare class ArifaClient {
|
|
16
20
|
private apiKey;
|
|
@@ -37,7 +41,7 @@ export declare class ArifaClient {
|
|
|
37
41
|
/** Connect WebSocket */
|
|
38
42
|
private connect;
|
|
39
43
|
/** Send notification */
|
|
40
|
-
notify({ recipient, payload, client }: NotifyPayload): Promise<
|
|
44
|
+
notify({ recipient, payload, client, }: NotifyPayload): Promise<NotifyResponse>;
|
|
41
45
|
/** Disconnect WS */
|
|
42
46
|
disconnect(): void;
|
|
43
47
|
/** Return connection state */
|
package/dist/ArifaClient.js
CHANGED
|
@@ -80,9 +80,10 @@ export class ArifaClient {
|
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
/** Send notification */
|
|
83
|
-
async notify({ recipient, payload, client }) {
|
|
84
|
-
if (!recipient || !payload)
|
|
83
|
+
async notify({ recipient, payload, client, }) {
|
|
84
|
+
if (!recipient || !payload) {
|
|
85
85
|
throw new Error("recipient and payload are required");
|
|
86
|
+
}
|
|
86
87
|
const body = {
|
|
87
88
|
recipient,
|
|
88
89
|
payload,
|
|
@@ -97,11 +98,20 @@ export class ArifaClient {
|
|
|
97
98
|
},
|
|
98
99
|
body: JSON.stringify(body),
|
|
99
100
|
});
|
|
101
|
+
let json;
|
|
102
|
+
try {
|
|
103
|
+
json = await res.json();
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
throw new Error("Invalid server response");
|
|
107
|
+
}
|
|
100
108
|
if (!res.ok) {
|
|
101
|
-
|
|
102
|
-
throw new Error(`Failed to send notification: ${res.status} ${res.statusText} ${text}`);
|
|
109
|
+
throw new Error(json.message || "Notification request failed");
|
|
103
110
|
}
|
|
104
|
-
return
|
|
111
|
+
return {
|
|
112
|
+
success: json.success,
|
|
113
|
+
message: json.message,
|
|
114
|
+
};
|
|
105
115
|
}
|
|
106
116
|
/** Disconnect WS */
|
|
107
117
|
disconnect() {
|