Skip to content

Commit

Permalink
feat(example-todo): switch to middleware based sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 27, 2020
1 parent 0df7f09 commit 35fcbe3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 44 deletions.
3 changes: 2 additions & 1 deletion examples/todo/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {RestApplication, RestTags} from '@loopback/rest';
import {RestExplorerComponent} from '@loopback/rest-explorer';
import {ServiceMixin} from '@loopback/service-proxy';
import morgan from 'morgan';
Expand Down Expand Up @@ -62,6 +62,7 @@ export class TodoListApplication extends BootMixin(
this.expressMiddleware(morganFactory, defaultConfig, {
injectConfiguration: 'watch',
key: 'middleware.morgan',
chain: RestTags.REST_MIDDLEWARE_CHAIN,
});
}
}
45 changes: 2 additions & 43 deletions examples/todo/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/context';
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;
await this.invokeMiddleware(context);
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 {}

0 comments on commit 35fcbe3

Please sign in to comment.