UNPKG

@arisan/data-api

Version:

The Universal Database API Gateway for CLIO's Modules

104 lines (90 loc) 2.42 kB
'use strict'; // eslint-disable-line strict const ipaddr = require('ipaddr.js'); const isWithinValidPublicIpRange = require('../Utils').isWithinValidPublicIpRange; class BaseHandler { constructor(logger) { this.logger = logger; } checkValidPublicIp(ip, res, tag) { if (!ip) { this.handleError(res, 400, `${ tag }No Public IP`); return false; } if (!ipaddr.isValid(ip)) { this.handleError(res, 400, `${ tag }Malformed Public IP ${ ip }`); return false; } if (!isWithinValidPublicIpRange(ip)) { this.handleError(res, 400, `${ tag }Public IP in Invalid Range ${ ip }`); return false; } return true; } checkValidPort(port, res, tag) { if (!port) { this.handleError(res, 400, `${ tag }No Port`); return false; } if (port < 1 || port > 65535) { this.handleError(res, 400, `${ tag }Port in Invalid Range ${ port }`); return false; } return true; } checkValidProjectId(id, res, tag) { if (!id) { this.handleError(res, 400, `${ tag }No Project ID`); return false; } return true; } checkValidPublicAddress(address, res, tag) { if (!address) { this.handleError(res, 400, `${ tag }No Public Address`); return false; } return true; } checkValidWebPort(port, res, tag) { if (!port) { this.handleError(res, 400, `${ tag }No Web Port`); return false; } if (port < 1 || port > 65535) { this.handleError(res, 400, `${ tag }Web Port in Invalid Range ${ port }`); return false; } return true; } checkRequestBodyProperties(reqBody, res, tag, id, ...args) { if (!reqBody) { return false; } for (let i = 0; i < args.length; i++) { if (!{}.hasOwnProperty.call(reqBody, args[i])) { this.handleError(res, 400, `${ tag }ID ${ id } Request Body Contains No ${ args[i] }`); return false; } } return true; } info(message) { this.logger.info(message); } debug(message) { this.logger.debug(message); } error(message) { this.logger.error(message); } handleError(res, httpStatusCode, message) { if (message) { this.logger.error(message); res.status(httpStatusCode).send(message); } else { res.sendStatus(httpStatusCode); } } } module.exports = BaseHandler; //# sourceMappingURL=base-handler.js.map