(
cls: ServiceOrProviderClass<S>,
options: ServiceOptions = {},
)
| 160 | * @param options - Service options |
| 161 | */ |
| 162 | export 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 |
no test coverage detected