|
1
|
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk" |
|
2
|
import { |
|
3
|
LoaderOptions, |
|
4
|
ModuleProvider, |
|
5
|
ModulesSdkTypes, |
|
6
|
} from "@medusajs/framework/types" |
|
7
|
import { FileProviderService } from "@services" |
|
8
|
import { |
|
9
|
FileProviderIdentifierRegistrationName, |
|
10
|
FileProviderRegistrationPrefix, |
|
11
|
} from "@types" |
|
12
|
import { asFunction, asValue, Lifetime } from "@medusajs/framework/awilix" |
|
13
|
|
|
14
|
const registrationFn = async (klass, container, pluginOptions) => { |
|
15
|
const key = FileProviderService.getRegistrationIdentifier( |
|
16
|
klass, |
|
17
|
pluginOptions.id |
|
18
|
) |
|
19
|
|
|
20
|
container.register({ |
|
21
|
[FileProviderRegistrationPrefix + key]: asFunction( |
|
22
|
(cradle) => new klass(cradle, pluginOptions.options ?? {}), |
|
23
|
{ |
|
24
|
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON, |
|
25
|
} |
|
26
|
), |
|
27
|
}) |
|
28
|
|
|
29
|
container.registerAdd(FileProviderIdentifierRegistrationName, asValue(key)) |
|
30
|
} |
|
31
|
|
|
32
|
export default async ({ |
|
33
|
container, |
|
34
|
options, |
|
35
|
}: LoaderOptions< |
|
36
|
( |
|
37
|
| ModulesSdkTypes.ModuleServiceInitializeOptions |
|
38
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions |
|
39
|
) & { providers: ModuleProvider[] } |
|
40
|
>): Promise<void> => { |
|
41
|
await moduleProviderLoader({ |
|
42
|
container, |
|
43
|
providers: options?.providers || [], |
|
44
|
registerServiceFn: registrationFn, |
|
45
|
}) |
|
46
|
} |