MCPcopy
hub / github.com/loopbackio/loopback-next / createServiceBinding

Function createServiceBinding

packages/core/src/service.ts:162–198  ·  view source on GitHub ↗
(
  cls: ServiceOrProviderClass<S>,
  options: ServiceOptions = {},
)

Source from the content-addressed store, hash-verified

160 * @param options - Service options
161 */
162export function createServiceBinding<S>(
163 cls: ServiceOrProviderClass<S>,
164 options: ServiceOptions = {},
165): Binding<S> {
166 let name = options.name;
167 if (!name && isProviderClass(cls)) {
168 // Trim `Provider` from the default service name
169 // This is needed to keep backward compatibility
170 const templateFn = bindingTemplateFor(cls);
171 const template = Binding.bind<S>('template').apply(templateFn);
172 if (
173 template.tagMap[ContextTags.PROVIDER] &&
174 !template.tagMap[ContextTags.NAME]
175 ) {
176 // The class is a provider and no `name` tag is found
177 name = cls.name.replace(/Provider$/, '');
178 }
179 }
180 if (!name && isDynamicValueProviderClass(cls)) {
181 // Trim `Provider` from the default service name
182 const templateFn = bindingTemplateFor(cls);
183 const template = Binding.bind<S>('template').apply(templateFn);
184 if (
185 template.tagMap[ContextTags.DYNAMIC_VALUE_PROVIDER] &&
186 !template.tagMap[ContextTags.NAME]
187 ) {
188 // The class is a provider and no `name` tag is found
189 name = cls.name.replace(/Provider$/, '');
190 }
191 }
192 const binding = createBindingFromClass(cls, {
193 name,
194 type: CoreTags.SERVICE,
195 ...options,
196 }).apply(asService(options.interface ?? cls));
197 return binding;
198}
199
200/**
201 * Create a binding template for a service interface

Callers 2

serviceMethod · 0.90
service.unit.tsFile · 0.85

Calls 7

isProviderClassFunction · 0.90
bindingTemplateForFunction · 0.90
createBindingFromClassFunction · 0.90
asServiceFunction · 0.85
applyMethod · 0.80
bindMethod · 0.45

Tested by

no test coverage detected