From 2da6e5bf521192ec8450be893bfbbbd0e1b8fd2b Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 12 Dec 2024 13:49:53 +0200 Subject: [PATCH] docs: add documentation on sending requests to custom routes with JS SDK --- .../learn/fundamentals/admin/tips/page.mdx | 2 + www/apps/book/generated/edit-dates.mjs | 2 +- www/apps/resources/app/js-sdk/page.mdx | 54 +++++++++++++++++++ www/apps/resources/generated/edit-dates.mjs | 2 +- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/www/apps/book/app/learn/fundamentals/admin/tips/page.mdx b/www/apps/book/app/learn/fundamentals/admin/tips/page.mdx index c1e22bd4fd392..83d158fd72ee0 100644 --- a/www/apps/book/app/learn/fundamentals/admin/tips/page.mdx +++ b/www/apps/book/app/learn/fundamentals/admin/tips/page.mdx @@ -126,6 +126,8 @@ export default ProductWidget +You can also send requests to custom routes as explained in the [JS SDK reference](!resources!/js-sdk). + --- ## Routing Functionalities diff --git a/www/apps/book/generated/edit-dates.mjs b/www/apps/book/generated/edit-dates.mjs index afcf9fc0afeb2..0ee94396832a8 100644 --- a/www/apps/book/generated/edit-dates.mjs +++ b/www/apps/book/generated/edit-dates.mjs @@ -50,7 +50,7 @@ export const generatedEditDates = { "app/learn/fundamentals/scheduled-jobs/execution-number/page.mdx": "2024-10-21T13:30:21.371Z", "app/learn/fundamentals/api-routes/parameters/page.mdx": "2024-11-19T16:37:47.251Z", "app/learn/fundamentals/api-routes/http-methods/page.mdx": "2024-10-21T13:30:21.367Z", - "app/learn/fundamentals/admin/tips/page.mdx": "2024-11-19T16:43:01.662Z", + "app/learn/fundamentals/admin/tips/page.mdx": "2024-12-12T11:43:26.003Z", "app/learn/fundamentals/api-routes/cors/page.mdx": "2024-12-09T13:04:04.357Z", "app/learn/fundamentals/admin/ui-routes/page.mdx": "2024-12-09T16:44:40.198Z", "app/learn/fundamentals/api-routes/middlewares/page.mdx": "2024-12-09T13:04:03.712Z", diff --git a/www/apps/resources/app/js-sdk/page.mdx b/www/apps/resources/app/js-sdk/page.mdx index bd4ee2f6ff9c7..bb8f7e55b8dd6 100644 --- a/www/apps/resources/app/js-sdk/page.mdx +++ b/www/apps/resources/app/js-sdk/page.mdx @@ -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`: + + + + + ```ts + sdk.client.fetch(`/custom`) + .then((data) => { + console.log(data) + }) + ``` + + + + + ```ts + sdk.client.fetch(`/custom`, { + method: "post", + body: { + id: "123" + } + }).then((data) => { + console.log(data) + }) + ``` + + + + + ```ts + sdk.client.fetch(`/custom`, { + method: "delete", + }).then(() => { + console.log("success") + }) + ``` + + + + +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 diff --git a/www/apps/resources/generated/edit-dates.mjs b/www/apps/resources/generated/edit-dates.mjs index 4b05a92414874..c31f4dbf71e6c 100644 --- a/www/apps/resources/generated/edit-dates.mjs +++ b/www/apps/resources/generated/edit-dates.mjs @@ -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",