Skip to content

Commit

Permalink
feat: update sequence.ts to invoke middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jun 23, 2020
1 parent a622ff6 commit e2ff6b2
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 25 deletions.
23 changes: 17 additions & 6 deletions examples/access-control-migration/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@
// 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 {
AuthenticationBindings,
AuthenticateFn,
AUTHENTICATION_STRATEGY_NOT_FOUND,
USER_PROFILE_NOT_FOUND,
} from '@loopback/authentication';

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,
Expand All @@ -38,6 +46,9 @@ export class MySequence implements SequenceHandler {
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
Expand Down
10 changes: 10 additions & 0 deletions examples/express-composition/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,6 +19,13 @@ import {
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,
Expand All @@ -29,6 +37,8 @@ export class MySequence implements SequenceHandler {
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);
Expand Down
10 changes: 10 additions & 0 deletions examples/file-transfer/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,6 +19,13 @@ import {
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,
Expand All @@ -29,6 +37,8 @@ export class MySequence implements SequenceHandler {
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);
Expand Down
17 changes: 13 additions & 4 deletions examples/lb3-application/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context, inject} from '@loopback/core';
import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,8 +19,14 @@ import {
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,
Expand All @@ -30,12 +37,14 @@ export class MySequence implements SequenceHandler {
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 (error) {
this.reject(context, error);
} catch (err) {
this.reject(context, err);
}
}
}
11 changes: 11 additions & 0 deletions examples/multi-tenancy/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -19,6 +20,13 @@ import {MultiTenancyAction, MultiTenancyBindings} from './multi-tenancy';
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,
Expand All @@ -32,6 +40,9 @@ export class MySequence implements SequenceHandler {
async handle(context: RequestContext) {
try {
const {request, response} = context;
const finished = await this.invokeMiddleware(context);
if (finished) return;

await this.multiTenancy(context);
const route = this.findRoute(request);
const args = await this.parseParams(request, route);
Expand Down
11 changes: 11 additions & 0 deletions examples/passport-login/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -24,6 +25,13 @@ import {
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)
Expand All @@ -38,6 +46,9 @@ export class MySequence implements SequenceHandler {
async handle(context: RequestContext) {
try {
const {request, response} = context;
const finished = await this.invokeMiddleware(context);
if (finished) return;

const route = this.findRoute(request);

// usually authentication is done before proceeding to parse params
Expand Down
17 changes: 13 additions & 4 deletions examples/rest-crud/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context, inject} from '@loopback/core';
import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,8 +19,14 @@ import {
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,
Expand All @@ -30,12 +37,14 @@ export class MySequence implements SequenceHandler {
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 (error) {
this.reject(context, error);
} catch (err) {
this.reject(context, err);
}
}
}
10 changes: 10 additions & 0 deletions examples/soap-calculator/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,6 +19,13 @@ import {
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,
Expand All @@ -29,6 +37,8 @@ export class MySequence implements SequenceHandler {
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);
Expand Down
23 changes: 17 additions & 6 deletions examples/todo-jwt/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@
// 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 {
AuthenticateFn,
AuthenticationBindings,
AUTHENTICATION_STRATEGY_NOT_FOUND,
USER_PROFILE_NOT_FOUND,
} from '@loopback/authentication';

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,
Expand All @@ -38,6 +46,9 @@ export class MySequence implements SequenceHandler {
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);
Expand Down
17 changes: 13 additions & 4 deletions examples/todo-list/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context, inject} from '@loopback/core';
import {inject} from '@loopback/core';
import {
FindRoute,
InvokeMethod,
InvokeMiddleware,
ParseParams,
Reject,
RequestContext,
Expand All @@ -18,8 +19,14 @@ import {
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,
Expand All @@ -30,12 +37,14 @@ export class MySequence implements SequenceHandler {
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 (error) {
this.reject(context, error);
} catch (err) {
this.reject(context, err);
}
}
}
Loading

0 comments on commit e2ff6b2

Please sign in to comment.