docs: update middlewares to use defineMiddlewares (#8283)

This commit is contained in:
Shahed Nasser
2024-07-25 18:14:06 +03:00
committed by GitHub
parent 47c132c70b
commit eb64ae75a6
7 changed files with 51 additions and 39 deletions

View File

@@ -81,8 +81,8 @@ For example:
export const highlights = [["25", "parseCorsOrigins", "A utility function that parses the CORS configurations in `medusa-config.js`"]]
```ts title="src/api/middlewares.ts" highlights={highlights} collapsibleLines="1-10" expandButtonLabel="Show Imports"
import {
MiddlewaresConfig,
import { defineMiddlewares } from "@medusajs/medusa"
import type {
MedusaNextFunction,
MedusaRequest,
MedusaResponse,
@@ -91,7 +91,7 @@ import { ConfigModule } from "@medusajs/types"
import { parseCorsOrigins } from "@medusajs/utils"
import cors from "cors"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
matcher: "/custom*",
@@ -114,7 +114,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
This retrieves the configurations exported from `medusa-config.js` and applies the `storeCors` to routes starting with `/custom`.

View File

@@ -14,19 +14,19 @@ A middleware is a function executed when a request is sent to an API Route. It's
## How to Create a Middleware?
Middlewares are defined in the special file `src/api/middlewares.ts`. The file must export an object of middleware configurations.
Middlewares are defined in the special file `src/api/middlewares.ts`. Use the `defineMiddlewares` function imported from `@medusajs/medusa` to define the middlewares, and export its value.
For example:
```ts title="src/api/middlewares.ts"
import { defineMiddlewares } from "@medusajs/medusa"
import type {
MedusaNextFunction,
MedusaRequest,
MedusaResponse,
MiddlewaresConfig,
} from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
matcher: "/store*",
@@ -43,10 +43,10 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
The middleware configurations object has the property `routes`. Its value is an array of middleware route objects, each having the following properties:
The `defineMiddlewares` function accepts a middleware configurations object that has the property `routes`. `routes`'s value is an array of middleware route objects, each having the following properties:
- `matcher`: a string or regular expression indicating the API route path to apply the middleware on.
- `middlewares`: An array of middleware functions.
@@ -110,14 +110,14 @@ For example:
export const highlights = [["12", "method", "Apply the middleware only on `POST` requests"]]
```ts title="src/api/middlewares.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
import { defineMiddlewares } from "@medusajs/medusa"
import type {
MedusaNextFunction,
MedusaRequest,
MedusaResponse,
MiddlewaresConfig,
} from "@medusajs/medusa"
export const config: MiddlewaresConfig = {
export default defineMiddlewares({
routes: [
{
matcher: "/store*",
@@ -135,7 +135,7 @@ export const config: MiddlewaresConfig = {
],
},
],
}
})
```
The object in the `routes` array accepts the property `method` whose value is one or more HTTP methods to apply the middleware to.

View File

@@ -128,21 +128,24 @@ For example:
export const highlights = [
[
"7",
"10",
"authenticate",
"Only authenticated admin users can access routes starting with `/custom/admin`",
],
[
"11",
"14",
"authenticate",
"Only authenticated customers can access routes starting with `/custom/customers`",
],
]
```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*",
@@ -153,7 +156,7 @@ export const config: MiddlewaresConfig = {
middlewares: [authenticate("customer", ["session", "bearer"])],
},
],
}
})
```
The `authenticate` middleware function accepts three parameters:

View File

@@ -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`.

View File

@@ -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:

View File

@@ -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.

View File

@@ -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.