docs: add documentation on sending requests to custom routes with JS SDK (#10582)
This commit is contained in:
@@ -290,6 +290,60 @@ The `Medusa` initializer accepts as a parameter an object with the following pro
|
||||
|
||||
---
|
||||
|
||||
## Send Requests to Custom Routes
|
||||
|
||||
The sidebar shows the different methods that you can use to send requests to Medusa's API routes.
|
||||
|
||||
To send requests to custom routes, the JS SDK has a `client.fetch` method that wraps the [JavaScript Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) that you can use. The method automatically appends configurations and headers, such as authentication headers, to your request.
|
||||
|
||||
For example, to send a request to a custom route at `http://localhost:9000/custom`:
|
||||
|
||||
<CodeTabs group="request-type">
|
||||
<CodeTab label="GET" value="get">
|
||||
|
||||
```ts
|
||||
sdk.client.fetch(`/custom`)
|
||||
.then((data) => {
|
||||
console.log(data)
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="POST" value="post">
|
||||
|
||||
```ts
|
||||
sdk.client.fetch(`/custom`, {
|
||||
method: "post",
|
||||
body: {
|
||||
id: "123"
|
||||
}
|
||||
}).then((data) => {
|
||||
console.log(data)
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="DELETE" value="delete">
|
||||
|
||||
```ts
|
||||
sdk.client.fetch(`/custom`, {
|
||||
method: "delete",
|
||||
}).then(() => {
|
||||
console.log("success")
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
</CodeTabs>
|
||||
|
||||
The `fetch` method accepts as a first parameter the route's path relative to the `baseUrl` configuration you passed when you initialized the SDK.
|
||||
|
||||
In the second parameter, you can pass an object of [request configurations](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit). You don't need to configure the content-type to be JSON, or stringify the `body` or `query` value, as that's handled by the method.
|
||||
|
||||
The method returns a Promise that, when resolved, has the data returned by the request. If the request returns a JSON object, it'll be automatically parsed to a JavaScript object and returned.
|
||||
|
||||
---
|
||||
|
||||
## Medusa JS SDK Tips
|
||||
|
||||
### Use Tanstack (React) Query in Admin Customizations
|
||||
|
||||
@@ -2248,7 +2248,7 @@ export const generatedEditDates = {
|
||||
"app/commerce-modules/store/links-to-other-modules/page.mdx": "2024-06-26T07:19:49.931Z",
|
||||
"app/examples/page.mdx": "2024-12-11T09:07:47.589Z",
|
||||
"app/medusa-cli/commands/build/page.mdx": "2024-11-11T11:00:49.665Z",
|
||||
"app/js-sdk/page.mdx": "2024-10-16T12:12:34.512Z",
|
||||
"app/js-sdk/page.mdx": "2024-12-12T11:41:51.152Z",
|
||||
"references/js_sdk/admin/Admin/properties/js_sdk.admin.Admin.apiKey/page.mdx": "2024-12-09T13:21:58.136Z",
|
||||
"references/js_sdk/admin/Admin/properties/js_sdk.admin.Admin.campaign/page.mdx": "2024-12-09T13:21:58.212Z",
|
||||
"references/js_sdk/admin/Admin/properties/js_sdk.admin.Admin.claim/page.mdx": "2024-12-09T13:21:57.972Z",
|
||||
|
||||
Reference in New Issue
Block a user