diff --git a/.changeset/neat-seals-help.md b/.changeset/neat-seals-help.md new file mode 100644 index 0000000000..f3b0c6d196 --- /dev/null +++ b/.changeset/neat-seals-help.md @@ -0,0 +1,5 @@ +--- +"@medusajs/modules-sdk": patch +--- + +feat(modules-sdk): run hooks on application start diff --git a/packages/modules-sdk/src/medusa-app.ts b/packages/modules-sdk/src/medusa-app.ts index 9ba593ab93..062a38a5ed 100644 --- a/packages/modules-sdk/src/medusa-app.ts +++ b/packages/modules-sdk/src/medusa-app.ts @@ -308,12 +308,16 @@ export async function MedusaApp( })) } - return { - modules: allModules, - link: remoteLink, - query, - entitiesMap: schema.getTypeMap(), - notFound, - runMigrations, + try { + return { + modules: allModules, + link: remoteLink, + query, + entitiesMap: schema.getTypeMap(), + notFound, + runMigrations, + } + } finally { + await MedusaModule.onApplicationStart() } } diff --git a/packages/modules-sdk/src/medusa-module.ts b/packages/modules-sdk/src/medusa-module.ts index 19c036071e..6144d93ff8 100644 --- a/packages/modules-sdk/src/medusa-module.ts +++ b/packages/modules-sdk/src/medusa-module.ts @@ -1,5 +1,6 @@ import { ExternalModuleDeclaration, + IModuleService, InternalModuleDeclaration, LinkModuleDefinition, LoadedModule, @@ -85,6 +86,21 @@ export class MedusaModule { return MedusaModule.getModuleInstance(key) }) } + + public static onApplicationStart(): void { + for (const instances of MedusaModule.instances_.values()) { + for (const instance of Object.values(instances) as IModuleService[]) { + if (instance?.__hooks) { + instance.__hooks?.onApplicationStart + ?.bind(instance)() + .catch(() => { + // The module should handle this and log it + return void 0 + }) + } + } + } + } public static clearInstances(): void { MedusaModule.instances_.clear()