Files
medusa-store/www/apps/docs/content/development/strategies/overview.mdx
Shahed Nasser c28935b4e8 docs: update endpoints to use file-routing approach (#5397)
- Move the original guides for creating endpoints and middlewares to sub-sections in the Endpoints category.
- Replace existing guides for endpoints and middlewares with the new approach.
- Update all endpoints-related snippets across docs to use this new approach.
2023-10-19 15:56:26 +00:00

58 lines
2.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
description: "Learn what a Strategy is in Medusa. A strategy is an isolated piece of business logic that can be overridden and customized."
---
import DocCard from '@theme/DocCard';
import Icons from '@theme/Icon';
# Strategy
In this document, youll learn what a Strategy is in Medusa.
## Introduction
A strategy is an isolated piece of business logic that can be overridden and customized. Its a TypeScript or JavaScript class that doesnt have to follow a specific definition, but performs only a single functionality.
For example, in the core `@medusajs/medusa` package, strategies are used to implement functionalities like cart completion and product import.
These strategy classes are then resolved in API Routes, services, or wherever needed using dependency injection and used to perform their designated functionality.
For example, the `CartCompletionStrategy` is resolved in the Complete Cart API Route that is defined in the core `@medusajs/medusa` package. Its then used to complete the cart and place the order:
```ts
export default async (req, res) => {
// ...
const completionStrat: AbstractCartCompletionStrategy =
req.scope.resolve(
"cartCompletionStrategy"
)
const {
response_code,
response_body,
} = await completionStrat.complete(
id,
idempotencyKey,
req.request_context
)
res.status(response_code).json(response_body)
}
```
When a strategy is overridden, dependency injection then resolves the strategy (in the code example above, `CartCompletionStrategy`) to the custom strategy that the developer created. Then, the method (in the code example above, `complete`) of the custom strategy will be executed instead of the one defined in the core package.
## Custom Development
Developers can override strategies defined in the core to customize their functionality within their Medusa backend codebase or within a plugin.
<DocCard item={{
type: 'link',
href: '/development/strategies/override-strategy',
label: 'Backend: Override a Strategy',
customProps: {
icon: Icons['academic-cap-solid'],
description: 'Learn how to override a strategy.',
}
}} />