From df3c79d489a5a338682c0978df6029c71adaeae9 Mon Sep 17 00:00:00 2001 From: Yaapa Hage Date: Wed, 25 Mar 2020 12:46:18 +0530 Subject: [PATCH] docs: more feedback More feedback --- docs/site/Req-res-cycle.md | 48 +++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/docs/site/Req-res-cycle.md b/docs/site/Req-res-cycle.md index e794ededfecc..730e3671d849 100644 --- a/docs/site/Req-res-cycle.md +++ b/docs/site/Req-res-cycle.md @@ -74,7 +74,7 @@ they are capable of adding new endpoints and determining the result of a request ### The request-response cycle The request-response cycle involves many components, especially if the request -is to a controller-endpoint which interacts with a model. +is to a controller-endpoint which interacts with the database. ![Components of LoopBack 4 request-response cycle](./imgs/req-res-high-level.png) @@ -111,6 +111,10 @@ route for invocation. parses LoopBack-relevant request paremeters from the request body, URL segment, and query parameters. +It is also responsible for validating the property types of the request body, +as defined in the model. If the request body does not match the schema, it +throws a HTTP 422 error. + ##### 3. InvokeMethod [InvokeMethod](https://loopback.io/doc/en/lb4/apidocs.rest.invokemethodprovider.html) @@ -138,6 +142,44 @@ responsible for sending error back to the client in case any of the above helper methods throw or encounter any errors; this includes `4xx`, `5xx`, and any other type of errors. +The implementation of the `handle()` method of the default sequence looks like +this: + +```ts +async handle(context: RequestContext) { + try { + 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(response, result); + } catch (err) { + this.reject(context, err); + } +} +``` + +Any provider can be plugged in in the sequence for additional functionality in +the app. For example, the [authentication provider](https://loopback.io/doc/en/lb4/apidocs.authentication.authenticateactionprovider.html) +may be plugged in right after the `FindRoute` phase to enforce authentication +on all requests to the app. + +```ts +export class MySequence implements SequenceHandler { + constructor( + ... + @inject(AuthenticationBindings.AUTH_ACTION) + protected authenticateRequest: AuthenticateFn, + ) {} + + async handle(context: RequestContext) { + try { + const {request, response} = context; + const route = this.findRoute(request); + await this.authenticateRequest(request); +... +``` + #### Request to a controller endpoint Requests to a controller endpoint are handled in a very different context than @@ -198,6 +240,10 @@ All controllers, services, and repositories; and their methods can be intercepte This makes interceptors a powerful participant in the request-response cycle, since they can modify the request and response objects and call their methods. +{% include tip.html content="The [authorization component](https://loopback.io/doc/en/lb4/apidocs.authorization.html) +is an example of the use of interceptors to add additional functionality to the +app." %} + ##### Walk through of a request Now that we know all the components that may be involve in a request to a