|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Allowed Fields in API Routes`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn about allowed fields in API routes, how to set them on custom API routes, and how to override them for Medusa's API routes. |
|
8
|
|
|
9
|
## What are Allowed Fields? |
|
10
|
|
|
11
|
An API route's `fields` query parameter accepts any field or relation name by default, including custom [linked data models](../retrieve-custom-links/page.mdx) that you add after the route is created. |
|
12
|
|
|
13
|
To restrict which fields and relations a route can retrieve, pass an `allowed` array to the route's `validateAndTransformQuery` configuration. Once you set `allowed`, a client can only request a field whose full path appears in that array. A relation in `allowed` doesn't grant access to the fields nested under it. Medusa silently removes every other field from the query before it executes. |
|
14
|
|
|
15
|
<Note title="Tip"> |
|
16
|
|
|
17
|
Set `allowed` even on your custom API routes, not only on Medusa's routes. Since the `fields` query parameter is open by default, a route without `allowed` exposes every relation you link to its underlying data model, including relations you add later without realizing they're now reachable through that route. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
`allowed` is different than [disallowed fields](../disallowed-fields/page.mdx): |
|
22
|
|
|
23
|
- `allowed` is an opt-in list. Without it, a route retrieves any field a client requests. |
|
24
|
- `disallowed` is a deny list that Medusa always enforces, even for fields in the `allowed` list. Medusa's API routes don't set it, so use it on your own routes to block a segment that the allowed list would otherwise let through. |
|
25
|
|
|
26
|
--- |
|
27
|
|
|
28
|
## How to Set Allowed Fields in Custom API Routes? |
|
29
|
|
|
30
|
To restrict the fields and relations retrievable by a custom API route, pass an `allowed` array to the `validateAndTransformQuery` configuration: |
|
31
|
|
|
32
|
export const setHighlights = [ |
|
33
|
["18", "allowed", "The only fields and relations this route can retrieve."], |
|
34
|
] |
|
35
|
|
|
36
|
```ts title="src/api/middlewares.ts" highlights={setHighlights} |
|
37
|
import { |
|
38
|
validateAndTransformQuery, |
|
39
|
defineMiddlewares, |
|
40
|
} from "@medusajs/framework/http" |
|
41
|
import { createFindParams } from "@medusajs/medusa/api/utils/validators" |
|
42
|
|
|
43
|
export default defineMiddlewares({ |
|
44
|
routes: [ |
|
45
|
{ |
|
46
|
matcher: "/store/custom", |
|
47
|
method: "GET", |
|
48
|
middlewares: [ |
|
49
|
validateAndTransformQuery( |
|
50
|
createFindParams(), |
|
51
|
{ |
|
52
|
defaults: ["id", "title"], |
|
53
|
isList: true, |
|
54
|
allowed: ["id", "title", "brand"], |
|
55
|
} |
|
56
|
), |
|
57
|
], |
|
58
|
}, |
|
59
|
], |
|
60
|
}) |
|
61
|
``` |
|
62
|
|
|
63
|
In this example, a request to `GET /store/custom?fields=*owner` doesn't retrieve the `owner` field, since it's not in the `allowed` array, even though the underlying data model has an `owner` relation. |
|
64
|
|
|
65
|
An entry in `allowed` only matches a requested field equal to it. Medusa normalizes the `*` and `.*` prefixes and suffixes before it compares the paths, so `variants` in `allowed` also allows `*variants`. It rejects `variants.options` and `variants.prices`, unless you add those exact paths to the array as well: |
|
66
|
|
|
67
|
export const nestedHighlights = [ |
|
68
|
["19", `"variants"`, "Allows `variants` and `*variants`."], |
|
69
|
["20", `"variants.options"`, "Allows the nested `options` relation."], |
|
70
|
["21", `"variants.prices"`, "Allows the nested `prices` relation."], |
|
71
|
] |
|
72
|
|
|
73
|
```ts title="src/api/middlewares.ts" highlights={nestedHighlights} |
|
74
|
import { |
|
75
|
validateAndTransformQuery, |
|
76
|
defineMiddlewares, |
|
77
|
} from "@medusajs/framework/http" |
|
78
|
import { createFindParams } from "@medusajs/medusa/api/utils/validators" |
|
79
|
|
|
80
|
export default defineMiddlewares({ |
|
81
|
routes: [ |
|
82
|
{ |
|
83
|
matcher: "/store/custom", |
|
84
|
method: "GET", |
|
85
|
middlewares: [ |
|
86
|
validateAndTransformQuery( |
|
87
|
createFindParams(), |
|
88
|
{ |
|
89
|
defaults: ["id", "title"], |
|
90
|
isList: true, |
|
91
|
allowed: [ |
|
92
|
"variants", |
|
93
|
"variants.options", |
|
94
|
"variants.prices", |
|
95
|
], |
|
96
|
} |
|
97
|
), |
|
98
|
], |
|
99
|
}, |
|
100
|
], |
|
101
|
}) |
|
102
|
``` |
|
103
|
|
|
104
|
Medusa strips a rejected field silently. The request still succeeds, but the response doesn't include the field. |
|
105
|
|
|
106
|
<Note> |
|
107
|
|
|
108
|
The `order` query parameter is the exception. If you sort by a field that isn't in `allowed`, the request fails with a `400` error. |
|
109
|
|
|
110
|
</Note> |
|
111
|
|
|
112
|
### Allowed Fields and the Relations Limit |
|
113
|
|
|
114
|
An allowed field must also comply with the [http.storeRelationsLimit](../../../configurations/medusa-config/page.mdx#httpstorerelationslimit) configuration, which caps how many relations a Store API route expands in one request. The two restrictions are separate: |
|
115
|
|
|
116
|
- `allowed` controls which paths a client can request. |
|
117
|
- `http.storeRelationsLimit` controls how deep those paths can go. |
|
118
|
|
|
119
|
So, adding a path to `allowed` doesn't exempt it from the limit. Medusa counts every segment of a path that selects a whole relation, such as `*items.variant.options`, and every segment but the last of a path that selects a property, such as `items.variant.title`. Both count as three relations. |
|
120
|
|
|
121
|
<Note title="Important"> |
|
122
|
|
|
123
|
A path that exceeds the limit fails the request with a `400` error, unlike a path that isn't in `allowed`, which Medusa strips silently. A route can also set a lower limit than the application-wide configuration, in which case the route's limit applies and raising `http.storeRelationsLimit` has no effect on it. |
|
124
|
|
|
125
|
</Note> |
|
126
|
|
|
127
|
--- |
|
128
|
|
|
129
|
## API Routes that Restrict Retrievable Fields |
|
130
|
|
|
131
|
Some of Medusa's API routes already set `allowed`, which means you can't pass your custom linked data models in the `fields` query parameter of these routes. Medusa restricts these routes to keep them performant and secure. |
|
132
|
|
|
133
|
Every [Store API route](!api!/store) sets `allowed`. Each route allows its default fields and a small set of extra fields and relations, and rejects everything else. Refer to the API reference of an API route for its full list of allowed fields. |
|
134
|
|
|
135
|
The Admin API routes that restrict the fields and relations you can retrieve are: |
|
136
|
|
|
137
|
- [Customer Admin API Routes](!api!/admin/customers) |
|
138
|
- [Product Category Admin API Routes](!api!/admin/product-categories) |
|
139
|
|
|
140
|
--- |
|
141
|
|
|
142
|
## How to Override Allowed Fields of Medusa's API Routes? |
|
143
|
|
|
144
|
For the routes mentioned above, you need to override the allowed fields and relations to be retrieved. You can do this by applying a [global middleware](../middlewares/page.mdx) to those routes. |
|
145
|
|
|
146
|
Since every Store API route restricts its retrievable fields, retrieving a custom linked data model through a Store API route always requires this override. |
|
147
|
|
|
148
|
<Note title="Warning" type="warning"> |
|
149
|
|
|
150
|
Only add the specific fields and relations that the client needs. A broad entry, such as a relation that pulls in multiple relations, or a sensitive relation like an order, a payment, or another customer, exposes that data to anyone who calls the route. Store API routes are public, so treat every path you add to `allowed` as data you're willing to publish. |
|
151
|
|
|
152
|
</Note> |
|
153
|
|
|
154
|
For example, to allow retrieving the `b2b_company` of a customer using the [Get Customer Store API Route](!api!/store/customers/get-customer), create the file `src/api/middlewares.ts` with the following content: |
|
155
|
|
|
156
|
export const overrideHighlights = [ |
|
157
|
["10", "allowFields", "Allow retrieving the `b2b_company` relation."], |
|
158
|
] |
|
159
|
|
|
160
|
```ts title="src/api/middlewares.ts" highlights={overrideHighlights} |
|
161
|
import { |
|
162
|
allowFields, |
|
163
|
defineMiddlewares, |
|
164
|
} from "@medusajs/framework/http" |
|
165
|
|
|
166
|
export default defineMiddlewares({ |
|
167
|
routes: [ |
|
168
|
{ |
|
169
|
matcher: "/store/customers/me", |
|
170
|
middlewares: [allowFields("b2b_company")], |
|
171
|
}, |
|
172
|
], |
|
173
|
}) |
|
174
|
``` |
|
175
|
|
|
176
|
In this example, you apply the `allowFields` middleware to the [Get Customer Store API Route](!api!/store/customers/get-customer). The middleware adds `b2b_company` to the fields and relations that the route already allows. |
|
177
|
|
|
178
|
<Note> |
|
179
|
|
|
180
|
`allowFields` is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). In earlier versions, write the middleware yourself and push the fields to the request's `allowed` property: |
|
181
|
|
|
182
|
```ts |
|
183
|
import { |
|
184
|
allowFields, |
|
185
|
defineMiddlewares, |
|
186
|
} from "@medusajs/framework/http" |
|
187
|
|
|
188
|
export default defineMiddlewares({ |
|
189
|
routes: [ |
|
190
|
{ |
|
191
|
matcher: "/store/customers/me", |
|
192
|
middlewares: [ |
|
193
|
(req, res, next) => { |
|
194
|
req.allowed.push("b2b_company") |
|
195
|
next() |
|
196
|
}, |
|
197
|
], |
|
198
|
}, |
|
199
|
], |
|
200
|
}) |
|
201
|
``` |
|
202
|
|
|
203
|
</Note> |
|
204
|
|
|
205
|
`allowFields` accepts the field paths as separate parameters, an array, or a mix of both: |
|
206
|
|
|
207
|
```ts |
|
208
|
allowFields("b2b_company", "b2b_company.name") |
|
209
|
allowFields(["b2b_company", "b2b_company.name"]) |
|
210
|
``` |
|
211
|
|
|
212
|
Pass the normalized field path, without a `*`, `+`, or `.*` prefix or suffix. |
|
213
|
|
|
214
|
Each `allowFields` middleware adds to what the route and the other middlewares allow, so several plugins or middlewares can each expose their own fields on the same route. |
|
215
|
|
|
216
|
<Note> |
|
217
|
|
|
218
|
Learn how to create a middleware in the [Middlewares](../middlewares/page.mdx) chapter. |
|
219
|
|
|
220
|
</Note> |
|
221
|
|
|
222
|
You can now retrieve the `b2b_company` field using the `fields` query parameter of the [Get Customer Store API Route](!api!/store/customers/get-customer): |
|
223
|
|
|
224
|
```bash |
|
225
|
curl 'http://localhost:9000/store/customers/me?fields=*b2b_company' \ |
|
226
|
-H 'x-publishable-api-key: {your_publishable_api_key}' \ |
|
227
|
-H 'Authorization: Bearer {jwt_token}' |
|
228
|
``` |
|
229
|
|
|
230
|
In this example, you retrieve the `b2b_company` relation of the customer using the `fields` query parameter. |
|
231
|
|
|
232
|
<Note title="Important"> |
|
233
|
|
|
234
|
This approach only works using a global middleware. It doesn't work in a route middleware. |
|
235
|
|
|
236
|
</Note> |