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.
@@ -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
- [![website](https://img.shields.io/badge/my_portfolio-000?style=for-the-badge&logo=ko-fi&logoColor=white)](https://stackverify.site/)
77
80
  [![linkedin](https://img.shields.io/badge/linkedin-0A66C2?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/)
78
81
  [![twitter](https://img.shields.io/badge/twitter-1DA1F2?style=for-the-badge&logo=twitter&logoColor=white)](https://twitter.com/)
79
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stackverify",
3
- "version": "1.0.3",
3
+ "version": "2.0.0",
4
4
  "description": "Node.js SDK for StackVerify API (SMS, Email, WhatsApp)",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
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
- const text = await res.text().catch(() => null);
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
- if (!res.ok) throw new Error(`StackVerify SDK Error: ${text || res.status}`);
22
- try { return JSON.parse(text); } catch { return text; }
36
+ return data;
23
37
  }
24
38
 
25
- async sendSMS({ recipients, body, sender_id }) {
26
- if (!recipients || !Array.isArray(recipients) || recipients.length === 0)
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
- if (!body) throw new Error("body is required");
29
- if (!sender_id) throw new Error("sender_id is required");
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({ recipients, body, sender_id }),
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
  }