docs: added middleware documentation (#3000)

* docs: added a middleware documentation

* docs: fixes based on eslint
This commit is contained in:
Shahed Nasser
2023-01-11 20:15:17 +02:00
committed by GitHub
parent 76d6d26b3a
commit dd87e62536
5 changed files with 112 additions and 26 deletions
@@ -12,46 +12,51 @@ If youre interested in learning what a price selection strategy is and how it
Create a TypeScript or JavaScript file in `src/strategies` of your Medusa server project with a class that extends the `AbstractPriceSelectionStrategy` class:
```typescript title=src/strategies/price.ts
import { AbstractPriceSelectionStrategy, IPriceSelectionStrategy, PriceSelectionContext, PriceSelectionResult } from "@medusajs/medusa";
```ts title=src/strategies/price.ts
import {
AbstractPriceSelectionStrategy,
IPriceSelectionStrategy,
PriceSelectionContext,
PriceSelectionResult,
} from "@medusajs/medusa"
import { EntityManager } from "typeorm";
import { EntityManager } from "typeorm"
export default class MyPriceListStrategy extends AbstractPriceSelectionStrategy {
export default class MyStrategy extends AbstractPriceSelectionStrategy {
withTransaction(manager: EntityManager): IPriceSelectionStrategy {
if (!manager) {
return this
}
return new MyPriceListStrategy()
return new MyStrategy()
}
async calculateVariantPrice(
variant_id: string,
context: PriceSelectionContext
): Promise<PriceSelectionResult> {
//TODO
// TODO
}
}
```
You can use services or repositories in the strategy by adding them to the constructor and updating the parameters passed to the `MyPriceListStrategy` constructor in `withTransaction`. For example:
You can use services or repositories in the strategy by adding them to the constructor and updating the parameters passed to the `MyStrategy` constructor in `withTransaction`. For example:
```typescript
```ts
import {
AbstractPriceSelectionStrategy,
CustomerService,
IPriceSelectionStrategy,
PriceSelectionContext,
PriceSelectionResult
} from "@medusajs/medusa";
PriceSelectionResult,
} from "@medusajs/medusa"
export default class MyPriceListStrategy extends AbstractPriceSelectionStrategy {
export default class MyStrategy extends AbstractPriceSelectionStrategy {
private customerService: CustomerService
constructor({
customerService
customerService,
}) {
super()
this.customerService = customerService
@@ -62,11 +67,11 @@ export default class MyPriceListStrategy extends AbstractPriceSelectionStrategy
return this
}
return new MyPriceListStrategy({
customerService: this.customerService
return new MyStrategy({
customerService: this.customerService,
})
}
//...
// ...
}
```
@@ -80,10 +85,10 @@ This method accepts the variant ID as a first parameter and the [context](./inde
This method must return an object having the following fields:
```typescript noReport
```ts noReport
{
originalPrice, //number | null
calculatedPrice, //number | null
originalPrice, // number | null
calculatedPrice, // number | null
prices // MoneyAmount[]
}
```