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.
This commit is contained in:
Shahed Nasser
2023-10-19 15:56:26 +00:00
committed by GitHub
parent b38f73726d
commit c28935b4e8
170 changed files with 3658 additions and 3344 deletions
@@ -10,7 +10,7 @@ In this document, youll learn how to install the Discount Generator plugin on
In Medusa, merchants can create dynamic discounts that act as a template for other discounts. With dynamic discounts, merchants don't have to repeat certain conditions every time they want to create a new discount.
The discount generator plugin allows merchants and developers to generate new discounts from a dynamic discount. This can be done either by envoking the `/discount-code` endpoint or using the `DiscountGeneratorService`.
The discount generator plugin allows merchants and developers to generate new discounts from a dynamic discount. This can be done either by envoking the `/discount-code` API Route or using the `DiscountGeneratorService`.
---
@@ -43,11 +43,11 @@ const plugins = [
## Test the Plugin
### Using the Endpoint
### Using the API Route
The plugin registers a `POST` endpoint `/discount-code`. The endpoint accepts in the request body the parameter `discount_code` which is a string indicating the code of the dynamic discount to generate a new discount from. The endpoint then creates the new discount from the dynamic discount and returns it in the response.
The plugin registers a `POST` API Route `/discount-code`. The API Route accepts in the request body the parameter `discount_code` which is a string indicating the code of the dynamic discount to generate a new discount from. The API Route then creates the new discount from the dynamic discount and returns it in the response.
So, to test out the endpoint, run the following command in the root of your project to start the Medusa backend:
So, to test out the API Route, run the following command in the root of your project to start the Medusa backend:
```bash
npx medusa develop
@@ -55,41 +55,37 @@ npx medusa develop
Then, create a dynamic discount. You can do that either using the [Medusa admin](../../user-guide/discounts/create.mdx) which is available (if installed) at `http://localhost:7001` after starting the backend, or using the [Admin REST APIs](../../modules/discounts/admin/manage-discounts.mdx).
After that, send a `POST` request to the `/discount-code` endpoint, passing the `discount_code` parameter in the request body with the value being the code of the dynamic discount you just created. A new discount will be created with the same attributes as the dynamic discount code and returned in the response.
After that, send a `POST` request to the `/discount-code` API Route, passing the `discount_code` parameter in the request body with the value being the code of the dynamic discount you just created. A new discount will be created with the same attributes as the dynamic discount code and returned in the response.
### Using the DiscountGeneratorService
After installing the plugin, the `DiscountGeneratorService` is registered in the [dependency container](../../development/fundamentals/dependency-injection.md). So, you can resolve and use it in custom services, endpoints, or other resources.
After installing the plugin, the `DiscountGeneratorService` is registered in the [dependency container](../../development/fundamentals/dependency-injection.md). So, you can resolve and use it in custom services, API Routes, or other resources.
The `DiscountGeneratorService` has one method `generateDiscount`. This method requires passing the code of a dynamic discount as a parameter. It then creates a new discount having the same attributes as the dynamic discount, but with a different, random code.
Here's an example of using the service in an endpoint:
Here's an example of using the service in an API Route:
```ts title=src/api/index.ts
import { Request, Response, Router } from "express"
import bodyParser from "body-parser"
```ts title=src/api/store/generate-discount-code/route.ts
import type {
MedusaRequest,
MedusaResponse,
} from "@medusajs/medusa"
export default (rootDirectory: string): Router | Router[] => {
const router = Router()
export const POST = async (
req: MedusaRequest,
res: MedusaResponse
) => {
// skipping validation for simplicity
const { dynamicCode } = req.body
const discountGenerator = req.scope.resolve(
"discountGeneratorService"
)
const code = await discountGenerator.generateDiscount(
dynamicCode
)
router.use(
"/generate-discount-code",
bodyParser.json(),
async (req: Request, res: Response) => {
// skipping validation for simplicity
const { dynamicCode } = req.body
const discountGenerator = req.scope.resolve(
"discountGeneratorService"
)
const code = await discountGenerator.generateDiscount(
dynamicCode
)
res.json({
code,
})
res.json({
code,
})
return router
}
```
```