@loopback/docs
Version:
Documentation files rendered at [https://loopback.io](https://loopback.io)
48 lines (33 loc) • 1.74 kB
Markdown
---
lang: en
title: 'API docs: context.genericinterceptor'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: https://github.com/strongloop/loopback-next/tree/master/packages/context
permalink: /doc/en/lb4/apidocs.context.genericinterceptor.html
---
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@loopback/context](./context.md) > [GenericInterceptor](./context.genericinterceptor.md)
## GenericInterceptor type
An interceptor function to be invoked in a chain for the given context. It serves as the base interface for various types of interceptors, such as method invocation interceptor or request/response processing interceptor.
<b>Signature:</b>
```typescript
export declare type GenericInterceptor<C extends Context = Context> = (context: C, next: Next) => ValueOrPromise<NonVoid>;
```
<b>References:</b> [Context](./context.context.md)<!-- -->, [Next](./context.next.md)<!-- -->, [ValueOrPromise](./context.valueorpromise.md)<!-- -->, [NonVoid](./context.nonvoid.md)
## Remarks
We choose `NonVoid` as the return type to avoid possible bugs that an interceptor forgets to return the value from `next()`<!-- -->. For example, the code below will fail to compile.
```ts
const myInterceptor: Interceptor = async (ctx, next) {
// preprocessing
// ...
// There is a subtle bug that the result from `next()` is not further
// returned back to the upstream interceptors
const result = await next();
// postprocessing
// ...
// We must have `return ...` here
// either return `result` or another value if the interceptor decides to
// have its own response
}
```