Skip to content

Commit 24c24a3

Browse files
committed
docs: clarify interceptor argument examples
Signed-off-by: z1551778201-ctrl <Nurtai1120@outlook.com>
1 parent 7629098 commit 24c24a3

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

docs/site/Interceptor.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,10 @@ bindings. It extends `Context` with additional properties as follows:
530530
- `target` (`object`): Target class (for static methods) or prototype/object
531531
(for instance methods)
532532
- `methodName` (`string`): Method name
533-
- `args` (`InvocationArgs`, i.e., `any[]`): An array of arguments
533+
- `args` (`InvocationArgs`, i.e., `any[]`): An array of arguments in the
534+
same order as the target method parameters. For example, when intercepting
535+
`greet(name, age)`, `args[0]` is the value of `name` and `args[1]` is the
536+
value of `age`.
534537
- `source`: Source information about the invoker of the invocation
535538

536539
```ts
@@ -559,7 +562,9 @@ export class InvocationContext extends Context {
559562
```
560563

561564
It's possible for an interceptor to mutate items in the `args` array to pass in
562-
transformed input to downstream interceptors and the target method.
565+
transformed input to downstream interceptors and the target method. For example,
566+
an interceptor can update `args[0]` before calling `next()` so the target method
567+
receives the transformed first argument.
563568

564569
### Source for an invocation
565570

@@ -701,10 +706,12 @@ Here are some example interceptor functions:
701706
};
702707
```
703708

704-
4. An provider class for an interceptor that performs parameter validation
709+
4. A provider class for an interceptor that performs parameter validation
705710

706711
To leverage dependency injection, a provider class can be defined as the
707-
interceptor:
712+
interceptor. The example below validates the first argument of an intercepted
713+
method, such as `greetWithNameValidation(name)`. The first method argument is
714+
available as `invocationCtx.args[0]`.
708715

709716
```ts
710717
/**
@@ -722,17 +729,32 @@ Here are some example interceptor functions:
722729
invocationCtx: InvocationContext,
723730
next: () => ValueOrPromise<T>,
724731
) {
725-
const name = invocationCtx.args[0];
726-
if (!this.validNames.includes(name)) {
727-
throw new Error(
728-
`Name '${name}' is not on the list of '${this.validNames}`,
729-
);
730-
}
732+
const name = invocationCtx.args[0];
733+
if (!this.validNames.includes(name)) {
734+
throw new Error(
735+
`Name '${name}' is not on the list of '${this.validNames}'`,
736+
);
737+
}
731738
return next();
732739
}
733740
}
734741
```
735742

743+
The provider can be bound and then referenced by binding key from
744+
`@intercept`:
745+
746+
```ts
747+
ctx.bind('valid-names').to(['John', 'Mary']);
748+
ctx.bind('name-validator').toProvider(NameValidator);
749+
750+
class MyController {
751+
@intercept('name-validator')
752+
async greetWithNameValidation(name: string) {
753+
return `Hello, ${name}`;
754+
}
755+
}
756+
```
757+
736758
5. A synchronous interceptor to log method invocations:
737759

738760
```ts

0 commit comments

Comments
 (0)