1
0
Fork 0
master
Matthew 2021-08-09 14:18:40 -04:00
parent 647e3be766
commit f07d155d96
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
1 changed files with 152 additions and 152 deletions

View File

@ -1,152 +1,152 @@
/* eslint-disable consistent-return */ /* eslint-disable consistent-return */
import { Request, Response, NextFunction, Router as router } from 'express'; import { Request, Response, NextFunction, Router as router } from 'express';
import { Server } from '.'; import { Server } from '.';
export default class Route { export default class Route {
public server: Server; public server: Server;
public router: router; public router: router;
public conf: { path: string, deprecated?: boolean, maintenance?: boolean }; public conf: { path: string, deprecated?: boolean, maintenance?: boolean };
protected constructor(server: Server, conf: { path: string, deprecated?: boolean, maintenance?: boolean }) { protected constructor(server: Server, conf: { path: string, deprecated?: boolean, maintenance?: boolean }) {
this.conf = { this.conf = {
path: null, path: null,
deprecated: false, deprecated: false,
maintenance: false, maintenance: false,
}; };
this.server = server; this.server = server;
this.router = router(); this.router = router();
this.conf = conf; this.conf = conf;
} }
public bind() {} public bind() {}
public deprecated(): void { public deprecated(): void {
this.router.all('*', (_req, res) => { this.router.all('*', (_req, res) => {
res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED }); res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED });
}); });
} }
public maintenance(): void { public maintenance(): void {
this.router.all('*', (_req, res) => { this.router.all('*', (_req, res) => {
res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE }); res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE });
}); });
} }
public init(): void { public init(): void {
this.router.all('*', (req, res, next) => { this.router.all('*', (req, res, next) => {
this.server.client.signale.log(`'${req.method}' request from '${req.ip}' to '${req.hostname}${req.path}'.`); // this.server.client.signale.log(`'${req.method}' request from '${req.ip}' to '${req.hostname}${req.path}'.`);
if (this.conf.maintenance === true) res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE }); if (this.conf.maintenance === true) res.status(503).json({ code: this.constants.codes.MAINTENANCE_OR_UNAVAILABLE, message: this.constants.messages.MAINTENANCE_OR_UNAVAILABLE });
else if (this.conf.deprecated === true) res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED }); else if (this.conf.deprecated === true) res.status(501).json({ code: this.constants.codes.DEPRECATED, message: this.constants.messages.DEPRECATED });
else next(); else next();
}); });
} }
/** /**
* This function checks for the presense of a Bearer token with Security.extractBearer(), * This function checks for the presense of a Bearer token with Security.extractBearer(),
* then it will attempt to validate it with Security.checkBearer(). * then it will attempt to validate it with Security.checkBearer().
* If it can authenticate the request, it'll add a custom property on Request called * If it can authenticate the request, it'll add a custom property on Request called
* `account`, which will hold an the bearer token's account owner. The account is of the * `account`, which will hold an the bearer token's account owner. The account is of the
* type `AccountInterface`. * type `AccountInterface`.
* @param req The Request object from Express. * @param req The Request object from Express.
* @param res The Response object from Express. * @param res The Response object from Express.
* @param next The NextFunction from Express. * @param next The NextFunction from Express.
* @example Security.authorize(req, res, next); * @example Security.authorize(req, res, next);
*/ */
public async authorize(req: Request, res: Response, next: NextFunction) { public async authorize(req: Request, res: Response, next: NextFunction) {
const account = await this.server.security.checkBearer(this.server.security.extractBearer(req)); const account = await this.server.security.checkBearer(this.server.security.extractBearer(req));
if (!account) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); if (!account) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED });
Object.defineProperty(req, 'account', { value: account, writable: true, enumerable: true, configurable: true }); Object.defineProperty(req, 'account', { value: account, writable: true, enumerable: true, configurable: true });
next(); next();
} }
/** /**
* This function calls Util.handleError() internally, however it also sends a generic * This function calls Util.handleError() internally, however it also sends a generic
* response to the user. * response to the user.
* @param error The Error object. * @param error The Error object.
* @param res The Response object from Express. * @param res The Response object from Express.
*/ */
public handleError(error: Error, res: Response): void { public handleError(error: Error, res: Response): void {
this.server.client.util.handleError(error); this.server.client.util.handleError(error);
res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR }); res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR });
} }
get constants() { get constants() {
return { return {
codes: { codes: {
/** /**
* SUCCESS 100 * SUCCESS 100
* Used if the request was processed successfully. * Used if the request was processed successfully.
*/ */
SUCCESS: 100, SUCCESS: 100,
/** /**
* UNAUTHORIZED 101 * UNAUTHORIZED 101
* Used if the client calling the request couldn't be correctly authenticated. * Used if the client calling the request couldn't be correctly authenticated.
*/ */
UNAUTHORIZED: 101, UNAUTHORIZED: 101,
/** /**
* PERMISSION DENIED 103 * PERMISSION DENIED 103
* Used if the client calling the request doesn't have access to the resource specified. * Used if the client calling the request doesn't have access to the resource specified.
*/ */
PERMISSION_DENIED: 103, PERMISSION_DENIED: 103,
/** /**
* NOT FOUND 104 * NOT FOUND 104
* Used if the resource the client requested doesn't exist. * Used if the resource the client requested doesn't exist.
*/ */
NOT_FOUND: 104, NOT_FOUND: 104,
/** /**
* ACCOUNT NOT FOUND 1041 * ACCOUNT NOT FOUND 1041
* Used if the account specified by the client couldn't be found. * Used if the account specified by the client couldn't be found.
*/ */
ACCOUNT_NOT_FOUND: 1041, ACCOUNT_NOT_FOUND: 1041,
/** /**
* CLIENT ERROR 1044 * CLIENT ERROR 1044
* Used in cases of user error. Examples are incorrect parameters, incorrect headers, or an invalid request. * Used in cases of user error. Examples are incorrect parameters, incorrect headers, or an invalid request.
*/ */
CLIENT_ERROR: 1044, CLIENT_ERROR: 1044,
/** /**
* SERVER ERROR 105 * SERVER ERROR 105
* Used in cases of an internal error that caused the bind() function to throw. * Used in cases of an internal error that caused the bind() function to throw.
*/ */
SERVER_ERROR: 105, SERVER_ERROR: 105,
/** /**
* DEPRECATED 1051 * DEPRECATED 1051
* Returned back to the user if the resource requested is deprecated. * Returned back to the user if the resource requested is deprecated.
*/ */
DEPRECATED: 1051, DEPRECATED: 1051,
/** /**
* MAINTENANCE OR UNAVAILABLE 1053 * MAINTENANCE OR UNAVAILABLE 1053
* Used if the resource requested is currently in maintenance, not finished, or temporarily disabled. * Used if the resource requested is currently in maintenance, not finished, or temporarily disabled.
*/ */
MAINTENANCE_OR_UNAVAILABLE: 1053, MAINTENANCE_OR_UNAVAILABLE: 1053,
}, },
messages: { messages: {
/** /**
* The credentials you supplied are invalid. * The credentials you supplied are invalid.
*/ */
UNAUTHORIZED: ['CREDENTIALS_INVALID', 'The credentials you supplied are invalid.'], UNAUTHORIZED: ['CREDENTIALS_INVALID', 'The credentials you supplied are invalid.'],
/** /**
* You do not have valid credentials to access this resource. * You do not have valid credentials to access this resource.
*/ */
PERMISSION_DENIED: ['PERMISSION_DENIED', 'You do not have valid credentials to access this resource.'], PERMISSION_DENIED: ['PERMISSION_DENIED', 'You do not have valid credentials to access this resource.'],
/** /**
* The resource you requested cannot be located. * The resource you requested cannot be located.
*/ */
NOT_FOUND: ['NOT_FOUND', 'The resource you requested cannot be located.'], NOT_FOUND: ['NOT_FOUND', 'The resource you requested cannot be located.'],
/** /**
* An internal error has occurred, Engineers have been notified. * An internal error has occurred, Engineers have been notified.
*/ */
SERVER_ERROR: ['INTERNAL_ERROR', 'An internal error has occurred, Engineers have been notified.'], SERVER_ERROR: ['INTERNAL_ERROR', 'An internal error has occurred, Engineers have been notified.'],
/** /**
* The endpoint or resource you\'re trying to access has been deprecated. * The endpoint or resource you\'re trying to access has been deprecated.
*/ */
DEPRECATED: ['ENDPOINT_OR_RESOURCE_DEPRECATED', 'The endpoint or resource you\'re trying to access has been deprecated.'], DEPRECATED: ['ENDPOINT_OR_RESOURCE_DEPRECATED', 'The endpoint or resource you\'re trying to access has been deprecated.'],
/** /**
* The endpoint or resource you\'re trying to access is either in maintenance or is not available. * The endpoint or resource you\'re trying to access is either in maintenance or is not available.
*/ */
MAINTENANCE_OR_UNAVAILABLE: ['SERVICE_UNAVAILABLE', 'The endpoint or resource you\'re trying to access is either in maintenance or is not available.'], MAINTENANCE_OR_UNAVAILABLE: ['SERVICE_UNAVAILABLE', 'The endpoint or resource you\'re trying to access is either in maintenance or is not available.'],
}, },
}; };
} }
} }