stackverify 1.0.3 → 2.0.0
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/{src/README.md → README.md} +4 -1
- package/package.json +1 -1
- package/src/index.js +47 -8
|
@@ -8,6 +8,10 @@ A simple Node.js SDK to interact with the StackVerify API for sending SMS and ch
|
|
|
8
8
|
npm install stackverify
|
|
9
9
|
```
|
|
10
10
|
# Usage
|
|
11
|
+
|
|
12
|
+
Get your apikey .
|
|
13
|
+
- [@stackverify](https://stackverify.site)
|
|
14
|
+
|
|
11
15
|
```
|
|
12
16
|
import StackVerify from "stackverify";
|
|
13
17
|
|
|
@@ -73,7 +77,6 @@ messageId — ID returned by sendSMS (required)
|
|
|
73
77
|
- [@samwuel simiyu](https://github.com/Trojan-254)
|
|
74
78
|
|
|
75
79
|
## 🔗 Links
|
|
76
|
-
[](https://stackverify.site/)
|
|
77
80
|
[](https://www.linkedin.com/)
|
|
78
81
|
[](https://twitter.com/)
|
|
79
82
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,8 +3,10 @@ import fetch from "node-fetch";
|
|
|
3
3
|
class StackVerify {
|
|
4
4
|
constructor({ apiKey, baseUrl = "https://stackverify.site/api/v1" }) {
|
|
5
5
|
if (!apiKey) throw new Error("API key is required");
|
|
6
|
+
|
|
6
7
|
this.apiKey = apiKey;
|
|
7
8
|
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
9
|
+
this.mode = apiKey.startsWith("sk_test_") ? "test" : "live";
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
async _request(path, options = {}) {
|
|
@@ -16,26 +18,63 @@ class StackVerify {
|
|
|
16
18
|
...options,
|
|
17
19
|
});
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
let data;
|
|
22
|
+
try {
|
|
23
|
+
data = await res.json();
|
|
24
|
+
} catch {
|
|
25
|
+
data = null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!res.ok) {
|
|
29
|
+
const error = new Error(data?.message || "StackVerify API Error");
|
|
30
|
+
error.status = res.status;
|
|
31
|
+
error.error = data?.error || null;
|
|
32
|
+
error.retry_after = data?.retry_after || null;
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
20
35
|
|
|
21
|
-
|
|
22
|
-
try { return JSON.parse(text); } catch { return text; }
|
|
36
|
+
return data;
|
|
23
37
|
}
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Send SMS
|
|
41
|
+
* Required permission: sms:send
|
|
42
|
+
*/
|
|
43
|
+
async sendSMS({
|
|
44
|
+
recipients,
|
|
45
|
+
body,
|
|
46
|
+
sender_id,
|
|
47
|
+
templateId,
|
|
48
|
+
scheduleAt,
|
|
49
|
+
}) {
|
|
50
|
+
if (!Array.isArray(recipients) || recipients.length === 0)
|
|
27
51
|
throw new Error("recipients must be a non-empty array");
|
|
28
|
-
|
|
29
|
-
if (!
|
|
52
|
+
|
|
53
|
+
if (!body && !templateId)
|
|
54
|
+
throw new Error("Either body or templateId is required");
|
|
55
|
+
|
|
56
|
+
if (!sender_id)
|
|
57
|
+
throw new Error("sender_id is required");
|
|
30
58
|
|
|
31
59
|
return this._request("/sms/send", {
|
|
32
60
|
method: "POST",
|
|
33
|
-
body: JSON.stringify({
|
|
61
|
+
body: JSON.stringify({
|
|
62
|
+
recipients,
|
|
63
|
+
body,
|
|
64
|
+
sender_id,
|
|
65
|
+
templateId,
|
|
66
|
+
scheduleAt,
|
|
67
|
+
}),
|
|
34
68
|
});
|
|
35
69
|
}
|
|
36
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Get SMS Status
|
|
73
|
+
* Required permission: sms:read
|
|
74
|
+
*/
|
|
37
75
|
async getSMSStatus(messageId) {
|
|
38
76
|
if (!messageId) throw new Error("messageId is required");
|
|
77
|
+
|
|
39
78
|
return this._request(`/sms/status/${encodeURIComponent(messageId)}`);
|
|
40
79
|
}
|
|
41
80
|
}
|