|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Retrieve Custom Links from Medusa's API Route`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn how to retrieve custom data models linked to existing Medusa data models from Medusa's API routes. |
|
8
|
|
|
9
|
## Why Retrieve Custom Linked Data Models? |
|
10
|
|
|
11
|
Often, you'll link custom data models to existing Medusa data models to implement custom features or expand on existing ones. |
|
12
|
|
|
13
|
For example, to add brands for products, you can create a `Brand` data model in a Brand Module, then [define a link](../../module-links/page.mdx) to the [Product Module](!resources!/commerce-modules/product)'s `Product` data model. |
|
14
|
|
|
15
|
When you implement this customization, you might need to retrieve the brand of a product using the existing [Get Product API Route](!api!/admin/products/get-a-product). You can do this by passing the linked data model's name in the `fields` query parameter of the API route. |
|
16
|
|
|
17
|
--- |
|
18
|
|
|
19
|
## How to Retrieve Custom Linked Data Models Using `fields`? |
|
20
|
|
|
21
|
Most of Medusa's API routes accept a `fields` query parameter that allows you to specify the fields and relations to retrieve in the resource, such as a product. |
|
22
|
|
|
23
|
For example, to retrieve the brand of a product, you can pass the `brand` field in the `fields` query parameter of the [Get Product API Route](!api!/admin/products/get-a-product): |
|
24
|
|
|
25
|
```bash |
|
26
|
curl 'http://localhost:9000/admin/products/{id}?fields=*brand' \ |
|
27
|
-H 'Authorization: Bearer {access_token}' |
|
28
|
``` |
|
29
|
|
|
30
|
The `fields` query parameter accepts a comma-separated list of fields and relations to retrieve. To learn more about using the `fields` query parameter, refer to the [API Reference](!api!/store/select-fields-and-relations). |
|
31
|
|
|
32
|
By prefixing `brand` with an asterisk (`*`), you retrieve all the default fields of the product, including the `brand` field. If you don't include the `*` prefix, the response will only include the product's brand. |
|
33
|
|
|
34
|
--- |
|
35
|
|
|
36
|
## Override Allowed Fields and Relations of Medusa's API Routes |
|
37
|
|
|
38
|
Some of Medusa's API routes restrict the fields and relations you can retrieve, which means you can't pass your custom linked data models in the `fields` query parameter. Medusa makes this restriction to ensure the API routes are performant and secure. |
|
39
|
|
|
40
|
Every Store API route sets this restriction. A route only accepts a requested field whose full path is in its allowed list, and a relation in that list doesn't grant access to the fields nested under it. Medusa silently strips every other field from the query. |
|
41
|
|
|
42
|
<Note> |
|
43
|
|
|
44
|
Learn more about restricting retrievable fields, including how to apply the same restriction to your custom API routes and the full list of restricted core routes, in the [Allowed Fields in API Routes](../allowed-fields/page.mdx) chapter. |
|
45
|
|
|
46
|
</Note> |
|
47
|
|
|
48
|
For these routes, 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. |
|
49
|
|
|
50
|
For example, to allow retrieving the `brand` of a product using the [List Products Store API Route](!api!/store/products/list-products), create the file `src/api/middlewares.ts` with the following content: |
|
51
|
|
|
52
|
<Note title="Warning" type="warning"> |
|
53
|
|
|
54
|
Only add the specific paths that the client needs. A broad entry, such as a relation that pulls in a whole data model, 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. |
|
55
|
|
|
56
|
</Note> |
|
57
|
|
|
58
|
export const highlights = [ |
|
59
|
["10", "allowFields", "Allow retrieving the `brand` relation."], |
|
60
|
] |
|
61
|
|
|
62
|
```ts title="src/api/middlewares.ts" highlights={highlights} |
|
63
|
import { |
|
64
|
allowFields, |
|
65
|
defineMiddlewares, |
|
66
|
} from "@medusajs/framework/http" |
|
67
|
|
|
68
|
export default defineMiddlewares({ |
|
69
|
routes: [ |
|
70
|
{ |
|
71
|
matcher: "/store/products", |
|
72
|
middlewares: [allowFields("brand")], |
|
73
|
}, |
|
74
|
], |
|
75
|
}) |
|
76
|
``` |
|
77
|
|
|
78
|
In this example, you apply the `allowFields` middleware to the [List Products Store API Route](!api!/store/products/list-products). The middleware adds `brand` to the fields and relations that the route already allows. |
|
79
|
|
|
80
|
<Note> |
|
81
|
|
|
82
|
`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, as explained in the [Allowed Fields](../allowed-fields/page.mdx#how-to-override-allowed-fields-of-medusas-api-routes) chapter. |
|
83
|
|
|
84
|
</Note> |
|
85
|
|
|
86
|
Pass the plain field path, without a `*`, `+`, or `.*` prefix or suffix. To also retrieve a field nested under the link, such as `brand.name`, pass that full path too. |
|
87
|
|
|
88
|
You can now retrieve the `brand` field using the `fields` query parameter of the [List Products Store API Route](!api!/store/products/list-products): |
|
89
|
|
|
90
|
```bash |
|
91
|
curl 'http://localhost:9000/store/products?fields=*brand' \ |
|
92
|
-H 'x-publishable-api-key: {your_publishable_api_key}' |
|
93
|
``` |
|
94
|
|
|
95
|
In this example, you retrieve the `brand` relation of each product using the `fields` query parameter. |
|
96
|
|
|
97
|
<Note title="Important"> |
|
98
|
|
|
99
|
This approach only works using a global middleware. It doesn't work in a route middleware. |
|
100
|
|
|
101
|
</Note> |
|
102
|
|
|
103
|
--- |
|
104
|
|
|
105
|
## Disallowed Fields in API Routes |
|
106
|
|
|
107
|
An API route can also block specific field path segments as a hard security boundary, which strips them from the query even if they're in the allowed list. |
|
108
|
|
|
109
|
Medusa's API routes don't set disallowed fields, so overriding a route's allowed fields is enough to retrieve your custom linked data model. Learn how to block a segment on your own API routes in the [Disallowed Fields in API Routes](../disallowed-fields/page.mdx) chapter. |