-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
84 lines (75 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var request = require('request');
var baseUrl = 'https://jusibe.com/smsapi/';
/**
* Create new Jusibe instances
* @param {String} publicKey Jusibe Public Key
* @param {String} accessToken Jusibe Access Token
* @return {Jusibe}
*/
function Jusibe(publicKey, accessToken) {
if (!(publicKey || accessToken)) {
throw new Error('Provide both Jusibe PUBLIC_KEY and ACCESS_TOKEN');
}
if (!(this instanceof Jusibe)) {
return new Jusibe(publicKey, accessToken);
}
this.options = {
auth: {
user: publicKey,
pass: accessToken
},
json: true
};
}
var methods = {
/**
* Send SMS
* @function
* @param {Object} payload sms Object
* @return {Promise}
*/
sendSMS(payload) {
var options = Object.assign({ qs: payload, method: 'POST' }, this.options);
return this._makeRequest('send_sms/', options);
},
/**
* Get Available Jusibe Credits
* @function
* @return {Promise}
*/
getCredits() {
return this._makeRequest('get_credits/', this.options);
},
/**
* Check the delivery status of SMS sent
* @function
* @param {String} messageID ID of the message
* @return {Promise}
*/
deliveryStatus(messageID) {
var options = Object.assign({ qs: {
message_id: messageID
} }, this.options);
return this._makeRequest('delivery_status/', options);
},
/**
* Make HTTP request
* @function
* @param {String} url Request URL
* @param {Object} options Request options
* @return {Promise}
*/
_makeRequest(url, options) {
return new Promise((resolve, reject) => {
request(`${baseUrl}${url}`, options, (error, response, body) => {
if (response.statusCode === 200) {
resolve(response);
} else {
reject(response);
}
});
});
}
};
Object.assign(Jusibe.prototype, methods);
module.exports = Jusibe;