docs: update middlewares to use defineMiddlewares (#8283)

This commit is contained in:
Shahed Nasser
2024-07-25 17:14:06 +02:00
committed by GitHub
parent 47c132c70b
commit eb64ae75a6
7 changed files with 51 additions and 39 deletions
@@ -27,13 +27,16 @@ When you protect routes with the `authenticate` middleware, you specify in its f
For example:
export const highlights = [
["8", `"user"`, "The actor type that must be authenticated to access the specified routes."]
["11", `"user"`, "The actor type that must be authenticated to access the specified routes."]
]
```ts title="src/api/middlewares.ts" highlights={highlights}
import { MiddlewaresConfig, authenticate } from "@medusajs/medusa"
import {
defineMiddlewares,
authenticate
} from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
matcher: "/custom/admin*",
@@ -42,7 +45,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
By specifying `user` as the first parameter of `authenticate`, only authenticated users of actor type `user` can access API routes starting with `/custom/admin`.
@@ -176,15 +176,18 @@ The last step is to apply the `authenticate` middleware on the API routes that r
To do that, create the file `src/api/middlewares.ts` with the following content:
export const middlewareHighlights = [
["9", "authenticate", "Require the user to be authenticated to access the `/manager` API route when sending a `POST` request."],
["10", "allowUnregistered", "The user doesn't need to be registered to access the API route."],
["17", "authenticate", "Require the user to be authenticated as a manager when accessing `/manager/me` API routes."]
["12", "authenticate", "Require the user to be authenticated to access the `/manager` API route when sending a `POST` request."],
["13", "allowUnregistered", "The user doesn't need to be registered to access the API route."],
["20", "authenticate", "Require the user to be authenticated as a manager when accessing `/manager/me` API routes."]
]
```ts title="src/api/middlewares.ts" highlights={middlewareHighlights}
import { MiddlewaresConfig, authenticate } from "@medusajs/medusa"
import {
defineMiddlewares,
authenticate
} from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
matcher: "/manager",
@@ -202,7 +205,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
This applies middlewares on two route patterns:
@@ -347,10 +347,10 @@ For example, suppose an administrator changes the product data in the ERP system
Then, create the file `src/api/middlewares.ts` with the following content:
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig } from "@medusajs/medusa"
import { defineMiddlewares } from "@medusajs/medusa"
import { raw } from "body-parser"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
method: ["POST", "PUT"],
@@ -359,7 +359,7 @@ For example, suppose an administrator changes the product data in the ERP system
middlewares: [raw({ type: "application/json" })],
},
],
}
})
```
This replaces the default JSON middleware with the raw middleware, which is useful for webhook routes.
@@ -455,9 +455,12 @@ The route handler creates a vendor using the Marketplace Modules main service
Next, create the file `src/api/middlewares.ts` with the following content:
```ts title="src/api/middlewares.ts"
import { MiddlewaresConfig, authenticate } from "@medusajs/medusa"
import {
defineMiddlewares,
authenticate
} from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
matcher: "/vendors",
@@ -475,7 +478,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
This applies two middlewares:
@@ -705,7 +708,7 @@ Finally, in `src/api/middlewares.ts`, apply a middleware on the create products
```ts title="src/api/middlewares.ts"
import {
MiddlewaresConfig,
defineMiddlewares,
authenticate
} from "@medusajs/medusa"
import {
@@ -715,7 +718,7 @@ import {
AdminCreateProduct
} from "@medusajs/medusa/dist/api/admin/products/validators"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
// ...
{
@@ -727,7 +730,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
### Test it Out
@@ -1339,7 +1342,7 @@ The next steps of this example depend on your use case. This section provides so
You can use [Medusas admin API routes for orders](!api!/admin) to allow vendors to manage their orders. This requires you to add the following middleware in `src/api/middlewares.ts`:
```ts title="src/api/middlewares.ts"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
// ...
{
@@ -1350,7 +1353,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
You can also re-create or override any of the existing API routes, similar to what you did with the complete cart API route.