loopbackio / loopbackio/loopback-next

docs: Improve examples of Interceptors with more implementation details

Offen
#4,450 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

developer-experience Docs good first issue IoC/Context
Vorherrschende Sprache
TypeScript
Sterne
5.1k
Forks
1.1k
Ø Merge
2 T. 21 Std.
Gemergte PRs (30 T.)
27

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Interceptors-Dokumentation im verknüpften Beispiel und prüfen Sie die verknüpften Eigenschaften der InvocationContext API, insbesondere args. Klären Sie, woran der Interceptor angehängt ist, wie die Argumente der abgefangenen Methode auf args[0] abgebildet werden, und fügen Sie die angeforderten Implementierungsdetails hinzu; die Akzeptanzkriterien sollten erfüllt sein, wenn das Beispiel ohne diese offenen Fragen verständlich ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.