@ariary/notification 3.0.1 → 3.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/README.md +78 -32
- package/dist/index.d.mts +52 -25
- package/dist/index.d.ts +52 -25
- package/dist/index.js +65 -73
- package/dist/index.mjs +65 -68
- package/package.json +34 -49
package/README.md
CHANGED
|
@@ -1,56 +1,102 @@
|
|
|
1
|
+
# @ariary/notification
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
SMS and notification task SDK for the Ariary API.
|
|
3
4
|
|
|
4
|
-
##
|
|
5
|
+
## Install
|
|
5
6
|
|
|
6
7
|
```bash
|
|
7
|
-
|
|
8
|
+
yarn add @ariary/notification
|
|
8
9
|
```
|
|
9
10
|
|
|
10
|
-
##
|
|
11
|
+
## Setup
|
|
11
12
|
|
|
12
|
-
```
|
|
13
|
-
import
|
|
14
|
-
```
|
|
15
|
-
## Configuration
|
|
13
|
+
```ts
|
|
14
|
+
import Ariari from '@ariary/notification'
|
|
16
15
|
|
|
17
|
-
```typescript
|
|
18
16
|
Ariari.config({
|
|
19
17
|
projectId: 'your-project-id',
|
|
20
|
-
|
|
21
|
-
})
|
|
18
|
+
secret: 'your-secret',
|
|
19
|
+
})
|
|
22
20
|
```
|
|
23
21
|
|
|
24
|
-
## Send
|
|
22
|
+
## Send SMS
|
|
23
|
+
|
|
24
|
+
`Ariari.send()` accepts one or more messages. Phone numbers are automatically normalized (`+261340000000`, `261340000000`, `0340000000` all work).
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// single recipient
|
|
28
|
+
const task = await Ariari.send({ phone: '0340000000', message: 'Hello!' })
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
// multiple recipients, same message
|
|
31
|
+
const task = await Ariari.send({ phone: ['0340000000', '0330000000'], message: 'Hello!' })
|
|
32
|
+
|
|
33
|
+
// multiple messages at once
|
|
27
34
|
const task = await Ariari.send(
|
|
28
|
-
{ phone: '
|
|
29
|
-
{ phone:
|
|
30
|
-
)
|
|
35
|
+
{ phone: '0340000000', message: 'Hello!' },
|
|
36
|
+
{ phone: '+261330000000', message: 'Hi there!' }
|
|
37
|
+
)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Track a Task
|
|
41
|
+
|
|
42
|
+
`send()` returns a `Task` object. Call `.status()` to check delivery progress.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const task = await Ariari.send({ phone: '0340000000', message: 'Hello!' })
|
|
46
|
+
|
|
47
|
+
// status counters only
|
|
48
|
+
const status = await task.status()
|
|
49
|
+
// { total: 1, sent: 1, failed: 0, delivered: 1 }
|
|
50
|
+
|
|
51
|
+
// status + all SMS details
|
|
52
|
+
const details = await task.status(100)
|
|
53
|
+
// { task: { _id, status, createdAt, ... }, sms: [{ smsId, phone, message, status, retryCount }], total: 1 }
|
|
54
|
+
|
|
55
|
+
// paginated: 20 per page, page 2
|
|
56
|
+
const page2 = await task.status(20, 2)
|
|
31
57
|
```
|
|
32
58
|
|
|
33
|
-
|
|
59
|
+
SMS statuses: `SCHEDULED` | `PENDING` | `SENT` | `DELIVERED` | `FAILED`
|
|
60
|
+
|
|
61
|
+
## Retrieve an existing Task
|
|
34
62
|
|
|
35
|
-
|
|
36
|
-
const status = await task.status();
|
|
37
|
-
// Returns TaskSummary: { total: 1, sent: 1, failed: 0, pending: 0 }
|
|
63
|
+
Store `task.id` to retrieve it later with `.getTask()`.
|
|
38
64
|
|
|
39
|
-
|
|
40
|
-
//
|
|
65
|
+
```ts
|
|
66
|
+
// after sending
|
|
67
|
+
const task = await Ariari.send({ phone: '0340000000', message: 'Hello!' })
|
|
68
|
+
const taskId = task.id // save this
|
|
69
|
+
|
|
70
|
+
// later
|
|
71
|
+
const task = await Ariari.getTask(taskId)
|
|
72
|
+
const status = await task.status()
|
|
41
73
|
```
|
|
42
74
|
|
|
43
|
-
##
|
|
75
|
+
## Multiple instances
|
|
76
|
+
|
|
77
|
+
Use `name` to manage separate Ariari instances with different credentials. `Ariari.config()` returns the instance directly.
|
|
44
78
|
|
|
45
|
-
```
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
})
|
|
79
|
+
```ts
|
|
80
|
+
const marketing = Ariari.config({
|
|
81
|
+
name: 'marketing',
|
|
82
|
+
projectId: 'project-a',
|
|
83
|
+
secret: 'secret-a',
|
|
84
|
+
})
|
|
51
85
|
|
|
52
|
-
const
|
|
86
|
+
const alerts = Ariari.config({
|
|
87
|
+
name: 'alerts',
|
|
88
|
+
projectId: 'project-b',
|
|
89
|
+
secret: 'secret-b',
|
|
90
|
+
})
|
|
53
91
|
|
|
54
|
-
|
|
55
|
-
|
|
92
|
+
await marketing.send({ phone: '0340000000', message: 'Promo!' })
|
|
93
|
+
await alerts.send({ phone: '0330000000', message: 'Server down' })
|
|
56
94
|
```
|
|
95
|
+
|
|
96
|
+
You can also retrieve a named instance later with `Ariari.get()`.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const marketing = Ariari.get('marketing')
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Without `name`, the instance defaults to `"main"` and is used by static methods (`Ariari.send`, `Ariari.getTask`).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,46 +1,73 @@
|
|
|
1
|
+
type Data = {
|
|
2
|
+
body?: Record<string, any>;
|
|
3
|
+
params?: Record<string, any>;
|
|
4
|
+
public?: boolean;
|
|
5
|
+
};
|
|
6
|
+
type PData = Omit<Data, 'body'>;
|
|
1
7
|
type Config = {
|
|
2
8
|
projectId: string;
|
|
3
|
-
|
|
9
|
+
secret: string;
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
};
|
|
12
|
+
declare const createRequester: (config: Config) => {
|
|
13
|
+
get: <T = any>(url: string, data?: PData) => Promise<T>;
|
|
14
|
+
post: <T = any>(url: string, data?: Data) => Promise<T>;
|
|
15
|
+
patch: <T = any>(url: string, data?: Data) => Promise<T>;
|
|
16
|
+
put: <T = any>(url: string, data?: Data) => Promise<T>;
|
|
17
|
+
delete: <T = any>(url: string, data?: PData) => Promise<T>;
|
|
4
18
|
};
|
|
19
|
+
|
|
5
20
|
type Message = {
|
|
6
21
|
phone: string[] | string;
|
|
7
22
|
message: string;
|
|
8
23
|
};
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
count: number;
|
|
12
|
-
order: -1 | 1;
|
|
13
|
-
};
|
|
14
|
-
type TaskSummary = {
|
|
24
|
+
type SmsStatus = 'SCHEDULED' | 'PENDING' | 'SENT' | 'DELIVERED' | 'FAILED';
|
|
25
|
+
type TaskStatus = {
|
|
15
26
|
total: number;
|
|
16
27
|
sent: number;
|
|
17
28
|
failed: number;
|
|
18
|
-
|
|
29
|
+
delivered: number;
|
|
19
30
|
};
|
|
20
|
-
type
|
|
21
|
-
|
|
31
|
+
type TaskResponse = {
|
|
32
|
+
_id: string;
|
|
22
33
|
projectId: string;
|
|
23
|
-
|
|
24
|
-
smsDetails: Array<{
|
|
25
|
-
smsId: string;
|
|
26
|
-
phone: string;
|
|
27
|
-
message: string;
|
|
28
|
-
status: 'PENDING' | 'SENT' | 'FAILED';
|
|
29
|
-
retryCount: number;
|
|
30
|
-
}>;
|
|
34
|
+
status: TaskStatus;
|
|
31
35
|
createdAt: string;
|
|
36
|
+
scheduledAt?: string;
|
|
37
|
+
};
|
|
38
|
+
type SmsDetail = {
|
|
39
|
+
smsId: string;
|
|
40
|
+
phone: string;
|
|
41
|
+
message: string;
|
|
42
|
+
status: SmsStatus;
|
|
43
|
+
retryCount: number;
|
|
44
|
+
};
|
|
45
|
+
type TaskWithDetails = {
|
|
46
|
+
task: TaskResponse;
|
|
47
|
+
sms: SmsDetail[];
|
|
48
|
+
total: number;
|
|
32
49
|
};
|
|
33
50
|
|
|
34
51
|
declare class Task {
|
|
35
52
|
id: string;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
private _requester;
|
|
54
|
+
constructor(id: string, requester: ReturnType<typeof createRequester>);
|
|
55
|
+
get(): Promise<TaskStatus>;
|
|
56
|
+
get(count: number, page?: number): Promise<TaskWithDetails>;
|
|
39
57
|
}
|
|
40
58
|
declare class Ariari {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
59
|
+
private _config;
|
|
60
|
+
private _requester;
|
|
61
|
+
private constructor();
|
|
62
|
+
static config(nameAndConfig: Config & {
|
|
63
|
+
name?: string;
|
|
64
|
+
}): Ariari;
|
|
65
|
+
static init: typeof Ariari.config;
|
|
66
|
+
static get: (name?: string) => Ariari;
|
|
67
|
+
send: (...data: Message[]) => Promise<Task>;
|
|
68
|
+
getTask: (taskId: string) => Task;
|
|
69
|
+
static send: (...data: Message[]) => Promise<Task>;
|
|
70
|
+
static getTask: (taskId: string) => Promise<Task>;
|
|
44
71
|
}
|
|
45
72
|
|
|
46
|
-
export { Ariari
|
|
73
|
+
export { Ariari as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,73 @@
|
|
|
1
|
+
type Data = {
|
|
2
|
+
body?: Record<string, any>;
|
|
3
|
+
params?: Record<string, any>;
|
|
4
|
+
public?: boolean;
|
|
5
|
+
};
|
|
6
|
+
type PData = Omit<Data, 'body'>;
|
|
1
7
|
type Config = {
|
|
2
8
|
projectId: string;
|
|
3
|
-
|
|
9
|
+
secret: string;
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
};
|
|
12
|
+
declare const createRequester: (config: Config) => {
|
|
13
|
+
get: <T = any>(url: string, data?: PData) => Promise<T>;
|
|
14
|
+
post: <T = any>(url: string, data?: Data) => Promise<T>;
|
|
15
|
+
patch: <T = any>(url: string, data?: Data) => Promise<T>;
|
|
16
|
+
put: <T = any>(url: string, data?: Data) => Promise<T>;
|
|
17
|
+
delete: <T = any>(url: string, data?: PData) => Promise<T>;
|
|
4
18
|
};
|
|
19
|
+
|
|
5
20
|
type Message = {
|
|
6
21
|
phone: string[] | string;
|
|
7
22
|
message: string;
|
|
8
23
|
};
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
count: number;
|
|
12
|
-
order: -1 | 1;
|
|
13
|
-
};
|
|
14
|
-
type TaskSummary = {
|
|
24
|
+
type SmsStatus = 'SCHEDULED' | 'PENDING' | 'SENT' | 'DELIVERED' | 'FAILED';
|
|
25
|
+
type TaskStatus = {
|
|
15
26
|
total: number;
|
|
16
27
|
sent: number;
|
|
17
28
|
failed: number;
|
|
18
|
-
|
|
29
|
+
delivered: number;
|
|
19
30
|
};
|
|
20
|
-
type
|
|
21
|
-
|
|
31
|
+
type TaskResponse = {
|
|
32
|
+
_id: string;
|
|
22
33
|
projectId: string;
|
|
23
|
-
|
|
24
|
-
smsDetails: Array<{
|
|
25
|
-
smsId: string;
|
|
26
|
-
phone: string;
|
|
27
|
-
message: string;
|
|
28
|
-
status: 'PENDING' | 'SENT' | 'FAILED';
|
|
29
|
-
retryCount: number;
|
|
30
|
-
}>;
|
|
34
|
+
status: TaskStatus;
|
|
31
35
|
createdAt: string;
|
|
36
|
+
scheduledAt?: string;
|
|
37
|
+
};
|
|
38
|
+
type SmsDetail = {
|
|
39
|
+
smsId: string;
|
|
40
|
+
phone: string;
|
|
41
|
+
message: string;
|
|
42
|
+
status: SmsStatus;
|
|
43
|
+
retryCount: number;
|
|
44
|
+
};
|
|
45
|
+
type TaskWithDetails = {
|
|
46
|
+
task: TaskResponse;
|
|
47
|
+
sms: SmsDetail[];
|
|
48
|
+
total: number;
|
|
32
49
|
};
|
|
33
50
|
|
|
34
51
|
declare class Task {
|
|
35
52
|
id: string;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
private _requester;
|
|
54
|
+
constructor(id: string, requester: ReturnType<typeof createRequester>);
|
|
55
|
+
get(): Promise<TaskStatus>;
|
|
56
|
+
get(count: number, page?: number): Promise<TaskWithDetails>;
|
|
39
57
|
}
|
|
40
58
|
declare class Ariari {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
59
|
+
private _config;
|
|
60
|
+
private _requester;
|
|
61
|
+
private constructor();
|
|
62
|
+
static config(nameAndConfig: Config & {
|
|
63
|
+
name?: string;
|
|
64
|
+
}): Ariari;
|
|
65
|
+
static init: typeof Ariari.config;
|
|
66
|
+
static get: (name?: string) => Ariari;
|
|
67
|
+
send: (...data: Message[]) => Promise<Task>;
|
|
68
|
+
getTask: (taskId: string) => Task;
|
|
69
|
+
static send: (...data: Message[]) => Promise<Task>;
|
|
70
|
+
static getTask: (taskId: string) => Promise<Task>;
|
|
44
71
|
}
|
|
45
72
|
|
|
46
|
-
export { Ariari
|
|
73
|
+
export { Ariari as default };
|
package/dist/index.js
CHANGED
|
@@ -20,40 +20,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
|
|
24
|
-
Task: () => Task
|
|
23
|
+
default: () => src_default
|
|
25
24
|
});
|
|
26
25
|
module.exports = __toCommonJS(src_exports);
|
|
27
26
|
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// src/http/index.ts
|
|
38
|
-
async function request(method, endpoint, options = {}) {
|
|
39
|
-
const config2 = getConfig();
|
|
40
|
-
if (!config2) {
|
|
41
|
-
throw new Error("Ariari not configured. Call Ariari.config() first.");
|
|
27
|
+
// ../common/http.ts
|
|
28
|
+
async function request(verb, endpoint, options, config) {
|
|
29
|
+
if (!config) throw new Error("Ariari package not configured.");
|
|
30
|
+
const url = `${config.baseUrl || "https://back.ariari.mg"}${endpoint}`;
|
|
31
|
+
const headers = { "Content-Type": "application/json" };
|
|
32
|
+
if (!options.public) {
|
|
33
|
+
headers["x-project-id"] = config.projectId;
|
|
34
|
+
headers["x-secret"] = config.secret;
|
|
42
35
|
}
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
const headers = {
|
|
46
|
-
"Content-Type": "application/json",
|
|
47
|
-
"x-project-id": config2.projectId
|
|
48
|
-
};
|
|
49
|
-
if (options.requiresSecret !== false) {
|
|
50
|
-
headers["x-secret-id"] = config2.secretId;
|
|
51
|
-
}
|
|
52
|
-
const fetchOptions = {
|
|
53
|
-
method,
|
|
54
|
-
headers
|
|
55
|
-
};
|
|
56
|
-
if (options.body && (method === "POST" || method === "PATCH" || method === "PUT")) {
|
|
36
|
+
const fetchOptions = { method: verb, headers };
|
|
37
|
+
if ("body" in options && options.body) {
|
|
57
38
|
fetchOptions.body = JSON.stringify(options.body);
|
|
58
39
|
}
|
|
59
40
|
try {
|
|
@@ -71,57 +52,68 @@ async function request(method, endpoint, options = {}) {
|
|
|
71
52
|
throw new Error("Network error");
|
|
72
53
|
}
|
|
73
54
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
55
|
+
var createRequester = (config) => ({
|
|
56
|
+
get: (url, data = {}) => request("GET", url, data, config),
|
|
57
|
+
post: (url, data = {}) => request("POST", url, data, config),
|
|
58
|
+
patch: (url, data = {}) => request("PATCH", url, data, config),
|
|
59
|
+
put: (url, data = {}) => request("PUT", url, data, config),
|
|
60
|
+
delete: (url, data = {}) => request("DELETE", url, data, config)
|
|
61
|
+
});
|
|
80
62
|
|
|
81
|
-
//
|
|
82
|
-
|
|
63
|
+
// ../common/phone.ts
|
|
64
|
+
var normalizePhoneNumber = (phone) => {
|
|
83
65
|
let normalized = phone.replace(/\D/g, "");
|
|
84
|
-
if (normalized.startsWith("261"))
|
|
85
|
-
normalized = "0" + normalized.slice(3);
|
|
86
|
-
}
|
|
66
|
+
if (normalized.startsWith("261")) normalized = "0" + normalized.slice(3);
|
|
87
67
|
return normalized;
|
|
88
|
-
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/index.ts
|
|
71
|
+
var instances = {};
|
|
89
72
|
var Task = class {
|
|
90
|
-
constructor(id) {
|
|
73
|
+
constructor(id, requester) {
|
|
91
74
|
this.id = id;
|
|
75
|
+
this._requester = requester;
|
|
92
76
|
}
|
|
93
|
-
async
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
77
|
+
async get(count, page) {
|
|
78
|
+
if (count === void 0) {
|
|
79
|
+
const response = await this._requester.get(`/api/notif-task/${this.id}`);
|
|
80
|
+
return response.status;
|
|
81
|
+
}
|
|
82
|
+
const details = page ? `${count},${page}` : `${count}`;
|
|
83
|
+
return this._requester.get(`/api/notif-task/${this.id}?details=${details}`);
|
|
99
84
|
}
|
|
100
85
|
};
|
|
101
|
-
var
|
|
102
|
-
|
|
103
|
-
|
|
86
|
+
var _Ariari = class _Ariari {
|
|
87
|
+
constructor({ name = "main", ...config }) {
|
|
88
|
+
this.send = async (...data) => {
|
|
89
|
+
const messages = data.map((item) => ({
|
|
90
|
+
phones: (Array.isArray(item.phone) ? item.phone : [item.phone]).map(normalizePhoneNumber),
|
|
91
|
+
message: item.message
|
|
92
|
+
}));
|
|
93
|
+
const response = await this._requester.post("/api/sms/bulk", { body: { messages } });
|
|
94
|
+
if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
|
|
95
|
+
throw new Error("Invalid response: no SMS IDs returned");
|
|
96
|
+
}
|
|
97
|
+
if (!response.notifTaskId) {
|
|
98
|
+
throw new Error("Invalid response: no notifTaskId returned");
|
|
99
|
+
}
|
|
100
|
+
return new Task(response.notifTaskId, this._requester);
|
|
101
|
+
};
|
|
102
|
+
this.getTask = (taskId) => new Task(taskId, this._requester);
|
|
103
|
+
this._config = config;
|
|
104
|
+
this._requester = createRequester(config);
|
|
105
|
+
instances[name] = this;
|
|
104
106
|
}
|
|
105
|
-
static
|
|
106
|
-
|
|
107
|
-
phones: (Array.isArray(item.phone) ? item.phone : [item.phone]).map(normalizePhoneNumber),
|
|
108
|
-
message: item.message
|
|
109
|
-
}));
|
|
110
|
-
const response = await httpPost("/api/sms/bulk", { messages });
|
|
111
|
-
if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
|
|
112
|
-
throw new Error("Invalid response: no SMS IDs returned");
|
|
113
|
-
}
|
|
114
|
-
if (!response.notifTaskId) {
|
|
115
|
-
throw new Error("Invalid response: no notifTaskId returned");
|
|
116
|
-
}
|
|
117
|
-
return new Task(response.notifTaskId);
|
|
118
|
-
}
|
|
119
|
-
static async tasks(param) {
|
|
120
|
-
return httpGet(`/api/sms?from=${param.from}&count=${param.count}&order=${param.order}`);
|
|
107
|
+
static config(nameAndConfig) {
|
|
108
|
+
return new _Ariari(nameAndConfig);
|
|
121
109
|
}
|
|
122
110
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
Ariari
|
|
126
|
-
|
|
127
|
-
}
|
|
111
|
+
_Ariari.init = _Ariari.config;
|
|
112
|
+
_Ariari.get = (name = "main") => {
|
|
113
|
+
if (!instances[name]) throw new Error(`The instance of Ariari with the name "${name}" doesn't exist yet. Call Ariari.config(... )`);
|
|
114
|
+
return instances[name];
|
|
115
|
+
};
|
|
116
|
+
_Ariari.send = async (...data) => await _Ariari.get("main")?.send(...data);
|
|
117
|
+
_Ariari.getTask = async (taskId) => _Ariari.get("main")?.getTask(taskId);
|
|
118
|
+
var Ariari = _Ariari;
|
|
119
|
+
var src_default = Ariari;
|
package/dist/index.mjs
CHANGED
|
@@ -1,32 +1,14 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
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.");
|
|
1
|
+
// ../common/http.ts
|
|
2
|
+
async function request(verb, endpoint, options, config) {
|
|
3
|
+
if (!config) throw new Error("Ariari package not configured.");
|
|
4
|
+
const url = `${config.baseUrl || "https://back.ariari.mg"}${endpoint}`;
|
|
5
|
+
const headers = { "Content-Type": "application/json" };
|
|
6
|
+
if (!options.public) {
|
|
7
|
+
headers["x-project-id"] = config.projectId;
|
|
8
|
+
headers["x-secret"] = config.secret;
|
|
15
9
|
}
|
|
16
|
-
const
|
|
17
|
-
|
|
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")) {
|
|
10
|
+
const fetchOptions = { method: verb, headers };
|
|
11
|
+
if ("body" in options && options.body) {
|
|
30
12
|
fetchOptions.body = JSON.stringify(options.body);
|
|
31
13
|
}
|
|
32
14
|
try {
|
|
@@ -44,56 +26,71 @@ async function request(method, endpoint, options = {}) {
|
|
|
44
26
|
throw new Error("Network error");
|
|
45
27
|
}
|
|
46
28
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
29
|
+
var createRequester = (config) => ({
|
|
30
|
+
get: (url, data = {}) => request("GET", url, data, config),
|
|
31
|
+
post: (url, data = {}) => request("POST", url, data, config),
|
|
32
|
+
patch: (url, data = {}) => request("PATCH", url, data, config),
|
|
33
|
+
put: (url, data = {}) => request("PUT", url, data, config),
|
|
34
|
+
delete: (url, data = {}) => request("DELETE", url, data, config)
|
|
35
|
+
});
|
|
53
36
|
|
|
54
|
-
//
|
|
55
|
-
|
|
37
|
+
// ../common/phone.ts
|
|
38
|
+
var normalizePhoneNumber = (phone) => {
|
|
56
39
|
let normalized = phone.replace(/\D/g, "");
|
|
57
|
-
if (normalized.startsWith("261"))
|
|
58
|
-
normalized = "0" + normalized.slice(3);
|
|
59
|
-
}
|
|
40
|
+
if (normalized.startsWith("261")) normalized = "0" + normalized.slice(3);
|
|
60
41
|
return normalized;
|
|
61
|
-
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/index.ts
|
|
45
|
+
var instances = {};
|
|
62
46
|
var Task = class {
|
|
63
|
-
constructor(id) {
|
|
47
|
+
constructor(id, requester) {
|
|
64
48
|
this.id = id;
|
|
49
|
+
this._requester = requester;
|
|
65
50
|
}
|
|
66
|
-
async
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
51
|
+
async get(count, page) {
|
|
52
|
+
if (count === void 0) {
|
|
53
|
+
const response = await this._requester.get(`/api/notif-task/${this.id}`);
|
|
54
|
+
return response.status;
|
|
55
|
+
}
|
|
56
|
+
const details = page ? `${count},${page}` : `${count}`;
|
|
57
|
+
return this._requester.get(`/api/notif-task/${this.id}?details=${details}`);
|
|
72
58
|
}
|
|
73
59
|
};
|
|
74
|
-
var
|
|
75
|
-
|
|
76
|
-
|
|
60
|
+
var _Ariari = class _Ariari {
|
|
61
|
+
constructor({ name = "main", ...config }) {
|
|
62
|
+
this.send = async (...data) => {
|
|
63
|
+
const messages = data.map((item) => ({
|
|
64
|
+
phones: (Array.isArray(item.phone) ? item.phone : [item.phone]).map(normalizePhoneNumber),
|
|
65
|
+
message: item.message
|
|
66
|
+
}));
|
|
67
|
+
const response = await this._requester.post("/api/sms/bulk", { body: { messages } });
|
|
68
|
+
if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
|
|
69
|
+
throw new Error("Invalid response: no SMS IDs returned");
|
|
70
|
+
}
|
|
71
|
+
if (!response.notifTaskId) {
|
|
72
|
+
throw new Error("Invalid response: no notifTaskId returned");
|
|
73
|
+
}
|
|
74
|
+
return new Task(response.notifTaskId, this._requester);
|
|
75
|
+
};
|
|
76
|
+
this.getTask = (taskId) => new Task(taskId, this._requester);
|
|
77
|
+
this._config = config;
|
|
78
|
+
this._requester = createRequester(config);
|
|
79
|
+
instances[name] = this;
|
|
77
80
|
}
|
|
78
|
-
static
|
|
79
|
-
|
|
80
|
-
phones: (Array.isArray(item.phone) ? item.phone : [item.phone]).map(normalizePhoneNumber),
|
|
81
|
-
message: item.message
|
|
82
|
-
}));
|
|
83
|
-
const response = await httpPost("/api/sms/bulk", { messages });
|
|
84
|
-
if (!response.data || !Array.isArray(response.data) || response.data.length === 0) {
|
|
85
|
-
throw new Error("Invalid response: no SMS IDs returned");
|
|
86
|
-
}
|
|
87
|
-
if (!response.notifTaskId) {
|
|
88
|
-
throw new Error("Invalid response: no notifTaskId returned");
|
|
89
|
-
}
|
|
90
|
-
return new Task(response.notifTaskId);
|
|
91
|
-
}
|
|
92
|
-
static async tasks(param) {
|
|
93
|
-
return httpGet(`/api/sms?from=${param.from}&count=${param.count}&order=${param.order}`);
|
|
81
|
+
static config(nameAndConfig) {
|
|
82
|
+
return new _Ariari(nameAndConfig);
|
|
94
83
|
}
|
|
95
84
|
};
|
|
85
|
+
_Ariari.init = _Ariari.config;
|
|
86
|
+
_Ariari.get = (name = "main") => {
|
|
87
|
+
if (!instances[name]) throw new Error(`The instance of Ariari with the name "${name}" doesn't exist yet. Call Ariari.config(... )`);
|
|
88
|
+
return instances[name];
|
|
89
|
+
};
|
|
90
|
+
_Ariari.send = async (...data) => await _Ariari.get("main")?.send(...data);
|
|
91
|
+
_Ariari.getTask = async (taskId) => _Ariari.get("main")?.getTask(taskId);
|
|
92
|
+
var Ariari = _Ariari;
|
|
93
|
+
var src_default = Ariari;
|
|
96
94
|
export {
|
|
97
|
-
|
|
98
|
-
Task
|
|
95
|
+
src_default as default
|
|
99
96
|
};
|
package/package.json
CHANGED
|
@@ -1,49 +1,34 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@ariary/notification",
|
|
3
|
-
"version": "3.0.
|
|
4
|
-
"description": "SMS et Notification Task SDK pour l'API Ariary",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.mjs",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.mjs",
|
|
12
|
-
"require": "./dist/index.js"
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
"files": [
|
|
16
|
-
"dist"
|
|
17
|
-
],
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
},
|
|
36
|
-
"keywords": [
|
|
37
|
-
"ariary",
|
|
38
|
-
"sms",
|
|
39
|
-
"notification",
|
|
40
|
-
"api"
|
|
41
|
-
],
|
|
42
|
-
"author": "Ariary",
|
|
43
|
-
"license": "ISC",
|
|
44
|
-
"dependencies": {},
|
|
45
|
-
"devDependencies": {
|
|
46
|
-
"tsup": "^8.5.1",
|
|
47
|
-
"typescript": "^5.9.3"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@ariary/notification",
|
|
3
|
+
"version": "3.0.2",
|
|
4
|
+
"description": "SMS et Notification Task SDK pour l'API Ariary",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"ariary",
|
|
23
|
+
"sms",
|
|
24
|
+
"notification",
|
|
25
|
+
"api"
|
|
26
|
+
],
|
|
27
|
+
"author": "Ariary",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"dependencies": {},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"tsup": "^8.5.1",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
}
|
|
34
|
+
}
|