MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / registerInterceptor

Function registerInterceptor

packages/context/src/interceptor.ts:381–430  ·  view source on GitHub ↗
(
  ctx: Context,
  interceptor: Interceptor | Constructor<Provider<Interceptor>>,
  options: InterceptorBindingOptions = {},
)

Source from the content-addressed store, hash-verified

379 * @param options - Options for the interceptor binding
380 */
381export 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}

Callers 2

interceptorMethod · 0.90

Calls 9

isProviderClassFunction · 0.90
createBindingFromClassFunction · 0.90
asGlobalInterceptorFunction · 0.85
generateMethod · 0.80
toMethod · 0.80
applyMethod · 0.80
tagMethod · 0.80
addMethod · 0.65
bindMethod · 0.45

Tested by

no test coverage detected