| 218 | * class-based interceptors and runs the request through it. |
| 219 | */ |
| 220 | export function legacyInterceptorFnFactory(): HttpInterceptorFn { |
| 221 | let chain: ChainedInterceptorFn<any> | null = null; |
| 222 | |
| 223 | return (req, handler) => { |
| 224 | if (chain === null) { |
| 225 | const interceptors = inject(HTTP_INTERCEPTORS, {optional: true}) ?? []; |
| 226 | // Note: interceptors are wrapped right-to-left so that final execution order is |
| 227 | // left-to-right. That is, if `interceptors` is the array `[a, b, c]`, we want to |
| 228 | // produce a chain that is conceptually `c(b(a(end)))`, which we build from the inside |
| 229 | // out. |
| 230 | chain = interceptors.reduceRight( |
| 231 | adaptLegacyInterceptorToChain, |
| 232 | interceptorChainEndFn as ChainedInterceptorFn<any>, |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | return chain(req, handler); |
| 237 | }; |
| 238 | } |