-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(medusa): Middleware to add default SC on query if no SC already …
…exist on it (#3694)
- Loading branch information
1 parent
d97a6f3
commit 3a77e8a
Showing
7 changed files
with
107 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@medusajs/medusa": patch | ||
--- | ||
|
||
feat(medusa): Middleware to add default SC on store products query if no SC provided |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/medusa/src/api/middlewares/with-default-sales-channel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NextFunction, Request, Response } from "express" | ||
import SalesChannelFeatureFlag from "../../loaders/feature-flags/sales-channels" | ||
import { SalesChannelService } from "../../services" | ||
import { FlagRouter } from "../../utils/flag-router" | ||
|
||
/** | ||
* Middleware that includes the default sales channel on the request, if no sales channels present | ||
* @param context Object of options | ||
* @param context.attachChannelAsArray Whether to attach the default sales channel as an array or just a string | ||
*/ | ||
export function withDefaultSalesChannel({ | ||
attachChannelAsArray, | ||
}: { | ||
attachChannelAsArray?: boolean | ||
}): (req: Request, res: Response, next: NextFunction) => Promise<void> { | ||
return async (req: Request, _, next: NextFunction) => { | ||
const featureFlagRouter = req.scope.resolve( | ||
"featureFlagRouter" | ||
) as FlagRouter | ||
|
||
if ( | ||
!featureFlagRouter.isFeatureEnabled(SalesChannelFeatureFlag.key) || | ||
req.query.sales_channel_id?.length || | ||
req.get("x-publishable-api-key") | ||
) { | ||
return next() | ||
} | ||
|
||
const salesChannelService: SalesChannelService = req.scope.resolve( | ||
"salesChannelService" | ||
) | ||
|
||
try { | ||
const defaultSalesChannel = await salesChannelService.retrieveDefault() | ||
if (defaultSalesChannel?.id) { | ||
req.query.sales_channel_id = attachChannelAsArray | ||
? [defaultSalesChannel.id] | ||
: defaultSalesChannel.id | ||
} | ||
} catch { | ||
} finally { | ||
next() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters