Skip to content

docs: Improve examples of Interceptors with more implementation details #4450

@emonddr

Description

@emonddr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsIoC/Context@loopback/context: Dependency Injection, Inversion of Controldeveloper-experienceIssues affecting ease of use and overall experience of LB usersgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions