Comparing version 2.2.23 to 2.2.24
@@ -133,2 +133,7 @@ /// <reference types="node" /> | ||
parseTempMiddleWares(): any; | ||
parseUseDecorator(): any; | ||
useMiddleWares: { | ||
path: string; | ||
middleWares: MiddleWareInterface[]; | ||
}[]; | ||
} | ||
@@ -135,0 +140,0 @@ export interface ServerConfigurations { |
@@ -37,2 +37,6 @@ /** | ||
tempMiddleWares: TempMiddleWares[]; | ||
useMiddleWares: { | ||
path: string; | ||
middleWares: MiddleWareInterface[]; | ||
}[]; | ||
constructor(options?: { | ||
@@ -42,2 +46,4 @@ base?: string; | ||
}); | ||
parseUseDecorator(): void; | ||
private parseUseMiddleWares; | ||
use(path: string, ...middleWares: MiddleWareInterface[]): this; | ||
@@ -44,0 +50,0 @@ build(): this; |
@@ -52,2 +52,3 @@ "use strict"; | ||
this.options = options || {}; | ||
this.useMiddleWares = this.useMiddleWares || []; | ||
this.setBase(); | ||
@@ -63,6 +64,13 @@ this.getRouters = this.getRouters || []; | ||
this.staticFolder = this.options.staticFolder || this.staticFolder; | ||
this.parseUseDecorator(); | ||
this.parseTempMiddleWares(); | ||
} | ||
use(path, ...middleWares) { | ||
parseUseDecorator() { | ||
this.useMiddleWares.map(obj => { | ||
this.parseUseMiddleWares(obj.path, ...obj.middleWares); | ||
}); | ||
} | ||
parseUseMiddleWares(path, ...middleWares) { | ||
this.globalMiddleWares = this.globalMiddleWares || []; | ||
path = `${this.base}/${path}`.replace(/(https?:\/\/)|(\/)+/g, "$1$2"); | ||
const pattern = new url_pattern_1.default(path); | ||
@@ -83,2 +91,11 @@ const applyMiddleWares = (comingMiddleWares) => { | ||
applyMiddleWares(middleWares); | ||
} | ||
use(path, ...middleWares) { | ||
this.useMiddleWares = this.useMiddleWares || []; | ||
if (this.base) { | ||
this.parseUseMiddleWares(path, ...middleWares); | ||
} | ||
else { | ||
this.useMiddleWares.push({ path, middleWares }); | ||
} | ||
return this; | ||
@@ -85,0 +102,0 @@ } |
{ | ||
"name": "grandjs", | ||
"version": "2.2.23", | ||
"version": "2.2.24", | ||
"description": "A backend framework for solid web apps based on node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -120,3 +120,5 @@ /** | ||
tempMiddleWares: TempMiddleWares[] | ||
parseTempMiddleWares():any | ||
parseTempMiddleWares(): any | ||
parseUseDecorator(): any | ||
useMiddleWares: {path:string, middleWares:MiddleWareInterface[]}[] | ||
} | ||
@@ -123,0 +125,0 @@ export interface ServerConfigurations{ |
@@ -48,5 +48,7 @@ /** | ||
tempMiddleWares: TempMiddleWares[] | ||
useMiddleWares: {path:string, middleWares:MiddleWareInterface[]}[] | ||
constructor(options?: { base?: string, staticFolder?: StaticFolderInterface }) { | ||
this.tempMiddleWares = this.tempMiddleWares || []; | ||
this.options = options || {}; | ||
this.useMiddleWares = this.useMiddleWares || []; | ||
this.setBase(); | ||
@@ -62,20 +64,35 @@ this.getRouters = this.getRouters || []; | ||
this.staticFolder = this.options.staticFolder || this.staticFolder; | ||
this.parseUseDecorator(); | ||
this.parseTempMiddleWares(); | ||
} | ||
parseUseDecorator() { | ||
this.useMiddleWares.map(obj => { | ||
this.parseUseMiddleWares(obj.path, ...obj.middleWares); | ||
}); | ||
} | ||
private parseUseMiddleWares?(path:string, ...middleWares: MiddleWareInterface[]) { | ||
this.globalMiddleWares = this.globalMiddleWares || []; | ||
path = `${this.base}/${path}`.replace(/(https?:\/\/)|(\/)+/g, "$1$2"); | ||
const pattern = new RouteParser(path); | ||
const applyMiddleWares = (comingMiddleWares: MiddleWareInterface[]) => { | ||
comingMiddleWares.map((func) => { | ||
const middleWare = (req: Request, res: Response, next: Function) => { | ||
if (pattern.match(req.pathname)) { | ||
func(req, res, next); | ||
} else { | ||
return next(); | ||
} | ||
} | ||
this.globalMiddleWares.push(middleWare); | ||
}) | ||
} | ||
applyMiddleWares(middleWares); | ||
} | ||
use(path: string, ...middleWares: MiddleWareInterface[]): this { | ||
this.globalMiddleWares = this.globalMiddleWares || []; | ||
const pattern = new RouteParser(path); | ||
const applyMiddleWares = (comingMiddleWares: MiddleWareInterface[]) => { | ||
comingMiddleWares.map((func) => { | ||
const middleWare = (req: Request, res: Response, next: Function) => { | ||
if (pattern.match(req.pathname)) { | ||
func(req, res, next); | ||
} else { | ||
return next(); | ||
} | ||
} | ||
this.globalMiddleWares.push(middleWare); | ||
}) | ||
this.useMiddleWares = this.useMiddleWares || []; | ||
if (this.base) { | ||
this.parseUseMiddleWares(path, ...middleWares); | ||
} else { | ||
this.useMiddleWares.push({ path, middleWares }); | ||
} | ||
applyMiddleWares(middleWares); | ||
return this; | ||
@@ -82,0 +99,0 @@ } |
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
261478
59
5689