arrow-express
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -44,3 +44,3 @@ import Express from "express"; | ||
private static getRoutePath; | ||
private static isResponseAlreadyEnded; | ||
private static canSendResponse; | ||
} | ||
@@ -47,0 +47,0 @@ type ApplicationOptions = { |
@@ -131,3 +131,3 @@ "use strict"; | ||
response = _a.sent(); | ||
if (AppConfigurator.isResponseAlreadyEnded(res)) { | ||
if (AppConfigurator.canSendResponse(res)) { | ||
if (!res.statusCode) { | ||
@@ -141,7 +141,5 @@ res.status(200); | ||
error_1 = _a.sent(); | ||
if (AppConfigurator.isResponseAlreadyEnded(res)) { | ||
if (AppConfigurator.canSendResponse(res)) { | ||
if (error_1 instanceof request_error_1.RequestError) { | ||
res | ||
.status(error_1.httpCode || 500) | ||
.send(error_1.response || "Internal error"); | ||
res.status(error_1.httpCode || 500).send(error_1.response || "Internal error"); | ||
} | ||
@@ -167,5 +165,3 @@ else { | ||
AppConfigurator.prototype.getExpressRoutesAsStrings = function () { | ||
return this._express._router.stack | ||
.filter(function (r) { return r.route; }) | ||
.map(AppConfigurator.expressRouteAsString); | ||
return this._express._router.stack.filter(function (r) { return r.route; }).map(AppConfigurator.expressRouteAsString); | ||
}; | ||
@@ -189,3 +185,3 @@ // STATIC | ||
}; | ||
AppConfigurator.isResponseAlreadyEnded = function (res) { | ||
AppConfigurator.canSendResponse = function (res) { | ||
return !res.writableEnded; | ||
@@ -192,0 +188,0 @@ }; |
@@ -5,3 +5,3 @@ import { Route, RouteConfigurator } from "arrow-express"; | ||
import { UserService } from "../../../data/services/user.service"; | ||
import { AuthorizeGuard, UserContext } from "../../guards/authorize.guard"; | ||
import { AuthorizeGuard } from "../../guards/authorize.guard"; | ||
@@ -12,3 +12,3 @@ export function GetMyselfRoute(userService: UserService): RouteConfigurator { | ||
.path("myself") | ||
.handler(async (req, res): Promise<User> => { | ||
.handler(async (req): Promise<User> => { | ||
const context = await AuthorizeGuard(req); | ||
@@ -15,0 +15,0 @@ return await userService.getUserById(context.userId); |
@@ -5,5 +5,5 @@ import { Route, RouteConfigurator } from "arrow-express"; | ||
import { UserService } from "../../../data/services/user.service"; | ||
import { AuthorizeGuard, UserContext } from "../../guards/authorize.guard"; | ||
import { AuthorizeGuard } from "../../guards/authorize.guard"; | ||
export function GetUserById(userService: UserService): RouteConfigurator { | ||
export function GetUserByIdRoute(userService: UserService): RouteConfigurator { | ||
return Route() | ||
@@ -10,0 +10,0 @@ .method("get") |
@@ -1,15 +0,9 @@ | ||
import {Controller, ControllerConfiguration, RouteConfigurator} from "arrow-express"; | ||
import { Controller, ControllerConfiguration, RouteConfigurator } from "arrow-express"; | ||
import { UserService } from '../../data/services/user.service'; | ||
import {GetUserById} from './routes/getUserById.route'; | ||
import {GetMyselfRoute} from "./routes/getMyself.route"; | ||
import { UserService } from "../../data/services/user.service"; | ||
import { GetUserByIdRoute } from "./routes/getUserById.route"; | ||
import { GetMyselfRoute } from "./routes/getMyself.route"; | ||
export function UserController(userService: UserService): ControllerConfiguration { | ||
return Controller() | ||
.prefix('users') | ||
.registerRoutes( | ||
GetUserById(userService), | ||
GetMyselfRoute(userService) | ||
); | ||
return Controller().prefix("users").registerRoutes(GetUserByIdRoute(userService), GetMyselfRoute(userService)); | ||
} | ||
@@ -32,5 +32,3 @@ import Express from "express"; | ||
if (this._configured) { | ||
throw new ConfigurationError( | ||
"Cannot configure application multiple times" | ||
); | ||
throw new ConfigurationError("Cannot configure application multiple times"); | ||
} else { | ||
@@ -58,5 +56,3 @@ this._configured = true; | ||
*/ | ||
registerControllers( | ||
...controllers: ControllerConfiguration[] | ||
): AppConfigurator { | ||
registerControllers(...controllers: ControllerConfiguration[]): AppConfigurator { | ||
controllers.forEach(controller => this.registerController(controller)); | ||
@@ -79,6 +75,3 @@ return this; | ||
controller.getControllers().forEach(subController => { | ||
this.startController( | ||
subController, | ||
AppConfigurator.getRoutePath(controller.getPrefix(), prefix) | ||
); | ||
this.startController(subController, AppConfigurator.getRoutePath(controller.getPrefix(), prefix)); | ||
}); | ||
@@ -90,32 +83,17 @@ controller.getRoutes().forEach(route => { | ||
private registerRouteInExpress( | ||
controller: ControllerConfiguration, | ||
route: RouteConfigurator, | ||
prefix?: string | ||
) { | ||
const routePath = AppConfigurator.getRoutePath( | ||
prefix, | ||
controller.getPrefix(), | ||
route.getPath() | ||
); | ||
private registerRouteInExpress(controller: ControllerConfiguration, route: RouteConfigurator, prefix?: string) { | ||
const routePath = AppConfigurator.getRoutePath(prefix, controller.getPrefix(), route.getPath()); | ||
if (!route.getMethod()) { | ||
throw new ConfigurationError( | ||
`Route ${routePath} has no method specified` | ||
); | ||
throw new ConfigurationError(`Route ${routePath} has no method specified`); | ||
} | ||
this._express[route.getMethod()]( | ||
`/${routePath}`, | ||
this.createApplicationRequestHandler(route.getRequestHandler()) | ||
); | ||
this._express[route.getMethod()](`/${routePath}`, this.createApplicationRequestHandler(route.getRequestHandler())); | ||
} | ||
private createApplicationRequestHandler( | ||
routeRequestHandler: RequestHandler | ||
): Express.RequestHandler { | ||
private createApplicationRequestHandler(routeRequestHandler: RequestHandler): Express.RequestHandler { | ||
return async (req: Express.Request, res: Express.Response) => { | ||
try { | ||
const response = await routeRequestHandler(req, res); | ||
if (AppConfigurator.isResponseAlreadyEnded(res)) { | ||
if (AppConfigurator.canSendResponse(res)) { | ||
if (!res.statusCode) { | ||
@@ -127,7 +105,5 @@ res.status(200); | ||
} catch (error) { | ||
if (AppConfigurator.isResponseAlreadyEnded(res)) { | ||
if (AppConfigurator.canSendResponse(res)) { | ||
if (error instanceof RequestError) { | ||
res | ||
.status(error.httpCode || 500) | ||
.send(error.response || "Internal error"); | ||
res.status(error.httpCode || 500).send(error.response || "Internal error"); | ||
} else { | ||
@@ -145,5 +121,3 @@ res.status(500).send("Internal error"); | ||
if (this.logRequests) { | ||
console.log( | ||
`Request ${req.method}:${req.path} Response status: ${res.statusCode}` | ||
); | ||
console.log(`Request ${req.method}:${req.path} Response status: ${res.statusCode}`); | ||
} | ||
@@ -153,5 +127,3 @@ } | ||
private getExpressRoutesAsStrings() { | ||
return this._express._router.stack | ||
.filter(r => r.route) | ||
.map(AppConfigurator.expressRouteAsString); | ||
return this._express._router.stack.filter(r => r.route).map(AppConfigurator.expressRouteAsString); | ||
} | ||
@@ -173,3 +145,3 @@ // STATIC | ||
private static isResponseAlreadyEnded(res: Express.Response) { | ||
private static canSendResponse(res: Express.Response) { | ||
return !res.writableEnded; | ||
@@ -176,0 +148,0 @@ } |
{ | ||
"name": "arrow-express", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Library to bootstrap express applications with zero configuration", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -49,2 +49,3 @@ # Arrow Express | ||
.configure(); | ||
ExpressApp.listen(3000); | ||
@@ -51,0 +52,0 @@ ``` |
191
792135
15776