(
ctx: Context,
interceptor: Interceptor | Constructor<Provider<Interceptor>>,
options: InterceptorBindingOptions = {},
)
| 379 | * @param options - Options for the interceptor binding |
| 380 | */ |
| 381 | export function registerInterceptor( |
| 382 | ctx: Context, |
| 383 | interceptor: Interceptor | Constructor<Provider<Interceptor>>, |
| 384 | options: InterceptorBindingOptions = {}, |
| 385 | ) { |
| 386 | let {global} = options; |
| 387 | const {group, source} = options; |
| 388 | if (group != null || source != null) { |
| 389 | // If group or source is set, assuming global |
| 390 | global = global !== false; |
| 391 | } |
| 392 | |
| 393 | const namespace = |
| 394 | (options.namespace ?? options.defaultNamespace ?? global) |
| 395 | ? GLOBAL_INTERCEPTOR_NAMESPACE |
| 396 | : LOCAL_INTERCEPTOR_NAMESPACE; |
| 397 | |
| 398 | let binding: Binding<Interceptor>; |
| 399 | if (isProviderClass(interceptor)) { |
| 400 | binding = createBindingFromClass(interceptor, { |
| 401 | defaultNamespace: namespace, |
| 402 | ...options, |
| 403 | }); |
| 404 | if (binding.tagMap[ContextTags.GLOBAL_INTERCEPTOR]) { |
| 405 | global = true; |
| 406 | } |
| 407 | ctx.add(binding); |
| 408 | } else { |
| 409 | let key = options.key; |
| 410 | if (!key) { |
| 411 | const name = options.name ?? interceptor.name; |
| 412 | if (!name) { |
| 413 | key = BindingKey.generate<Interceptor>(namespace).key; |
| 414 | } else { |
| 415 | key = `${namespace}.${name}`; |
| 416 | } |
| 417 | } |
| 418 | binding = ctx |
| 419 | .bind(key as BindingAddress<Interceptor>) |
| 420 | .to(interceptor as Interceptor); |
| 421 | } |
| 422 | if (global) { |
| 423 | binding.apply(asGlobalInterceptor(group)); |
| 424 | if (source) { |
| 425 | binding.tag({[ContextTags.GLOBAL_INTERCEPTOR_SOURCE]: source}); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return binding; |
| 430 | } |
no test coverage detected