docs: create docs workspace (#5174)
* docs: migrate ui docs to docs universe * created yarn workspace * added eslint and tsconfig configurations * fix eslint configurations * fixed eslint configurations * shared tailwind configurations * added shared ui package * added more shared components * migrating more components * made details components shared * move InlineCode component * moved InputText * moved Loading component * Moved Modal component * moved Select components * Moved Tooltip component * moved Search components * moved ColorMode provider * Moved Notification components and providers * used icons package * use UI colors in api-reference * moved Navbar component * used Navbar and Search in UI docs * added Feedback to UI docs * general enhancements * fix color mode * added copy colors file from ui-preset * added features and enhancements to UI docs * move Sidebar component and provider * general fixes and preparations for deployment * update docusaurus version * adjusted versions * fix output directory * remove rootDirectory property * fix yarn.lock * moved code component * added vale for all docs MD and MDX * fix tests * fix vale error * fix deployment errors * change ignore commands * add output directory * fix docs test * general fixes * content fixes * fix announcement script * added changeset * fix vale checks * added nofilter option * fix vale error
This commit is contained in:
1 parent
19c5d5ba36
commit
fa7c94b4cc
3209 files changed
+32188
-31018
No files matched your search
@@ -0,0 +1,81 @@
|
||||
---
|
||||
description: "Learn how to override a strategy in a Medusa backend or plugin."
|
||||
addHowToData: true
|
||||
---
|
||||
|
||||
# How to Override a Strategy
|
||||
|
||||
In this document, you’ll learn how to override a strategy in a Medusa backend or plugin.
|
||||
|
||||
## Overview
|
||||
|
||||
The Medusa core defines and uses strategies for certain functionalities, which allows developers to override these functionalities and implement them as fits for their use case.
|
||||
|
||||
For example, the cart completion process is implemented within a `CartCompletionStrategy` that is used inside the Complete Cart endpoint. If you need to change the cart completion process, you can override the `CartCompletionStrategy` and implement your own strategy. The Medusa backend will then use your strategy instead of the one defined in the core.
|
||||
|
||||
### Hierarchy of Strategy Resolution
|
||||
|
||||
When the Medusa backend starts, the strategies defined in the core are registered in the dependency container first.
|
||||
|
||||
If a strategy is overridden in a plugin, it will be registered in the dependency container in place of the original strategy.
|
||||
|
||||
If a strategy is overridden in the Medusa backend codebase, it will be registered in the dependency container in place of the original strategy.
|
||||
|
||||
So, if a strategy is overridden within a plugin and in the Medusa backend codebase, the one in the codebase is registered and used instead of the one in the plugin.
|
||||
|
||||
:::tip
|
||||
|
||||
You can learn more about the dependency container and injection [here](../fundamentals/dependency-injection.md).
|
||||
|
||||
:::
|
||||
|
||||
### Existing Resources
|
||||
|
||||
This guide explains how to override a strategy in general. It doesn’t explain how to override specific strategies.
|
||||
|
||||
There are other resources that provide steps specific to a strategy type:
|
||||
|
||||
- [How to override the Cart Completion Strategy](../../modules/carts-and-checkout/backend/cart-completion-strategy.md)
|
||||
- [How to override the Tax Calculation Strategy](../../modules/taxes/backend/tax-calculation-strategy.md)
|
||||
- [How to override the Price Selection Strategy](../../modules/price-lists/backend/override-price-selection-strategy.md)
|
||||
- [How to override a Batch Job Strategy](../batch-jobs/customize-import.md)
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Create Strategy Class
|
||||
|
||||
A strategy class is created in a TypeScript or JavaScript file in the `src/strategies` directory of your Medusa backend or plugin.
|
||||
|
||||
The class must extend or implement a strategy interface or class from within the core, depending on what strategy you’re overriding. For example, if you’re overriding the `CartCompletionStrategy`, you must extend the `AbstractCartCompletionStrategy`.
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Implement Functionalities
|
||||
|
||||
Each strategy class is required to implement certain methods defined by the interface it implements or the abstract class it extends. Depending on what the strategy is used for, you must implement the functionalities in each of the required methods.
|
||||
|
||||
For example, if you’re overriding the `CartCompletionStrategy`, you must implement the `complete` method using the signature defined in `AbstractCartCompletionStrategy`.
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Run Build Command
|
||||
|
||||
In the directory of the Medusa backend, run the `build` command to transpile the files in the `src` directory into the `dist` directory:
|
||||
|
||||
```bash npm2yarn
|
||||
npm run build
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Test it Out
|
||||
|
||||
Run your backend to test it out:
|
||||
|
||||
```bash npm2yarn
|
||||
npx medusa develop
|
||||
```
|
||||
|
||||
You can test now whether your strategy is working by performing the actions that run your strategy.
|
||||
|
||||
For example, if you’re overriding the `CartCompletionStrategy`, you can simulate the cart completion flow and see if your strategy is being used.
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
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, you’ll learn what a Strategy is in Medusa.
|
||||
|
||||
## Introduction
|
||||
|
||||
A strategy is an isolated piece of business logic that can be overridden and customized. It’s a TypeScript or JavaScript class that doesn’t 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 endpoints, services, or wherever needed using dependency injection and used to perform their designated functionality.
|
||||
|
||||
For example, the `CartCompletionStrategy` is resolved in the Complete Cart endpoint that is defined in the core `@medusajs/medusa` package. It’s 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.',
|
||||
}
|
||||
}} />
|
||||
Reference in new issue
Block a user