Skip to content

Commit

Permalink
Add compliled lib files to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehtesham Hasnain authored and Ehtesham Hasnain committed Sep 30, 2020
1 parent ccee16f commit 6e386dd
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { routeBlocker } from "./route-blocker";
export interface RoutesBlockingMap {
[route: string]: boolean;
}
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.routeBlocker = void 0;
var route_blocker_1 = require("./route-blocker");
Object.defineProperty(exports, "routeBlocker", { enumerable: true, get: function () { return route_blocker_1.routeBlocker; } });
16 changes: 16 additions & 0 deletions lib/route-blocker.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextFunction, Request, Response } from "express";
import { RoutesBlockingMap } from ".";
declare class RouteBlocker {
private static instance;
private routesBlockingMap;
constructor();
get routeBlocks(): RoutesBlockingMap;
middleware(route: string): (req: Request, res: Response, next: NextFunction) => Response<any> | undefined;
disableRoute(route: string): void;
enableRoute(route: string): void;
clearBlockings(): void;
private isRouteBlocked;
private routeBlockingExists;
}
export declare const routeBlocker: RouteBlocker;
export {};
58 changes: 58 additions & 0 deletions lib/route-blocker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.routeBlocker = void 0;
const utils_1 = require("./utils");
class RouteBlocker {
constructor() {
this.routesBlockingMap = {};
if (RouteBlocker.instance instanceof RouteBlocker) {
return RouteBlocker.instance;
}
RouteBlocker.instance = this;
}
get routeBlocks() {
return this.routesBlockingMap;
}
middleware(route) {
return (req, res, next) => {
if (this.isRouteBlocked(route)) {
return res.status(503).send(`Route - ${route} currently unavailable`);
}
next();
};
}
disableRoute(route) {
if (utils_1.isValidString(route)) {
this.routesBlockingMap[route] = true;
console.warn(`Route "${route}": disabled ⛔️`);
}
else {
console.error("Invalid route key 🤡!");
}
}
enableRoute(route) {
if (utils_1.isValidString(route)) {
console.info(`Route "${route}": enabled ✅`);
this.routesBlockingMap[route] = false;
}
else {
console.error("Invalid route key 🤡!");
}
}
clearBlockings() {
this.routesBlockingMap = {};
}
isRouteBlocked(route) {
if (!this.routeBlockingExists(route)) {
return false;
}
return this.routesBlockingMap[route] === true;
}
routeBlockingExists(route) {
if (route in this.routesBlockingMap) {
return true;
}
return false;
}
}
exports.routeBlocker = new RouteBlocker();
1 change: 1 addition & 0 deletions lib/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function isValidString(stringVariable: string): boolean;
13 changes: 13 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidString = void 0;
function isValidString(stringVariable) {
if (stringVariable && !containsSpaces(stringVariable)) {
return true;
}
return false;
}
exports.isValidString = isValidString;
function containsSpaces(stringVariable) {
return stringVariable.indexOf(" ") !== -1;
}

0 comments on commit 6e386dd

Please sign in to comment.