docs: update middlewares to use defineMiddlewares (#8283)
This commit is contained in:
1 parent
47c132c70b
commit
eb64ae75a6
7 files changed
+50
-38
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`"]]
|
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"
|
```ts title="src/api/middlewares.ts" highlights={highlights} collapsibleLines="1-10" expandButtonLabel="Show Imports"
|
||||||
import {
|
import { defineMiddlewares } from "@medusajs/medusa"
|
||||||
MiddlewaresConfig,
|
import type {
|
||||||
MedusaNextFunction,
|
MedusaNextFunction,
|
||||||
MedusaRequest,
|
MedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
@@ -91,7 +91,7 @@ import { ConfigModule } from "@medusajs/types"
|
|||||||
import { parseCorsOrigins } from "@medusajs/utils"
|
import { parseCorsOrigins } from "@medusajs/utils"
|
||||||
import cors from "cors"
|
import cors from "cors"
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export default defineMiddlewares({
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/custom*",
|
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`.
|
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?
|
## 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:
|
For example:
|
||||||
|
|
||||||
```ts title="src/api/middlewares.ts"
|
```ts title="src/api/middlewares.ts"
|
||||||
|
import { defineMiddlewares } from "@medusajs/medusa"
|
||||||
import type {
|
import type {
|
||||||
MedusaNextFunction,
|
MedusaNextFunction,
|
||||||
MedusaRequest,
|
MedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
MiddlewaresConfig,
|
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export default defineMiddlewares({
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/store*",
|
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.
|
- `matcher`: a string or regular expression indicating the API route path to apply the middleware on.
|
||||||
- `middlewares`: An array of middleware functions.
|
- `middlewares`: An array of middleware functions.
|
||||||
@@ -110,14 +110,14 @@ For example:
|
|||||||
export const highlights = [["12", "method", "Apply the middleware only on `POST` requests"]]
|
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"
|
```ts title="src/api/middlewares.ts" highlights={highlights} collapsibleLines="1-7" expandButtonLabel="Show Imports"
|
||||||
|
import { defineMiddlewares } from "@medusajs/medusa"
|
||||||
import type {
|
import type {
|
||||||
MedusaNextFunction,
|
MedusaNextFunction,
|
||||||
MedusaRequest,
|
MedusaRequest,
|
||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
MiddlewaresConfig,
|
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export default defineMiddlewares({
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/store*",
|
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.
|
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 = [
|
export const highlights = [
|
||||||
[
|
[
|
||||||
"7",
|
"10",
|
||||||
"authenticate",
|
"authenticate",
|
||||||
"Only authenticated admin users can access routes starting with `/custom/admin`",
|
"Only authenticated admin users can access routes starting with `/custom/admin`",
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"11",
|
"14",
|
||||||
"authenticate",
|
"authenticate",
|
||||||
"Only authenticated customers can access routes starting with `/custom/customers`",
|
"Only authenticated customers can access routes starting with `/custom/customers`",
|
||||||
],
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
```ts title="src/api/middlewares.ts" highlights={highlights}
|
```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: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/custom/admin*",
|
matcher: "/custom/admin*",
|
||||||
@@ -153,7 +156,7 @@ export const config: MiddlewaresConfig = {
|
|||||||
middlewares: [authenticate("customer", ["session", "bearer"])],
|
middlewares: [authenticate("customer", ["session", "bearer"])],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
The `authenticate` middleware function accepts three parameters:
|
The `authenticate` middleware function accepts three parameters:
|
||||||
|
|||||||
@@ -27,13 +27,16 @@ When you protect routes with the `authenticate` middleware, you specify in its f
|
|||||||
For example:
|
For example:
|
||||||
|
|
||||||
export const highlights = [
|
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}
|
```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: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/custom/admin*",
|
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`.
|
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:
|
To do that, create the file `src/api/middlewares.ts` with the following content:
|
||||||
|
|
||||||
export const middlewareHighlights = [
|
export const middlewareHighlights = [
|
||||||
["9", "authenticate", "Require the user to be authenticated to access the `/manager` API route when sending a `POST` request."],
|
["12", "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."],
|
["13", "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."]
|
["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}
|
```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: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/manager",
|
matcher: "/manager",
|
||||||
@@ -202,7 +205,7 @@ export const config: MiddlewaresConfig = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
This applies middlewares on two route patterns:
|
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:
|
Then, create the file `src/api/middlewares.ts` with the following content:
|
||||||
|
|
||||||
```ts title="src/api/middlewares.ts"
|
```ts title="src/api/middlewares.ts"
|
||||||
import { MiddlewaresConfig } from "@medusajs/medusa"
|
import { defineMiddlewares } from "@medusajs/medusa"
|
||||||
import { raw } from "body-parser"
|
import { raw } from "body-parser"
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export default defineMiddlewares({
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
method: ["POST", "PUT"],
|
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" })],
|
middlewares: [raw({ type: "application/json" })],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
This replaces the default JSON middleware with the raw middleware, which is useful for webhook routes.
|
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 Module’s main service
|
|||||||
Next, create the file `src/api/middlewares.ts` with the following content:
|
Next, create the file `src/api/middlewares.ts` with the following content:
|
||||||
|
|
||||||
```ts title="src/api/middlewares.ts"
|
```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: [
|
routes: [
|
||||||
{
|
{
|
||||||
matcher: "/vendors",
|
matcher: "/vendors",
|
||||||
@@ -475,7 +478,7 @@ export const config: MiddlewaresConfig = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
This applies two middlewares:
|
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"
|
```ts title="src/api/middlewares.ts"
|
||||||
import {
|
import {
|
||||||
MiddlewaresConfig,
|
defineMiddlewares,
|
||||||
authenticate
|
authenticate
|
||||||
} from "@medusajs/medusa"
|
} from "@medusajs/medusa"
|
||||||
import {
|
import {
|
||||||
@@ -715,7 +718,7 @@ import {
|
|||||||
AdminCreateProduct
|
AdminCreateProduct
|
||||||
} from "@medusajs/medusa/dist/api/admin/products/validators"
|
} from "@medusajs/medusa/dist/api/admin/products/validators"
|
||||||
|
|
||||||
export const config: MiddlewaresConfig = {
|
export default defineMiddlewares({
|
||||||
routes: [
|
routes: [
|
||||||
// ...
|
// ...
|
||||||
{
|
{
|
||||||
@@ -727,7 +730,7 @@ export const config: MiddlewaresConfig = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### Test it Out
|
### 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 [Medusa’s 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`:
|
You can use [Medusa’s 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"
|
```ts title="src/api/middlewares.ts"
|
||||||
export const config: MiddlewaresConfig = {
|
export default defineMiddlewares({
|
||||||
routes: [
|
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.
|
You can also re-create or override any of the existing API routes, similar to what you did with the complete cart API route.
|
||||||
|
|||||||
Reference in new issue
Block a user