@ariary/notification 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 CHANGED
@@ -1,76 +1,59 @@
1
- # @ariary/sms
1
+ # Ariari Notification
2
2
 
3
- SDK SMS et Notification Task pour l'API Ariary
3
+ Simple SDK for sending SMS notifications.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- npm install @ariary/sms
8
+ npm install @ariari/notification
9
9
  ```
10
10
 
11
11
  ## Usage
12
12
 
13
- ### Initialisation
13
+ ### Configuration
14
14
 
15
15
  ```typescript
16
- import { SmsSdk } from '@ariary/sms';
16
+ import Ariari from '@ariari/notification';
17
17
 
18
- const sdk = new SmsSdk({
18
+ Ariari.config({
19
19
  projectId: 'your-project-id',
20
- secretId: 'your-secret-id',
21
- baseUrl: 'https://back.ariari.mg/payment/api-docs'
20
+ secretId: 'your-secret-id'
22
21
  });
23
22
  ```
24
23
 
25
- ### SMS Service
24
+ ### Send Messages
26
25
 
27
26
  ```typescript
28
- // Envoyer un SMS unique
29
- await sdk.sms.notify('Hello World', '261234567890');
27
+ const task = await Ariari.send(
28
+ { phone: '+261123456789', message: 'Message 1' },
29
+ { phone: ['+261987654321', '+261555555555'], message: 'Message 2' }
30
+ );
30
31
 
31
- // Envoyer un SMS à plusieurs destinataires
32
- await sdk.sms.notify('Hello World', ['261234567890', '261234567891']);
33
-
34
- // Envoyer des messages différents (bulk)
35
- await sdk.sms.notifyBulk([
36
- { message: 'Message 1', numbers: ['261234567890'] },
37
- { message: 'Message 2', numbers: ['261234567891'] }
38
- ]);
39
-
40
- // Récupérer l'historique SMS
41
- const smsList = await sdk.sms.getAll();
32
+ // Check task status
33
+ await task.status();
42
34
  ```
43
35
 
44
- ### NotifTask Service
36
+ ### Check Task Status
45
37
 
46
- ```typescript
47
- // Créer une tâche de notification
48
- const task = await sdk.notifTask.create({
49
- message: 'Hello World',
50
- phones: ['261234567890', '261234567891']
51
- });
38
+ You can also check the status of a task by ID:
52
39
 
53
- // Récupérer toutes les tâches
54
- const tasks = await sdk.notifTask.findAll('project-id');
40
+ ```typescript
41
+ const task = new Ariari.Task('task-id-123');
42
+ await task.status();
43
+ await task.detailedStatus();
44
+ ```
55
45
 
56
- // Récupérer une tâche
57
- const task = await sdk.notifTask.findOne('task-id');
46
+ ### List Tasks
58
47
 
59
- // Mettre à jour une tâche
60
- await sdk.notifTask.update('task-id', {
61
- message: 'Updated message'
48
+ ```typescript
49
+ const result = await Ariari.tasks({
50
+ from: 0,
51
+ count: 20,
52
+ order: -1
62
53
  });
63
54
 
64
- // Récupérer les détails SMS d'une tâche
65
- const details = await sdk.notifTask.getSmsDetails('task-id');
66
-
67
- // Réessayer les SMS échoués
68
- await sdk.notifTask.retryFailedSms('task-id');
69
55
 
70
- // Supprimer une tâche
71
- await sdk.notifTask.remove('task-id');
56
+ const nextResult = await Ariari.tasks(result.next);
57
+ ...
58
+ const prevResult = await Ariari.tasks(result.prev);
72
59
  ```
73
-
74
- ## License
75
-
76
- ISC
package/dist/index.d.mts CHANGED
@@ -1,18 +1,46 @@
1
- import { SmsService } from './sms/index.mjs';
2
- export { BulkSmsDto, BulkSmsResponseDto, CreateSmsDto, MultiSmsResponseDto, ResponseSmsDto, SendSmsDto, SendSmsResponseDto } from './sms/index.mjs';
3
- import { NotifTaskService } from './notif-task/index.mjs';
4
- export { CreateNotifTaskDto, NotifTaskDetailsDto, ResponseNotifTaskDto, RetryFailedSmsResponseDto, SmsDetail, UpdateNotifTaskDto } from './notif-task/index.mjs';
5
- import { A as ApiConfig } from './client-BOI4a9h5.mjs';
6
- export { a as ApiClient } from './client-BOI4a9h5.mjs';
1
+ interface ApiConfig {
2
+ projectId: string;
3
+ secretId: string;
4
+ baseUrl?: string;
5
+ }
6
+
7
+ type Message = {
8
+ phone: string[] | string;
9
+ message: string;
10
+ };
11
+ type GetTaskParam = {
12
+ from: number;
13
+ count: number;
14
+ order: -1 | 1;
15
+ };
7
16
 
8
- /**
9
- * SDK pour SMS et Notification Tasks Ariary
10
- */
11
- declare class SmsSdk {
12
- private client;
13
- sms: SmsService;
14
- notifTask: NotifTaskService;
15
- constructor(config: ApiConfig);
17
+ declare class Task {
18
+ id: string;
19
+ constructor(id: string);
20
+ status: () => Promise<unknown>;
21
+ detailedStatus: () => Promise<unknown>;
22
+ }
23
+ declare class AriariBridge {
24
+ private static instance;
25
+ private constructor();
26
+ static getInstance(): AriariBridge;
27
+ config(cfg: ApiConfig): void;
28
+ send(...data: Message[]): Promise<Task>;
29
+ tasks: ({ from, count, order }: GetTaskParam) => Promise<{
30
+ tasks: never[];
31
+ next: {
32
+ from: number;
33
+ count: number;
34
+ order: 1 | -1;
35
+ };
36
+ prev: {
37
+ from: number;
38
+ count: number;
39
+ order: 1 | -1;
40
+ } | undefined;
41
+ }>;
42
+ Task: typeof Task;
16
43
  }
44
+ declare const Ariari: AriariBridge;
17
45
 
18
- export { ApiConfig, NotifTaskService, SmsSdk, SmsService };
46
+ export { Ariari as default };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,46 @@
1
- import { SmsService } from './sms/index.js';
2
- export { BulkSmsDto, BulkSmsResponseDto, CreateSmsDto, MultiSmsResponseDto, ResponseSmsDto, SendSmsDto, SendSmsResponseDto } from './sms/index.js';
3
- import { NotifTaskService } from './notif-task/index.js';
4
- export { CreateNotifTaskDto, NotifTaskDetailsDto, ResponseNotifTaskDto, RetryFailedSmsResponseDto, SmsDetail, UpdateNotifTaskDto } from './notif-task/index.js';
5
- import { A as ApiConfig } from './client-BOI4a9h5.js';
6
- export { a as ApiClient } from './client-BOI4a9h5.js';
1
+ interface ApiConfig {
2
+ projectId: string;
3
+ secretId: string;
4
+ baseUrl?: string;
5
+ }
6
+
7
+ type Message = {
8
+ phone: string[] | string;
9
+ message: string;
10
+ };
11
+ type GetTaskParam = {
12
+ from: number;
13
+ count: number;
14
+ order: -1 | 1;
15
+ };
7
16
 
8
- /**
9
- * SDK pour SMS et Notification Tasks Ariary
10
- */
11
- declare class SmsSdk {
12
- private client;
13
- sms: SmsService;
14
- notifTask: NotifTaskService;
15
- constructor(config: ApiConfig);
17
+ declare class Task {
18
+ id: string;
19
+ constructor(id: string);
20
+ status: () => Promise<unknown>;
21
+ detailedStatus: () => Promise<unknown>;
22
+ }
23
+ declare class AriariBridge {
24
+ private static instance;
25
+ private constructor();
26
+ static getInstance(): AriariBridge;
27
+ config(cfg: ApiConfig): void;
28
+ send(...data: Message[]): Promise<Task>;
29
+ tasks: ({ from, count, order }: GetTaskParam) => Promise<{
30
+ tasks: never[];
31
+ next: {
32
+ from: number;
33
+ count: number;
34
+ order: 1 | -1;
35
+ };
36
+ prev: {
37
+ from: number;
38
+ count: number;
39
+ order: 1 | -1;
40
+ } | undefined;
41
+ }>;
42
+ Task: typeof Task;
16
43
  }
44
+ declare const Ariari: AriariBridge;
17
45
 
18
- export { ApiConfig, NotifTaskService, SmsSdk, SmsService };
46
+ export { Ariari as default };
package/dist/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,244 +15,104 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
31
21
  var src_exports = {};
32
22
  __export(src_exports, {
33
- ApiClient: () => ApiClient,
34
- NotifTaskService: () => NotifTaskService,
35
- SmsSdk: () => SmsSdk,
36
- SmsService: () => SmsService
23
+ default: () => src_default
37
24
  });
38
25
  module.exports = __toCommonJS(src_exports);
39
26
 
40
- // src/client.ts
41
- var import_axios = __toESM(require("axios"));
42
- var ApiClient = class {
43
- constructor(config, baseUrl) {
44
- this.config = config;
45
- const finalBaseUrl = baseUrl || config.baseUrl || "https://back.ariari.mg/payment/api-docs";
46
- this.client = import_axios.default.create({
47
- baseURL: finalBaseUrl,
48
- headers: {
49
- "Content-Type": "application/json"
50
- }
51
- });
52
- }
53
- /**
54
- * Effectue une requête POST avec les en-têtes d'authentification
55
- */
56
- async post(endpoint, data, requiresSecret = true) {
57
- const headers = {
58
- "x-project-id": this.config.projectId
59
- };
60
- if (requiresSecret) {
61
- headers["x-secret-id"] = this.config.secretId;
62
- }
63
- try {
64
- const response = await this.client.post(endpoint, data, { headers });
65
- return response.data;
66
- } catch (error) {
67
- throw this.handleError(error);
68
- }
69
- }
70
- /**
71
- * Effectue une requête GET avec les en-têtes d'authentification
72
- */
73
- async get(endpoint, requiresSecret = true) {
74
- const headers = {
75
- "x-project-id": this.config.projectId
76
- };
77
- if (requiresSecret) {
78
- headers["x-secret-id"] = this.config.secretId;
79
- }
80
- try {
81
- const response = await this.client.get(endpoint, { headers });
82
- return response.data;
83
- } catch (error) {
84
- throw this.handleError(error);
85
- }
86
- }
87
- /**
88
- * Effectue une requête PATCH avec les en-têtes d'authentification
89
- */
90
- async patch(endpoint, data, requiresSecret = true) {
91
- const headers = {
92
- "x-project-id": this.config.projectId
93
- };
94
- if (requiresSecret) {
95
- headers["x-secret-id"] = this.config.secretId;
96
- }
97
- try {
98
- const response = await this.client.patch(endpoint, data, { headers });
99
- return response.data;
100
- } catch (error) {
101
- throw this.handleError(error);
102
- }
103
- }
104
- /**
105
- * Effectue une requête PUT avec les en-têtes d'authentification
106
- */
107
- async put(endpoint, data, requiresSecret = true) {
108
- const headers = {
109
- "x-project-id": this.config.projectId
110
- };
111
- if (requiresSecret) {
112
- headers["x-secret-id"] = this.config.secretId;
113
- }
114
- try {
115
- const response = await this.client.put(endpoint, data, { headers });
116
- return response.data;
117
- } catch (error) {
118
- throw this.handleError(error);
119
- }
120
- }
121
- /**
122
- * Effectue une requête DELETE avec les en-têtes d'authentification
123
- */
124
- async delete(endpoint, requiresSecret = true) {
125
- const headers = {
126
- "x-project-id": this.config.projectId
127
- };
128
- if (requiresSecret) {
129
- headers["x-secret-id"] = this.config.secretId;
27
+ // src/config/index.ts
28
+ var config = null;
29
+ function setConfig(cfg) {
30
+ config = cfg;
31
+ }
32
+ function getConfig() {
33
+ return config;
34
+ }
35
+
36
+ // src/http/index.ts
37
+ async function request(method, endpoint, options = {}) {
38
+ const config2 = getConfig();
39
+ if (!config2) {
40
+ throw new Error("Ariari not configured. Call Ariari.config() first.");
41
+ }
42
+ const baseUrl = config2.baseUrl || "https://back.ariari.mg";
43
+ const url = `${baseUrl}${endpoint}`;
44
+ const headers = {
45
+ "Content-Type": "application/json",
46
+ "x-project-id": config2.projectId
47
+ };
48
+ if (options.requiresSecret !== false) {
49
+ headers["x-secret-id"] = config2.secretId;
50
+ }
51
+ const fetchOptions = {
52
+ method,
53
+ headers
54
+ };
55
+ if (options.body && (method === "POST" || method === "PATCH" || method === "PUT")) {
56
+ fetchOptions.body = JSON.stringify(options.body);
57
+ }
58
+ try {
59
+ const response = await fetch(url, fetchOptions);
60
+ const data = await response.json();
61
+ if (!response.ok) {
62
+ const message = data?.message || "Unknown error";
63
+ throw new Error(`[${response.status}] ${message}`);
130
64
  }
131
- try {
132
- const response = await this.client.delete(endpoint, { headers });
133
- return response.data;
134
- } catch (error) {
135
- throw this.handleError(error);
65
+ return data;
66
+ } catch (error) {
67
+ if (error instanceof Error) {
68
+ throw error;
136
69
  }
137
- }
138
- /**
139
- * Gère les erreurs API
140
- */
141
- handleError(error) {
142
- if (error.response) {
143
- const status = error.response.status;
144
- const message = error.response.data?.message || error.response.statusText;
145
- return new Error(`[${status}] ${message}`);
146
- }
147
- return error;
148
- }
149
- };
70
+ throw new Error("Network error");
71
+ }
72
+ }
73
+ async function httpGet(endpoint, requiresSecret = true) {
74
+ return request("GET", endpoint, { requiresSecret });
75
+ }
76
+ async function httpPost(endpoint, body, requiresSecret = true) {
77
+ return request("POST", endpoint, { body, requiresSecret });
78
+ }
150
79
 
151
- // src/sms.ts
152
- var SmsService = class {
153
- constructor(client) {
154
- this.client = client;
155
- }
156
- async notify(message, numbers) {
157
- const phones = Array.isArray(numbers) ? numbers : [numbers];
158
- const response = await this.client.post(
159
- "/api/sms/multi",
160
- { phones, message },
161
- true
162
- );
163
- return response;
164
- }
165
- async notifyBulk(messages) {
166
- const bulkMessages = messages.map((item) => ({
167
- message: item.message,
168
- phones: Array.isArray(item.numbers) ? item.numbers : [item.numbers]
169
- }));
170
- const response = await this.client.post(
171
- "/api/sms/bulk",
172
- { messages: bulkMessages },
173
- true
174
- );
175
- return response;
176
- }
177
- async getAll() {
178
- const response = await this.client.get(
179
- "/api/sms",
180
- true
181
- );
182
- return response;
80
+ // src/index.ts
81
+ var Task = class {
82
+ constructor(id) {
83
+ this.status = () => httpGet(`/api/notif-task/${this.id}`);
84
+ this.detailedStatus = () => httpGet(`/api/notif-task/${this.id}/detailed`);
85
+ this.id = id;
183
86
  }
184
87
  };
185
-
186
- // src/notif-task.ts
187
- var NotifTaskService = class {
188
- constructor(client) {
189
- this.client = client;
190
- }
191
- async create(createNotifTaskDto) {
192
- const response = await this.client.post(
193
- "/api/notif-task",
194
- createNotifTaskDto,
195
- true
196
- );
197
- return response;
198
- }
199
- async findAll(projectId) {
200
- const response = await this.client.get(
201
- `/api/notif-task?projectId=${projectId}`,
202
- true
203
- );
204
- return response;
205
- }
206
- async findOne(id) {
207
- const response = await this.client.get(
208
- `/api/notif-task/${id}`,
209
- true
210
- );
211
- return response;
212
- }
213
- async update(id, updateNotifTaskDto) {
214
- const response = await this.client.patch(
215
- `/api/notif-task/${id}`,
216
- updateNotifTaskDto,
217
- true
218
- );
219
- return response;
88
+ var AriariBridge = class _AriariBridge {
89
+ constructor() {
90
+ this.tasks = async ({ from = 0, count = 20, order = -1 }) => {
91
+ return {
92
+ tasks: [],
93
+ next: { from: from + count + 1, count, order },
94
+ prev: from > 0 ? { from: from - count - 1, count, order } : void 0
95
+ };
96
+ };
97
+ this.Task = Task;
220
98
  }
221
- async remove(id) {
222
- const response = await this.client.delete(
223
- `/api/notif-task/${id}`,
224
- true
225
- );
226
- return response;
99
+ static getInstance() {
100
+ if (!_AriariBridge.instance) {
101
+ _AriariBridge.instance = new _AriariBridge();
102
+ }
103
+ return _AriariBridge.instance;
227
104
  }
228
- async getSmsDetails(id) {
229
- const response = await this.client.get(
230
- `/api/notif-task/${id}/sms-details`,
231
- true
232
- );
233
- return response;
105
+ config(cfg) {
106
+ setConfig(cfg);
234
107
  }
235
- async retryFailedSms(id) {
236
- const response = await this.client.post(
237
- `/api/notif-task/${id}/retry-failed-sms`,
238
- {},
239
- true
240
- );
241
- return response;
108
+ async send(...data) {
109
+ const messages = data.reduce((old, item) => {
110
+ old.push(...Array.isArray(item.phone) ? item.phone.map((phone) => ({ phone, message: item.message })) : [item]);
111
+ return old;
112
+ }, []);
113
+ const one = await httpPost("/api/sms/bulk", { messages });
114
+ return new Task(one.id);
242
115
  }
243
116
  };
244
-
245
- // src/index.ts
246
- var SmsSdk = class {
247
- constructor(config) {
248
- const finalConfig = { ...config, baseUrl: config.baseUrl || "https://back.ariari.mg/payment/api-docs" };
249
- this.client = new ApiClient(finalConfig);
250
- this.sms = new SmsService(this.client);
251
- this.notifTask = new NotifTaskService(this.client);
252
- }
253
- };
254
- // Annotate the CommonJS export names for ESM import in node:
255
- 0 && (module.exports = {
256
- ApiClient,
257
- NotifTaskService,
258
- SmsSdk,
259
- SmsService
260
- });
117
+ var Ariari = AriariBridge.getInstance();
118
+ var src_default = Ariari;