arifa-client 1.0.7 → 1.0.9
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/dist/arifa-client.esm.js +127 -0
- package/dist/{ArifaClient.js → arifa-client.iife.js} +40 -56
- package/dist/{ArifaClient.d.ts → types/ArifaClient.d.ts} +1 -0
- package/dist/types/ArifaClient.d.ts.map +1 -0
- package/dist/{arifa.d.ts → types/arifa.d.ts} +1 -0
- package/dist/types/arifa.d.ts.map +1 -0
- package/package.json +11 -12
- package/dist/arifa.js +0 -19
- package/dist/arifa.min.js +0 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
class ArifaClient {
|
|
2
|
+
constructor({ apiKey, client, wsUrl, apiEndpoint }) {
|
|
3
|
+
this.ws = null;
|
|
4
|
+
this.isConnected = false;
|
|
5
|
+
this.reconnectAttempts = 0;
|
|
6
|
+
this.reconnectTimer = null;
|
|
7
|
+
this.recipient = null;
|
|
8
|
+
this.listeners = [];
|
|
9
|
+
this.connectionListeners = [];
|
|
10
|
+
this.apiKey = apiKey;
|
|
11
|
+
this.client = client;
|
|
12
|
+
this.wsUrl = wsUrl || "wss://notifications.arifa.dev/ws";
|
|
13
|
+
this.apiEndpoint = apiEndpoint || "https://notifications.arifa.dev/notify";
|
|
14
|
+
}
|
|
15
|
+
safeParse(input) {
|
|
16
|
+
try {
|
|
17
|
+
if (typeof input === "object")
|
|
18
|
+
return input;
|
|
19
|
+
let parsed = JSON.parse(input);
|
|
20
|
+
if (typeof parsed === "string")
|
|
21
|
+
parsed = JSON.parse(parsed);
|
|
22
|
+
return parsed;
|
|
23
|
+
}
|
|
24
|
+
catch (_a) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Subscribe once, add many listeners */
|
|
29
|
+
subscribe(recipient) {
|
|
30
|
+
if (!this.recipient) {
|
|
31
|
+
this.recipient = recipient;
|
|
32
|
+
this.connect();
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
listen: (callback) => {
|
|
36
|
+
this.listeners.push(callback);
|
|
37
|
+
},
|
|
38
|
+
unsubscribe: () => {
|
|
39
|
+
this.listeners = [];
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** Listen to connection state changes */
|
|
44
|
+
onConnectionChange(callback) {
|
|
45
|
+
this.connectionListeners.push(callback);
|
|
46
|
+
}
|
|
47
|
+
emitConnection(state) {
|
|
48
|
+
this.connectionListeners.forEach((cb) => cb(state));
|
|
49
|
+
}
|
|
50
|
+
/** Connect WebSocket */
|
|
51
|
+
connect() {
|
|
52
|
+
if (!this.recipient)
|
|
53
|
+
return;
|
|
54
|
+
if (this.ws && this.ws.readyState !== WebSocket.CLOSED)
|
|
55
|
+
return;
|
|
56
|
+
this.ws = new WebSocket(`${this.wsUrl}/connect?api_key=${this.apiKey}&recipient=${this.recipient}&client=${this.client}`);
|
|
57
|
+
this.ws.onopen = () => {
|
|
58
|
+
this.isConnected = true;
|
|
59
|
+
this.reconnectAttempts = 0;
|
|
60
|
+
this.emitConnection("connected");
|
|
61
|
+
};
|
|
62
|
+
this.ws.onmessage = (event) => {
|
|
63
|
+
const parsed = this.safeParse(event.data);
|
|
64
|
+
if (!parsed)
|
|
65
|
+
return;
|
|
66
|
+
this.listeners.forEach((fn) => fn(parsed));
|
|
67
|
+
};
|
|
68
|
+
this.ws.onclose = (event) => {
|
|
69
|
+
this.isConnected = false;
|
|
70
|
+
this.emitConnection("disconnected");
|
|
71
|
+
if (event.code === 4001 || event.code === 4003 || !navigator.onLine)
|
|
72
|
+
return;
|
|
73
|
+
const timeout = Math.min(30000, 2000 * 2 ** this.reconnectAttempts);
|
|
74
|
+
this.reconnectAttempts++;
|
|
75
|
+
this.reconnectTimer = window.setTimeout(() => this.connect(), timeout);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Send notification */
|
|
79
|
+
async notify({ recipient, payload, client, origin, }) {
|
|
80
|
+
if (!recipient || !payload) {
|
|
81
|
+
throw new Error("recipient and payload are required");
|
|
82
|
+
}
|
|
83
|
+
const body = {
|
|
84
|
+
recipient,
|
|
85
|
+
payload,
|
|
86
|
+
api_key: this.apiKey,
|
|
87
|
+
client: client || this.client,
|
|
88
|
+
};
|
|
89
|
+
const res = await fetch(this.apiEndpoint, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: {
|
|
92
|
+
"Content-Type": "application/json",
|
|
93
|
+
Origin: origin || window.location.origin,
|
|
94
|
+
},
|
|
95
|
+
body: JSON.stringify(body),
|
|
96
|
+
});
|
|
97
|
+
let json;
|
|
98
|
+
try {
|
|
99
|
+
json = await res.json();
|
|
100
|
+
}
|
|
101
|
+
catch (_a) {
|
|
102
|
+
throw new Error("Invalid server response");
|
|
103
|
+
}
|
|
104
|
+
if (!res.ok) {
|
|
105
|
+
throw new Error(json.message || "Notification request failed");
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
success: json.success,
|
|
109
|
+
message: json.message,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** Disconnect WS */
|
|
113
|
+
disconnect() {
|
|
114
|
+
var _a;
|
|
115
|
+
if (this.reconnectTimer)
|
|
116
|
+
clearTimeout(this.reconnectTimer);
|
|
117
|
+
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
|
|
118
|
+
this.isConnected = false;
|
|
119
|
+
this.emitConnection("disconnected");
|
|
120
|
+
}
|
|
121
|
+
/** Return connection state */
|
|
122
|
+
connected() {
|
|
123
|
+
return this.isConnected;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export { ArifaClient };
|
|
@@ -1,24 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
(function (factory) {
|
|
11
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
12
|
-
var v = factory(require, exports);
|
|
13
|
-
if (v !== undefined) module.exports = v;
|
|
14
|
-
}
|
|
15
|
-
else if (typeof define === "function" && define.amd) {
|
|
16
|
-
define(["require", "exports"], factory);
|
|
17
|
-
}
|
|
18
|
-
})(function (require, exports) {
|
|
19
|
-
"use strict";
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.ArifaClient = void 0;
|
|
1
|
+
var ArifaSDK = (function (exports) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
22
4
|
class ArifaClient {
|
|
23
5
|
constructor({ apiKey, client, wsUrl, apiEndpoint }) {
|
|
24
6
|
this.ws = null;
|
|
@@ -91,46 +73,44 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
91
73
|
this.emitConnection("disconnected");
|
|
92
74
|
if (event.code === 4001 || event.code === 4003 || !navigator.onLine)
|
|
93
75
|
return;
|
|
94
|
-
const timeout = Math.min(30000, 2000 *
|
|
76
|
+
const timeout = Math.min(30000, 2000 * 2 ** this.reconnectAttempts);
|
|
95
77
|
this.reconnectAttempts++;
|
|
96
78
|
this.reconnectTimer = window.setTimeout(() => this.connect(), timeout);
|
|
97
79
|
};
|
|
98
80
|
}
|
|
99
81
|
/** Send notification */
|
|
100
|
-
notify(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
body: JSON.stringify(body),
|
|
118
|
-
});
|
|
119
|
-
let json;
|
|
120
|
-
try {
|
|
121
|
-
json = yield res.json();
|
|
122
|
-
}
|
|
123
|
-
catch (_b) {
|
|
124
|
-
throw new Error("Invalid server response");
|
|
125
|
-
}
|
|
126
|
-
if (!res.ok) {
|
|
127
|
-
throw new Error(json.message || "Notification request failed");
|
|
128
|
-
}
|
|
129
|
-
return {
|
|
130
|
-
success: json.success,
|
|
131
|
-
message: json.message,
|
|
132
|
-
};
|
|
82
|
+
async notify({ recipient, payload, client, origin, }) {
|
|
83
|
+
if (!recipient || !payload) {
|
|
84
|
+
throw new Error("recipient and payload are required");
|
|
85
|
+
}
|
|
86
|
+
const body = {
|
|
87
|
+
recipient,
|
|
88
|
+
payload,
|
|
89
|
+
api_key: this.apiKey,
|
|
90
|
+
client: client || this.client,
|
|
91
|
+
};
|
|
92
|
+
const res = await fetch(this.apiEndpoint, {
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: {
|
|
95
|
+
"Content-Type": "application/json",
|
|
96
|
+
Origin: origin || window.location.origin,
|
|
97
|
+
},
|
|
98
|
+
body: JSON.stringify(body),
|
|
133
99
|
});
|
|
100
|
+
let json;
|
|
101
|
+
try {
|
|
102
|
+
json = await res.json();
|
|
103
|
+
}
|
|
104
|
+
catch (_a) {
|
|
105
|
+
throw new Error("Invalid server response");
|
|
106
|
+
}
|
|
107
|
+
if (!res.ok) {
|
|
108
|
+
throw new Error(json.message || "Notification request failed");
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
success: json.success,
|
|
112
|
+
message: json.message,
|
|
113
|
+
};
|
|
134
114
|
}
|
|
135
115
|
/** Disconnect WS */
|
|
136
116
|
disconnect() {
|
|
@@ -146,5 +126,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
146
126
|
return this.isConnected;
|
|
147
127
|
}
|
|
148
128
|
}
|
|
129
|
+
|
|
149
130
|
exports.ArifaClient = ArifaClient;
|
|
150
|
-
|
|
131
|
+
|
|
132
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jB,yCAAyC;IACzC,SAAS,CAAC,SAAS,EAAE,MAAM;2BAOJ,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI;;;IASlD,yCAAyC;IACzC,kBAAkB,CAAC,QAAQ,EAAE,kBAAkB;IAI/C,OAAO,CAAC,cAAc;IAItB,wBAAwB;IACxB,OAAO,CAAC,OAAO;IAmCf,wBAAwB;IAClB,MAAM,CAAC,EACX,SAAS,EACT,OAAO,EACP,MAAM,EACN,MAAM,GACP,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC;IAuC1C,oBAAoB;IACpB,UAAU;IAOV,8BAA8B;IAC9B,SAAS;CAGV"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arifa.d.ts","sourceRoot":"","sources":["../../src/arifa.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arifa-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "JavaScript/TypeScript client SDK for Arifa Realtime Notification Service",
|
|
5
|
-
"main": "dist/arifa.js",
|
|
6
|
-
"module": "dist/arifa.js",
|
|
7
|
-
"
|
|
8
|
-
"unpkg": "dist/arifa.min.js",
|
|
9
|
-
"types": "dist/arifa.d.ts",
|
|
5
|
+
"main": "dist/arifa-client.iife.js",
|
|
6
|
+
"module": "dist/arifa-client.esm.js",
|
|
7
|
+
"types": "dist/types/arifa.d.ts",
|
|
10
8
|
"files": [
|
|
11
9
|
"dist"
|
|
12
10
|
],
|
|
13
11
|
"scripts": {
|
|
14
|
-
"build": "
|
|
15
|
-
"
|
|
16
|
-
"build:all": "npm run build && npm run build:minify",
|
|
17
|
-
"prepublishOnly": "npm run build:all"
|
|
12
|
+
"build": "rollup -c",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
18
14
|
},
|
|
19
15
|
"keywords": [
|
|
20
16
|
"arifa",
|
|
@@ -26,7 +22,10 @@
|
|
|
26
22
|
"author": "Peter Nyando",
|
|
27
23
|
"license": "MIT",
|
|
28
24
|
"devDependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
25
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
26
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
27
|
+
"rollup": "^4.54.0",
|
|
28
|
+
"tslib": "^2.8.1",
|
|
29
|
+
"typescript": "^5.9.3"
|
|
31
30
|
}
|
|
32
31
|
}
|
package/dist/arifa.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
(function (factory) {
|
|
2
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
-
var v = factory(require, exports);
|
|
4
|
-
if (v !== undefined) module.exports = v;
|
|
5
|
-
}
|
|
6
|
-
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "./ArifaClient"], factory);
|
|
8
|
-
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ArifaClient = void 0;
|
|
13
|
-
const ArifaClient_1 = require("./ArifaClient");
|
|
14
|
-
Object.defineProperty(exports, "ArifaClient", { enumerable: true, get: function () { return ArifaClient_1.ArifaClient; } });
|
|
15
|
-
// For script tag usage
|
|
16
|
-
if (typeof window !== "undefined") {
|
|
17
|
-
window.ArifaClient = ArifaClient_1.ArifaClient;
|
|
18
|
-
}
|
|
19
|
-
});
|
package/dist/arifa.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(e=>{"object"==typeof module&&"object"==typeof module.exports?e(require,exports):"function"==typeof define&&define.amd&&define(["require","exports","./ArifaClient"],e)})(function(e,i){Object.defineProperty(i,"__esModule",{value:!0}),i.ArifaClient=void 0;let t=e("./ArifaClient");Object.defineProperty(i,"ArifaClient",{enumerable:!0,get:function(){return t.ArifaClient}}),"undefined"!=typeof window&&(window.ArifaClient=t.ArifaClient)});
|