@scvo/router
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -9,2 +9,3 @@ "use strict"; | ||
__export(require("./helpers")); | ||
__export(require("./route-errors")); | ||
//# sourceMappingURL=index.js.map |
@@ -38,2 +38,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var route_errors_1 = require("./route-errors"); | ||
var RouteMatch = /** @class */ (function () { | ||
@@ -53,2 +54,3 @@ function RouteMatch(route, request, context) { | ||
this.layoutName = 'default'; | ||
this.errors = []; | ||
this.route.tasks = this.route.tasks || []; | ||
@@ -70,9 +72,15 @@ this.response.cookies = request.cookies; | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
return [2 /*return*/, this.response]; | ||
case 3: | ||
err_1 = _a.sent(); | ||
this.response.statusCode = 500; | ||
this.response.contentType = 'application/json'; | ||
this.response.body = JSON.stringify(err_1, null, 4); | ||
return [3 /*break*/, 4]; | ||
if (!(err_1 instanceof route_errors_1.RouteError)) { | ||
err_1 = new route_errors_1.RouteError(err_1, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} | ||
this.errors.push(err_1); | ||
throw err_1; | ||
case 4: return [2 /*return*/]; | ||
@@ -85,30 +93,65 @@ } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var i, task, routerTask, _a, _b, err_2; | ||
var i, task, routerTask, _a, _b, err_2, err_3; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_c.trys.push([0, 5, , 6]); | ||
_c.trys.push([0, 7, , 8]); | ||
i = 0; | ||
_c.label = 1; | ||
case 1: | ||
if (!(i < this.route.tasks.length)) return [3 /*break*/, 4]; | ||
if (!(i < this.route.tasks.length)) return [3 /*break*/, 6]; | ||
task = this.route.tasks[i]; | ||
_c.label = 2; | ||
case 2: | ||
_c.trys.push([2, 4, , 5]); | ||
routerTask = this.context.routerTasks[task.taskType]; | ||
_a = this.data; | ||
_b = task.name; | ||
return [4 /*yield*/, routerTask.execute(this, task.config)]; | ||
case 2: | ||
return [4 /*yield*/, routerTask.execute(this, task)]; | ||
case 3: | ||
_a[_b] = _c.sent(); | ||
_c.label = 3; | ||
case 3: | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
err_2 = _c.sent(); | ||
if (!(err_2 instanceof route_errors_1.RouteTaskError)) { | ||
err_2 = new route_errors_1.RouteTaskError(err_2, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
task: task, | ||
redirectTo: task.errorRoute || this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} | ||
else { | ||
err_2.redirectTo = err_2.redirectTo || task.errorRoute || this.route.errorRoute || null; | ||
} | ||
if (err_2.redirectTo) { | ||
throw err_2; | ||
} | ||
else { | ||
console.log('#### RouteMatch.runTasks() -> Task error (but continuing):', err_2); | ||
} | ||
return [3 /*break*/, 5]; | ||
case 5: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 4: | ||
case 6: | ||
//console.log('#### ROUTEMATCH.runTasks() -> Tasks run. Date:', this.data); | ||
return [2 /*return*/]; | ||
case 5: | ||
err_2 = _c.sent(); | ||
console.error('#### RouteMatch -> Failed to run tasks:', err_2); | ||
throw err_2; | ||
case 6: return [2 /*return*/]; | ||
case 7: | ||
err_3 = _c.sent(); | ||
if (!(err_3 instanceof route_errors_1.RouteError)) { | ||
err_3 = new route_errors_1.RouteError(err_3, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} | ||
else { | ||
err_3.redirectTo = err_3.redirectTo || this.route.errorRoute || null; | ||
} | ||
console.error('#### RouteMatch -> Failed to run tasks:', err_3); | ||
throw err_3; | ||
case 8: return [2 /*return*/]; | ||
} | ||
@@ -120,20 +163,51 @@ }); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var routerDestination, response, err_3; | ||
var routerDestination, response, err_4, err_5; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
_a.trys.push([0, 5, , 6]); | ||
routerDestination = this.context.routerDestinations[this.route.destination.destinationType]; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, routerDestination.execute(this)]; | ||
case 1: | ||
case 2: | ||
response = _a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
err_4 = _a.sent(); | ||
if (!(err_4 instanceof route_errors_1.RouteDestinationError)) { | ||
err_4 = new route_errors_1.RouteDestinationError(err_4, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
destination: this.route.destination, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} | ||
else { | ||
err_4.redirectTo = err_4.redirectTo || this.route.errorRoute || null; | ||
} | ||
throw err_4; | ||
case 4: | ||
//console.log('#### ROUTEMATCH.runDestination() -> Destination completed:'); | ||
Object.assign(this.response, response); | ||
this.response.cookies = response.cookies; // Overwriting these in case cookies are cleared in the response | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
err_3 = _a.sent(); | ||
console.error('#### RouteMatch -> Failed to run destination:', err_3); | ||
throw err_3; | ||
case 3: return [2 /*return*/]; | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
err_5 = _a.sent(); | ||
if (!(err_5 instanceof route_errors_1.RouteError)) { | ||
err_5 = new route_errors_1.RouteError(err_5, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} | ||
else { | ||
err_5.redirectTo = err_5.redirectTo || this.route.errorRoute || null; | ||
} | ||
console.error('#### RouteMatch -> Failed to run destination:', err_5); | ||
throw err_5; | ||
case 6: return [2 /*return*/]; | ||
} | ||
@@ -140,0 +214,0 @@ }); |
@@ -43,2 +43,3 @@ "use strict"; | ||
var route_match_1 = require("./route-match"); | ||
var route_errors_1 = require("./route-errors"); | ||
/** Class for managing incoming requests, routing them to Elasticsearch queries, and rendering output */ | ||
@@ -105,3 +106,3 @@ var Router = /** @class */ (function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var routeMatch, err_1; | ||
var routeMatch, response, err_1; | ||
return __generator(this, function (_a) { | ||
@@ -115,7 +116,6 @@ switch (_a.label) { | ||
console.log('[ROUTER], Request:', routeMatch.request.fullUrl, '| Match:', routeMatch.route.name); | ||
return [4 /*yield*/, routeMatch.execute()]; | ||
return [4 /*yield*/, this.executeRoute(routeMatch)]; | ||
case 2: | ||
_a.sent(); | ||
//console.log('#### ROUTER.execute() -> All done. returning response'); | ||
return [2 /*return*/, routeMatch.response]; | ||
response = _a.sent(); | ||
return [2 /*return*/, response]; | ||
case 3: | ||
@@ -164,2 +164,41 @@ err_1 = _a.sent(); | ||
}; | ||
Router.prototype.executeRoute = function (routeMatch) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, err_2, newRoute; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 4]); | ||
return [4 /*yield*/, routeMatch.execute()]; | ||
case 1: | ||
response = _a.sent(); | ||
return [2 /*return*/, response]; | ||
case 2: | ||
err_2 = _a.sent(); | ||
if (!(err_2 instanceof route_errors_1.RouteError)) { | ||
err_2 = new route_errors_1.RouteError(err_2, { | ||
statusCode: 500, | ||
sourceRoute: routeMatch, | ||
redirectTo: routeMatch.route.errorRoute, | ||
data: {} | ||
}); | ||
} | ||
if (!this.routes.hasOwnProperty(err_2.redirectTo)) { | ||
console.log('#### ROUTER.executeRoute() -> Error thrown in route "', routeMatch.route.name, '" but no where to redirect'); | ||
throw err_2; | ||
} | ||
newRoute = this.routes[err_2.redirectTo]; | ||
if (routeMatch.route.name === newRoute.name) { | ||
console.log('#### ROUTER.executeRoute() -> Recursion detected! "', routeMatch.route.name, '" is redirecting to "', newRoute.name, '"'); | ||
throw err_2; | ||
} | ||
routeMatch.route = newRoute; | ||
console.log('#### ROUTER.executeRoute() -> About to redirect:', routeMatch.route.name); | ||
return [4 /*yield*/, this.executeRoute(routeMatch)]; | ||
case 3: return [2 /*return*/, _a.sent()]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return Router; | ||
@@ -166,0 +205,0 @@ }()); |
{ | ||
"name": "@scvo/router", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "A moderately oppinionated web application framework based around mapping routes to tasks and then rendering with handlebars", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -1,4 +0,5 @@ | ||
export * from './interfaces'; | ||
export * from './router'; | ||
export * from './route-match'; | ||
export * from './helpers'; | ||
export * from './interfaces'; | ||
export * from './router'; | ||
export * from './route-match'; | ||
export * from './helpers'; | ||
export * from './route-errors'; |
import * as Url from 'url'; | ||
import { RouteError } from './route-errors'; | ||
@@ -10,3 +11,2 @@ // Module imports | ||
routes: IRoutes; | ||
template: string; | ||
routerTasks: IRouterTasks; | ||
@@ -49,11 +49,13 @@ routerDestinations: IRouterDestinations; | ||
queryEquals: string; | ||
tasks: IRouteTask[]; | ||
tasks: IRouteTask<any>[]; | ||
destination: IRouteDestination; | ||
defaultParams: any; | ||
errorRoute?: string; | ||
} | ||
export interface IRouteTask { | ||
export interface IRouteTask<T> { | ||
name: string; | ||
taskType: string; | ||
config: any; | ||
config: T; | ||
errorRoute?: string; | ||
} | ||
@@ -80,4 +82,11 @@ | ||
context: IContext; | ||
errors: RouteError[]; | ||
} | ||
export interface IRouteRedirect { | ||
sourceRoute: string; | ||
destinationRoute: string; | ||
data: any; | ||
} | ||
export type HttpVerb = 'GET' | 'POST' | 'HEAD' | 'PUT' | 'DELETE' | 'OPTIONS' | 'CONNECT'; | ||
@@ -109,3 +118,3 @@ | ||
name: string; | ||
execute: (routeMatch: IRouteMatch, config: any) => Promise<any>; | ||
execute: (routeMatch: IRouteMatch, task: IRouteTask<any>) => Promise<any>; | ||
new: (...args: any[]) => void; | ||
@@ -112,0 +121,0 @@ } |
@@ -8,2 +8,6 @@ import * as util from 'util'; | ||
} from './interfaces'; | ||
import { | ||
RouteError, RouteTaskError, | ||
RouteDestinationError | ||
} from './route-errors'; | ||
@@ -20,2 +24,3 @@ export class RouteMatch implements IRouteMatch { | ||
layoutName: string = 'default'; | ||
errors: RouteError[] = []; | ||
@@ -27,12 +32,19 @@ constructor(public route: IRoute, public request: IRouterRequest, public context: IContext) { | ||
public async execute(): Promise<void> { | ||
public async execute(): Promise<IRouterResponse> { | ||
try { | ||
await this.runTasks(); | ||
await this.runDestination(); | ||
return this.response; | ||
} catch(err) { | ||
this.response.statusCode = 500; | ||
this.response.contentType = 'application/json'; | ||
this.response.body = JSON.stringify(err, null, 4); | ||
if (!(err instanceof RouteError)) { | ||
err = new RouteError(err, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} | ||
this.errors.push(err); | ||
throw err; | ||
} | ||
return; | ||
} | ||
@@ -46,4 +58,24 @@ | ||
//console.log('#### ROUTEMATCH.runTasks() -> Running task:', task.name, '| type:', task.taskType); | ||
var routerTask = this.context.routerTasks[task.taskType]; | ||
this.data[task.name] = await routerTask.execute(this, task.config); | ||
var routerTask; | ||
try { | ||
routerTask = this.context.routerTasks[task.taskType]; | ||
this.data[task.name] = await routerTask.execute(this, task); | ||
} catch(err) { | ||
if (!(err instanceof RouteTaskError)) { | ||
err = new RouteTaskError(err, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
task: task, | ||
redirectTo: task.errorRoute || this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} else { | ||
err.redirectTo = err.redirectTo || task.errorRoute || this.route.errorRoute || null; | ||
} | ||
if (err.redirectTo) { | ||
throw err; | ||
} else { | ||
console.log('#### RouteMatch.runTasks() -> Task error (but continuing):', err); | ||
} | ||
} | ||
//console.log('#### ROUTEMATCH.runTasks() -> Task completed:', task.name); | ||
@@ -54,2 +86,12 @@ } | ||
} catch(err) { | ||
if (!(err instanceof RouteError)) { | ||
err = new RouteError(err, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} else { | ||
err.redirectTo = err.redirectTo || this.route.errorRoute || null; | ||
} | ||
console.error('#### RouteMatch -> Failed to run tasks:', err); | ||
@@ -61,6 +103,22 @@ throw err; | ||
private async runDestination(): Promise<void> { | ||
var routerDestination; | ||
try { | ||
var routerDestination = this.context.routerDestinations[this.route.destination.destinationType]; | ||
routerDestination = this.context.routerDestinations[this.route.destination.destinationType]; | ||
//console.log('#### ROUTEMATCH.runDestination() -> Running destination:', routerDestination.name); | ||
var response = await routerDestination.execute(this); | ||
try { | ||
var response = await routerDestination.execute(this); | ||
} catch(err) { | ||
if (!(err instanceof RouteDestinationError)) { | ||
err = new RouteDestinationError(err, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
destination: this.route.destination, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} else { | ||
err.redirectTo = err.redirectTo || this.route.errorRoute || null; | ||
} | ||
throw err; | ||
} | ||
//console.log('#### ROUTEMATCH.runDestination() -> Destination completed:'); | ||
@@ -70,2 +128,12 @@ Object.assign(this.response, response); | ||
} catch(err) { | ||
if (!(err instanceof RouteError)) { | ||
err = new RouteError(err, { | ||
statusCode: 500, | ||
sourceRoute: this, | ||
redirectTo: this.route.errorRoute || null, | ||
data: {} | ||
}); | ||
} else { | ||
err.redirectTo = err.redirectTo || this.route.errorRoute || null; | ||
} | ||
console.error('#### RouteMatch -> Failed to run destination:', err); | ||
@@ -72,0 +140,0 @@ throw err; |
@@ -21,2 +21,6 @@ // System imports | ||
import { RouteMatch } from './route-match'; | ||
import { | ||
RouteError, RouteTaskError, | ||
RouteDestinationError | ||
} from './route-errors'; | ||
@@ -30,3 +34,2 @@ /** Class for managing incoming requests, routing them to Elasticsearch queries, and rendering output */ | ||
routes: IRoutes; | ||
template: string; | ||
uaId: string; | ||
@@ -105,7 +108,5 @@ routerTasks: IRouterTasks = {}; | ||
await routeMatch.execute(); | ||
var response: IRouterResponse = await this.executeRoute(routeMatch); | ||
//console.log('#### ROUTER.execute() -> All done. returning response'); | ||
return routeMatch.response; | ||
return response; | ||
} catch(err) { | ||
@@ -153,2 +154,33 @@ throw err; | ||
} | ||
private async executeRoute(routeMatch: RouteMatch): Promise<IRouterResponse> { | ||
try { | ||
var response = await routeMatch.execute(); | ||
return response; | ||
} catch(err) { | ||
if (!(err instanceof RouteError)) { | ||
err = new RouteError(err, { | ||
statusCode: 500, | ||
sourceRoute: routeMatch, | ||
redirectTo: routeMatch.route.errorRoute, | ||
data: {} | ||
}); | ||
} | ||
if (!this.routes.hasOwnProperty(err.redirectTo)) { | ||
console.log('#### ROUTER.executeRoute() -> Error thrown in route "', routeMatch.route.name ,'" but no where to redirect'); | ||
throw err; | ||
} | ||
var newRoute = this.routes[err.redirectTo]; | ||
if (routeMatch.route.name === newRoute.name) { | ||
console.log('#### ROUTER.executeRoute() -> Recursion detected! "', routeMatch.route.name, '" is redirecting to "', newRoute.name, '"'); | ||
throw err; | ||
} | ||
routeMatch.route = newRoute; | ||
console.log('#### ROUTER.executeRoute() -> About to redirect:', routeMatch.route.name); | ||
return await this.executeRoute(routeMatch); | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
280627
60
2937