1 import { Constructor, SearchTypes } from "@medusajs/framework/types"
2 import { MedusaError } from "@medusajs/framework/utils"
3 import { SearchProviderRegistrationPrefix } from "@types"
4
5 type InjectedDependencies = {
6 [
7 key: `${typeof SearchProviderRegistrationPrefix}${string}`
8 ]: SearchTypes.ISearchProvider
9 }
10
11 /**
12 * Registry over the registered providers. Multiple providers can be used, and
13 * each index can select which provider to use for itself.
14 */
15 export class SearchProviderService {
16 protected readonly providers_: Map<string, SearchTypes.ISearchProvider> =
17 new Map()
18
19 constructor(container: InjectedDependencies) {
20 for (const key of Object.keys(container)) {
21 if (!key.startsWith(SearchProviderRegistrationPrefix)) {
22 continue
23 }
24
25 const provider = container[key]
26 if (!provider?.identifier) {
27 continue
28 }
29
30 // Definitions bind to a provider by identifier, so two registrations of
31 // the same provider class would be indistinguishable to them — the second
32 // would silently shadow the first.
33 if (this.providers_.has(provider.identifier)) {
34 throw new MedusaError(
35 MedusaError.Types.INVALID_DATA,
36 `The search provider "${provider.identifier}" is registered twice. Register each provider once; index definitions select it through their "provider" field.`
37 )
38 }
39
40 this.providers_.set(provider.identifier, provider)
41 }
42 }
43
44 static getRegistrationIdentifier(
45 providerClass: Constructor<SearchTypes.ISearchProvider>,
46 optionName?: string
47 ): string {
48 return `${(providerClass as any).identifier}_${optionName}`
49 }
50
51 retrieve(identifier: string): SearchTypes.ISearchProvider {
52 const provider = this.providers_.get(identifier)
53
54 if (!provider) {
55 throw new MedusaError(
56 MedusaError.Types.NOT_FOUND,
57 `Search provider "${identifier}" is not registered. Registered providers: ${
58 [...this.providers_.keys()].join(", ") || "(none)"
59 }`
60 )
61 }
62
63 return provider
64 }
65
66 list(): SearchTypes.ISearchProvider[] {
67 return [...this.providers_.values()]
68 }
69
70 /**
71 * Used by definitions that do not name a provider. Resolves to the sole
72 * registered provider, or to the module's `default_provider` option.
73 */
74 getDefaultIdentifier(configured?: string): string {
75 if (configured) {
76 // Surfaces a typo in `default_provider` at boot rather than on first query.
77 this.retrieve(configured)
78 return configured
79 }
80
81 const identifiers = [...this.providers_.keys()]
82
83 if (identifiers.length === 1) {
84 return identifiers[0]
85 }
86
87 if (!identifiers.length) {
88 throw new MedusaError(
89 MedusaError.Types.INVALID_DATA,
90 "The Search Module requires at least one provider to be registered"
91 )
92 }
93
94 throw new MedusaError(
95 MedusaError.Types.INVALID_DATA,
96 `Several search providers are registered (${identifiers.join(
97 ", "
98 )}), so "default_provider" must be set, or every index definition must name its provider`
99 )
100 }
101 }