Skip to content

Commit

Permalink
docs: more feedback
Browse files Browse the repository at this point in the history
More feedback
  • Loading branch information
Yaapa Hage committed Mar 25, 2020
1 parent ed5a0ee commit df3c79d
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion docs/site/Req-res-cycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit df3c79d

Please sign in to comment.