docs: update middlewares to use defineMiddlewares (#8283)
This commit is contained in:
1 parent
47c132c70b
commit
eb64ae75a6
7 files changed
+51
-39
No files matched your search
@@ -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`.
|
||||
@@ -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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user