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

Nobody has claimed this yet.

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:

/**
 * A binding provider class to produce an interceptor that validates the
 * `name` argument
 */
class NameValidator implements Provider<Interceptor> {
  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<T>(
    invocationCtx: InvocationContext,
    next: () => ValueOrPromise<T>,
  ) {
    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 for more tips on writing good issues

Acceptane Criteria

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

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.