UNPKG

@del-internet/sdk-radius

Version:
237 lines (236 loc) 9.55 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const client_1 = __importDefault(require("@avonjs/client")); const await_to_js_1 = __importDefault(require("await-to-js")); const node_crypto_1 = require("node:crypto"); class Client { constructor(config) { this.config = config; this.debugger = (data) => { if (this.config.debug === false) { return; } console.debug(data); }; } get client() { return new client_1.default(this.config.endpoint); } paginate() { return __awaiter(this, arguments, void 0, function* (page = 1, perPage = 50, filters) { const timestamp = `${Date.now()}`; const pagination = this.cleanObject({ page: `${page}`, perPage: `${perPage}`, filters, }); const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders(Object.assign({ timestamp }, pagination))) .resource('users') .paginate(pagination); const { data, meta } = yield this.handleResponse(response); return { data: data, count: Number(meta.count), }; }); } search(username_1) { return __awaiter(this, arguments, void 0, function* (username, perPage = 50) { return this.paginate(1, perPage, { 'Text:username': username }); }); } exists(username) { return __awaiter(this, void 0, void 0, function* () { const { data, count } = yield this.search(username); return count > 0; }); } createUser(user) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders(Object.assign({ timestamp }, user))) .resource('users') .create(this.stringify(user)); return this.handleResponse(response); }); } updateUser(username, user) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders(Object.assign({ timestamp }, user))) .resource('users') .update(username, this.stringify(user)); return this.handleResponse(response); }); } setUserSpeed(username, profile) { return __awaiter(this, void 0, void 0, function* () { return this.updateUser(username, profile); }); } viewUser(username) { return __awaiter(this, void 0, void 0, function* () { return this.findUser(username); }); } findUser(username) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp })) .resource('users') .view(username); const { data } = yield this.handleResponse(response); // @ts-ignore return data.fields; }); } deleteUser(username) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp })) .resource('users') .delete(username); return this.handleResponse(response); }); } attachStaticIp(username, ip) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp, ip })) .resource('users') .action('attach-static-ip') .inline(username, this.stringify({ ip })); return this.handleResponse(response); }); } detachStaticIp(username) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp })) .resource('users') .action('detach-static-ip', true) .inline(username); return this.handleResponse(response); }); } attachIpV6(username, ip) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp, ip })) .resource('users') .action('attach-ip-v6') .inline(username, this.stringify({ ip })); return this.handleResponse(response); }); } detachIpV6(username) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp })) .resource('users') .action('detach-ip-v6', true) .inline(username); return this.handleResponse(response); }); } block(username) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp })) .resource('users') .action('block') .inline(username); return this.handleResponse(response); }); } coa(username) { return __awaiter(this, void 0, void 0, function* () { const timestamp = `${Date.now()}`; const response = yield this.client .withParam('timestamp', timestamp) .withHeaders(this.securityHeaders({ timestamp })) .resource('users') .action('refresh-coa') .inline(username); return this.handleResponse(response); }); } securityHeaders(payload = {}) { var _a, _b; const token = (0, node_crypto_1.createHmac)('sha256', this.config.secret) .update(this.stringify(payload)) .digest('hex'); return { [(_a = this.config.headers.client) !== null && _a !== void 0 ? _a : 'client']: this.config.client, [(_b = this.config.headers.secret) !== null && _b !== void 0 ? _b : 'secret']: token, }; } cleanObject(data) { return Object.fromEntries(Object.entries(data).filter(([_, value]) => value !== undefined)); } stringify(payload = {}) { return JSON.stringify(payload, Object.keys(payload).sort()); } handleResponse(response) { return __awaiter(this, void 0, void 0, function* () { var _a; const [error, result] = yield (0, await_to_js_1.default)(response.json()); if (response.ok) { return result; } if (error) { this.debug(error); throw new Error(`Request failed with error [${response.status}][${response.statusText}]`); } this.debug(result); // try to throw validation error if (response.status === 422 && result.meta.errors) { const errors = result.meta.errors; const first = Object.keys(errors)[0]; throw new Error((_a = result.meta.errors[first]) !== null && _a !== void 0 ? _a : result.message); } // finally throw the message throw new Error(result.message); }); } debug(...args) { this.debugger(...args); } debugUsing(debugUsing) { this.debugger = debugUsing; return this; } } exports.default = Client;