loopbackio / loopbackio/loopback-next

docs: Improve examples of Interceptors with more implementation details

Open
#4,450 4 comments 1 reaction 0 assignees View on GitHub
developer-experience Docs good first issue IoC/Context
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

In https://loopback.io/doc/en/lb4/Interceptors.html#example-interceptors
there are different examples of interceptors.

To understand the example implementations of these interceptors, let's
also show an example implementation of `what` they are intercepting.

For example:

```ts
/**
* A binding provider class to produce an interceptor that validates the
* `name` argument
*/
class NameValidator implements Provider {
constructor(@inject('valid-names') private validNames: string[]) {}

value() {
return this.intercept.bind(this);
}

// (doc improvement)
// This interceptor works as an authorizer for certain endpoints,
// which takes in the first argument as the name.
async intercept(
invocationCtx: InvocationContext,
next: () => ValueOrPromise,
) {
const name = invocationCtx.args[0];
if (!this.validNames.includes(name)) {
throw new Error(
`Name '${name}' is not on the list of '${this.validNames}`,
);
}
return next();
}
}
```
It is not clear what this interceptor is attached to. Is it a function, a class, a request?

How do we know that the `name` we need to validate is in args[0] of the invocation context?

Providing more implementation details would shed some light on how to use them.

_See [Reporting Issues](http://loopback.io/doc/en/contrib/Reporting-issues.html) for more tips on writing good issues_

## Acceptane Criteria

- [ ] Refine the docs to include more details described above.
- [ ] For the example snippet, it implies we need to add more details to `InvocationContext`'s properties, like `args`, see https://loopback.io/doc/en/lb4/apidocs.context.invocationcontext.html#properties

Contributor guide

Open the contributing guide

Research direction

Start with the Interceptors documentation at the linked example and review the linked InvocationContext API properties, especially args. Clarify what the interceptor is attached to, how the intercepted method's arguments map to args[0], and add the requested implementation details; the acceptance criteria should be satisfied when the example is understandable without these unanswered questions.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.