Skip to content

Commit 7b2423b

Browse files
committed
init commit
0 parents  commit 7b2423b

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Infobip-JS
2+
3+
This is a javascript client that covers **partially** the API of [Infobip](https://infobip.com) for sending SMS.
4+
5+
### Features
6+
7+
- Send single message to single number
8+
- Send single message to multiple numbers
9+
- Preview SMS cost and count
10+
- Support for transliteration
11+
- Supports **only** username/password authentication mechanism
12+
13+
### How to use
14+
15+
```javascript
16+
const InfobipAPI = require('infobip-js')
17+
18+
// generate client
19+
const InfoBip = new InfobipAPI({
20+
username: 'username',
21+
password: 'password'
22+
})
23+
24+
try {
25+
// ** Send single message
26+
// SENDER_ID is not supported in all countries
27+
const res = await InfoBip.from('SENDER_ID')
28+
.to('99812723737')
29+
.message('Test')
30+
.send()
31+
console.log(res)
32+
} catch (err) {
33+
console.error(err)
34+
}
35+
36+
try {
37+
// ** Send message to multiple destinations
38+
const res = await InfoBip.from('SENDER_ID')
39+
.to(['99812723737', '9999383883'])
40+
.message('Test')
41+
.send('sms-multi')
42+
console.log(res)
43+
} catch (err) {
44+
console.error(err)
45+
}
46+
47+
try {
48+
// ** Get cost estimation on a transliterated message
49+
const res = await InfoBip.message('Test 2')
50+
.transliteration('GREEK')
51+
.preview()
52+
console.log(res)
53+
} catch (err) {
54+
console.error(err)
55+
}
56+
```
57+
58+
### Contributions Welcome

index.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const fetch = require('node-fetch')
2+
3+
const BASE_URL = "https://kmxl1.api.infobip.com"
4+
5+
const endpoints = Object.freeze({
6+
'sms-single': 'sms/1/text/single',
7+
'sms-multi': 'sms/2/text/single',
8+
'sms-preview': 'sms/1/preview'
9+
})
10+
11+
function InfobipAPI (auth) {
12+
_auth = auth || {}
13+
_options = {}
14+
15+
_authShape = {
16+
username: null,
17+
password: null
18+
}
19+
20+
function _checkIfNotEmpty (payload, shape) {
21+
Object.keys(shape).forEach(key => {
22+
const optional =
23+
shape[key] &&
24+
shape[key].hasOwnProperty('optional') &&
25+
shape[key].optional
26+
if (!optional && !payload[key]) {
27+
throw new Error(`'${key}' is required`)
28+
}
29+
})
30+
}
31+
32+
function _buildAuthorizationHeader () {
33+
let buffer = new Buffer.from(_auth.username + ':' + _auth.password)
34+
return `Basic ${buffer.toString('base64')}`
35+
}
36+
37+
function _buildURL (endpoint) {
38+
return `${BASE_URL}/${endpoints[endpoint]}`
39+
}
40+
41+
function _cleanup () {
42+
_options = {}
43+
}
44+
45+
// public members
46+
this.from = function (from) {
47+
_options = Object.assign({}, _options, { from })
48+
return this
49+
}
50+
51+
this.to = function (to) {
52+
_options = Object.assign({}, _options, { to })
53+
return this
54+
}
55+
56+
this.message = function (text) {
57+
_options = Object.assign({}, _options, { text })
58+
return this
59+
}
60+
61+
this.transliteration = function (language) {
62+
_options = Object.assign({}, _options, { transliteration: language })
63+
return this
64+
}
65+
66+
this.send = function (endpoint = 'sms-single') {
67+
_checkIfNotEmpty(_options, {
68+
from: null,
69+
to: null,
70+
text: null,
71+
transliteration: {
72+
optional: true
73+
}
74+
})
75+
_checkIfNotEmpty(_auth, _authShape)
76+
const URL = _buildURL(endpoint)
77+
const headers = {
78+
'Content-Type': 'application/json',
79+
Accept: 'application/json',
80+
Authorization: _buildAuthorizationHeader()
81+
}
82+
83+
return fetch(URL, {
84+
method: 'POST',
85+
body: JSON.stringify(_options),
86+
headers
87+
}).then(res => {
88+
_cleanup()
89+
return res.json()
90+
})
91+
}
92+
93+
this.preview = function () {
94+
_checkIfNotEmpty(_options, {
95+
text: null,
96+
transliteration: {
97+
optional: true
98+
}
99+
})
100+
_checkIfNotEmpty(_auth, _authShape)
101+
const URL = _buildURL('sms-preview')
102+
const headers = {
103+
'Content-Type': 'application/json',
104+
Accept: 'application/json',
105+
Authorization: _buildAuthorizationHeader()
106+
}
107+
108+
return fetch(URL, {
109+
method: 'POST',
110+
body: JSON.stringify(_options),
111+
headers
112+
}).then(res => {
113+
_cleanup()
114+
return res.json()
115+
})
116+
}
117+
}
118+
119+
module.exports = InfobipAPI

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "infobip-js",
3+
"version": "0.0.1",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/pitops/infobip-js.git"
9+
},
10+
"bugs": {
11+
"url": "https://github.com/pitops/infobip-js/issues"
12+
},
13+
"homepage": "https://github.com/pitops/infobip-js#readme",
14+
"keywords": ["sms-wrapper", "send sms"],
15+
"author": "Petros Kyriakou",
16+
"dependencies": {
17+
"node-fetch": "^2.6.0"
18+
}
19+
}

0 commit comments

Comments
 (0)