-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rest): introduce RequestContext
Rework all places where we were accepting a pair of `(ParsedRequest, ServerResponse)` to use an context object instead. Two context types are introduced: `HandlerContext` is an interface describing objects required to handle an incoming HTTP request. There are two properties for starter: - context.request - context.response Low-level entities like Sequence Actions should be using this interface to allow easy interoperability with potentially any HTTP framework and more importantly, to keep the code easy to test. `RequestContext` is a class extending our IoC `Context` and implementing `HandlerContext` interface. This object is used when invoking the sequence handler. By combining both IoC and HTTP contexts into a single object, there will be (hopefully) less confusion among LB4 users about what shape a "context" object has in different places. This is a breaking change affecting the following users: - Custom sequence classes - Custom sequence handlers registered via `app.handler` - Custom implementations of the built-in sequence action `Reject`
- Loading branch information
Showing
20 changed files
with
179 additions
and
138 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: @loopback/rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Context} from '@loopback/context'; | ||
import {ServerResponse} from 'http'; | ||
import {HandlerContext, ParsedRequest} from './types'; | ||
import {RestBindings} from './keys'; | ||
|
||
/** | ||
* A per-request Context combining an IoC container with handler context | ||
* (request, response, etc.). | ||
*/ | ||
export class RequestContext extends Context implements HandlerContext { | ||
constructor( | ||
public readonly request: ParsedRequest, | ||
public readonly response: ServerResponse, | ||
parent: Context, | ||
name?: string, | ||
) { | ||
super(parent, name); | ||
this._setupBindings(request, response); | ||
} | ||
|
||
private _setupBindings(request: ParsedRequest, response: ServerResponse) { | ||
this.bind(RestBindings.Http.REQUEST) | ||
.to(request) | ||
.lock(); | ||
|
||
this.bind(RestBindings.Http.RESPONSE) | ||
.to(response) | ||
.lock(); | ||
|
||
this.bind(RestBindings.Http.CONTEXT) | ||
.to(this) | ||
.lock(); | ||
} | ||
} |
Oops, something went wrong.