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:
Shahed Nasser
2023-09-21 20:57:15 +03:00
committed by GitHub
parent 19c5d5ba36
commit fa7c94b4cc
3209 changed files with 32188 additions and 31018 deletions

View File

@@ -0,0 +1,130 @@
---
description: 'Learn how to override the cart completion strategy to implement your custom cart completion strategy.'
addHowToData: true
---
# How to Override Cart Completion Strategy
In this document, youll learn how to override the cart completion strategy.
:::note
This guide only explains how to override the cart completion strategy. Its highly recommended to first understand how Medusa implements the cart completion strategy as explained [here](../cart.md#cart-completion-process).
:::
---
## Step 1: Create Strategy Class
Create a TypeScript or JavaScript file in `src/strategies` of your Medusa backend project with a class that extends the `AbstractCartCompletionStrategy` class:
```ts title=src/strategies/cart-completion.ts
import {
AbstractCartCompletionStrategy,
CartCompletionResponse,
IdempotencyKey } from "@medusajs/medusa"
import {
RequestContext,
} from "@medusajs/medusa/dist/types/request"
class CartCompletionStrategy
extends AbstractCartCompletionStrategy {
complete(
cartId: string,
idempotencyKey: IdempotencyKey,
context: RequestContext
): Promise<CartCompletionResponse> {
throw new Error("Method not implemented.")
}
}
export default CartCompletionStrategy
```
The class includes the `complete` method defined as abstract in `AbstractCartCompletionStrategy`. At the moment, the method only throws an error.
### Using a Constructor
You can use a constructor to access services and resources registered in the dependency container using dependency injection. For example:
<!-- eslint-disable prefer-rest-params -->
```ts title=src/strategies/cart-completion.ts
// ...
import { IdempotencyKeyService } from "@medusajs/medusa"
type InjectedDependencies = {
idempotencyKeyService: IdempotencyKeyService
}
class CartCompletionStrategy
extends AbstractCartCompletionStrategy {
protected readonly idempotencyKeyService_:
IdempotencyKeyService
constructor(
{ idempotencyKeyService }: InjectedDependencies
) {
super(arguments[0])
this.idempotencyKeyService_ = idempotencyKeyService
}
// ...
}
export default CartCompletionStrategy
```
In the above example, you inject the `IdempotencyKeyService` in the constructor. This allows you to use the `IdempotencyKeyService` within your class.
---
## Step 2: Implement the complete Method
The cart completion strategy is required to implement a single method: the `complete` method. This method is used in the [Complete Cart endpoint](https://docs.medusajs.com/api/store#carts_postcartscartcomplete) to handle the logic of completing the cart.
The method accepts three parameters:
- `cartId`: the first parameter of the method, which is a string indicating the ID of the cart to complete.
- `idempotencyKey`: the second parameter of the method, which is an instance of the `IdempotencyKey` entity. The idempotency key is retrieved based on the idempotency key passed in the header of the request, and its used to determine the current point reached in the checkout flow to avoid inconsistencies on interruptions. You can learn more about the idempotency key [here](../cart.md#idempotency-key). You can also learn how to use it within your strategy by following [this guide](../../../development/idempotency-key/use-service.md)
- `context`: the third parameter of the method, which is an object that holds a single property `ip`. `ip` is a string indicating the IP of the customer.
The completion strategy is expected to return an object with the following properties:
- `response_code`: a number indicating the response code.
- `response_body`: an object that will be returned to the client.
You can refer to this guide to learn how the cart conceptual guide is implemented in the Medusa backend. This can help you understand how details such as inventory, taxes, and more are handled.
---
## 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
```
Then, try out your strategy using the Complete Cart endpoint. You should see the logic you implemented used for completing the cart.
---
## See Also
- [How to implement cart functionalities in the storefront](../storefront/implement-cart.mdx)
- [How to implement checkout flow in the storefront](../storefront/implement-checkout-flow.mdx)