vweb-mvc
Advanced tools
Comparing version 1.2.11 to 1.2.13
@@ -32,2 +32,3 @@ import 'vweb-core'; | ||
constructor(context: any); | ||
addFilter(path: string | RegExp, filter: FilterAdapter): void; | ||
startup(): void; | ||
@@ -34,0 +35,0 @@ getFilterChain(req: any): FilterChain; |
@@ -41,3 +41,5 @@ "use strict"; | ||
for (let filter of this.filters) { | ||
this.logger.debug(`Do filter ${filter.annotation.name} prepare `); | ||
if (this.logger.isDebugEnabled()) { | ||
this.logger.debug(`Filter ${filter.constructor.name} prepare `); | ||
} | ||
let rs = yield filter.prepare(parameter, options); | ||
@@ -61,3 +63,5 @@ if (rs === false) { | ||
let filter = this.filters[i]; | ||
this.logger.debug(`Do filter ${filter.annotation.name} complete `); | ||
if (this.logger.isDebugEnabled()) { | ||
this.logger.debug(`Filter ${filter.constructor.name} complete `); | ||
} | ||
filter.complete(parameter, result, options); | ||
@@ -88,2 +92,14 @@ } | ||
} | ||
addFilter(path, filter) { | ||
if (path instanceof RegExp) { | ||
this.filterMap.set(path, filter); | ||
return; | ||
} | ||
let regex = path; | ||
if (!regex.startsWith('/')) { | ||
regex = '/' + regex; | ||
} | ||
regex = regex.replace('**', '.@').replace('*', '[^/]*').replace('@', '*'); | ||
this.filterMap.set(new RegExp(regex, 'g'), filter); | ||
} | ||
startup() { | ||
@@ -96,11 +112,3 @@ let { context, logger, filterMap } = this; | ||
return; | ||
if (regex instanceof RegExp) { | ||
filterMap.set(regex, filter); | ||
return; | ||
} | ||
if (!regex.startsWith('/')) { | ||
regex = '/' + regex; | ||
} | ||
regex = regex.replace('**', '.@').replace('*', '[^/]*').replace('@', '*'); | ||
filterMap.set(new RegExp(regex, 'g'), filter); | ||
this.addFilter(regex, filter); | ||
} | ||
@@ -107,0 +115,0 @@ else { |
import { HttpMessageConverter } from "./MessageConverter"; | ||
import { FilterRegulator } from "./filter"; | ||
import InterceptorRegulator from "../interceptor/"; | ||
import { VWebMvcConfig } from "../declare"; | ||
import VWebApplicationContext from "vweb-core/lib/VWebApp"; | ||
import { CorsRegistry } from "./cors"; | ||
import { CorsRegistry, FilterRegistry, InterceptorRegistry } from "./config"; | ||
import { Express } from "express"; | ||
export declare abstract class WebMvcConfigurationSupport { | ||
abstract addCorsMappings(registry: CorsRegistry): any; | ||
export declare class WebMvcConfigurationSupport { | ||
addCorsMappings(registry: CorsRegistry): void; | ||
addInterceptors(registry: InterceptorRegistry): void; | ||
addFilters(registry: FilterRegistry): void; | ||
} | ||
@@ -18,2 +21,3 @@ export default class ControllerAdapter { | ||
readonly filterRegulator: FilterRegulator; | ||
readonly interceptorRegulator: InterceptorRegulator; | ||
readonly messageConverters: Array<HttpMessageConverter>; | ||
@@ -20,0 +24,0 @@ readonly parameterConverters: {}; |
@@ -13,4 +13,4 @@ "use strict"; | ||
const model_2 = require("../model"); | ||
const index_1 = require("../interceptor/index"); | ||
const cors_1 = require("./cors"); | ||
const interceptor_1 = require("../interceptor/"); | ||
const config_1 = require("./config"); | ||
let createError = require('http-errors'); | ||
@@ -40,3 +40,5 @@ let { util, LogicError } = require('vweb-core'); | ||
if (prepare === false) { | ||
logger.debug('Filter interrupt request'); | ||
if (logger.isDebugEnabled()) { | ||
logger.debug('Request interrupt in filter-chain'); | ||
} | ||
return; | ||
@@ -137,2 +139,5 @@ } | ||
class WebMvcConfigurationSupport { | ||
addCorsMappings(registry) { } | ||
addInterceptors(registry) { } | ||
addFilters(registry) { } | ||
} | ||
@@ -150,2 +155,3 @@ exports.WebMvcConfigurationSupport = WebMvcConfigurationSupport; | ||
this.filterRegulator = new filter_1.FilterRegulator(context); | ||
this.interceptorRegulator = new interceptor_1.default(context, express); | ||
this.messageConverters = [new MessageConverter_1.NumberMessageConverter()]; | ||
@@ -161,25 +167,45 @@ this.parameterConverters = {}; | ||
for (let configuration of configurations) { | ||
let registry = new cors_1.CorsRegistrySupport(); | ||
configuration.addCorsMappings(registry); | ||
for (let registration of registry.registrations) { | ||
express.all(registration.path, (req, res, next) => { | ||
for (let { name, value } of registration.getHeaders()) { | ||
if (name === 'Access-Control-Allow-Origin') { | ||
if (value === '$origin') { | ||
value = req.header('origin'); | ||
{ | ||
let registry = new config_1.CorsRegistrySupport(); | ||
configuration.addCorsMappings(registry); | ||
for (let registration of registry.registrations) { | ||
let headers = registration.getHeaders(); | ||
express.all(registration.path, (req, res, next) => { | ||
for (let { name, value } of headers) { | ||
if (name === 'Access-Control-Allow-Origin') { | ||
if (value === '$origin') { | ||
value = req.header('origin'); | ||
} | ||
} | ||
res.header(name, value); | ||
} | ||
res.header(name, value); | ||
} | ||
if (/OPTIONS/i.test(req.method)) { | ||
res.status(200).end(); | ||
} | ||
else { | ||
next(); | ||
} | ||
if (logger.isDebugEnabled()) { | ||
logger.debug(`Cors interceptor set headers ${JSON.stringify(headers)}`); | ||
} | ||
if (/OPTIONS/i.test(req.method)) { | ||
res.status(200).end(); | ||
} | ||
else { | ||
next(); | ||
} | ||
}); | ||
} | ||
} | ||
{ | ||
let registry = new config_1.InterceptorRegistrySupport(); | ||
configuration.addInterceptors(registry); | ||
registry.getInterceptors().forEach(({ name, path, handle }) => { | ||
this.interceptorRegulator.addInterceptor(name, path, handle); | ||
}); | ||
} | ||
{ | ||
let registry = new config_1.FilterRegistrySupport(); | ||
configuration.addFilters(registry); | ||
registry.getFilters().forEach(({ path, handle }) => { | ||
this.filterRegulator.addFilter(path, handle); | ||
}); | ||
} | ||
} | ||
} | ||
(0, index_1.default)(context, express); | ||
this.interceptorRegulator.startup(); | ||
this.filterRegulator.startup(); | ||
@@ -186,0 +212,0 @@ let { log4j } = context; |
import Session from '../session/wrapper'; | ||
import { Express, Request, Response } from "express"; | ||
export declare class InterceptorHandler { | ||
import VWebApplicationContext from "vweb-core/lib/VWebApp"; | ||
export default class InterceptorRegulator { | ||
private context; | ||
private express; | ||
constructor(context: any, express: Express); | ||
handle(interceptor: any): void; | ||
constructor(context: VWebApplicationContext, express: Express); | ||
addInterceptor(name: string, path: string, interceptor: HttpInterceptor): void; | ||
startup(): void; | ||
} | ||
@@ -12,3 +14,1 @@ export declare class HttpInterceptor { | ||
} | ||
declare const _default: (context: any, express: any) => InterceptorHandler; | ||
export default _default; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HttpInterceptor = exports.InterceptorHandler = void 0; | ||
exports.HttpInterceptor = void 0; | ||
const model_1 = require("../model"); | ||
@@ -19,21 +19,13 @@ const wrapper_1 = require("../session/wrapper"); | ||
let qs = require("querystring"); | ||
class InterceptorHandler { | ||
class InterceptorRegulator { | ||
constructor(context, express) { | ||
this.context = context; | ||
this.express = express; | ||
context.getComponent(anno_1.Interceptor).forEach(interceptor => { | ||
this.handle(interceptor); | ||
}); | ||
} | ||
handle(interceptor) { | ||
if (!(interceptor instanceof HttpInterceptor)) { | ||
throw new Error(`${interceptor.name} must be extends interceptor.HttpInterceptor`); | ||
} | ||
addInterceptor(name, path, interceptor) { | ||
const { express, context } = this; | ||
let { annotation } = interceptor; | ||
let logger = context.log4j.getLogger(annotation.name); | ||
const { path } = annotation; | ||
path.split(',').forEach(path => { | ||
logger.info(`${annotation.name} intercept [${path}]`); | ||
express.all(path, (req, res, next) => { | ||
let logger = context.log4j.getLogger(name); | ||
path.split(',').forEach(route => { | ||
logger.info(`${name} intercept [${route}]`); | ||
express.all(route, (req, res, next) => { | ||
let proxy = new Proxy(interceptor, { | ||
@@ -79,4 +71,10 @@ get: (target, prop, receiver) => { | ||
} | ||
startup() { | ||
this.context.getComponent(anno_1.Interceptor).forEach(interceptor => { | ||
const { name, path } = interceptor.annotation; | ||
this.addInterceptor(name, path, interceptor); | ||
}); | ||
} | ||
} | ||
exports.InterceptorHandler = InterceptorHandler; | ||
exports.default = InterceptorRegulator; | ||
class HttpInterceptor { | ||
@@ -89,4 +87,1 @@ intercept(request, response, session) { | ||
exports.HttpInterceptor = HttpInterceptor; | ||
exports.default = (context, express) => { | ||
return new InterceptorHandler(context, express); | ||
}; |
{ | ||
"name": "vweb-mvc", | ||
"version": "1.2.11", | ||
"version": "1.2.13", | ||
"description": "mvc ", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index", |
80534
2097