feat: Add an analytics module and local and posthog providers (#12505)
* feat: Add an analytics module and local and posthog providers * fix: Add tests and wire up in missing places * fix: Address feedback and add missing module typing * fix: Address feedback and add missing module typing --------- Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
co-authored by
Adrien de Peretti
Oli Juhl
parent
52bd9f9a53
commit
b9a51e217d
@@ -0,0 +1,45 @@
|
||||
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk"
|
||||
import {
|
||||
LoaderOptions,
|
||||
ModuleProvider,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/framework/types"
|
||||
import { asFunction, asValue, Lifetime } from "awilix"
|
||||
import ProviderService, {
|
||||
AnalyticsProviderIdentifierRegistrationName,
|
||||
AnalyticsProviderRegistrationPrefix,
|
||||
} from "../services/provider-service"
|
||||
|
||||
const registrationFn = async (klass, container, pluginOptions) => {
|
||||
const key = ProviderService.getRegistrationIdentifier(klass, pluginOptions.id)
|
||||
|
||||
container.register({
|
||||
[AnalyticsProviderRegistrationPrefix + key]: asFunction(
|
||||
(cradle) => new klass(cradle, pluginOptions.options ?? {}),
|
||||
{
|
||||
lifetime: klass.LIFE_TIME || Lifetime.SINGLETON,
|
||||
}
|
||||
),
|
||||
})
|
||||
|
||||
container.registerAdd(
|
||||
AnalyticsProviderIdentifierRegistrationName,
|
||||
asValue(key)
|
||||
)
|
||||
}
|
||||
|
||||
export default async ({
|
||||
container,
|
||||
options,
|
||||
}: LoaderOptions<
|
||||
(
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
) & { providers: ModuleProvider[] }
|
||||
>): Promise<void> => {
|
||||
await moduleProviderLoader({
|
||||
container,
|
||||
providers: options?.providers || [],
|
||||
registerServiceFn: registrationFn,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user