@ariary/notification 1.0.8 → 2.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/dist/index.d.mts CHANGED
@@ -1,2 +1,30 @@
1
+ type Message = {
2
+ phone: string[] | string;
3
+ message: string;
4
+ };
5
+ type GetTaskParam = {
6
+ from: number;
7
+ count: number;
8
+ order: -1 | 1;
9
+ };
1
10
 
2
- export { }
11
+ declare class Task {
12
+ id: string;
13
+ constructor(id: string);
14
+ status(): Promise<unknown>;
15
+ detailedStatus(): Promise<unknown>;
16
+ }
17
+ declare class Notification {
18
+ send(...data: Message[]): Promise<Task>;
19
+ sendMessage(data: Message): Promise<Task>;
20
+ getMessages(param: GetTaskParam): Promise<unknown>;
21
+ }
22
+ declare class Ariari {
23
+ private static notification;
24
+ static config(config: any): void;
25
+ static sendMessage(data: Message): Promise<Task>;
26
+ static send(...data: Message[]): Promise<Task>;
27
+ static getMessages(param: GetTaskParam): Promise<unknown>;
28
+ }
29
+
30
+ export { Ariari, type GetTaskParam, type Message, Notification, Task };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,30 @@
1
+ type Message = {
2
+ phone: string[] | string;
3
+ message: string;
4
+ };
5
+ type GetTaskParam = {
6
+ from: number;
7
+ count: number;
8
+ order: -1 | 1;
9
+ };
1
10
 
2
- export { }
11
+ declare class Task {
12
+ id: string;
13
+ constructor(id: string);
14
+ status(): Promise<unknown>;
15
+ detailedStatus(): Promise<unknown>;
16
+ }
17
+ declare class Notification {
18
+ send(...data: Message[]): Promise<Task>;
19
+ sendMessage(data: Message): Promise<Task>;
20
+ getMessages(param: GetTaskParam): Promise<unknown>;
21
+ }
22
+ declare class Ariari {
23
+ private static notification;
24
+ static config(config: any): void;
25
+ static sendMessage(data: Message): Promise<Task>;
26
+ static send(...data: Message[]): Promise<Task>;
27
+ static getMessages(param: GetTaskParam): Promise<unknown>;
28
+ }
29
+
30
+ export { Ariari, type GetTaskParam, type Message, Notification, Task };
package/dist/index.js CHANGED
@@ -1 +1,140 @@
1
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 src_exports = {};
22
+ __export(src_exports, {
23
+ Ariari: () => Ariari,
24
+ Notification: () => Notification,
25
+ Task: () => Task
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+
29
+ // src/config/index.ts
30
+ var config = null;
31
+ function setConfig(cfg) {
32
+ config = cfg;
33
+ }
34
+ function getConfig() {
35
+ return config;
36
+ }
37
+
38
+ // src/http/index.ts
39
+ async function request(method, endpoint, options = {}) {
40
+ const config2 = getConfig();
41
+ if (!config2) {
42
+ throw new Error("Ariari not configured. Call Ariari.config() first.");
43
+ }
44
+ const baseUrl = config2.baseUrl || "https://back.ariari.mg";
45
+ const url = `${baseUrl}${endpoint}`;
46
+ const headers = {
47
+ "Content-Type": "application/json",
48
+ "x-project-id": config2.projectId
49
+ };
50
+ if (options.requiresSecret !== false) {
51
+ headers["x-secret-id"] = config2.secretId;
52
+ }
53
+ const fetchOptions = {
54
+ method,
55
+ headers
56
+ };
57
+ if (options.body && (method === "POST" || method === "PATCH" || method === "PUT")) {
58
+ fetchOptions.body = JSON.stringify(options.body);
59
+ }
60
+ try {
61
+ const response = await fetch(url, fetchOptions);
62
+ const data = await response.json();
63
+ if (!response.ok) {
64
+ const message = data?.message || "Unknown error";
65
+ throw new Error(`[${response.status}] ${message}`);
66
+ }
67
+ return data;
68
+ } catch (error) {
69
+ if (error instanceof Error) {
70
+ throw error;
71
+ }
72
+ throw new Error("Network error");
73
+ }
74
+ }
75
+ async function httpGet(endpoint, requiresSecret = true) {
76
+ return request("GET", endpoint, { requiresSecret });
77
+ }
78
+ async function httpPost(endpoint, body, requiresSecret = true) {
79
+ return request("POST", endpoint, { body, requiresSecret });
80
+ }
81
+
82
+ // src/index.ts
83
+ function normalizePhoneNumber(phone) {
84
+ let normalized = phone.replace(/\D/g, "");
85
+ if (normalized.startsWith("261")) {
86
+ normalized = "0" + normalized.slice(3);
87
+ }
88
+ return normalized;
89
+ }
90
+ var Task = class {
91
+ constructor(id) {
92
+ this.id = id;
93
+ }
94
+ async status() {
95
+ return httpGet(`/api/sms/${this.id}/status`);
96
+ }
97
+ async detailedStatus() {
98
+ return httpGet(`/api/sms/${this.id}/detailed-status`);
99
+ }
100
+ };
101
+ var Notification = class {
102
+ async send(...data) {
103
+ const messages = data.map((item) => ({
104
+ phones: (Array.isArray(item.phone) ? item.phone : [item.phone]).map(normalizePhoneNumber),
105
+ message: item.message
106
+ }));
107
+ const response = await httpPost("/api/sms/bulk", { messages });
108
+ if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
109
+ throw new Error("Invalid response: no SMS IDs returned");
110
+ }
111
+ return new Task(response.data[0]);
112
+ }
113
+ async sendMessage(data) {
114
+ return this.send(data);
115
+ }
116
+ async getMessages(param) {
117
+ return httpGet(`/api/sms?from=${param.from}&count=${param.count}&order=${param.order}`);
118
+ }
119
+ };
120
+ var Ariari = class {
121
+ static config(config2) {
122
+ setConfig(config2);
123
+ }
124
+ static async sendMessage(data) {
125
+ return this.notification.sendMessage(data);
126
+ }
127
+ static async send(...data) {
128
+ return this.notification.send(...data);
129
+ }
130
+ static async getMessages(param) {
131
+ return this.notification.getMessages(param);
132
+ }
133
+ };
134
+ Ariari.notification = new Notification();
135
+ // Annotate the CommonJS export names for ESM import in node:
136
+ 0 && (module.exports = {
137
+ Ariari,
138
+ Notification,
139
+ Task
140
+ });
package/dist/index.mjs CHANGED
@@ -0,0 +1,111 @@
1
+ // src/config/index.ts
2
+ var config = null;
3
+ function setConfig(cfg) {
4
+ config = cfg;
5
+ }
6
+ function getConfig() {
7
+ return config;
8
+ }
9
+
10
+ // src/http/index.ts
11
+ async function request(method, endpoint, options = {}) {
12
+ const config2 = getConfig();
13
+ if (!config2) {
14
+ throw new Error("Ariari not configured. Call Ariari.config() first.");
15
+ }
16
+ const baseUrl = config2.baseUrl || "https://back.ariari.mg";
17
+ const url = `${baseUrl}${endpoint}`;
18
+ const headers = {
19
+ "Content-Type": "application/json",
20
+ "x-project-id": config2.projectId
21
+ };
22
+ if (options.requiresSecret !== false) {
23
+ headers["x-secret-id"] = config2.secretId;
24
+ }
25
+ const fetchOptions = {
26
+ method,
27
+ headers
28
+ };
29
+ if (options.body && (method === "POST" || method === "PATCH" || method === "PUT")) {
30
+ fetchOptions.body = JSON.stringify(options.body);
31
+ }
32
+ try {
33
+ const response = await fetch(url, fetchOptions);
34
+ const data = await response.json();
35
+ if (!response.ok) {
36
+ const message = data?.message || "Unknown error";
37
+ throw new Error(`[${response.status}] ${message}`);
38
+ }
39
+ return data;
40
+ } catch (error) {
41
+ if (error instanceof Error) {
42
+ throw error;
43
+ }
44
+ throw new Error("Network error");
45
+ }
46
+ }
47
+ async function httpGet(endpoint, requiresSecret = true) {
48
+ return request("GET", endpoint, { requiresSecret });
49
+ }
50
+ async function httpPost(endpoint, body, requiresSecret = true) {
51
+ return request("POST", endpoint, { body, requiresSecret });
52
+ }
53
+
54
+ // src/index.ts
55
+ function normalizePhoneNumber(phone) {
56
+ let normalized = phone.replace(/\D/g, "");
57
+ if (normalized.startsWith("261")) {
58
+ normalized = "0" + normalized.slice(3);
59
+ }
60
+ return normalized;
61
+ }
62
+ var Task = class {
63
+ constructor(id) {
64
+ this.id = id;
65
+ }
66
+ async status() {
67
+ return httpGet(`/api/sms/${this.id}/status`);
68
+ }
69
+ async detailedStatus() {
70
+ return httpGet(`/api/sms/${this.id}/detailed-status`);
71
+ }
72
+ };
73
+ var Notification = class {
74
+ async send(...data) {
75
+ const messages = data.map((item) => ({
76
+ phones: (Array.isArray(item.phone) ? item.phone : [item.phone]).map(normalizePhoneNumber),
77
+ message: item.message
78
+ }));
79
+ const response = await httpPost("/api/sms/bulk", { messages });
80
+ if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
81
+ throw new Error("Invalid response: no SMS IDs returned");
82
+ }
83
+ return new Task(response.data[0]);
84
+ }
85
+ async sendMessage(data) {
86
+ return this.send(data);
87
+ }
88
+ async getMessages(param) {
89
+ return httpGet(`/api/sms?from=${param.from}&count=${param.count}&order=${param.order}`);
90
+ }
91
+ };
92
+ var Ariari = class {
93
+ static config(config2) {
94
+ setConfig(config2);
95
+ }
96
+ static async sendMessage(data) {
97
+ return this.notification.sendMessage(data);
98
+ }
99
+ static async send(...data) {
100
+ return this.notification.send(...data);
101
+ }
102
+ static async getMessages(param) {
103
+ return this.notification.getMessages(param);
104
+ }
105
+ };
106
+ Ariari.notification = new Notification();
107
+ export {
108
+ Ariari,
109
+ Notification,
110
+ Task
111
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ariary/notification",
3
- "version": "1.0.8",
3
+ "version": "2.0.0",
4
4
  "description": "SMS et Notification Task SDK pour l'API Ariary",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",