From ff5d5738873515ea08e136e8eee994975912d6dc Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 22 May 2024 15:34:53 +0530 Subject: [PATCH] refactor: register plugin routes before the Medusa core routes (#7395) --- packages/medusa/src/loaders/api.ts | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/medusa/src/loaders/api.ts b/packages/medusa/src/loaders/api.ts index 689ac95d7d..21d3bcda16 100644 --- a/packages/medusa/src/loaders/api.ts +++ b/packages/medusa/src/loaders/api.ts @@ -28,6 +28,28 @@ export default async ({ app, container, plugins }: Options) => { ContainerRegistrationKeys.CONFIG_MODULE ) + /** + * Always load plugin routes before the Medusa core routes, since it + * will allow the plugin to define routes with higher priority + * than Medusa. Here are couple of examples. + * + * - Plugin registers a route called "/products/active" + * - Medusa registers a route called "/products/:id" + * + * Now, if Medusa routes gets registered first, then the "/products/active" + * route will never be resolved, because it will be handled by the + * "/products/:id" route. + */ + await Promise.all( + plugins.map(async (pluginDetails) => { + return new RoutesLoader({ + app: app, + configModule, + rootDir: path.join(pluginDetails.resolve, "api"), + }).load() + }) + ) + // TODO: Figure out why this is causing issues with test when placed inside ./api.ts // Adding this here temporarily // Test: (packages/medusa/src/api/routes/admin/currencies/update-currency.ts) @@ -47,15 +69,5 @@ export default async ({ app, container, plugins }: Options) => { ) } - await Promise.all( - plugins.map(async (pluginDetails) => { - return new RoutesLoader({ - app: app, - configModule, - rootDir: path.join(pluginDetails.resolve, "api"), - }).load() - }) - ) - return app }