fitsms 1.0.2 → 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/index.js +39 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -24,13 +24,48 @@ class FitSMS {
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Send an SMS (v3 API)
|
|
27
|
-
* @param {string|string[]} recipients - e.g., "
|
|
27
|
+
* @param {string|string[]} recipients - e.g., "9476XXXXX,9476XXXXX"
|
|
28
28
|
* @param {string} message - The SMS body
|
|
29
29
|
*/
|
|
30
30
|
async send(recipients, message, type = "plain") {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
const allowedTypes = ["plain", "unicode"];
|
|
32
|
+
if (!allowedTypes.includes(type)) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`FitSMS Validation Error: Invalid type '${type}'. Use 'plain' or 'unicode'.`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 2. Process and Validate Recipients
|
|
39
|
+
let rawList = Array.isArray(recipients)
|
|
40
|
+
? recipients
|
|
41
|
+
: recipients.split(",");
|
|
42
|
+
|
|
43
|
+
const validatedNumbers = rawList.map((num) => {
|
|
44
|
+
// Remove all non-numeric characters (spaces, +, dashes)
|
|
45
|
+
let cleaned = num.replace(/\D/g, "");
|
|
46
|
+
|
|
47
|
+
// Convert local 07... format to 947...
|
|
48
|
+
if (cleaned.startsWith("07") && cleaned.length === 10) {
|
|
49
|
+
cleaned = "94" + cleaned.substring(1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Convert 7... format to 947...
|
|
53
|
+
if (cleaned.startsWith("7") && cleaned.length === 9) {
|
|
54
|
+
cleaned = "94" + cleaned;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Final Sri Lankan Format Check (947XXXXXXXX)
|
|
58
|
+
const slRegex = /^(94)(7[01245678])\d{7}$/;
|
|
59
|
+
if (!slRegex.test(cleaned)) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`FitSMS Validation Error: Invalid Sri Lankan number found: ${num}`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return cleaned;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const recipientList = validatedNumbers.join(",");
|
|
34
69
|
|
|
35
70
|
try {
|
|
36
71
|
const response = await axios.post(
|