@quartix/sms 1.1.0 → 1.1.4
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.cjs +49 -16
- package/dist/index.d.cts +46 -5
- package/dist/index.d.ts +46 -5
- package/dist/index.js +49 -16
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -39,7 +39,7 @@ var QuartixSmsError = class extends Error {
|
|
|
39
39
|
response;
|
|
40
40
|
};
|
|
41
41
|
var AuthenticationError = class extends QuartixSmsError {
|
|
42
|
-
constructor(message = "Invalid
|
|
42
|
+
constructor(message = "Invalid Gateway Key") {
|
|
43
43
|
super(message, 401);
|
|
44
44
|
this.name = "AuthenticationError";
|
|
45
45
|
}
|
|
@@ -55,41 +55,42 @@ var ValidationError = class extends QuartixSmsError {
|
|
|
55
55
|
var QuartixSms = class {
|
|
56
56
|
options;
|
|
57
57
|
constructor(options) {
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
const opts = typeof options === "string" ? { gatewayKey: options } : options;
|
|
59
|
+
if (!opts.gatewayKey) {
|
|
60
|
+
throw new ValidationError("Gateway Key is required");
|
|
60
61
|
}
|
|
61
62
|
this.options = {
|
|
62
|
-
|
|
63
|
-
baseUrl:
|
|
64
|
-
timeout:
|
|
63
|
+
gatewayKey: opts.gatewayKey,
|
|
64
|
+
baseUrl: opts.baseUrl?.replace(/\/$/, "") || "https://api.quartix.in",
|
|
65
|
+
timeout: opts.timeout || 3e4
|
|
65
66
|
};
|
|
66
67
|
}
|
|
67
68
|
async request(path, init) {
|
|
68
|
-
const
|
|
69
|
+
const cleanPath = path.startsWith("/") ? path.slice(1) : path;
|
|
70
|
+
const url = `${this.options.baseUrl}/${cleanPath}`;
|
|
69
71
|
const controller = new AbortController();
|
|
70
72
|
const timeoutId = setTimeout(() => controller.abort(), this.options.timeout);
|
|
71
73
|
try {
|
|
72
|
-
const response = await fetch(url
|
|
74
|
+
const response = await fetch(url, {
|
|
73
75
|
...init,
|
|
74
76
|
headers: {
|
|
75
77
|
"Content-Type": "application/json",
|
|
76
|
-
"x-gateway-
|
|
77
|
-
"
|
|
78
|
-
"User-Agent": "QuartixSmsSDK/1.0.0 (Node.js)",
|
|
78
|
+
"x-gateway-key": this.options.gatewayKey,
|
|
79
|
+
"User-Agent": "QuartixSmsSDK/1.1.0 (Node.js)",
|
|
79
80
|
...init?.headers
|
|
80
81
|
},
|
|
81
82
|
signal: controller.signal
|
|
82
83
|
});
|
|
83
84
|
const data = await response.json().catch(() => ({}));
|
|
84
85
|
if (!response.ok) {
|
|
85
|
-
if (response.status === 401) throw new AuthenticationError(data.
|
|
86
|
-
if (response.status === 400) throw new ValidationError(data.
|
|
87
|
-
throw new QuartixSmsError(data.
|
|
86
|
+
if (response.status === 401) throw new AuthenticationError(data.error || "Unauthorized");
|
|
87
|
+
if (response.status === 400) throw new ValidationError(data.error || "Bad Request");
|
|
88
|
+
throw new QuartixSmsError(data.error || "API Request Failed", response.status, data);
|
|
88
89
|
}
|
|
89
90
|
return data;
|
|
90
91
|
} catch (error) {
|
|
91
92
|
if (error.name === "AbortError") {
|
|
92
|
-
throw new QuartixSmsError(
|
|
93
|
+
throw new QuartixSmsError(`Request Timeout (${this.options.timeout}ms)`, 408);
|
|
93
94
|
}
|
|
94
95
|
if (error instanceof QuartixSmsError) throw error;
|
|
95
96
|
throw new QuartixSmsError(error.message || "Network Error");
|
|
@@ -101,7 +102,7 @@ var QuartixSms = class {
|
|
|
101
102
|
* Sends a single SMS message.
|
|
102
103
|
*/
|
|
103
104
|
async send(options) {
|
|
104
|
-
const { to, message, simIndex = 0, externalId } = options;
|
|
105
|
+
const { to, message, simIndex = 0, gatewayId, externalId } = options;
|
|
105
106
|
if (!to || !message) {
|
|
106
107
|
throw new ValidationError("Recipient and message body are required");
|
|
107
108
|
}
|
|
@@ -111,6 +112,7 @@ var QuartixSms = class {
|
|
|
111
112
|
recipient: to,
|
|
112
113
|
body: message,
|
|
113
114
|
sim_index: simIndex,
|
|
115
|
+
gatewayId,
|
|
114
116
|
external_id: externalId
|
|
115
117
|
})
|
|
116
118
|
});
|
|
@@ -120,6 +122,37 @@ var QuartixSms = class {
|
|
|
120
122
|
status: data.status || "PENDING"
|
|
121
123
|
};
|
|
122
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Sends multiple SMS messages in one batch.
|
|
127
|
+
*/
|
|
128
|
+
async sendBatch(options) {
|
|
129
|
+
if (!options.messages || !Array.isArray(options.messages) || options.messages.length === 0) {
|
|
130
|
+
throw new ValidationError("Messages array is required and cannot be empty");
|
|
131
|
+
}
|
|
132
|
+
const data = await this.request("api/sms/batch", {
|
|
133
|
+
method: "POST",
|
|
134
|
+
body: JSON.stringify({
|
|
135
|
+
messages: options.messages.map((m) => ({
|
|
136
|
+
recipient: m.to,
|
|
137
|
+
body: m.message,
|
|
138
|
+
sim_index: m.simIndex ?? 0,
|
|
139
|
+
external_id: m.externalId
|
|
140
|
+
})),
|
|
141
|
+
gatewayId: options.gatewayId
|
|
142
|
+
})
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
success: true,
|
|
146
|
+
totalSent: data.totalSent || 0,
|
|
147
|
+
totalFailed: data.totalFailed || 0,
|
|
148
|
+
results: (data.results || []).map((r) => ({
|
|
149
|
+
success: r.success,
|
|
150
|
+
messageId: r.id,
|
|
151
|
+
status: r.status || "PENDING",
|
|
152
|
+
error: r.error
|
|
153
|
+
}))
|
|
154
|
+
};
|
|
155
|
+
}
|
|
123
156
|
/**
|
|
124
157
|
* Retrieves the status of a specific message.
|
|
125
158
|
*/
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,42 @@
|
|
|
1
|
+
interface BatchSendOptions {
|
|
2
|
+
/**
|
|
3
|
+
* List of messages to send.
|
|
4
|
+
*/
|
|
5
|
+
messages: Array<{
|
|
6
|
+
/**
|
|
7
|
+
* Recipient phone number.
|
|
8
|
+
*/
|
|
9
|
+
to: string;
|
|
10
|
+
/**
|
|
11
|
+
* Message content.
|
|
12
|
+
*/
|
|
13
|
+
message: string;
|
|
14
|
+
/**
|
|
15
|
+
* Optional SIM index (0 or 1).
|
|
16
|
+
*/
|
|
17
|
+
simIndex?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Optional custom ID.
|
|
20
|
+
*/
|
|
21
|
+
externalId?: string;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Optional global Gateway ID to use for all messages in this batch.
|
|
25
|
+
*/
|
|
26
|
+
gatewayId?: string;
|
|
27
|
+
}
|
|
28
|
+
interface BatchSmsResponse {
|
|
29
|
+
success: boolean;
|
|
30
|
+
totalSent: number;
|
|
31
|
+
totalFailed: number;
|
|
32
|
+
results: Array<SmsResponse>;
|
|
33
|
+
}
|
|
1
34
|
interface QuartixSmsOptions {
|
|
2
35
|
/**
|
|
3
|
-
* Your Quartix
|
|
4
|
-
*
|
|
36
|
+
* Your Quartix Gateway Key.
|
|
37
|
+
* This identifies your company or a specific gateway.
|
|
5
38
|
*/
|
|
6
|
-
|
|
39
|
+
gatewayKey: string;
|
|
7
40
|
/**
|
|
8
41
|
* The base URL of the Quartix SMS Gateway backend.
|
|
9
42
|
* @default "https://api.quartix.in"
|
|
@@ -29,6 +62,10 @@ interface SendSmsOptions {
|
|
|
29
62
|
* @default 0
|
|
30
63
|
*/
|
|
31
64
|
simIndex?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Optional Gateway ID to use for this message.
|
|
67
|
+
*/
|
|
68
|
+
gatewayId?: string;
|
|
32
69
|
/**
|
|
33
70
|
* Optional custom ID to track this message in your own system.
|
|
34
71
|
*/
|
|
@@ -50,12 +87,16 @@ interface GatewayStats {
|
|
|
50
87
|
|
|
51
88
|
declare class QuartixSms {
|
|
52
89
|
private options;
|
|
53
|
-
constructor(options: QuartixSmsOptions);
|
|
90
|
+
constructor(options: QuartixSmsOptions | string);
|
|
54
91
|
private request;
|
|
55
92
|
/**
|
|
56
93
|
* Sends a single SMS message.
|
|
57
94
|
*/
|
|
58
95
|
send(options: SendSmsOptions): Promise<SmsResponse>;
|
|
96
|
+
/**
|
|
97
|
+
* Sends multiple SMS messages in one batch.
|
|
98
|
+
*/
|
|
99
|
+
sendBatch(options: BatchSendOptions): Promise<BatchSmsResponse>;
|
|
59
100
|
/**
|
|
60
101
|
* Retrieves the status of a specific message.
|
|
61
102
|
*/
|
|
@@ -78,4 +119,4 @@ declare class ValidationError extends QuartixSmsError {
|
|
|
78
119
|
constructor(message: string);
|
|
79
120
|
}
|
|
80
121
|
|
|
81
|
-
export { AuthenticationError, type GatewayStats, QuartixSms, QuartixSmsError, type QuartixSmsOptions, type SendSmsOptions, type SmsResponse, ValidationError };
|
|
122
|
+
export { AuthenticationError, type BatchSendOptions, type BatchSmsResponse, type GatewayStats, QuartixSms, QuartixSmsError, type QuartixSmsOptions, type SendSmsOptions, type SmsResponse, ValidationError };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,42 @@
|
|
|
1
|
+
interface BatchSendOptions {
|
|
2
|
+
/**
|
|
3
|
+
* List of messages to send.
|
|
4
|
+
*/
|
|
5
|
+
messages: Array<{
|
|
6
|
+
/**
|
|
7
|
+
* Recipient phone number.
|
|
8
|
+
*/
|
|
9
|
+
to: string;
|
|
10
|
+
/**
|
|
11
|
+
* Message content.
|
|
12
|
+
*/
|
|
13
|
+
message: string;
|
|
14
|
+
/**
|
|
15
|
+
* Optional SIM index (0 or 1).
|
|
16
|
+
*/
|
|
17
|
+
simIndex?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Optional custom ID.
|
|
20
|
+
*/
|
|
21
|
+
externalId?: string;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Optional global Gateway ID to use for all messages in this batch.
|
|
25
|
+
*/
|
|
26
|
+
gatewayId?: string;
|
|
27
|
+
}
|
|
28
|
+
interface BatchSmsResponse {
|
|
29
|
+
success: boolean;
|
|
30
|
+
totalSent: number;
|
|
31
|
+
totalFailed: number;
|
|
32
|
+
results: Array<SmsResponse>;
|
|
33
|
+
}
|
|
1
34
|
interface QuartixSmsOptions {
|
|
2
35
|
/**
|
|
3
|
-
* Your Quartix
|
|
4
|
-
*
|
|
36
|
+
* Your Quartix Gateway Key.
|
|
37
|
+
* This identifies your company or a specific gateway.
|
|
5
38
|
*/
|
|
6
|
-
|
|
39
|
+
gatewayKey: string;
|
|
7
40
|
/**
|
|
8
41
|
* The base URL of the Quartix SMS Gateway backend.
|
|
9
42
|
* @default "https://api.quartix.in"
|
|
@@ -29,6 +62,10 @@ interface SendSmsOptions {
|
|
|
29
62
|
* @default 0
|
|
30
63
|
*/
|
|
31
64
|
simIndex?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Optional Gateway ID to use for this message.
|
|
67
|
+
*/
|
|
68
|
+
gatewayId?: string;
|
|
32
69
|
/**
|
|
33
70
|
* Optional custom ID to track this message in your own system.
|
|
34
71
|
*/
|
|
@@ -50,12 +87,16 @@ interface GatewayStats {
|
|
|
50
87
|
|
|
51
88
|
declare class QuartixSms {
|
|
52
89
|
private options;
|
|
53
|
-
constructor(options: QuartixSmsOptions);
|
|
90
|
+
constructor(options: QuartixSmsOptions | string);
|
|
54
91
|
private request;
|
|
55
92
|
/**
|
|
56
93
|
* Sends a single SMS message.
|
|
57
94
|
*/
|
|
58
95
|
send(options: SendSmsOptions): Promise<SmsResponse>;
|
|
96
|
+
/**
|
|
97
|
+
* Sends multiple SMS messages in one batch.
|
|
98
|
+
*/
|
|
99
|
+
sendBatch(options: BatchSendOptions): Promise<BatchSmsResponse>;
|
|
59
100
|
/**
|
|
60
101
|
* Retrieves the status of a specific message.
|
|
61
102
|
*/
|
|
@@ -78,4 +119,4 @@ declare class ValidationError extends QuartixSmsError {
|
|
|
78
119
|
constructor(message: string);
|
|
79
120
|
}
|
|
80
121
|
|
|
81
|
-
export { AuthenticationError, type GatewayStats, QuartixSms, QuartixSmsError, type QuartixSmsOptions, type SendSmsOptions, type SmsResponse, ValidationError };
|
|
122
|
+
export { AuthenticationError, type BatchSendOptions, type BatchSmsResponse, type GatewayStats, QuartixSms, QuartixSmsError, type QuartixSmsOptions, type SendSmsOptions, type SmsResponse, ValidationError };
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var QuartixSmsError = class extends Error {
|
|
|
10
10
|
response;
|
|
11
11
|
};
|
|
12
12
|
var AuthenticationError = class extends QuartixSmsError {
|
|
13
|
-
constructor(message = "Invalid
|
|
13
|
+
constructor(message = "Invalid Gateway Key") {
|
|
14
14
|
super(message, 401);
|
|
15
15
|
this.name = "AuthenticationError";
|
|
16
16
|
}
|
|
@@ -26,41 +26,42 @@ var ValidationError = class extends QuartixSmsError {
|
|
|
26
26
|
var QuartixSms = class {
|
|
27
27
|
options;
|
|
28
28
|
constructor(options) {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const opts = typeof options === "string" ? { gatewayKey: options } : options;
|
|
30
|
+
if (!opts.gatewayKey) {
|
|
31
|
+
throw new ValidationError("Gateway Key is required");
|
|
31
32
|
}
|
|
32
33
|
this.options = {
|
|
33
|
-
|
|
34
|
-
baseUrl:
|
|
35
|
-
timeout:
|
|
34
|
+
gatewayKey: opts.gatewayKey,
|
|
35
|
+
baseUrl: opts.baseUrl?.replace(/\/$/, "") || "https://api.quartix.in",
|
|
36
|
+
timeout: opts.timeout || 3e4
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
async request(path, init) {
|
|
39
|
-
const
|
|
40
|
+
const cleanPath = path.startsWith("/") ? path.slice(1) : path;
|
|
41
|
+
const url = `${this.options.baseUrl}/${cleanPath}`;
|
|
40
42
|
const controller = new AbortController();
|
|
41
43
|
const timeoutId = setTimeout(() => controller.abort(), this.options.timeout);
|
|
42
44
|
try {
|
|
43
|
-
const response = await fetch(url
|
|
45
|
+
const response = await fetch(url, {
|
|
44
46
|
...init,
|
|
45
47
|
headers: {
|
|
46
48
|
"Content-Type": "application/json",
|
|
47
|
-
"x-gateway-
|
|
48
|
-
"
|
|
49
|
-
"User-Agent": "QuartixSmsSDK/1.0.0 (Node.js)",
|
|
49
|
+
"x-gateway-key": this.options.gatewayKey,
|
|
50
|
+
"User-Agent": "QuartixSmsSDK/1.1.0 (Node.js)",
|
|
50
51
|
...init?.headers
|
|
51
52
|
},
|
|
52
53
|
signal: controller.signal
|
|
53
54
|
});
|
|
54
55
|
const data = await response.json().catch(() => ({}));
|
|
55
56
|
if (!response.ok) {
|
|
56
|
-
if (response.status === 401) throw new AuthenticationError(data.
|
|
57
|
-
if (response.status === 400) throw new ValidationError(data.
|
|
58
|
-
throw new QuartixSmsError(data.
|
|
57
|
+
if (response.status === 401) throw new AuthenticationError(data.error || "Unauthorized");
|
|
58
|
+
if (response.status === 400) throw new ValidationError(data.error || "Bad Request");
|
|
59
|
+
throw new QuartixSmsError(data.error || "API Request Failed", response.status, data);
|
|
59
60
|
}
|
|
60
61
|
return data;
|
|
61
62
|
} catch (error) {
|
|
62
63
|
if (error.name === "AbortError") {
|
|
63
|
-
throw new QuartixSmsError(
|
|
64
|
+
throw new QuartixSmsError(`Request Timeout (${this.options.timeout}ms)`, 408);
|
|
64
65
|
}
|
|
65
66
|
if (error instanceof QuartixSmsError) throw error;
|
|
66
67
|
throw new QuartixSmsError(error.message || "Network Error");
|
|
@@ -72,7 +73,7 @@ var QuartixSms = class {
|
|
|
72
73
|
* Sends a single SMS message.
|
|
73
74
|
*/
|
|
74
75
|
async send(options) {
|
|
75
|
-
const { to, message, simIndex = 0, externalId } = options;
|
|
76
|
+
const { to, message, simIndex = 0, gatewayId, externalId } = options;
|
|
76
77
|
if (!to || !message) {
|
|
77
78
|
throw new ValidationError("Recipient and message body are required");
|
|
78
79
|
}
|
|
@@ -82,6 +83,7 @@ var QuartixSms = class {
|
|
|
82
83
|
recipient: to,
|
|
83
84
|
body: message,
|
|
84
85
|
sim_index: simIndex,
|
|
86
|
+
gatewayId,
|
|
85
87
|
external_id: externalId
|
|
86
88
|
})
|
|
87
89
|
});
|
|
@@ -91,6 +93,37 @@ var QuartixSms = class {
|
|
|
91
93
|
status: data.status || "PENDING"
|
|
92
94
|
};
|
|
93
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Sends multiple SMS messages in one batch.
|
|
98
|
+
*/
|
|
99
|
+
async sendBatch(options) {
|
|
100
|
+
if (!options.messages || !Array.isArray(options.messages) || options.messages.length === 0) {
|
|
101
|
+
throw new ValidationError("Messages array is required and cannot be empty");
|
|
102
|
+
}
|
|
103
|
+
const data = await this.request("api/sms/batch", {
|
|
104
|
+
method: "POST",
|
|
105
|
+
body: JSON.stringify({
|
|
106
|
+
messages: options.messages.map((m) => ({
|
|
107
|
+
recipient: m.to,
|
|
108
|
+
body: m.message,
|
|
109
|
+
sim_index: m.simIndex ?? 0,
|
|
110
|
+
external_id: m.externalId
|
|
111
|
+
})),
|
|
112
|
+
gatewayId: options.gatewayId
|
|
113
|
+
})
|
|
114
|
+
});
|
|
115
|
+
return {
|
|
116
|
+
success: true,
|
|
117
|
+
totalSent: data.totalSent || 0,
|
|
118
|
+
totalFailed: data.totalFailed || 0,
|
|
119
|
+
results: (data.results || []).map((r) => ({
|
|
120
|
+
success: r.success,
|
|
121
|
+
messageId: r.id,
|
|
122
|
+
status: r.status || "PENDING",
|
|
123
|
+
error: r.error
|
|
124
|
+
}))
|
|
125
|
+
};
|
|
126
|
+
}
|
|
94
127
|
/**
|
|
95
128
|
* Retrieves the status of a specific message.
|
|
96
129
|
*/
|