@quartix/sms 1.1.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/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # @quartix/sms
2
+
3
+ The official Node.js SDK for the Quartix SMS Gateway. Send SMS messages programmatically through your own Android devices.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @quartix/sms
9
+ ```
10
+
11
+ ## Quick Start (ID-only Auth)
12
+
13
+ ```typescript
14
+ import { QuartixSms } from '@quartix/sms';
15
+
16
+ // Use your Gateway ID as the API Key for simplified setup
17
+ const sms = new QuartixSms({
18
+ apiKey: 'YOUR_GATEWAY_ID',
19
+ baseUrl: 'https://api.quartix.in' // Optional
20
+ });
21
+
22
+ async function main() {
23
+ try {
24
+ const result = await sms.send({
25
+ to: '+919876543210',
26
+ message: 'Hello from Quartix SDK!'
27
+ });
28
+
29
+ console.log('Message sent! ID:', result.messageId);
30
+ } catch (error) {
31
+ console.error('Failed to send SMS:', error.message);
32
+ }
33
+ }
34
+
35
+ main();
36
+ ```
37
+
38
+ ## Batch Sending
39
+
40
+ Send multiple messages in a single API call for maximum efficiency.
41
+
42
+ ```typescript
43
+ const batchResult = await sms.sendBatch({
44
+ messages: [
45
+ { to: '+919000000001', message: 'Alert 1' },
46
+ { to: '+919000000002', message: 'Alert 2' },
47
+ ]
48
+ });
49
+
50
+ console.log(`Successfully queued ${batchResult.totalSent} messages.`);
51
+ ```
52
+
53
+ ## Features
54
+
55
+ - **Simplified Auth**: Use your Gateway ID directly as the token.
56
+ - **Send SMS**: Queue messages to be sent by your connected Android devices.
57
+ - **Batch Operations**: Send messages to multiple recipients in one go.
58
+ - **Message Status**: Track if your message is PENDING, SENT, or DELIVERED.
59
+ - **TypeScript Support**: Full type safety out of the box.
60
+
61
+ ## API Reference
62
+
63
+ ### `new QuartixSms(options)`
64
+ - `apiKey`: Your Gateway ID or Account API Key.
65
+ - `baseUrl`: The API endpoint (defaults to production).
66
+ - `timeout`: Request timeout in ms (default: 30000).
67
+
68
+ ### `sms.send(options)`
69
+ - `to`: Recipient phone number.
70
+ - `message`: Text content.
71
+ - `simIndex`: (Optional) Use specific SIM slot (0 or 1).
72
+ - `gatewayId`: (Optional) Override targeted gateway.
73
+
74
+ ### `sms.sendBatch(options)`
75
+ - `messages`: Array of message objects (to, message, simIndex).
76
+ - `gatewayId`: (Optional) Global gateway override for the batch.
77
+
78
+ ### `sms.getMessageStatus(id)`
79
+ - Returns the current status of a message.
80
+
81
+ ## License
82
+
83
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AuthenticationError: () => AuthenticationError,
24
+ QuartixSms: () => QuartixSms,
25
+ QuartixSmsError: () => QuartixSmsError,
26
+ ValidationError: () => ValidationError
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/errors.ts
31
+ var QuartixSmsError = class extends Error {
32
+ constructor(message, statusCode, response) {
33
+ super(message);
34
+ this.statusCode = statusCode;
35
+ this.response = response;
36
+ this.name = "QuartixSmsError";
37
+ }
38
+ statusCode;
39
+ response;
40
+ };
41
+ var AuthenticationError = class extends QuartixSmsError {
42
+ constructor(message = "Invalid API Key") {
43
+ super(message, 401);
44
+ this.name = "AuthenticationError";
45
+ }
46
+ };
47
+ var ValidationError = class extends QuartixSmsError {
48
+ constructor(message) {
49
+ super(message, 400);
50
+ this.name = "ValidationError";
51
+ }
52
+ };
53
+
54
+ // src/client.ts
55
+ var QuartixSms = class {
56
+ options;
57
+ constructor(options) {
58
+ if (!options.apiKey) {
59
+ throw new ValidationError("API Key is required");
60
+ }
61
+ this.options = {
62
+ apiKey: options.apiKey,
63
+ baseUrl: options.baseUrl || "https://api.quartix.in",
64
+ timeout: options.timeout || 3e4
65
+ };
66
+ }
67
+ async request(path, init) {
68
+ const url = new URL(path, this.options.baseUrl.endsWith("/") ? this.options.baseUrl : `${this.options.baseUrl}/`);
69
+ const controller = new AbortController();
70
+ const timeoutId = setTimeout(() => controller.abort(), this.options.timeout);
71
+ try {
72
+ const response = await fetch(url.toString(), {
73
+ ...init,
74
+ headers: {
75
+ "Content-Type": "application/json",
76
+ "x-gateway-api-key": this.options.apiKey,
77
+ "Authorization": `Bearer ${this.options.apiKey}`,
78
+ "User-Agent": "QuartixSmsSDK/1.0.0 (Node.js)",
79
+ ...init?.headers
80
+ },
81
+ signal: controller.signal
82
+ });
83
+ const data = await response.json().catch(() => ({}));
84
+ if (!response.ok) {
85
+ if (response.status === 401) throw new AuthenticationError(data.message);
86
+ if (response.status === 400) throw new ValidationError(data.message);
87
+ throw new QuartixSmsError(data.message || "API Request Failed", response.status, data);
88
+ }
89
+ return data;
90
+ } catch (error) {
91
+ if (error.name === "AbortError") {
92
+ throw new QuartixSmsError("Request Timeout", 408);
93
+ }
94
+ if (error instanceof QuartixSmsError) throw error;
95
+ throw new QuartixSmsError(error.message || "Network Error");
96
+ } finally {
97
+ clearTimeout(timeoutId);
98
+ }
99
+ }
100
+ /**
101
+ * Sends a single SMS message.
102
+ */
103
+ async send(options) {
104
+ const { to, message, simIndex = 0, externalId } = options;
105
+ if (!to || !message) {
106
+ throw new ValidationError("Recipient and message body are required");
107
+ }
108
+ const data = await this.request("api/sms/send", {
109
+ method: "POST",
110
+ body: JSON.stringify({
111
+ recipient: to,
112
+ body: message,
113
+ sim_index: simIndex,
114
+ external_id: externalId
115
+ })
116
+ });
117
+ return {
118
+ success: true,
119
+ messageId: data.id,
120
+ status: data.status || "PENDING"
121
+ };
122
+ }
123
+ /**
124
+ * Retrieves the status of a specific message.
125
+ */
126
+ async getMessageStatus(messageId) {
127
+ const data = await this.request(`api/sms/messages/${messageId}`);
128
+ return {
129
+ success: true,
130
+ messageId: data.id,
131
+ status: data.status
132
+ };
133
+ }
134
+ /**
135
+ * Retrieves overall statistics for the account/gateway.
136
+ */
137
+ async getStats() {
138
+ return this.request("api/sms/stats");
139
+ }
140
+ };
141
+ // Annotate the CommonJS export names for ESM import in node:
142
+ 0 && (module.exports = {
143
+ AuthenticationError,
144
+ QuartixSms,
145
+ QuartixSmsError,
146
+ ValidationError
147
+ });
@@ -0,0 +1,81 @@
1
+ interface QuartixSmsOptions {
2
+ /**
3
+ * Your Quartix API Key.
4
+ * Can be an Account-level API Key or a Gateway-specific API Key.
5
+ */
6
+ apiKey: string;
7
+ /**
8
+ * The base URL of the Quartix SMS Gateway backend.
9
+ * @default "https://api.quartix.in"
10
+ */
11
+ baseUrl?: string;
12
+ /**
13
+ * Optional timeout for requests in milliseconds.
14
+ * @default 30000
15
+ */
16
+ timeout?: number;
17
+ }
18
+ interface SendSmsOptions {
19
+ /**
20
+ * The recipient phone number in E.164 format (e.g., +919876543210).
21
+ */
22
+ to: string;
23
+ /**
24
+ * The message content.
25
+ */
26
+ message: string;
27
+ /**
28
+ * Optional SIM index to use (0 or 1).
29
+ * @default 0
30
+ */
31
+ simIndex?: number;
32
+ /**
33
+ * Optional custom ID to track this message in your own system.
34
+ */
35
+ externalId?: string;
36
+ }
37
+ interface SmsResponse {
38
+ success: boolean;
39
+ messageId?: string;
40
+ status: 'PENDING' | 'SENT' | 'DELIVERED' | 'FAILED';
41
+ error?: string;
42
+ }
43
+ interface GatewayStats {
44
+ totalSent: number;
45
+ totalPending: number;
46
+ totalFailed: number;
47
+ isActive: boolean;
48
+ lastSeen?: string;
49
+ }
50
+
51
+ declare class QuartixSms {
52
+ private options;
53
+ constructor(options: QuartixSmsOptions);
54
+ private request;
55
+ /**
56
+ * Sends a single SMS message.
57
+ */
58
+ send(options: SendSmsOptions): Promise<SmsResponse>;
59
+ /**
60
+ * Retrieves the status of a specific message.
61
+ */
62
+ getMessageStatus(messageId: string): Promise<SmsResponse>;
63
+ /**
64
+ * Retrieves overall statistics for the account/gateway.
65
+ */
66
+ getStats(): Promise<GatewayStats>;
67
+ }
68
+
69
+ declare class QuartixSmsError extends Error {
70
+ statusCode?: number | undefined;
71
+ response?: any | undefined;
72
+ constructor(message: string, statusCode?: number | undefined, response?: any | undefined);
73
+ }
74
+ declare class AuthenticationError extends QuartixSmsError {
75
+ constructor(message?: string);
76
+ }
77
+ declare class ValidationError extends QuartixSmsError {
78
+ constructor(message: string);
79
+ }
80
+
81
+ export { AuthenticationError, type GatewayStats, QuartixSms, QuartixSmsError, type QuartixSmsOptions, type SendSmsOptions, type SmsResponse, ValidationError };
@@ -0,0 +1,81 @@
1
+ interface QuartixSmsOptions {
2
+ /**
3
+ * Your Quartix API Key.
4
+ * Can be an Account-level API Key or a Gateway-specific API Key.
5
+ */
6
+ apiKey: string;
7
+ /**
8
+ * The base URL of the Quartix SMS Gateway backend.
9
+ * @default "https://api.quartix.in"
10
+ */
11
+ baseUrl?: string;
12
+ /**
13
+ * Optional timeout for requests in milliseconds.
14
+ * @default 30000
15
+ */
16
+ timeout?: number;
17
+ }
18
+ interface SendSmsOptions {
19
+ /**
20
+ * The recipient phone number in E.164 format (e.g., +919876543210).
21
+ */
22
+ to: string;
23
+ /**
24
+ * The message content.
25
+ */
26
+ message: string;
27
+ /**
28
+ * Optional SIM index to use (0 or 1).
29
+ * @default 0
30
+ */
31
+ simIndex?: number;
32
+ /**
33
+ * Optional custom ID to track this message in your own system.
34
+ */
35
+ externalId?: string;
36
+ }
37
+ interface SmsResponse {
38
+ success: boolean;
39
+ messageId?: string;
40
+ status: 'PENDING' | 'SENT' | 'DELIVERED' | 'FAILED';
41
+ error?: string;
42
+ }
43
+ interface GatewayStats {
44
+ totalSent: number;
45
+ totalPending: number;
46
+ totalFailed: number;
47
+ isActive: boolean;
48
+ lastSeen?: string;
49
+ }
50
+
51
+ declare class QuartixSms {
52
+ private options;
53
+ constructor(options: QuartixSmsOptions);
54
+ private request;
55
+ /**
56
+ * Sends a single SMS message.
57
+ */
58
+ send(options: SendSmsOptions): Promise<SmsResponse>;
59
+ /**
60
+ * Retrieves the status of a specific message.
61
+ */
62
+ getMessageStatus(messageId: string): Promise<SmsResponse>;
63
+ /**
64
+ * Retrieves overall statistics for the account/gateway.
65
+ */
66
+ getStats(): Promise<GatewayStats>;
67
+ }
68
+
69
+ declare class QuartixSmsError extends Error {
70
+ statusCode?: number | undefined;
71
+ response?: any | undefined;
72
+ constructor(message: string, statusCode?: number | undefined, response?: any | undefined);
73
+ }
74
+ declare class AuthenticationError extends QuartixSmsError {
75
+ constructor(message?: string);
76
+ }
77
+ declare class ValidationError extends QuartixSmsError {
78
+ constructor(message: string);
79
+ }
80
+
81
+ export { AuthenticationError, type GatewayStats, QuartixSms, QuartixSmsError, type QuartixSmsOptions, type SendSmsOptions, type SmsResponse, ValidationError };
package/dist/index.js ADDED
@@ -0,0 +1,117 @@
1
+ // src/errors.ts
2
+ var QuartixSmsError = class extends Error {
3
+ constructor(message, statusCode, response) {
4
+ super(message);
5
+ this.statusCode = statusCode;
6
+ this.response = response;
7
+ this.name = "QuartixSmsError";
8
+ }
9
+ statusCode;
10
+ response;
11
+ };
12
+ var AuthenticationError = class extends QuartixSmsError {
13
+ constructor(message = "Invalid API Key") {
14
+ super(message, 401);
15
+ this.name = "AuthenticationError";
16
+ }
17
+ };
18
+ var ValidationError = class extends QuartixSmsError {
19
+ constructor(message) {
20
+ super(message, 400);
21
+ this.name = "ValidationError";
22
+ }
23
+ };
24
+
25
+ // src/client.ts
26
+ var QuartixSms = class {
27
+ options;
28
+ constructor(options) {
29
+ if (!options.apiKey) {
30
+ throw new ValidationError("API Key is required");
31
+ }
32
+ this.options = {
33
+ apiKey: options.apiKey,
34
+ baseUrl: options.baseUrl || "https://api.quartix.in",
35
+ timeout: options.timeout || 3e4
36
+ };
37
+ }
38
+ async request(path, init) {
39
+ const url = new URL(path, this.options.baseUrl.endsWith("/") ? this.options.baseUrl : `${this.options.baseUrl}/`);
40
+ const controller = new AbortController();
41
+ const timeoutId = setTimeout(() => controller.abort(), this.options.timeout);
42
+ try {
43
+ const response = await fetch(url.toString(), {
44
+ ...init,
45
+ headers: {
46
+ "Content-Type": "application/json",
47
+ "x-gateway-api-key": this.options.apiKey,
48
+ "Authorization": `Bearer ${this.options.apiKey}`,
49
+ "User-Agent": "QuartixSmsSDK/1.0.0 (Node.js)",
50
+ ...init?.headers
51
+ },
52
+ signal: controller.signal
53
+ });
54
+ const data = await response.json().catch(() => ({}));
55
+ if (!response.ok) {
56
+ if (response.status === 401) throw new AuthenticationError(data.message);
57
+ if (response.status === 400) throw new ValidationError(data.message);
58
+ throw new QuartixSmsError(data.message || "API Request Failed", response.status, data);
59
+ }
60
+ return data;
61
+ } catch (error) {
62
+ if (error.name === "AbortError") {
63
+ throw new QuartixSmsError("Request Timeout", 408);
64
+ }
65
+ if (error instanceof QuartixSmsError) throw error;
66
+ throw new QuartixSmsError(error.message || "Network Error");
67
+ } finally {
68
+ clearTimeout(timeoutId);
69
+ }
70
+ }
71
+ /**
72
+ * Sends a single SMS message.
73
+ */
74
+ async send(options) {
75
+ const { to, message, simIndex = 0, externalId } = options;
76
+ if (!to || !message) {
77
+ throw new ValidationError("Recipient and message body are required");
78
+ }
79
+ const data = await this.request("api/sms/send", {
80
+ method: "POST",
81
+ body: JSON.stringify({
82
+ recipient: to,
83
+ body: message,
84
+ sim_index: simIndex,
85
+ external_id: externalId
86
+ })
87
+ });
88
+ return {
89
+ success: true,
90
+ messageId: data.id,
91
+ status: data.status || "PENDING"
92
+ };
93
+ }
94
+ /**
95
+ * Retrieves the status of a specific message.
96
+ */
97
+ async getMessageStatus(messageId) {
98
+ const data = await this.request(`api/sms/messages/${messageId}`);
99
+ return {
100
+ success: true,
101
+ messageId: data.id,
102
+ status: data.status
103
+ };
104
+ }
105
+ /**
106
+ * Retrieves overall statistics for the account/gateway.
107
+ */
108
+ async getStats() {
109
+ return this.request("api/sms/stats");
110
+ }
111
+ };
112
+ export {
113
+ AuthenticationError,
114
+ QuartixSms,
115
+ QuartixSmsError,
116
+ ValidationError
117
+ };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@quartix/sms",
3
+ "version": "1.1.0",
4
+ "description": "Quartix SMS SDK for Node.js and Browser",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
22
+ "dev": "tsup src/index.ts --format cjs,esm --watch --dts",
23
+ "lint": "eslint src/**/*.ts",
24
+ "test": "vitest run"
25
+ },
26
+ "keywords": [
27
+ "sms",
28
+ "gateway",
29
+ "quartix",
30
+ "sdk",
31
+ "messaging"
32
+ ],
33
+ "author": "Quartix Engineering",
34
+ "license": "MIT",
35
+ "devDependencies": {
36
+ "tsup": "^8.0.2",
37
+ "typescript": "^5.4.5",
38
+ "vitest": "^1.6.0",
39
+ "@types/node": "^20.12.7"
40
+ },
41
+ "dependencies": {}
42
+ }