forked from maddog986/node-whmcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whmcs.js
145 lines (123 loc) · 3.36 KB
/
whmcs.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Copyright (C) 2019. Drew Gauderman
*
* This source code is licensed under the MIT license found in the
* README.md file in the root directory of this source tree.
*/
const request = require('request');
//export the sonar class
module.exports = class WHMCS {
//class startup
constructor(opts) {
//require host, username, password
['host', 'identifier', 'secret'].forEach(name => {
if (!opts.hasOwnProperty(name)) {
throw new Error('options.' + name + ' is a required argument.');
}
});
this.opts = {
endpoint: 'includes/api.php',
...opts
};
}
//request that returns a promise
modem(opts, cb) {
let options = {
uri: `https://${this.opts.host}/${this.opts.endpoint}`,
method: opts.method || 'POST',
qs: {
identifier: this.opts.identifier,
secret: this.opts.secret,
responsetype: opts.responsetype || 'json',
...opts
},
json: true
};
//call the callback function
if (cb) {
return request(options, cb);
}
return new Promise((res, rej) =>
request(options, (e, r) => {
if (e) return rej(e);
//get the json
const jsonBody = r.body;
if(jsonBody == undefined) return rej("WHMCS_OFFLINE")
//whmcs returned an api error
if (jsonBody.error) return rej(jsonBody.error);
//generally we dont care about all the other details, we just want the data
if (!opts.raw) {
//get main keys to figure out how to return the info
const keys = Object.keys(jsonBody);
const secondKeys = Object.keys(jsonBody[keys[keys.length - 1]]);
//skips to the first row values
if (secondKeys.length === 1) {
return res(jsonBody[keys[keys.length - 1]][secondKeys[0]]);
}
}
//return the body
return res(jsonBody);
})
);
}
//get something
call(action, opts = {}, cb) {
//if no opts but callback function
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({
action: action,
...opts
}, cb);
}
//get something
get(action, opts = {}, cb) {
//if no opts but callback function
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({
action: `Get${action}`,
...opts
}, cb);
}
//add something, just an alias for the get function but with "Add" appended to the action
add(action, opts = {}, cb) {
//if no opts but callback function
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({
action: `Add${action}`,
...opts
}, cb);
}
//update something, just an alias for the get function but with "Update" appended to the action
update(action, opts = {}, cb) {
//if no opts but callback function
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({
action: `Update${action}`,
...opts
}, cb);
}
//delete something, just an alias for the get function but with "Delete" appended to the action
delete(action, opts = {}, cb) {
//if no opts but callback function
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({
action: `Delete${action}`,
...opts
}, cb);
}
};