@ariary/notification 1.0.1 → 1.0.2

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.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,149 @@ 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
+ Ariari: () => Ariari,
24
+ getConfig: () => getConfig,
25
+ notificationService: () => notificationService,
26
+ setConfig: () => setConfig
37
27
  });
38
28
  module.exports = __toCommonJS(src_exports);
39
29
 
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;
130
- }
131
- try {
132
- const response = await this.client.delete(endpoint, { headers });
133
- return response.data;
134
- } catch (error) {
135
- throw this.handleError(error);
30
+ // src/config/index.ts
31
+ var config = null;
32
+ function setConfig(cfg) {
33
+ config = cfg;
34
+ }
35
+ function getConfig() {
36
+ return config;
37
+ }
38
+
39
+ // src/http/index.ts
40
+ async function request(method, endpoint, options = {}) {
41
+ const config2 = getConfig();
42
+ if (!config2) {
43
+ throw new Error("Ariari not configured. Call Ariari.config() first.");
44
+ }
45
+ const baseUrl = config2.baseUrl || "https://back.ariari.mg";
46
+ const url = `${baseUrl}${endpoint}`;
47
+ const headers = {
48
+ "Content-Type": "application/json",
49
+ "x-project-id": config2.projectId
50
+ };
51
+ if (options.requiresSecret !== false) {
52
+ headers["x-secret-id"] = config2.secretId;
53
+ }
54
+ const fetchOptions = {
55
+ method,
56
+ headers
57
+ };
58
+ if (options.body && (method === "POST" || method === "PATCH" || method === "PUT")) {
59
+ fetchOptions.body = JSON.stringify(options.body);
60
+ }
61
+ try {
62
+ const response = await fetch(url, fetchOptions);
63
+ const data = await response.json();
64
+ if (!response.ok) {
65
+ const message = data?.message || "Unknown error";
66
+ throw new Error(`[${response.status}] ${message}`);
136
67
  }
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}`);
68
+ return data;
69
+ } catch (error) {
70
+ if (error instanceof Error) {
71
+ throw error;
146
72
  }
147
- return error;
148
- }
149
- };
73
+ throw new Error("Network error");
74
+ }
75
+ }
76
+ async function httpGet(endpoint, requiresSecret = true) {
77
+ return request("GET", endpoint, { requiresSecret });
78
+ }
79
+ async function httpPost(endpoint, body, requiresSecret = true) {
80
+ return request("POST", endpoint, { body, requiresSecret });
81
+ }
82
+ async function httpPatch(endpoint, body, requiresSecret = true) {
83
+ return request("PATCH", endpoint, { body, requiresSecret });
84
+ }
85
+ async function httpDelete(endpoint, requiresSecret = true) {
86
+ return request("DELETE", endpoint, { requiresSecret });
87
+ }
150
88
 
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) {
89
+ // src/services/notification.ts
90
+ var smsApi = {
91
+ async send(data) {
92
+ const phones = Array.isArray(data.phone) ? data.phone : [data.phone];
93
+ return httpPost("/api/sms/multi", {
94
+ phones,
95
+ message: data.message
96
+ });
97
+ },
98
+ async sendBulk(messages) {
166
99
  const bulkMessages = messages.map((item) => ({
167
100
  message: item.message,
168
- phones: Array.isArray(item.numbers) ? item.numbers : [item.numbers]
101
+ phones: Array.isArray(item.phone) ? item.phone : [item.phone]
169
102
  }));
170
- const response = await this.client.post(
171
- "/api/sms/bulk",
172
- { messages: bulkMessages },
173
- true
174
- );
175
- return response;
176
- }
103
+ return httpPost("/api/sms/bulk", {
104
+ messages: bulkMessages
105
+ });
106
+ },
177
107
  async getAll() {
178
- const response = await this.client.get(
179
- "/api/sms",
180
- true
181
- );
182
- return response;
108
+ return httpGet("/api/sms");
183
109
  }
184
110
  };
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
- }
111
+ var notifTaskApi = {
199
112
  async findAll(projectId) {
200
- const response = await this.client.get(
201
- `/api/notif-task?projectId=${projectId}`,
202
- true
203
- );
204
- return response;
205
- }
113
+ return httpGet(`/api/notif-task?projectId=${projectId}`);
114
+ },
206
115
  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;
220
- }
221
- async remove(id) {
222
- const response = await this.client.delete(
223
- `/api/notif-task/${id}`,
224
- true
225
- );
226
- return response;
227
- }
116
+ return httpGet(`/api/notif-task/${id}`);
117
+ },
118
+ async update(id, data) {
119
+ return httpPatch(`/api/notif-task/${id}`, data);
120
+ },
121
+ async delete(id) {
122
+ return httpDelete(`/api/notif-task/${id}`);
123
+ },
228
124
  async getSmsDetails(id) {
229
- const response = await this.client.get(
230
- `/api/notif-task/${id}/sms-details`,
231
- true
232
- );
233
- return response;
234
- }
125
+ return httpGet(`/api/notif-task/${id}/sms-details`);
126
+ },
235
127
  async retryFailedSms(id) {
236
- const response = await this.client.post(
128
+ return httpPost(
237
129
  `/api/notif-task/${id}/retry-failed-sms`,
238
- {},
239
- true
130
+ {}
240
131
  );
241
- return response;
242
132
  }
243
133
  };
134
+ var notificationService = {
135
+ ...smsApi,
136
+ task: notifTaskApi
137
+ };
244
138
 
245
139
  // 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);
140
+ var AriariBridge = class _AriariBridge {
141
+ constructor() {
142
+ }
143
+ static getInstance() {
144
+ if (!_AriariBridge.instance) {
145
+ _AriariBridge.instance = new _AriariBridge();
146
+ }
147
+ return _AriariBridge.instance;
148
+ }
149
+ config(cfg) {
150
+ setConfig(cfg);
151
+ }
152
+ get notification() {
153
+ return notificationService;
252
154
  }
253
155
  };
156
+ var Ariari = AriariBridge.getInstance();
254
157
  // Annotate the CommonJS export names for ESM import in node:
255
158
  0 && (module.exports = {
256
- ApiClient,
257
- NotifTaskService,
258
- SmsSdk,
259
- SmsService
159
+ Ariari,
160
+ getConfig,
161
+ notificationService,
162
+ setConfig
260
163
  });