arifa-client 1.0.6 → 1.0.8

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.
@@ -1,128 +1,150 @@
1
- export class ArifaClient {
2
- apiKey;
3
- client;
4
- wsUrl;
5
- apiEndpoint;
6
- ws = null;
7
- isConnected = false;
8
- reconnectAttempts = 0;
9
- reconnectTimer = null;
10
- recipient = null;
11
- listeners = [];
12
- connectionListeners = [];
13
- constructor({ apiKey, client, wsUrl, apiEndpoint }) {
14
- this.apiKey = apiKey;
15
- this.client = client;
16
- this.wsUrl = wsUrl || "wss://notifications.arifa.dev/ws";
17
- this.apiEndpoint = apiEndpoint || "https://notifications.arifa.dev/notify";
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
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;
18
14
  }
19
- safeParse(input) {
20
- try {
21
- if (typeof input === "object")
22
- return input;
23
- let parsed = JSON.parse(input);
24
- if (typeof parsed === "string")
25
- parsed = JSON.parse(parsed);
26
- return parsed;
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;
22
+ class ArifaClient {
23
+ constructor({ apiKey, client, wsUrl, apiEndpoint }) {
24
+ this.ws = null;
25
+ this.isConnected = false;
26
+ this.reconnectAttempts = 0;
27
+ this.reconnectTimer = null;
28
+ this.recipient = null;
29
+ this.listeners = [];
30
+ this.connectionListeners = [];
31
+ this.apiKey = apiKey;
32
+ this.client = client;
33
+ this.wsUrl = wsUrl || "wss://notifications.arifa.dev/ws";
34
+ this.apiEndpoint = apiEndpoint || "https://notifications.arifa.dev/notify";
27
35
  }
28
- catch {
29
- return null;
36
+ safeParse(input) {
37
+ try {
38
+ if (typeof input === "object")
39
+ return input;
40
+ let parsed = JSON.parse(input);
41
+ if (typeof parsed === "string")
42
+ parsed = JSON.parse(parsed);
43
+ return parsed;
44
+ }
45
+ catch (_a) {
46
+ return null;
47
+ }
30
48
  }
31
- }
32
- /** Subscribe once, add many listeners */
33
- subscribe(recipient) {
34
- if (!this.recipient) {
35
- this.recipient = recipient;
36
- this.connect();
49
+ /** Subscribe once, add many listeners */
50
+ subscribe(recipient) {
51
+ if (!this.recipient) {
52
+ this.recipient = recipient;
53
+ this.connect();
54
+ }
55
+ return {
56
+ listen: (callback) => {
57
+ this.listeners.push(callback);
58
+ },
59
+ unsubscribe: () => {
60
+ this.listeners = [];
61
+ },
62
+ };
37
63
  }
38
- return {
39
- listen: (callback) => {
40
- this.listeners.push(callback);
41
- },
42
- unsubscribe: () => {
43
- this.listeners = [];
44
- },
45
- };
46
- }
47
- /** Listen to connection state changes */
48
- onConnectionChange(callback) {
49
- this.connectionListeners.push(callback);
50
- }
51
- emitConnection(state) {
52
- this.connectionListeners.forEach((cb) => cb(state));
53
- }
54
- /** Connect WebSocket */
55
- connect() {
56
- if (!this.recipient)
57
- return;
58
- if (this.ws && this.ws.readyState !== WebSocket.CLOSED)
59
- return;
60
- this.ws = new WebSocket(`${this.wsUrl}/connect?api_key=${this.apiKey}&recipient=${this.recipient}&client=${this.client}`);
61
- this.ws.onopen = () => {
62
- this.isConnected = true;
63
- this.reconnectAttempts = 0;
64
- this.emitConnection("connected");
65
- };
66
- this.ws.onmessage = (event) => {
67
- const parsed = this.safeParse(event.data);
68
- if (!parsed)
64
+ /** Listen to connection state changes */
65
+ onConnectionChange(callback) {
66
+ this.connectionListeners.push(callback);
67
+ }
68
+ emitConnection(state) {
69
+ this.connectionListeners.forEach((cb) => cb(state));
70
+ }
71
+ /** Connect WebSocket */
72
+ connect() {
73
+ if (!this.recipient)
69
74
  return;
70
- this.listeners.forEach((fn) => fn(parsed));
71
- };
72
- this.ws.onclose = (event) => {
73
- this.isConnected = false;
74
- this.emitConnection("disconnected");
75
- if ([4001, 4003].includes(event.code) || !navigator.onLine)
75
+ if (this.ws && this.ws.readyState !== WebSocket.CLOSED)
76
76
  return;
77
- const timeout = Math.min(30000, 2000 * 2 ** this.reconnectAttempts);
78
- this.reconnectAttempts++;
79
- this.reconnectTimer = window.setTimeout(() => this.connect(), timeout);
80
- };
81
- }
82
- /** Send notification */
83
- async notify({ recipient, payload, client, origin, }) {
84
- if (!recipient || !payload) {
85
- throw new Error("recipient and payload are required");
77
+ this.ws = new WebSocket(`${this.wsUrl}/connect?api_key=${this.apiKey}&recipient=${this.recipient}&client=${this.client}`);
78
+ this.ws.onopen = () => {
79
+ this.isConnected = true;
80
+ this.reconnectAttempts = 0;
81
+ this.emitConnection("connected");
82
+ };
83
+ this.ws.onmessage = (event) => {
84
+ const parsed = this.safeParse(event.data);
85
+ if (!parsed)
86
+ return;
87
+ this.listeners.forEach((fn) => fn(parsed));
88
+ };
89
+ this.ws.onclose = (event) => {
90
+ this.isConnected = false;
91
+ this.emitConnection("disconnected");
92
+ if (event.code === 4001 || event.code === 4003 || !navigator.onLine)
93
+ return;
94
+ const timeout = Math.min(30000, 2000 * Math.pow(2, this.reconnectAttempts));
95
+ this.reconnectAttempts++;
96
+ this.reconnectTimer = window.setTimeout(() => this.connect(), timeout);
97
+ };
86
98
  }
87
- const body = {
88
- recipient,
89
- payload,
90
- api_key: this.apiKey,
91
- client: client || this.client,
92
- };
93
- const res = await fetch(this.apiEndpoint, {
94
- method: "POST",
95
- headers: {
96
- "Content-Type": "application/json",
97
- Origin: origin || window.location.origin,
98
- },
99
- body: JSON.stringify(body),
100
- });
101
- let json;
102
- try {
103
- json = await res.json();
99
+ /** Send notification */
100
+ notify(_a) {
101
+ return __awaiter(this, arguments, void 0, function* ({ recipient, payload, client, origin, }) {
102
+ if (!recipient || !payload) {
103
+ throw new Error("recipient and payload are required");
104
+ }
105
+ const body = {
106
+ recipient,
107
+ payload,
108
+ api_key: this.apiKey,
109
+ client: client || this.client,
110
+ };
111
+ const res = yield fetch(this.apiEndpoint, {
112
+ method: "POST",
113
+ headers: {
114
+ "Content-Type": "application/json",
115
+ Origin: origin || window.location.origin,
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
+ };
133
+ });
104
134
  }
105
- catch {
106
- throw new Error("Invalid server response");
135
+ /** Disconnect WS */
136
+ disconnect() {
137
+ var _a;
138
+ if (this.reconnectTimer)
139
+ clearTimeout(this.reconnectTimer);
140
+ (_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
141
+ this.isConnected = false;
142
+ this.emitConnection("disconnected");
107
143
  }
108
- if (!res.ok) {
109
- throw new Error(json.message || "Notification request failed");
144
+ /** Return connection state */
145
+ connected() {
146
+ return this.isConnected;
110
147
  }
111
- return {
112
- success: json.success,
113
- message: json.message,
114
- };
115
- }
116
- /** Disconnect WS */
117
- disconnect() {
118
- if (this.reconnectTimer)
119
- clearTimeout(this.reconnectTimer);
120
- this.ws?.close();
121
- this.isConnected = false;
122
- this.emitConnection("disconnected");
123
- }
124
- /** Return connection state */
125
- connected() {
126
- return this.isConnected;
127
148
  }
128
- }
149
+ exports.ArifaClient = ArifaClient;
150
+ });
@@ -0,0 +1,2 @@
1
+ import { ArifaClient } from "./ArifaClient";
2
+ export { ArifaClient };
package/dist/arifa.js ADDED
@@ -0,0 +1,19 @@
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
+ // src/arifa.ts - FIXED VERSION
14
+ const ArifaClient_1 = require("./ArifaClient");
15
+ Object.defineProperty(exports, "ArifaClient", { enumerable: true, get: function () { return ArifaClient_1.ArifaClient; } });
16
+ // For script tag usage - IMPORTANT: Set it directly on window
17
+ // Don't use module.exports pattern
18
+ window.ArifaClient = ArifaClient_1.ArifaClient;
19
+ });
@@ -0,0 +1 @@
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}}),window.ArifaClient=t.ArifaClient});
package/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "arifa-client",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "JavaScript/TypeScript client SDK for Arifa Realtime Notification Service",
5
- "main": "dist/ArifaClient.js",
6
- "module": "dist/ArifaClient.js",
7
- "types": "dist/ArifaClient.d.ts",
5
+ "main": "dist/arifa.js",
6
+ "module": "dist/arifa.js",
7
+ "browser": "dist/arifa.js",
8
+ "unpkg": "dist/arifa.min.js",
9
+ "types": "dist/arifa.d.ts",
8
10
  "files": [
9
11
  "dist"
10
12
  ],
11
13
  "scripts": {
12
14
  "build": "tsc",
13
- "prepublishOnly": "npm run build"
15
+ "build:minify": "uglifyjs dist/arifa.js -o dist/arifa.min.js --compress --mangle",
16
+ "build:all": "npm run build && npm run build:minify",
17
+ "prepublishOnly": "npm run build:all"
14
18
  },
15
19
  "keywords": [
16
20
  "arifa",
@@ -21,8 +25,8 @@
21
25
  ],
22
26
  "author": "Peter Nyando",
23
27
  "license": "MIT",
24
- "type": "module",
25
28
  "devDependencies": {
26
- "typescript": "^5.9.3"
29
+ "typescript": "^5.9.3",
30
+ "uglify-js": "^3.19.3"
27
31
  }
28
32
  }