forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nestjs#802 from nestjs/790-feature-middleware
feature(@nestjs/core) superior MiddlewareConsumer, add exclude
- Loading branch information
Showing
19 changed files
with
317 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 15 additions & 6 deletions
21
packages/common/interfaces/middleware/middleware-config-proxy.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
import { MiddlewareConsumer } from './middleware-consumer.interface'; | ||
import { RequestMappingMetadata } from '../request-mapping-metadata.interface'; | ||
import { Type } from '../type.interface'; | ||
import { RouteInfo } from './middleware-configuration.interface'; | ||
import { MiddlewareConsumer } from './middleware-consumer.interface'; | ||
|
||
export interface MiddlewareConfigProxy { | ||
/** | ||
* Passes custom arguments to `resolve()` method of the middleware. | ||
* Delegates custom arguments to the `resolve()` method of the middleware. | ||
* | ||
* @param {} ...data | ||
* @returns {MiddlewareConfigProxy} | ||
*/ | ||
with(...data: any[]): MiddlewareConfigProxy; | ||
|
||
/** | ||
* Attaches passed either routes (strings) or controllers to the processed middleware(s). | ||
* When you pass Controller class Nest will attach middleware to every path defined within this controller. | ||
* Excludes routes from the currently processed middleware. | ||
* This excluded route has to use an exact same route path. | ||
* | ||
* @param {} ...routes | ||
* @returns {MiddlewareConfigProxy} | ||
*/ | ||
exclude(...routes: (string | RouteInfo)[]): MiddlewareConfigProxy; | ||
|
||
/** | ||
* Attaches passed either routes or controllers to the currently configured middleware. | ||
* If you pass a class, Nest would attach middleware to every path defined within this controller. | ||
* | ||
* @param {} ...routes | ||
* @returns {MiddlewareConsumer} | ||
*/ | ||
forRoutes(...routes: (string | Type<any>)[]): MiddlewareConsumer; | ||
forRoutes(...routes: (string | Type<any> | RouteInfo)[]): MiddlewareConsumer; | ||
} |
12 changes: 9 additions & 3 deletions
12
packages/common/interfaces/middleware/middleware-configuration.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { RequestMethod } from '../../enums'; | ||
import { Type } from '../type.interface'; | ||
|
||
export interface MiddlewareConfiguration { | ||
middleware: any; | ||
forRoutes: (Type<any> | string)[]; | ||
export interface RouteInfo { | ||
path: string; | ||
method: RequestMethod; | ||
} | ||
|
||
export interface MiddlewareConfiguration<T = any> { | ||
middleware: T; | ||
forRoutes: (Type<any> | string | RouteInfo)[]; | ||
} |
7 changes: 4 additions & 3 deletions
7
packages/common/interfaces/middleware/middleware-consumer.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { Type } from '../type.interface'; | ||
import { MiddlewareConfigProxy } from './middleware-config-proxy.interface'; | ||
|
||
export interface MiddlewareConsumer { | ||
/** | ||
* Takes single middleware class or array of classes | ||
* that subsequently could be attached to the passed either routes or controllers. | ||
* Takes either middleware class/function or array of classes/functions | ||
* that subsequently shall be attached to the passed routes. | ||
* | ||
* @param {any|any[]} middleware | ||
* @returns {MiddlewareConfigProxy} | ||
*/ | ||
apply(middleware: any | any[]): MiddlewareConfigProxy; | ||
apply(...middleware: (Type<any> | Function)[]): MiddlewareConfigProxy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export type MiddlewareFunction = (req?, res?, next?) => any; | ||
export type MiddlewareFunction< | ||
TRequest = any, | ||
TResponse = any, | ||
TResult = any | ||
> = (req?: TRequest, res?: TResponse, next?: Function) => TResult; |
2 changes: 1 addition & 1 deletion
2
packages/common/interfaces/middleware/nest-middleware.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { MiddlewareFunction } from './middleware.interface'; | ||
|
||
export interface NestMiddleware { | ||
resolve(...args): MiddlewareFunction | Promise<MiddlewareFunction>; | ||
resolve(...args: any[]): MiddlewareFunction | Promise<MiddlewareFunction>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.