diff --git a/docs/site/Context.md b/docs/site/Context.md index e5ab093fdace..45783e4ebe8c 100644 --- a/docs/site/Context.md +++ b/docs/site/Context.md @@ -118,12 +118,12 @@ as an example, we can create custom sequences that: Let's see this in action: ```ts -import {DefaultSequence, RestBindings} from '@loopback/rest'; +import {DefaultSequence, RestBindings, RequestContext} from '@loopback/rest'; class MySequence extends DefaultSequence { - async handle(request: ParsedRequest, response: ServerResponse) { - // we provide these value for convenience (taken from the Context) - // but they are still available in the sequence/request context + async handle(context: RequestContext) { + // RequestContext provides request/response properties for convenience + // and performance, but they are still available in the context too const req = await this.ctx.get(RestBindings.Http.REQUEST); const res = await this.ctx.get(RestBindings.Http.RESPONSE); this.send(res, `hello ${req.query.name}`); diff --git a/docs/site/Creating-components.md b/docs/site/Creating-components.md index 71362e42e2c8..524ecf72b8fa 100644 --- a/docs/site/Creating-components.md +++ b/docs/site/Creating-components.md @@ -213,7 +213,6 @@ The idiomatic solution has two parts: ```ts class AppSequence implements SequenceHandler { constructor( - @inject(RestBindings.Http.CONTEXT) protected ctx: Context, @inject(RestBindings.SequenceActions.FIND_ROUTE) protected findRoute: FindRoute, @inject(RestBindings.SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams, @inject(RestBindings.SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod, @@ -223,18 +222,19 @@ The idiomatic solution has two parts: @inject('authentication.actions.authenticate') protected authenticate: AuthenticateFn ) {} - async handle(req: ParsedRequest, res: ServerResponse) { + async handle(context: RequestContext) { try { - const route = this.findRoute(req); + const {request, response} = context; + const route = this.findRoute(request); // Invoke the new action: - const user = await this.authenticate(req); + const user = await this.authenticate(request); - const args = await parseOperationArgs(req, route); + const args = await parseOperationArgs(request, route); const result = await this.invoke(route, args); - this.send(res, result); - } catch (err) { - this.reject(res, req, err); + this.send(response, result); + } catch (error) { + this.reject(context, err); } } } diff --git a/docs/site/Implementing-features.md b/docs/site/Implementing-features.md index 53845c838e4c..a2dbf8b79a97 100644 --- a/docs/site/Implementing-features.md +++ b/docs/site/Implementing-features.md @@ -639,14 +639,15 @@ export class MySequence implements SequenceHandler { @inject(RestSequenceActions.REJECT) protected reject: Reject, ) {} - async handle(req: ParsedRequest, res: ServerResponse) { + async handle(context: RequestContext) { try { - const route = this.findRoute(req); - const args = await this.parseParams(req, route); + const {request, response} = context; + const route = this.findRoute(request); + const args = await this.parseParams(requset, route); const result = await this.invoke(route, args); - this.send(res, result); + this.send(response, result); } catch (err) { - this.reject(res, req, err); + this.reject(context, err); } } } @@ -660,23 +661,24 @@ Now it's time to customize the default sequence to print a common log line. Edit the `handle` method as follows: ```ts -async handle(req: ParsedRequest, res: ServerResponse) { +async handle(context: RequestContext) { try { - const route = this.findRoute(req); - const args = await this.parseParams(req, route); + const {request, response} = context; + const route = this.findRoute(request); + const args = await this.parseParams(request, route); const result = await this.invoke(route, args); - this.send(res, result); + this.send(response, result); this.log([ - req.socket.remoteAddress, + request.socket.remoteAddress, '-', '-', `[${strftime('%d/%b/%Y:%H:%M:%S %z', new Date())}]`, - `"${req.method} ${req.path} HTTP/${req.httpVersion}"`, - res.statusCode, + `"${request.method} ${request.path} HTTP/${request.httpVersion}"`, + response.statusCode, '-', ].join(' ')); } catch (err) { - this.reject(res, req, err); + this.reject(context, err); } } ``` diff --git a/packages/authentication/README.md b/packages/authentication/README.md index dc675db0cd36..9caf89c1939f 100644 --- a/packages/authentication/README.md +++ b/packages/authentication/README.md @@ -132,18 +132,19 @@ export class MySequence implements SequenceHandler { protected authenticateRequest: AuthenticateFn, ) {} - async handle(req: ParsedRequest, res: ServerResponse) { + async handle(context: RequestContext) { try { - const route = this.findRoute(req); + const {request, response} = context; + const route = this.findRoute(request); // This is the important line added to the default sequence implementation - await this.authenticateRequest(req); + await this.authenticateRequest(request); - const args = await this.parseParams(req, route); + const args = await this.parseParams(request, route); const result = await this.invoke(route, args); - this.send(res, result); - } catch (err) { - this.reject(res, req, err); + this.send(response, result); + } catch (error) { + this.reject(context, error); } } } diff --git a/packages/cli/generators/extension/templates/src/providers/README.md b/packages/cli/generators/extension/templates/src/providers/README.md index 96a549cc935f..8576d12cefe4 100644 --- a/packages/cli/generators/extension/templates/src/providers/README.md +++ b/packages/cli/generators/extension/templates/src/providers/README.md @@ -76,7 +76,9 @@ class LogSequence implements SequenceHandler { @inject(ExtensionStarterBindings.LOG_ACTION) protected logger: LogFn, ) {} - async handle(req: ParsedRequest, res: ServerResponse) { + async handle(context: RequestContext) { + const {request, response} = context; + // We define these variable outside so they can be accessed by logger. let args: any = []; let result: any; @@ -86,17 +88,17 @@ class LogSequence implements SequenceHandler { const start = this.logger.startTimer(); try { - const route = this.findRoute(req); - args = await this.parseParams(req, route); + const route = this.findRoute(request); + args = await this.parseParams(request, route); result = await this.invoke(route, args); - this.send(res, result); - } catch (err) { - result = err; // so we can log the error message in the logger - this.reject(res, req, err); + this.send(response, result); + } catch (error) { + result = error; // so we can log the error message in the logger + this.reject(context, error); } // We call the logger function given to us by LogProvider - this.logger(req, args, result, start); + this.logger(request, args, result, start); } } ```