whmcs-sdk
Version:
A comprehensive and easy-to-use Node SDK, designed to simplify interactions with the WHMCS API and streamline your development process.
132 lines • 4.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.whmcsApi = void 0;
const request_1 = __importDefault(require("request"));
class whmcsApi {
opts;
constructor(opts) {
['host', 'identifier', 'secret'].forEach(name => {
if (!opts.hasOwnProperty(name)) {
throw new Error(`[whmcs-sdk]: missing required option ${name}`);
}
});
this.opts = {
endpoint: 'includes/api.php',
...opts
};
}
/**
* Sends a request to the WHMCS API
* @param opts Options for the request
* @param cb Callback function
* @returns Promise<any>
*/
modem(opts, cb) {
const 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
};
if (cb) {
return (0, request_1.default)(options, cb);
}
return new Promise((resolve, reject) => {
(0, request_1.default)(options, (error, response) => {
if (error)
return reject(error);
const jsonBody = response.body;
if (jsonBody.error)
return reject(jsonBody.error);
if (!opts.raw) {
const keys = Object.keys(jsonBody);
const secondKeys = Object.keys(jsonBody[keys[keys.length - 1]]);
if (secondKeys.length === 1) {
return resolve(jsonBody[keys[keys.length - 1]][secondKeys[0]]);
}
}
return resolve(jsonBody);
});
});
}
/**
* Calls an action on the WHMCS API
* @param action The action to call
* @param opts Options for the request
* @param cb Callback function
* @returns Promise<any>
*/
call(action, opts = {}, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({ action, ...opts }, cb);
}
/**
* Calls a Get action on the WHMCS API
* @param action The action to call
* @param opts Options for the request
* @param cb Callback function
* @returns Promise<any>
*/
get(action, opts = {}, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({ action: `Get${action}`, ...opts }, cb);
}
/**
* Calls an Add action on the WHMCS API
* @param action The action to call
* @param opts Options for the request
* @param cb Callback function
* @returns Promise<any>
*/
add(action, opts = {}, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({ action: `Add${action}`, ...opts }, cb);
}
/**
* Calls an Update action on the WHMCS API
* @param action The action to call
* @param opts Options for the request
* @param cb Callback function
* @returns Promise<any>
*/
update(action, opts = {}, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({ action: `Update${action}`, ...opts }, cb);
}
/**
* Calls a Delete action on the WHMCS API
* @param action The action to call
* @param opts Options for the request
* @param cb Callback function
* @returns Promise<any>
*/
delete(action, opts = {}, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
return this.modem({ action: `Delete${action}`, ...opts }, cb);
}
}
exports.whmcsApi = whmcsApi;
//# sourceMappingURL=client.js.map