You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/apps/book/public/llms-full.txt
+34Lines changed: 34 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -56589,6 +56589,40 @@ The method returns a Promise that, when resolved, has the data returned by the r
56589
56589
56590
56590
***
56591
56591
56592
+
## Download Binary or File Responses
56593
+
56594
+
By default, the `fetch` method sets the `accept` header of the request to `application/json`, so it parses the response as JSON and returns a JavaScript object.
56595
+
56596
+
The `fetch` method decides whether to parse the response as JSON based on the request's `accept` header, not the response's content type. So, if your custom route returns a binary or file response, such as a PDF or an image, you must override the `accept` header to a value other than `application/json`. The method then returns the raw [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object without parsing it.
56597
+
56598
+
For example, to download a PDF file returned by a custom route:
In this example, you set the `accept` header to `application/pdf`, so the `fetch` method returns the raw `Response` object. You then use the [Response.blob](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) method to read the response as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), which you can download in the browser.
56621
+
56622
+
Since the method returns the raw `Response` object in this case, type the returned value as `Response`. You're then responsible for reading the response's body, using methods like `blob`, [arrayBuffer](https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer), or [text](https://developer.mozilla.org/en-US/docs/Web/API/Response/text).
56623
+
56624
+
***
56625
+
56592
56626
## Migrating from Fetch API to JS SDK
56593
56627
56594
56628
Prefer `sdk.client.fetch` over the raw [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) when sending requests to your Medusa backend.
Copy file name to clipboardExpand all lines: www/apps/resources/app/js-sdk/page.mdx
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -398,6 +398,44 @@ The method returns a Promise that, when resolved, has the data returned by the r
398
398
399
399
---
400
400
401
+
## Download Binary or File Responses
402
+
403
+
By default, the `fetch` method sets the `accept` header of the request to `application/json`, so it parses the response as JSON and returns a JavaScript object.
404
+
405
+
The `fetch` method decides whether to parse the response as JSON based on the request's `accept` header, not the response's content type. So, if your custom route returns a binary or file response, such as a PDF or an image, you must override the `accept` header to a value other than `application/json`. The method then returns the raw [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object without parsing it.
406
+
407
+
For example, to download a PDF file returned by a custom route:
408
+
409
+
```tsx
410
+
const response:Response=awaitsdk.client.fetch(
411
+
`/admin/orders/${orderId}/invoice`,
412
+
{
413
+
method: "GET",
414
+
headers: {
415
+
accept: "application/pdf",
416
+
},
417
+
}
418
+
)
419
+
420
+
const blob =awaitresponse.blob()
421
+
const url =window.URL.createObjectURL(blob)
422
+
const link =document.createElement("a")
423
+
link.href=url
424
+
link.download=`invoice-${orderId}.pdf`
425
+
link.click()
426
+
window.URL.revokeObjectURL(url)
427
+
```
428
+
429
+
In this example, you set the `accept` header to `application/pdf`, so the `fetch` method returns the raw `Response` object. You then use the [Response.blob](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) method to read the response as a [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), which you can download in the browser.
430
+
431
+
<Note>
432
+
433
+
Since the method returns the raw `Response` object in this case, type the returned value as `Response`. You're then responsible for reading the response's body, using methods like `blob`, [arrayBuffer](https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer), or [text](https://developer.mozilla.org/en-US/docs/Web/API/Response/text).
434
+
435
+
</Note>
436
+
437
+
---
438
+
401
439
## Migrating from Fetch API to JS SDK
402
440
403
441
Prefer `sdk.client.fetch` over the raw [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) when sending requests to your Medusa backend.
0 commit comments