From b664a4195a81c7cd4a4f71e4f7cacb9edb21347b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 15 Jul 2020 10:35:30 -0700 Subject: [PATCH] feat: switch to middleware based sequence for examples --- .../access-control-migration/src/sequence.ts | 66 +------------------ examples/express-composition/src/sequence.ts | 46 +------------ examples/file-transfer/src/sequence.ts | 46 +------------ examples/lb3-application/src/sequence.ts | 46 +------------ examples/rest-crud/src/sequence.ts | 46 +------------ examples/soap-calculator/src/sequence.ts | 46 +------------ examples/todo-jwt/src/sequence.ts | 66 +------------------ examples/todo-list/src/sequence.ts | 46 +------------ examples/todo/src/application.ts | 3 +- 9 files changed, 17 insertions(+), 394 deletions(-) diff --git a/examples/access-control-migration/src/sequence.ts b/examples/access-control-migration/src/sequence.ts index 31d2a5a552d3..d17f5e9fd00b 100644 --- a/examples/access-control-migration/src/sequence.ts +++ b/examples/access-control-migration/src/sequence.ts @@ -3,68 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import { - AuthenticateFn, - AuthenticationBindings, - AUTHENTICATION_STRATEGY_NOT_FOUND, - USER_PROFILE_NOT_FOUND, -} from '@loopback/authentication'; -import {Context, inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(RestBindings.Http.CONTEXT) public ctx: Context, - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - @inject(AuthenticationBindings.AUTH_ACTION) - protected authenticateRequest: AuthenticateFn, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - - const route = this.findRoute(request); - - //call authentication action - await this.authenticateRequest(request); - - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (error) { - if ( - error.code === AUTHENTICATION_STRATEGY_NOT_FOUND || - error.code === USER_PROFILE_NOT_FOUND - ) { - Object.assign(error, {statusCode: 401 /* Unauthorized */}); - } - this.reject(context, error); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/express-composition/src/sequence.ts b/examples/express-composition/src/sequence.ts index ef3adaf25a6e..bae08eee74b8 100644 --- a/examples/express-composition/src/sequence.ts +++ b/examples/express-composition/src/sequence.ts @@ -3,48 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - const route = this.findRoute(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (err) { - this.reject(context, err); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/file-transfer/src/sequence.ts b/examples/file-transfer/src/sequence.ts index ccb39e3f8490..06d2c3eb2c8b 100644 --- a/examples/file-transfer/src/sequence.ts +++ b/examples/file-transfer/src/sequence.ts @@ -3,48 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - const route = this.findRoute(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (err) { - this.reject(context, err); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/lb3-application/src/sequence.ts b/examples/lb3-application/src/sequence.ts index 2b16903ac3f3..a00a84247d30 100644 --- a/examples/lb3-application/src/sequence.ts +++ b/examples/lb3-application/src/sequence.ts @@ -3,48 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - const route = this.findRoute(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (err) { - this.reject(context, err); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/rest-crud/src/sequence.ts b/examples/rest-crud/src/sequence.ts index 6da07a6c1511..1bd5171aa378 100644 --- a/examples/rest-crud/src/sequence.ts +++ b/examples/rest-crud/src/sequence.ts @@ -3,48 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - const route = this.findRoute(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (err) { - this.reject(context, err); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/soap-calculator/src/sequence.ts b/examples/soap-calculator/src/sequence.ts index b21da36501c3..1011bc88bf98 100644 --- a/examples/soap-calculator/src/sequence.ts +++ b/examples/soap-calculator/src/sequence.ts @@ -3,48 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - const route = this.findRoute(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (err) { - this.reject(context, err); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/todo-jwt/src/sequence.ts b/examples/todo-jwt/src/sequence.ts index a19f201cef99..30158dce771c 100644 --- a/examples/todo-jwt/src/sequence.ts +++ b/examples/todo-jwt/src/sequence.ts @@ -3,68 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import { - AuthenticateFn, - AuthenticationBindings, - AUTHENTICATION_STRATEGY_NOT_FOUND, - USER_PROFILE_NOT_FOUND, -} from '@loopback/authentication'; -import {Context, inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(RestBindings.Http.CONTEXT) public ctx: Context, - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - @inject(AuthenticationBindings.AUTH_ACTION) - protected authenticateRequest: AuthenticateFn, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - - const route = this.findRoute(request); - - await this.authenticateRequest(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (error) { - // if error is coming from the JWT authentication extension - // make the statusCode 401 - if ( - error.code === AUTHENTICATION_STRATEGY_NOT_FOUND || - error.code === USER_PROFILE_NOT_FOUND - ) { - Object.assign(error, {statusCode: 401 /* Unauthorized */}); - } - this.reject(context, error); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/todo-list/src/sequence.ts b/examples/todo-list/src/sequence.ts index 7de21d79a76d..0a7ff1d0bd79 100644 --- a/examples/todo-list/src/sequence.ts +++ b/examples/todo-list/src/sequence.ts @@ -3,48 +3,6 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {inject} from '@loopback/core'; -import { - FindRoute, - InvokeMethod, - InvokeMiddleware, - ParseParams, - Reject, - RequestContext, - RestBindings, - Send, - SequenceHandler, -} from '@loopback/rest'; +import {MiddlewareSequence} from '@loopback/rest'; -const SequenceActions = RestBindings.SequenceActions; - -export class MySequence implements SequenceHandler { - /** - * Optional invoker for registered middleware in a chain. - * To be injected via SequenceActions.INVOKE_MIDDLEWARE. - */ - @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true}) - protected invokeMiddleware: InvokeMiddleware = () => false; - - constructor( - @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, - @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, - @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, - @inject(SequenceActions.SEND) public send: Send, - @inject(SequenceActions.REJECT) public reject: Reject, - ) {} - - async handle(context: RequestContext) { - try { - const {request, response} = context; - const finished = await this.invokeMiddleware(context); - if (finished) return; - const route = this.findRoute(request); - const args = await this.parseParams(request, route); - const result = await this.invoke(route, args); - this.send(response, result); - } catch (err) { - this.reject(context, err); - } - } -} +export class MySequence extends MiddlewareSequence {} diff --git a/examples/todo/src/application.ts b/examples/todo/src/application.ts index 18da73a63045..f514a10675eb 100644 --- a/examples/todo/src/application.ts +++ b/examples/todo/src/application.ts @@ -6,7 +6,7 @@ import {BootMixin} from '@loopback/boot'; import {ApplicationConfig} from '@loopback/core'; import {RepositoryMixin} from '@loopback/repository'; -import {Request, Response, RestApplication, RestTags} from '@loopback/rest'; +import {Request, Response, RestApplication} from '@loopback/rest'; import {RestExplorerComponent} from '@loopback/rest-explorer'; import {ServiceMixin} from '@loopback/service-proxy'; import morgan from 'morgan'; @@ -62,7 +62,6 @@ export class TodoListApplication extends BootMixin( this.expressMiddleware(morganFactory, defaultConfig, { injectConfiguration: 'watch', key: 'middleware.morgan', - chain: RestTags.REST_MIDDLEWARE_CHAIN, }); } }