unismsgateway 1.3.0 → 1.3.2

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.
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ /**
3
+ * Live integration test script for unismsgateway.
4
+ *
5
+ * Usage:
6
+ * 1. Copy .env.example to .env and fill in your credentials.
7
+ * 2. npm run test:live
8
+ *
9
+ * Set TEST_SEND=true in .env (or inline) to actually send an SMS.
10
+ * Without it, only init and balance checks run.
11
+ */
12
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || function (mod) {
25
+ if (mod && mod.__esModule) return mod;
26
+ var result = {};
27
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
+ __setModuleDefault(result, mod);
29
+ return result;
30
+ };
31
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
32
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
33
+ return new (P || (P = Promise))(function (resolve, reject) {
34
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
35
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
36
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
37
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
38
+ });
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ const dotenv = __importStar(require("dotenv"));
42
+ dotenv.config();
43
+ const lib_1 = require("../src/lib/lib");
44
+ // ─── Colour helpers ──────────────────────────────────────────────────────────
45
+ const GREEN = '\x1b[32m';
46
+ const RED = '\x1b[31m';
47
+ const YELLOW = '\x1b[33m';
48
+ const CYAN = '\x1b[36m';
49
+ const BOLD = '\x1b[1m';
50
+ const RESET = '\x1b[0m';
51
+ const pass = (msg) => console.log(` ${GREEN}✔${RESET} ${msg}`);
52
+ const fail = (msg) => console.log(` ${RED}✖${RESET} ${msg}`);
53
+ const info = (msg) => console.log(` ${CYAN}ℹ${RESET} ${msg}`);
54
+ const warn = (msg) => console.log(` ${YELLOW}⚠${RESET} ${msg}`);
55
+ const title = (msg) => console.log(`\n${BOLD}${msg}${RESET}`);
56
+ // ─── Env helpers ─────────────────────────────────────────────────────────────
57
+ function env(key) {
58
+ return process.env[key] || undefined;
59
+ }
60
+ function requireEnv(key) {
61
+ const val = process.env[key];
62
+ if (!val)
63
+ throw new Error(`Missing required env var: ${key}`);
64
+ return val;
65
+ }
66
+ // ─── Test counters ───────────────────────────────────────────────────────────
67
+ let passed = 0;
68
+ let failed = 0;
69
+ function runTest(label, fn) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ try {
72
+ yield fn();
73
+ passed++;
74
+ }
75
+ catch (err) {
76
+ failed++;
77
+ fail(`${label}: ${err instanceof Error ? err.message : String(err)}`);
78
+ }
79
+ });
80
+ }
81
+ // ─── Per-platform tests ───────────────────────────────────────────────────────
82
+ function testPlatform(platformId) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ title(`Platform: ${platformId.toUpperCase()}`);
85
+ let platform;
86
+ // 1. Initialisation
87
+ yield runTest('Init / config validation', () => __awaiter(this, void 0, void 0, function* () {
88
+ const param = {};
89
+ switch (platformId) {
90
+ case 'nest':
91
+ param.apiKey = requireEnv('NEST_API_KEY');
92
+ if (env('NEST_HOST'))
93
+ param.host = env('NEST_HOST');
94
+ if (env('NEST_PROTOCOL'))
95
+ param.protocol = env('NEST_PROTOCOL');
96
+ break;
97
+ case 'hubtel':
98
+ param.clientId = requireEnv('HUBTEL_CLIENT_ID');
99
+ param.clientSecret = requireEnv('HUBTEL_CLIENT_SECRET');
100
+ break;
101
+ case 'route':
102
+ param.username = requireEnv('ROUTE_USERNAME');
103
+ param.password = requireEnv('ROUTE_PASSWORD');
104
+ if (env('ROUTE_HOST'))
105
+ param.host = env('ROUTE_HOST');
106
+ if (env('ROUTE_PORT'))
107
+ param.port = Number(env('ROUTE_PORT'));
108
+ if (env('ROUTE_PROTOCOL'))
109
+ param.protocol = env('ROUTE_PROTOCOL');
110
+ break;
111
+ }
112
+ platform = (0, lib_1.init)({ platformId, param: param });
113
+ pass(`Initialized ${platformId} platform`);
114
+ }));
115
+ // 2. Balance check (only nest and hubtel expose getBalance)
116
+ if (platformId === 'nest' || platformId === 'hubtel') {
117
+ yield runTest('getBalance()', () => __awaiter(this, void 0, void 0, function* () {
118
+ const gateway = platform.getGateway();
119
+ if (!gateway.getBalance) {
120
+ warn('getBalance not implemented — skipped');
121
+ return;
122
+ }
123
+ const balance = yield gateway.getBalance();
124
+ pass(`Balance retrieved: ${JSON.stringify(balance)}`);
125
+ }));
126
+ }
127
+ else {
128
+ warn(`getBalance not supported by '${platformId}' — skipped`);
129
+ }
130
+ // 3. Send SMS (opt-in via TEST_SEND=true)
131
+ const shouldSend = env('TEST_SEND') === 'true';
132
+ if (!shouldSend) {
133
+ warn('TEST_SEND is not set to true — skipping live send');
134
+ return;
135
+ }
136
+ yield runTest('quickSend()', () => __awaiter(this, void 0, void 0, function* () {
137
+ const sendParams = {
138
+ From: requireEnv('TEST_FROM'),
139
+ To: requireEnv('TEST_TO'),
140
+ Content: env('TEST_CONTENT') || 'unismsgateway live test — please ignore.'
141
+ };
142
+ info(`Sending from "${sendParams.From}" to "${sendParams.To}"...`);
143
+ const result = yield platform.quickSend(sendParams);
144
+ if (!result.success) {
145
+ throw new Error(result.error || `Send failed: ${JSON.stringify(result)}`);
146
+ }
147
+ pass(`Message sent. Result: ${JSON.stringify(result)}`);
148
+ }));
149
+ });
150
+ }
151
+ // ─── Entry point ─────────────────────────────────────────────────────────────
152
+ function main() {
153
+ return __awaiter(this, void 0, void 0, function* () {
154
+ console.log(`\n${BOLD}═══════════════════════════════════════${RESET}`);
155
+ console.log(`${BOLD} unismsgateway — live test runner ${RESET}`);
156
+ console.log(`${BOLD}═══════════════════════════════════════${RESET}`);
157
+ const platformArg = env('GATEWAY_PLATFORM');
158
+ const platforms = platformArg
159
+ ? [platformArg]
160
+ : (env('TEST_ALL') === 'true' ? ['nest', 'hubtel', 'route'] : []);
161
+ if (platforms.length === 0) {
162
+ console.log(`\n${YELLOW}No platform selected.${RESET}`);
163
+ console.log('Set GATEWAY_PLATFORM=nest|hubtel|route in your .env file,');
164
+ console.log('or set TEST_ALL=true to test all configured platforms.\n');
165
+ process.exit(1);
166
+ }
167
+ for (const p of platforms) {
168
+ try {
169
+ yield testPlatform(p);
170
+ }
171
+ catch (_a) {
172
+ // individual test errors already captured above
173
+ }
174
+ }
175
+ // ─── Summary ────────────────────────────────────────────────────────────
176
+ title('Summary');
177
+ if (passed > 0)
178
+ pass(`${passed} check(s) passed`);
179
+ if (failed > 0)
180
+ fail(`${failed} check(s) failed`);
181
+ console.log();
182
+ process.exit(failed > 0 ? 1 : 0);
183
+ });
184
+ }
185
+ main().catch((err) => {
186
+ console.error(`\n${RED}Unexpected error:${RESET}`, err);
187
+ process.exit(1);
188
+ });
@@ -0,0 +1 @@
1
+ export * from './lib/lib';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./lib/lib"), exports);
@@ -0,0 +1,13 @@
1
+ import { ISmsGatewayDelegate, QuickSendParams, SendResult } from './types';
2
+ export interface HubtelSmsGatewayConfig {
3
+ clientId: string;
4
+ clientSecret: string;
5
+ }
6
+ /**
7
+ * Wraps hubtel-sms-extended and maps API responses to {@link SendResult}.
8
+ */
9
+ export declare class HubtelSmsGateway implements ISmsGatewayDelegate {
10
+ private _client;
11
+ constructor(config: HubtelSmsGatewayConfig);
12
+ quickSend(params: QuickSendParams, callback?: Function): Promise<SendResult>;
13
+ }
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.HubtelSmsGateway = void 0;
13
+ const hubtel_sms_extended_1 = require("hubtel-sms-extended");
14
+ /**
15
+ * Wraps hubtel-sms-extended and maps API responses to {@link SendResult}.
16
+ */
17
+ class HubtelSmsGateway {
18
+ constructor(config) {
19
+ this._client = new hubtel_sms_extended_1.HubtelSms({
20
+ clientId: config.clientId,
21
+ clientSecret: config.clientSecret
22
+ });
23
+ }
24
+ quickSend(params, callback) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ try {
27
+ const raw = yield this._client.quickSend({
28
+ From: params.From,
29
+ To: String(params.To),
30
+ Content: params.Content
31
+ });
32
+ const ok = Number(raw.Status) === 0;
33
+ const result = {
34
+ success: ok,
35
+ messageId: String(raw.MessageId),
36
+ data: raw,
37
+ error: ok ? undefined : `Hubtel Status=${raw.Status}`
38
+ };
39
+ if (callback) {
40
+ callback(result);
41
+ }
42
+ return result;
43
+ }
44
+ catch (error) {
45
+ const result = {
46
+ success: false,
47
+ error: error instanceof Error ? error.message : String(error)
48
+ };
49
+ if (callback) {
50
+ callback(result);
51
+ }
52
+ return result;
53
+ }
54
+ });
55
+ }
56
+ }
57
+ exports.HubtelSmsGateway = HubtelSmsGateway;
@@ -0,0 +1,5 @@
1
+ import { smsPlatform, IgatewaySettings, IgatewayParam, PlatformId, QuickSendParams, SendResult, ISmsGateway } from './platform';
2
+ export declare function init(settings: IgatewaySettings): smsPlatform;
3
+ export declare function getSmsPlatform(): smsPlatform | null;
4
+ export declare function reset(): void;
5
+ export { smsPlatform, IgatewaySettings, IgatewayParam, PlatformId, QuickSendParams, SendResult, ISmsGateway };
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.smsPlatform = exports.reset = exports.getSmsPlatform = exports.init = void 0;
4
+ const platform_1 = require("./platform");
5
+ Object.defineProperty(exports, "smsPlatform", { enumerable: true, get: function () { return platform_1.smsPlatform; } });
6
+ let smsPlatformInstance = null;
7
+ function init(settings) {
8
+ smsPlatformInstance = new platform_1.smsPlatform(settings);
9
+ smsPlatformInstance.init();
10
+ return smsPlatformInstance;
11
+ }
12
+ exports.init = init;
13
+ function getSmsPlatform() {
14
+ return smsPlatformInstance;
15
+ }
16
+ exports.getSmsPlatform = getSmsPlatform;
17
+ function reset() {
18
+ smsPlatformInstance = null;
19
+ }
20
+ exports.reset = reset;
@@ -0,0 +1,12 @@
1
+ import { ISmsGateway, QuickSendParams, SendResult, NestSmsConfig } from './types';
2
+ export declare class NestSmsGateway implements ISmsGateway {
3
+ private config;
4
+ constructor(config: NestSmsConfig);
5
+ init(): ISmsGateway;
6
+ private makeRequest;
7
+ quickSend(params: QuickSendParams, callback?: Function): Promise<SendResult>;
8
+ getBalance(): Promise<{
9
+ balance: number;
10
+ model: string;
11
+ }>;
12
+ }
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.NestSmsGateway = void 0;
32
+ const https = __importStar(require("https"));
33
+ const http = __importStar(require("http"));
34
+ const DEFAULT_HOST = 'api.smsonlinegh.com';
35
+ const DEFAULT_PROTOCOL = 'https';
36
+ class NestSmsGateway {
37
+ constructor(config) {
38
+ this.config = {
39
+ host: config.host || DEFAULT_HOST,
40
+ protocol: config.protocol || DEFAULT_PROTOCOL,
41
+ apiKey: config.apiKey
42
+ };
43
+ }
44
+ init() {
45
+ return this;
46
+ }
47
+ makeRequest(endpoint, data) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return new Promise((resolve, reject) => {
50
+ var _a;
51
+ const postData = data ? JSON.stringify(data) : '';
52
+ const protocol = this.config.protocol || DEFAULT_PROTOCOL;
53
+ const httpModule = protocol === 'https' ? https : http;
54
+ const defaultPort = protocol === 'https' ? 443 : 80;
55
+ const options = {
56
+ hostname: this.config.host || DEFAULT_HOST,
57
+ port: ((_a = this.config.host) === null || _a === void 0 ? void 0 : _a.includes(':'))
58
+ ? parseInt(this.config.host.split(':')[1])
59
+ : defaultPort,
60
+ path: `/v5/${endpoint}`,
61
+ method: 'POST',
62
+ headers: {
63
+ 'Host': this.config.host || DEFAULT_HOST,
64
+ 'Content-Type': 'application/json',
65
+ 'Accept': 'application/json',
66
+ 'Authorization': `key ${this.config.apiKey}`,
67
+ 'Content-Length': Buffer.byteLength(postData)
68
+ }
69
+ };
70
+ const req = httpModule.request(options, (res) => {
71
+ let responseBody = '';
72
+ res.on('data', (chunk) => {
73
+ responseBody += chunk;
74
+ });
75
+ res.on('end', () => {
76
+ try {
77
+ if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
78
+ const parsed = JSON.parse(responseBody);
79
+ resolve(parsed);
80
+ }
81
+ else {
82
+ reject(new Error(`HTTP ${res.statusCode}: ${responseBody}`));
83
+ }
84
+ }
85
+ catch (error) {
86
+ reject(new Error(`Failed to parse response: ${responseBody}`));
87
+ }
88
+ });
89
+ });
90
+ req.on('error', (error) => {
91
+ reject(error);
92
+ });
93
+ req.write(postData);
94
+ req.end();
95
+ });
96
+ });
97
+ }
98
+ quickSend(params, callback) {
99
+ var _a, _b, _c, _d, _e;
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ const endpoint = 'message/sms/send';
102
+ // SMSOnlineGH v5 expects: text, sender, destinations[] (see API docs — not from/to/content).
103
+ const requestBody = {
104
+ text: params.Content,
105
+ type: params.Type || 0,
106
+ sender: params.From,
107
+ destinations: [String(params.To)]
108
+ };
109
+ try {
110
+ const response = yield this.makeRequest(endpoint, requestBody);
111
+ const handshakeOk = Number((_a = response.handshake) === null || _a === void 0 ? void 0 : _a.id) === 0;
112
+ const data = (_b = response.data) !== null && _b !== void 0 ? _b : null;
113
+ const batchId = data && typeof data === 'object' ? data.batch : undefined;
114
+ const firstDest = data && typeof data === 'object'
115
+ ? (_c = data.destinations) === null || _c === void 0 ? void 0 : _c[0]
116
+ : undefined;
117
+ const result = {
118
+ success: handshakeOk,
119
+ data,
120
+ messageId: batchId || (firstDest === null || firstDest === void 0 ? void 0 : firstDest.id),
121
+ error: handshakeOk
122
+ ? undefined
123
+ : (((_d = response.handshake) === null || _d === void 0 ? void 0 : _d.label)
124
+ || `handshake id ${String((_e = response.handshake) === null || _e === void 0 ? void 0 : _e.id)}`)
125
+ };
126
+ if (callback) {
127
+ callback(result);
128
+ }
129
+ return result;
130
+ }
131
+ catch (error) {
132
+ const errorMessage = error instanceof Error ? error.message : String(error);
133
+ const result = {
134
+ success: false,
135
+ error: errorMessage
136
+ };
137
+ if (callback) {
138
+ callback(result);
139
+ }
140
+ return result;
141
+ }
142
+ });
143
+ }
144
+ getBalance() {
145
+ var _a, _b;
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ const endpoint = 'account/balance';
148
+ const response = yield this.makeRequest(endpoint);
149
+ return {
150
+ balance: ((_a = response.data) === null || _a === void 0 ? void 0 : _a.balance) || 0,
151
+ model: ((_b = response.data) === null || _b === void 0 ? void 0 : _b.model) || 'quantity'
152
+ };
153
+ });
154
+ }
155
+ }
156
+ exports.NestSmsGateway = NestSmsGateway;
@@ -0,0 +1,13 @@
1
+ import { IgatewaySettings, IgatewayParam, ISmsGateway, ISmsGatewayDelegate, QuickSendParams, SendResult } from './types';
2
+ export * from './types';
3
+ export declare class smsPlatform implements ISmsGateway {
4
+ private _settings;
5
+ private _gateway;
6
+ constructor(settings: IgatewaySettings);
7
+ private validateSettings;
8
+ private createGateway;
9
+ init(): ISmsGateway;
10
+ quickSend(param: QuickSendParams, callback?: Function): Promise<SendResult>;
11
+ getGateway(): ISmsGatewayDelegate;
12
+ }
13
+ export { IgatewaySettings, IgatewayParam };
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.smsPlatform = void 0;
14
+ const nest_gateway_1 = require("./nest-gateway");
15
+ const hubtel_gateway_1 = require("./hubtel-gateway");
16
+ const route_gateway_1 = require("./route-gateway");
17
+ __exportStar(require("./types"), exports);
18
+ const GATEWAY_CONFIGS = {
19
+ route: { requiresUsernamePassword: true },
20
+ hubtel: { requiresClientCredentials: true },
21
+ nest: { requiresApiKey: true }
22
+ };
23
+ class smsPlatform {
24
+ constructor(settings) {
25
+ this.validateSettings(settings);
26
+ this._settings = settings;
27
+ this._gateway = this.createGateway();
28
+ }
29
+ validateSettings(settings) {
30
+ const validPlatforms = ['route', 'hubtel', 'nest'];
31
+ if (!validPlatforms.includes(settings.platformId)) {
32
+ throw new Error(`Invalid platform ID. Supported platforms: ${validPlatforms.join(', ')}`);
33
+ }
34
+ const config = GATEWAY_CONFIGS[settings.platformId];
35
+ const param = settings.param;
36
+ if (config.requiresApiKey && !param.apiKey) {
37
+ throw new Error(`Platform '${settings.platformId}' requires 'apiKey' in param`);
38
+ }
39
+ if (config.requiresClientCredentials && (!param.clientId || !param.clientSecret)) {
40
+ throw new Error(`Platform '${settings.platformId}' requires 'clientId' and 'clientSecret' in param`);
41
+ }
42
+ if (config.requiresUsernamePassword && (!param.username || !param.password)) {
43
+ throw new Error(`Platform '${settings.platformId}' requires 'username' and 'password' in param`);
44
+ }
45
+ }
46
+ createGateway() {
47
+ const { platformId, param } = this._settings;
48
+ switch (platformId) {
49
+ case 'route':
50
+ return new route_gateway_1.RouteSmsGateway({
51
+ host: param.host || 'rslr.connectbind.com',
52
+ username: param.username,
53
+ password: param.password,
54
+ protocol: param.protocol || 'http',
55
+ port: param.port || 8080
56
+ });
57
+ case 'hubtel':
58
+ return new hubtel_gateway_1.HubtelSmsGateway({
59
+ clientId: param.clientId,
60
+ clientSecret: param.clientSecret
61
+ });
62
+ case 'nest':
63
+ return new nest_gateway_1.NestSmsGateway({
64
+ apiKey: param.apiKey,
65
+ host: param.host,
66
+ protocol: param.protocol
67
+ });
68
+ default:
69
+ throw new Error(`Unsupported platform: ${platformId}`);
70
+ }
71
+ }
72
+ init() {
73
+ return this;
74
+ }
75
+ quickSend(param, callback) {
76
+ if (!this._gateway) {
77
+ throw new Error('Gateway not initialized. Call init() first.');
78
+ }
79
+ return this._gateway.quickSend(param, callback);
80
+ }
81
+ getGateway() {
82
+ return this._gateway;
83
+ }
84
+ }
85
+ exports.smsPlatform = smsPlatform;
@@ -0,0 +1,12 @@
1
+ import { ISmsGatewayDelegate, QuickSendParams, SendResult } from './types';
2
+ export interface RouteSmsGatewayConfig {
3
+ host: string;
4
+ username: string;
5
+ password: string;
6
+ protocol: 'http' | 'https';
7
+ port: number;
8
+ }
9
+ export declare class RouteSmsGateway implements ISmsGatewayDelegate {
10
+ constructor(config: RouteSmsGatewayConfig);
11
+ quickSend(params: QuickSendParams, callback?: Function): Promise<SendResult>;
12
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.RouteSmsGateway = void 0;
13
+ const routemobilesms_1 = require("routemobilesms");
14
+ /**
15
+ * Adapts routemobilesms static `sendAsync` API to {@link ISmsGatewayDelegate}.
16
+ */
17
+ function toRouteDestination(to) {
18
+ if (typeof to === 'number') {
19
+ return to;
20
+ }
21
+ const digits = String(to).replace(/\D/g, '');
22
+ const n = Number(digits);
23
+ return Number.isNaN(n) ? 0 : n;
24
+ }
25
+ class RouteSmsGateway {
26
+ constructor(config) {
27
+ new routemobilesms_1.routeSms({
28
+ host: config.host,
29
+ username: config.username,
30
+ password: config.password,
31
+ protocol: config.protocol,
32
+ port: config.port
33
+ });
34
+ }
35
+ quickSend(params, callback) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ const sendParams = {
38
+ From: params.From,
39
+ To: toRouteDestination(params.To),
40
+ Content: params.Content
41
+ };
42
+ if (params.Type !== undefined) {
43
+ sendParams.config = { type: params.Type, dlr: 0 };
44
+ }
45
+ const raw = yield routemobilesms_1.routeSms.sendAsync(sendParams);
46
+ let result;
47
+ if (raw === undefined || raw === null) {
48
+ result = { success: false, error: 'No response from route SMS gateway' };
49
+ }
50
+ else if (Array.isArray(raw) && raw.length > 0) {
51
+ const first = raw[0];
52
+ const ok = first.status === 'successful';
53
+ result = {
54
+ success: ok,
55
+ messageId: first.id,
56
+ data: raw,
57
+ error: ok ? undefined : (first.message || first.code || 'Send failed')
58
+ };
59
+ }
60
+ else {
61
+ result = { success: false, error: 'Unexpected response from route SMS gateway', data: raw };
62
+ }
63
+ if (callback) {
64
+ callback(result);
65
+ }
66
+ return result;
67
+ });
68
+ }
69
+ }
70
+ exports.RouteSmsGateway = RouteSmsGateway;