|
1
|
import { asFunction, asValue, Lifetime } from "@medusajs/framework/awilix" |
|
2
|
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk" |
|
3
|
import { LoaderOptions } from "@medusajs/framework/types" |
|
4
|
import { SearchProviderService } from "@services" |
|
5
|
import { |
|
6
|
SearchModuleOptions, |
|
7
|
SearchProviderIdentifiersRegistrationName, |
|
8
|
SearchProviderRegistrationPrefix, |
|
9
|
} from "@types" |
|
10
|
import { hasMedusaSearchCredentials, MedusaSearchService } from "../providers" |
|
11
|
|
|
12
|
const registrationFn = async (klass, container, pluginOptions) => { |
|
13
|
const key = SearchProviderService.getRegistrationIdentifier( |
|
14
|
klass, |
|
15
|
pluginOptions.id |
|
16
|
) |
|
17
|
|
|
18
|
container.register({ |
|
19
|
[SearchProviderRegistrationPrefix + key]: asFunction( |
|
20
|
(cradle) => new klass(cradle, pluginOptions.options ?? {}), |
|
21
|
{ |
|
22
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
23
|
} |
|
24
|
), |
|
25
|
}) |
|
26
|
|
|
27
|
container.registerAdd(SearchProviderIdentifiersRegistrationName, asValue(key)) |
|
28
|
} |
|
29
|
|
|
30
|
export default async ({ |
|
31
|
container, |
|
32
|
options, |
|
33
|
}: LoaderOptions<SearchModuleOptions>): Promise<void> => { |
|
34
|
const { api_key, endpoint, environment_handle, fetch } = options?.cloud ?? {} |
|
35
|
|
|
36
|
// Register Medusa Cloud search when cloud options are present, same pattern |
|
37
|
// as payment's Medusa Payments and notification's cloud email. The key and |
|
38
|
// the environment handle may travel on the endpoint as basic auth instead. |
|
39
|
if (hasMedusaSearchCredentials({ api_key, endpoint, environment_handle })) { |
|
40
|
await registrationFn(MedusaSearchService, container, { |
|
41
|
options: { |
|
42
|
api_key, |
|
43
|
endpoint, |
|
44
|
environment_handle, |
|
45
|
fetch, |
|
46
|
}, |
|
47
|
id: "default", |
|
48
|
}) |
|
49
|
} |
|
50
|
|
|
51
|
await moduleProviderLoader({ |
|
52
|
container, |
|
53
|
providers: options?.providers || [], |
|
54
|
registerServiceFn: registrationFn, |
|
55
|
}) |
|
56
|
} |