@plumier/core
Advanced tools
Comparing version 1.0.0-dev.16 to 1.0.0-dev.17
import { Context } from "koa"; | ||
import { ActionResult, Invocation, Middleware, RouteContext } from "./types"; | ||
declare class NotFoundActionInvocation implements Invocation { | ||
context: Context; | ||
constructor(context: Context); | ||
proceed(): Promise<ActionResult>; | ||
} | ||
declare class ActionInvocation implements Invocation { | ||
context: RouteContext; | ||
constructor(context: RouteContext); | ||
proceed(): Promise<ActionResult>; | ||
} | ||
declare function pipe(middlewares: Middleware[], context: Context, invocation: Invocation): Promise<void>; | ||
export { pipe, ActionInvocation, NotFoundActionInvocation }; | ||
import { ActionResult, RouteInfo } from "./types"; | ||
declare function pipe(ctx: Context, route?: RouteInfo, state?: any): Promise<ActionResult>; | ||
export { pipe }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const types_1 = require("./types"); | ||
function getMiddleware(global, route) { | ||
const conMdws = types_1.MiddlewareUtil.extractDecorators(route); | ||
const result = []; | ||
for (const mdw of global) { | ||
result.push(mdw); | ||
} | ||
for (const mdw of conMdws) { | ||
result.push(mdw); | ||
} | ||
return result; | ||
} | ||
class MiddlewareInvocation { | ||
@@ -22,14 +33,13 @@ constructor(middleware, context, next) { | ||
} | ||
exports.NotFoundActionInvocation = NotFoundActionInvocation; | ||
class ActionInvocation { | ||
constructor(context) { | ||
constructor(context, route) { | ||
this.context = context; | ||
this.route = route; | ||
} | ||
async proceed() { | ||
const route = this.context.route; | ||
const config = this.context.config; | ||
const controller = config.dependencyResolver.resolve(route.controller.type); | ||
const result = controller[route.action.name].apply(controller, this.context.parameters); | ||
const controller = config.dependencyResolver.resolve(this.route.controller.type); | ||
const result = controller[this.route.action.name].apply(controller, this.context.parameters); | ||
const awaitedResult = await Promise.resolve(result); | ||
const status = config.responseStatus && config.responseStatus[route.method] || 200; | ||
const status = config.responseStatus && config.responseStatus[this.route.method] || 200; | ||
//if instance of action result, return immediately | ||
@@ -45,5 +55,15 @@ if (awaitedResult && awaitedResult.execute) { | ||
} | ||
exports.ActionInvocation = ActionInvocation; | ||
async function pipe(middlewares, context, invocation) { | ||
let invocationStack = invocation; | ||
function pipe(ctx, route, state) { | ||
const context = ctx; | ||
context.state = Object.assign(Object.assign({}, ctx.state), state); | ||
let middlewares; | ||
let invocationStack; | ||
if (!!route) { | ||
middlewares = getMiddleware(context.config.middlewares, route); | ||
invocationStack = new ActionInvocation(context, route); | ||
} | ||
else { | ||
middlewares = context.config.middlewares.slice(0); | ||
invocationStack = new NotFoundActionInvocation(context); | ||
} | ||
for (let i = middlewares.length; i--;) { | ||
@@ -53,5 +73,4 @@ const mdw = middlewares[i]; | ||
} | ||
const result = await invocationStack.proceed(); | ||
await result.execute(context); | ||
return invocationStack.proceed(); | ||
} | ||
exports.pipe = pipe; |
import { Context } from "koa"; | ||
import { Middleware, RouteInfo } from "./types"; | ||
declare function router(infos: RouteInfo[], globalMiddleware: Middleware[]): (ctx: Context) => Promise<void>; | ||
import { Configuration, RouteInfo } from "./types"; | ||
declare function router(infos: RouteInfo[], config: Configuration): (ctx: Context) => Promise<void>; | ||
export { router }; |
@@ -6,4 +6,4 @@ "use strict"; | ||
const tinspector_1 = require("tinspector"); | ||
const middleware_pipeline_1 = require("./middleware-pipeline"); | ||
const types_1 = require("./types"); | ||
const middleware_pipeline_1 = require("./middleware-pipeline"); | ||
// --------------------------------------------------------------------- // | ||
@@ -25,13 +25,2 @@ // ------------------------------- HELPER ------------------------------ // | ||
} | ||
function getMiddleware(global, route) { | ||
const conMdws = types_1.MiddlewareUtil.extractDecorators(route); | ||
const result = []; | ||
for (const mdw of global) { | ||
result.push(mdw); | ||
} | ||
for (const mdw of conMdws) { | ||
result.push(mdw); | ||
} | ||
return result; | ||
} | ||
function sendError(ctx, status, message) { | ||
@@ -44,9 +33,8 @@ ctx.status = status; | ||
/* ------------------------------------------------------------------------------- */ | ||
function router(infos, globalMiddleware) { | ||
function router(infos, config) { | ||
const matchCache = new Map(); | ||
const middlewareCache = new Map(); | ||
const getMatcherCached = tinspector_1.useCache(matchCache, getMatcher, (info, ctx) => `${ctx.method}${ctx.path}`); | ||
const getMiddlewareCached = tinspector_1.useCache(middlewareCache, getMiddleware, (global, route) => route.url); | ||
return async (ctx) => { | ||
try { | ||
ctx.config = config; | ||
const match = getMatcherCached(infos, ctx); | ||
@@ -59,8 +47,5 @@ if (match) { | ||
ctx.route = match.route; | ||
const middlewares = getMiddlewareCached(globalMiddleware, match.route); | ||
await middleware_pipeline_1.pipe(middlewares, ctx, new middleware_pipeline_1.ActionInvocation(ctx)); | ||
} | ||
else { | ||
await middleware_pipeline_1.pipe(globalMiddleware.slice(0), ctx, new middleware_pipeline_1.NotFoundActionInvocation(ctx)); | ||
} | ||
const result = await middleware_pipeline_1.pipe(ctx, ctx.route); | ||
await result.execute(ctx); | ||
} | ||
@@ -67,0 +52,0 @@ catch (e) { |
@@ -183,2 +183,6 @@ import Koa, { Context } from "koa"; | ||
/** | ||
* List of registered global middlewares | ||
*/ | ||
middlewares: Middleware[]; | ||
/** | ||
* Specify controller path (absolute or relative to entry point) or the controller classes array. | ||
@@ -215,3 +219,2 @@ */ | ||
export interface PlumierConfiguration extends Configuration { | ||
middleware: Middleware[]; | ||
facilities: Facility[]; | ||
@@ -218,0 +221,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { ActionResult, Configuration, Invocation, Middleware, ValidatorFunction } from "./types"; | ||
import { ActionResult, Invocation, Middleware, ValidatorFunction } from "./types"; | ||
declare module "typedconverter" { | ||
@@ -8,6 +8,5 @@ namespace val { | ||
declare class ValidationMiddleware implements Middleware { | ||
private config; | ||
constructor(config: Configuration); | ||
constructor(); | ||
execute(invocation: Readonly<Invocation>): Promise<ActionResult>; | ||
} | ||
export { ValidationMiddleware }; |
@@ -76,8 +76,5 @@ "use strict"; | ||
class ValidationMiddleware { | ||
constructor(config) { | ||
this.config = config; | ||
} | ||
constructor() { } | ||
async execute(invocation) { | ||
const ctx = invocation.context; | ||
ctx.config = this.config; | ||
if (!ctx.route) | ||
@@ -84,0 +81,0 @@ return invocation.proceed(); |
{ | ||
"name": "@plumier/core", | ||
"version": "1.0.0-dev.16+ceb0f65", | ||
"version": "1.0.0-dev.17+ab4fd91", | ||
"description": "Delightful Node.js Rest Framework", | ||
@@ -41,3 +41,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "ceb0f654c29fbf216513c3c60626511492ff56af", | ||
"gitHead": "ab4fd91080ec4f2268241e6ff244e2c3353c1e07", | ||
"devDependencies": { | ||
@@ -44,0 +44,0 @@ "upath": "^1.2.0" |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
121064
2768