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.
- package/README.md +402 -129
- package/dist/scripts/test-live.d.ts +11 -0
- package/dist/scripts/test-live.js +188 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +13 -0
- package/dist/src/lib/hubtel-gateway.d.ts +13 -0
- package/dist/src/lib/hubtel-gateway.js +57 -0
- package/dist/src/lib/lib.d.ts +5 -0
- package/dist/src/lib/lib.js +20 -0
- package/dist/src/lib/nest-gateway.d.ts +12 -0
- package/dist/src/lib/nest-gateway.js +156 -0
- package/dist/src/lib/platform.d.ts +13 -0
- package/dist/src/lib/platform.js +85 -0
- package/dist/src/lib/route-gateway.d.ts +12 -0
- package/dist/src/lib/route-gateway.js +70 -0
- package/dist/src/lib/types.d.ts +47 -0
- package/dist/src/lib/types.js +2 -0
- package/package.json +6 -2
- /package/dist/lib/{types.d.ts → j;[ ]p;;;j]/ts} +0 -0
package/README.md
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
# Unified
|
|
1
|
+
# Unified SMS Gateway
|
|
2
2
|
|
|
3
|
-
Most
|
|
4
|
-
However, each sms api specification is different from the other, hence the need to create separate implementation
|
|
5
|
-
for each sms gateway.
|
|
6
|
-
|
|
7
|
-
Unified sms gateway is library that brings most common sms gateways under a Unified api.
|
|
8
|
-
which means you only does one implementation in it works for all supported sms gateway.
|
|
9
|
-
you just have select or switch your sms platform and your code still works fine like nothing has changed
|
|
3
|
+
Most projects rely on more than one SMS provider so they can switch if a gateway is unavailable. Each provider’s API differs, so separate integrations are usually required.
|
|
10
4
|
|
|
5
|
+
**unismsgateway** exposes a single API for multiple SMS gateways. You implement once, then select or switch the platform; your send flow stays the same.
|
|
11
6
|
|
|
12
7
|
## Installation
|
|
13
8
|
|
|
@@ -15,170 +10,448 @@ you just have select or switch your sms platform and your code still works fine
|
|
|
15
10
|
npm install unismsgateway
|
|
16
11
|
```
|
|
17
12
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
| Platform ID | Provider | Required Params |
|
|
21
|
-
|-------------|----------|-----------------|
|
|
22
|
-
| `route` | routeMobile | username, password, host |
|
|
23
|
-
| `hubtel` | Hubtel SMS (Ghana) | clientId, clientSecret |
|
|
24
|
-
| `nest` | SMSOnlineGH / smsonlinegh | apiKey |
|
|
13
|
+
**Requirements:** Node.js `>= 12.0.0` (see `package.json` `engines`).
|
|
25
14
|
|
|
26
|
-
##
|
|
15
|
+
## Module import
|
|
27
16
|
|
|
28
|
-
|
|
17
|
+
**CommonJS**
|
|
29
18
|
|
|
30
19
|
```javascript
|
|
31
|
-
const unisms = require('unismsgateway')
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
protocol: 'http',
|
|
41
|
-
port: 8080
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
// For Hubtel
|
|
46
|
-
const hubtelGateway = unisms.init({
|
|
47
|
-
platformId: 'hubtel',
|
|
48
|
-
param: {
|
|
49
|
-
clientId: 'your-client-id',
|
|
50
|
-
clientSecret: 'your-client-secret'
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
// For SMSOnlineGH (nest)
|
|
55
|
-
const nestGateway = unisms.init({
|
|
56
|
-
platformId: 'nest',
|
|
57
|
-
param: {
|
|
58
|
-
apiKey: 'your-api-key',
|
|
59
|
-
// Optional: host, protocol (defaults to api.smsonlinegh.com, https)
|
|
60
|
-
}
|
|
61
|
-
})
|
|
20
|
+
const unisms = require('unismsgateway');
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**ESM / TypeScript**
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import * as unisms from 'unismsgateway';
|
|
27
|
+
// or named:
|
|
28
|
+
import { init, getSmsPlatform, reset, smsPlatform } from 'unismsgateway';
|
|
62
29
|
```
|
|
63
30
|
|
|
64
|
-
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Configuration overview
|
|
34
|
+
|
|
35
|
+
### `IgatewaySettings`
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
| Field | Type | Description |
|
|
39
|
+
| ------------ | --------------- | -------------------------------------- |
|
|
40
|
+
| `platformId` | `'route' | 'hubtel' |
|
|
41
|
+
| `param` | `IgatewayParam` | Provider-specific options (see below). |
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### `IgatewayParam` (all fields optional except what your `platformId` requires)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
| Field | Type | Used by | Description |
|
|
48
|
+
| -------------- | -------- | --------------- | ---------------------------------------------------------------------- |
|
|
49
|
+
| `username` | `string` | `route` | Route Mobile account username. **Required** for `route`. |
|
|
50
|
+
| `password` | `string` | `route` | Route Mobile account password. **Required** for `route`. |
|
|
51
|
+
| `host` | `string` | `route`, `nest` | API host. See per-gateway defaults below. |
|
|
52
|
+
| `port` | `number` | `route` | TCP port for Route Mobile. Default: `8080`. |
|
|
53
|
+
| `protocol` | `'http' | 'https'` | `route`, `nest` |
|
|
54
|
+
| `clientId` | `string` | `hubtel` | Hubtel client ID. **Required** for `hubtel`. |
|
|
55
|
+
| `clientSecret` | `string` | `hubtel` | Hubtel client secret. **Required** for `hubtel`. |
|
|
56
|
+
| `apiKey` | `string` | `nest` | SMSOnlineGH API key (`Authorization: key …`). **Required** for `nest`. |
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
Validation runs in `smsPlatform` when the instance is constructed: missing required fields for the chosen `platformId` throw `Error` with a clear message.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Environment variables
|
|
64
|
+
|
|
65
|
+
There are **two separate contexts**. Use the section that matches what you are doing.
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
| Context | Who reads env? | Purpose |
|
|
69
|
+
| --------------------------------------------------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------ |
|
|
70
|
+
| **Library usage** (`init()` in your app) | **Your code** — this package does **not** read `process.env`. | You choose variable names and map them into `platformId` and `param` yourself. |
|
|
71
|
+
| **Live integration test** (`npm test` / `scripts/test-live.ts`) | **The test script** via `dotenv` and `process.env`. | Fixed names in `.env` (see `.env.example`). |
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### Library usage: required and optional param fields
|
|
77
|
+
|
|
78
|
+
Nothing is read from the environment unless **you** wire it. Required fields are determined only by `platformId`:
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
| `platformId` | Required in `param` | Optional in `param` (defaults in this library) |
|
|
82
|
+
| ------------ | -------------------------- | --------------------------------------------------------------------------------------------- |
|
|
83
|
+
| `nest` | `apiKey` | `host` (default `api.smsonlinegh.com`), `protocol` (default `https`) |
|
|
84
|
+
| `hubtel` | `clientId`, `clientSecret` | — |
|
|
85
|
+
| `route` | `username`, `password` | `host` (default `rslr.connectbind.com`), `protocol` (default `http`), `port` (default `8080`) |
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
**Suggested env names for your app** (optional; you can rename them). Credential keys (`NEST_`*, `HUBTEL_`*, `ROUTE_*`) match [live test](#live-integration-test-environment-variables) and `.env.example`. Platform selection differs: the test script requires `GATEWAY_PLATFORM` (or `TEST_ALL`); in your app you choose any name (the example below uses `SMS_PLATFORM_ID`):
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
| Env name (suggestion) | Maps to `param` | Required when `platformId` is |
|
|
92
|
+
| ---------------------- | -------------------------- | ----------------------------- |
|
|
93
|
+
| `NEST_API_KEY` | `apiKey` | `nest` |
|
|
94
|
+
| `NEST_HOST` | `host` | optional for `nest` |
|
|
95
|
+
| `NEST_PROTOCOL` | `protocol` | optional for `nest` |
|
|
96
|
+
| `HUBTEL_CLIENT_ID` | `clientId` | `hubtel` |
|
|
97
|
+
| `HUBTEL_CLIENT_SECRET` | `clientSecret` | `hubtel` |
|
|
98
|
+
| `ROUTE_USERNAME` | `username` | `route` |
|
|
99
|
+
| `ROUTE_PASSWORD` | `password` | `route` |
|
|
100
|
+
| `ROUTE_HOST` | `host` | optional for `route` |
|
|
101
|
+
| `ROUTE_PORT` | `port` (use `Number(...)`) | optional for `route` |
|
|
102
|
+
| `ROUTE_PROTOCOL` | `protocol` | optional for `route` |
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
You may also use names like `SMSONLINEGH_API_KEY` / `SMSONLINEGH_HOST` in your app only — there is **no** built-in support for alternate names in the test script; that script expects `NEST_`* and `ROUTE_`* as in `.env.example`.
|
|
106
|
+
|
|
107
|
+
Example wiring: branch on `platformId` and build `param` so you do not mix unrelated fields.
|
|
65
108
|
|
|
66
109
|
```javascript
|
|
67
|
-
const unisms = require('unismsgateway')
|
|
110
|
+
const unisms = require('unismsgateway');
|
|
111
|
+
|
|
112
|
+
// Pick any env name for the active gateway; the live test uses GATEWAY_PLATFORM instead.
|
|
113
|
+
const platformId = process.env.SMS_PLATFORM_ID;
|
|
114
|
+
|
|
115
|
+
const paramByPlatform = {
|
|
116
|
+
route: {
|
|
117
|
+
username: process.env.ROUTE_USERNAME,
|
|
118
|
+
password: process.env.ROUTE_PASSWORD,
|
|
119
|
+
host: process.env.ROUTE_HOST,
|
|
120
|
+
port: process.env.ROUTE_PORT ? Number(process.env.ROUTE_PORT) : undefined,
|
|
121
|
+
protocol: process.env.ROUTE_PROTOCOL
|
|
122
|
+
},
|
|
123
|
+
hubtel: {
|
|
124
|
+
clientId: process.env.HUBTEL_CLIENT_ID,
|
|
125
|
+
clientSecret: process.env.HUBTEL_CLIENT_SECRET
|
|
126
|
+
},
|
|
127
|
+
nest: {
|
|
128
|
+
apiKey: process.env.NEST_API_KEY,
|
|
129
|
+
host: process.env.NEST_HOST,
|
|
130
|
+
protocol: process.env.NEST_PROTOCOL
|
|
131
|
+
}
|
|
132
|
+
};
|
|
68
133
|
|
|
69
|
-
// Initialize gateway
|
|
70
134
|
const gateway = unisms.init({
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
135
|
+
platformId,
|
|
136
|
+
param: paramByPlatform[platformId]
|
|
137
|
+
});
|
|
138
|
+
```
|
|
76
139
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
### Live integration test environment variables
|
|
143
|
+
|
|
144
|
+
The script `scripts/test-live.ts` loads `.env` (copy from `.env.example`) and expects **these exact names**. It does not use `SMS_PLATFORM_ID` or other app-specific aliases.
|
|
145
|
+
|
|
146
|
+
#### How a run is selected
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
| Variable | Required? | Description |
|
|
150
|
+
| ------------------ | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
151
|
+
| `GATEWAY_PLATFORM` | **Yes**, unless you use `TEST_ALL` | Must be exactly `nest`, `hubtel`, or `route`. |
|
|
152
|
+
| `TEST_ALL` | Optional | If set to `true`, the script runs `nest`, then `hubtel`, then `route` in order. You still need the **required** variables below for each platform you run; missing keys for a step cause that step to fail. |
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
#### Per gateway — credentials and overrides
|
|
156
|
+
|
|
157
|
+
`**nest` (SMSOnlineGH)**
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
| Variable | Required? | Purpose |
|
|
161
|
+
| --------------- | --------- | --------------------------------------------- |
|
|
162
|
+
| `NEST_API_KEY` | **Yes** | Maps to `param.apiKey`. |
|
|
163
|
+
| `NEST_HOST` | No | Overrides default host `api.smsonlinegh.com`. |
|
|
164
|
+
| `NEST_PROTOCOL` | No | Overrides default `https`. |
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
`**hubtel`**
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
| Variable | Required? | Purpose |
|
|
171
|
+
| ---------------------- | --------- | ----------------------------- |
|
|
172
|
+
| `HUBTEL_CLIENT_ID` | **Yes** | Maps to `param.clientId`. |
|
|
173
|
+
| `HUBTEL_CLIENT_SECRET` | **Yes** | Maps to `param.clientSecret`. |
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
`**route` (Route Mobile)**
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
| Variable | Required? | Purpose |
|
|
180
|
+
| ---------------- | --------- | --------------------------------------------------- |
|
|
181
|
+
| `ROUTE_USERNAME` | **Yes** | Maps to `param.username`. |
|
|
182
|
+
| `ROUTE_PASSWORD` | **Yes** | Maps to `param.password`. |
|
|
183
|
+
| `ROUTE_HOST` | No | Overrides default `rslr.connectbind.com`. |
|
|
184
|
+
| `ROUTE_PORT` | No | Overrides default `8080` (set as a numeric string). |
|
|
185
|
+
| `ROUTE_PROTOCOL` | No | Overrides default `http`. |
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
#### Live send (optional; all gateways)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
| Variable | Required? | Purpose |
|
|
192
|
+
| -------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
|
193
|
+
| `TEST_SEND` | No (default: do not send) | Set to `true` to call `quickSend()` and send a real SMS. If unset or not `true`, only init and balance checks run. |
|
|
194
|
+
| `TEST_FROM` | **Yes** when `TEST_SEND=true` | `QuickSendParams.From`. |
|
|
195
|
+
| `TEST_TO` | **Yes** when `TEST_SEND=true` | `QuickSendParams.To`. |
|
|
196
|
+
| `TEST_CONTENT` | No | Message body; if omitted, the script uses a built-in default string. |
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## How initialization works
|
|
202
|
+
|
|
203
|
+
1. `**init(settings: IgatewaySettings): smsPlatform`** (in `src/lib/lib.ts`):
|
|
204
|
+
- Validates and constructs a new `smsPlatform` with your `settings`.
|
|
205
|
+
- Stores it as the **module singleton** (`smsPlatformInstance`).
|
|
206
|
+
- Calls `smsPlatform.init()` on that instance (returns the same facade for chaining).
|
|
207
|
+
- Returns the `smsPlatform` instance.
|
|
208
|
+
2. `**smsPlatform` constructor** (in `src/lib/platform.ts`):
|
|
209
|
+
- Runs `validateSettings()` (platform id + required `param` fields for that id).
|
|
210
|
+
- Calls `createGateway()` to instantiate the underlying provider (`routeSms`, `HubtelSms`, or `NestSmsGateway`).
|
|
211
|
+
3. `**getSmsPlatform(): smsPlatform | null`**: Returns the current singleton, or `null` if `reset()` was called and no new `init()` has run.
|
|
212
|
+
|
|
213
|
+
There is **no async bootstrap**; after `init()` returns, `quickSend` is ready.
|
|
214
|
+
|
|
215
|
+
---
|
|
96
216
|
|
|
97
|
-
|
|
217
|
+
## Re-initializing and reset
|
|
218
|
+
|
|
219
|
+
- **Switch platform or credentials:** Call `**init(newSettings)`** again. Each call **replaces** the stored singleton with a new `smsPlatform`. You do not have to call `reset()` first.
|
|
220
|
+
- **Clear the singleton:** `**reset()`** sets the internal reference to `null`. `getSmsPlatform()` then returns `null` until the next `init()`. Use this when you want to guarantee nothing holds a gateway instance (e.g. tests or explicit teardown).
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
const unisms = require('unismsgateway');
|
|
224
|
+
|
|
225
|
+
const a = unisms.init({ platformId: 'nest', param: { apiKey: 'key-1' } });
|
|
226
|
+
// Later: new config
|
|
227
|
+
const b = unisms.init({ platformId: 'hubtel', param: { clientId: 'x', clientSecret: 'y' } });
|
|
228
|
+
// b replaces a; unisms.getSmsPlatform() === b
|
|
229
|
+
|
|
230
|
+
unisms.reset();
|
|
231
|
+
// unisms.getSmsPlatform() === null
|
|
232
|
+
|
|
233
|
+
const c = unisms.init({ platformId: 'nest', param: { apiKey: 'key-2' } });
|
|
98
234
|
```
|
|
99
235
|
|
|
100
|
-
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## Supported gateways
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
| `platformId` | Provider | Package / implementation |
|
|
242
|
+
| ------------ | ------------------ | --------------------------------------- |
|
|
243
|
+
| `route` | Route Mobile | `routemobilesms` |
|
|
244
|
+
| `hubtel` | Hubtel SMS (Ghana) | `hubtel-sms-extended` |
|
|
245
|
+
| `nest` | SMSOnlineGH | Built-in REST client (`NestSmsGateway`) |
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
**Configuration vs env:** Required and optional `param` fields are summarized in [Library usage: required and optional param fields](#library-usage-required-and-optional-param-fields). The live test runner’s `.env` names are listed in [Live integration test environment variables](#live-integration-test-environment-variables).
|
|
249
|
+
|
|
250
|
+
### `route` (Route Mobile)
|
|
251
|
+
|
|
252
|
+
**Required `param`:** `username`, `password`.
|
|
253
|
+
|
|
254
|
+
**Optional `param` (defaults in this library):**
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
| Field | Default if omitted |
|
|
258
|
+
| ---------- | ---------------------- |
|
|
259
|
+
| `host` | `rslr.connectbind.com` |
|
|
260
|
+
| `protocol` | `'http'` |
|
|
261
|
+
| `port` | `8080` |
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
These are passed into `routeSms` from `routemobilesms`.
|
|
101
265
|
|
|
102
266
|
```javascript
|
|
103
|
-
gateway.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
267
|
+
const gateway = unisms.init({
|
|
268
|
+
platformId: 'route',
|
|
269
|
+
param: {
|
|
270
|
+
username: 'your-username',
|
|
271
|
+
password: 'your-password',
|
|
272
|
+
host: 'rslr.connectbind.com',
|
|
273
|
+
protocol: 'http',
|
|
274
|
+
port: 8080
|
|
275
|
+
}
|
|
276
|
+
});
|
|
110
277
|
```
|
|
111
278
|
|
|
112
|
-
###
|
|
279
|
+
### `hubtel` (Hubtel)
|
|
280
|
+
|
|
281
|
+
**Required `param`:** `clientId`, `clientSecret`.
|
|
282
|
+
|
|
283
|
+
No `host` / `protocol` in `IgatewayParam` for Hubtel in this library; configuration follows `hubtel-sms-extended`.
|
|
113
284
|
|
|
114
285
|
```javascript
|
|
115
286
|
const gateway = unisms.init({
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const nestGateway = gateway.getGateway()
|
|
123
|
-
const balance = await nestGateway.getBalance()
|
|
124
|
-
console.log('Balance:', balance)
|
|
125
|
-
}
|
|
287
|
+
platformId: 'hubtel',
|
|
288
|
+
param: {
|
|
289
|
+
clientId: 'your-client-id',
|
|
290
|
+
clientSecret: 'your-client-secret'
|
|
291
|
+
}
|
|
292
|
+
});
|
|
126
293
|
```
|
|
127
294
|
|
|
128
|
-
###
|
|
295
|
+
### `nest` (SMSOnlineGH)
|
|
296
|
+
|
|
297
|
+
**Required `param`:** `apiKey`.
|
|
298
|
+
|
|
299
|
+
**Optional `param`:**
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
| Field | Default if omitted |
|
|
303
|
+
| ---------- | --------------------- |
|
|
304
|
+
| `host` | `api.smsonlinegh.com` |
|
|
305
|
+
| `protocol` | `'https'` |
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
Requests use `POST` to path `**/v5/<endpoint>`** (e.g. send: `message/sms/send`, balance: `account/balance`). Authorization header: `Authorization: key <apiKey>`.
|
|
129
309
|
|
|
130
310
|
```javascript
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
clientId: 'new-client-id',
|
|
139
|
-
clientSecret: 'new-secret'
|
|
140
|
-
}
|
|
141
|
-
})
|
|
311
|
+
const gateway = unisms.init({
|
|
312
|
+
platformId: 'nest',
|
|
313
|
+
param: {
|
|
314
|
+
apiKey: 'your-api-key'
|
|
315
|
+
// optional: host, protocol
|
|
316
|
+
}
|
|
317
|
+
});
|
|
142
318
|
```
|
|
143
319
|
|
|
144
|
-
|
|
320
|
+
**Balance (nest only):** The underlying `NestSmsGateway` implements `getBalance()`. Access it via the facade’s `getGateway()`:
|
|
321
|
+
|
|
322
|
+
```javascript
|
|
323
|
+
const gateway = unisms.init({
|
|
324
|
+
platformId: 'nest',
|
|
325
|
+
param: { apiKey: 'your-api-key' }
|
|
326
|
+
});
|
|
145
327
|
|
|
146
|
-
|
|
328
|
+
const nest = gateway.getGateway();
|
|
329
|
+
const balance = await nest.getBalance();
|
|
330
|
+
console.log(balance.balance, balance.model);
|
|
331
|
+
```
|
|
147
332
|
|
|
148
|
-
|
|
333
|
+
---
|
|
149
334
|
|
|
150
|
-
|
|
151
|
-
- `platformId`: `'route'` | `'hubtel'` | `'nest'`
|
|
152
|
-
- `param`: Platform-specific configuration object
|
|
335
|
+
## Sending messages
|
|
153
336
|
|
|
154
|
-
### `
|
|
337
|
+
### `QuickSendParams`
|
|
155
338
|
|
|
156
|
-
Get the current gateway instance. Returns `null` if not initialized.
|
|
157
339
|
|
|
158
|
-
|
|
340
|
+
| Field | Type | Required | Description |
|
|
341
|
+
| --------- | -------- | -------- | ---------------------------------------------------------------------- |
|
|
342
|
+
| `From` | `string` | yes | Sender ID or label. |
|
|
343
|
+
| `To` | `string | number` | yes |
|
|
344
|
+
| `Content` | `string` | yes | Message body. |
|
|
345
|
+
| `Type` | `number` | no | Message type; **nest** maps this to request body `type` (default `0`). |
|
|
159
346
|
|
|
160
|
-
Clear the current gateway instance.
|
|
161
347
|
|
|
162
|
-
### `quickSend(params
|
|
348
|
+
### `quickSend(params, callback?)`
|
|
163
349
|
|
|
164
|
-
|
|
350
|
+
Returns `Promise<SendResult>`. Optional `callback` is invoked with the same result when the promise completes.
|
|
165
351
|
|
|
166
|
-
|
|
167
|
-
- `From`: Sender ID/name
|
|
168
|
-
- `To`: Recipient phone number (string or number)
|
|
169
|
-
- `Content`: Message content
|
|
170
|
-
- `Type`: Optional message type (defaults to 0)
|
|
352
|
+
`**SendResult`:**
|
|
171
353
|
|
|
172
|
-
**Returns:**
|
|
173
354
|
```typescript
|
|
174
355
|
{
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
356
|
+
success: boolean;
|
|
357
|
+
messageId?: string;
|
|
358
|
+
data?: any;
|
|
359
|
+
error?: string;
|
|
179
360
|
}
|
|
180
361
|
```
|
|
181
362
|
|
|
363
|
+
**Example**
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
const unisms = require('unismsgateway');
|
|
367
|
+
|
|
368
|
+
const gateway = unisms.init({
|
|
369
|
+
platformId: 'nest',
|
|
370
|
+
param: { apiKey: 'your-api-key' }
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
async function sendSms() {
|
|
374
|
+
try {
|
|
375
|
+
const result = await gateway.quickSend({
|
|
376
|
+
From: 'SenderName',
|
|
377
|
+
To: '233XXXXXXXXX',
|
|
378
|
+
Content: 'Hello from unismsgateway!',
|
|
379
|
+
Type: 0
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
if (result.success) {
|
|
383
|
+
console.log('Sent:', result.messageId);
|
|
384
|
+
} else {
|
|
385
|
+
console.error('Failed:', result.error);
|
|
386
|
+
}
|
|
387
|
+
} catch (err) {
|
|
388
|
+
console.error(err);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
**With callback**
|
|
394
|
+
|
|
395
|
+
```javascript
|
|
396
|
+
gateway.quickSend(
|
|
397
|
+
{ From: 'SenderName', To: '233XXXXXXXXX', Content: 'Test' },
|
|
398
|
+
(response) => {
|
|
399
|
+
console.log(response);
|
|
400
|
+
}
|
|
401
|
+
);
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## Testing (live integration)
|
|
407
|
+
|
|
408
|
+
There is no unit test suite in this package. For **manual integration checks** against real gateways, use the script in `scripts/test-live.ts`.
|
|
409
|
+
|
|
410
|
+
**Setup**
|
|
411
|
+
|
|
412
|
+
1. Clone the repo and install dependencies: `npm install`
|
|
413
|
+
2. Copy `.env.example` to `.env` and set variables for your chosen platform — see [Live integration test environment variables](#live-integration-test-environment-variables) for required vs optional names per gateway.
|
|
414
|
+
3. Run:
|
|
415
|
+
|
|
416
|
+
```bash
|
|
417
|
+
npm test
|
|
418
|
+
# same as:
|
|
419
|
+
npm run test:live
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
**What runs**
|
|
423
|
+
|
|
424
|
+
1. **Init** — Builds `param` from your `.env`, calls `init()`, and checks configuration validation.
|
|
425
|
+
2. **Balance** — For `nest` and `hubtel` only, calls `getBalance()` when the adapter supports it. `route` skips this step.
|
|
426
|
+
3. **Send** — **Opt-in.** By default no SMS is sent. Set `TEST_SEND=true` and the send-related variables listed in [Live integration test environment variables](#live-integration-test-environment-variables).
|
|
427
|
+
|
|
428
|
+
Full variable reference (selection, per-gateway credentials, live send): [Live integration test environment variables](#live-integration-test-environment-variables). The script loads `.env` via `dotenv` (dev dependency). Exit code is `0` when all checks pass, non-zero if a step fails or no platform is selected.
|
|
429
|
+
|
|
430
|
+
---
|
|
431
|
+
|
|
432
|
+
## API reference
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
| Export | Description |
|
|
436
|
+
| ------------------ | -------------------------------------------------------------------- |
|
|
437
|
+
| `init(settings)` | Create and register the singleton `smsPlatform`, return it. |
|
|
438
|
+
| `getSmsPlatform()` | Current `smsPlatform` or `null` after `reset()` and before `init()`. |
|
|
439
|
+
| `reset()` | Clear the singleton. |
|
|
440
|
+
| `smsPlatform` | Class type for typing/advanced use. |
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
`**smsPlatform` instance methods**
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
| Method | Returns | Description |
|
|
447
|
+
| ------------------------------ | --------------------- | ---------------------------------------------- |
|
|
448
|
+
| `init()` | `ISmsGateway` | Returns `this` (facade). |
|
|
449
|
+
| `quickSend(params, callback?)` | `Promise<SendResult>` | Delegates to the active gateway. |
|
|
450
|
+
| `getGateway()` | `ISmsGateway` | Underlying adapter (for nest: `getBalance()`). |
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
---
|
|
454
|
+
|
|
182
455
|
## License
|
|
183
456
|
|
|
184
457
|
[MIT](https://choosealicense.com/licenses/mit/)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live integration test script for unismsgateway.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* 1. Copy .env.example to .env and fill in your credentials.
|
|
6
|
+
* 2. npm run test:live
|
|
7
|
+
*
|
|
8
|
+
* Set TEST_SEND=true in .env (or inline) to actually send an SMS.
|
|
9
|
+
* Without it, only init and balance checks run.
|
|
10
|
+
*/
|
|
11
|
+
export {};
|