fix(js-sdk): Preserve path in baseUrl (#10222)
**What** - Currently the `js-sdk` will discard the path in the `baseUrl` and only use the `origin`. As an example passing a `baseUrl` of `"http://localhost:9000/custom/path"`, will result in only `"http://localhost:9000"` being used. - This PR preserves the path, so using `sdk.admin.products()` will make a request to `"http://localhost:9000/custom/path/admin/products`. For an explanation of when this might be useful see this issue: https://github.com/medusajs/medusa/issues/10190#issuecomment-2492974845 Resolves CMRC-740
This commit is contained in:
1 parent
60b13c191e
commit
d6ff8d7aa1
3 files changed
+52
-2
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/js-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(js-sdk): Preserve path in baseUrl
|
||||||
@@ -12,6 +12,11 @@ const server = setupServer(
|
|||||||
test: "test",
|
test: "test",
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
|
http.get(`${baseUrl}/some/path/test`, ({ request, params, cookies }) => {
|
||||||
|
return HttpResponse.json({
|
||||||
|
test: "test",
|
||||||
|
})
|
||||||
|
}),
|
||||||
http.get(`${baseUrl}/throw`, ({ request, params, cookies }) => {
|
http.get(`${baseUrl}/throw`, ({ request, params, cookies }) => {
|
||||||
return new HttpResponse(null, {
|
return new HttpResponse(null, {
|
||||||
status: 500,
|
status: 500,
|
||||||
@@ -184,6 +189,42 @@ describe("Client", () => {
|
|||||||
|
|
||||||
global.window = undefined as any
|
global.window = undefined as any
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should handle baseUrl with path correctly", async () => {
|
||||||
|
const pathClient = new Client({
|
||||||
|
baseUrl: `${baseUrl}/some/path`,
|
||||||
|
})
|
||||||
|
|
||||||
|
const resp = await pathClient.fetch<any>("test")
|
||||||
|
expect(resp).toEqual({ test: "test" })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle baseUrl with trailing slash path correctly", async () => {
|
||||||
|
const pathClient = new Client({
|
||||||
|
baseUrl: `${baseUrl}/some/path/`,
|
||||||
|
})
|
||||||
|
|
||||||
|
const resp = await pathClient.fetch<any>("test")
|
||||||
|
expect(resp).toEqual({ test: "test" })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle baseUrl with just origin", async () => {
|
||||||
|
const originClient = new Client({
|
||||||
|
baseUrl,
|
||||||
|
})
|
||||||
|
|
||||||
|
const resp = await originClient.fetch<any>("test")
|
||||||
|
expect(resp).toEqual({ test: "test" })
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle baseUrl with just origin and trailing slash", async () => {
|
||||||
|
const originClient = new Client({
|
||||||
|
baseUrl: `${baseUrl}/`,
|
||||||
|
})
|
||||||
|
|
||||||
|
const resp = await originClient.fetch<any>("test")
|
||||||
|
expect(resp).toEqual({ test: "test" })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("GET requests", () => {
|
describe("GET requests", () => {
|
||||||
@@ -247,4 +288,6 @@ describe("Client", () => {
|
|||||||
global.window = undefined as any
|
global.window = undefined as any
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import qs from "qs"
|
|
||||||
import { events } from "fetch-event-stream"
|
import { events } from "fetch-event-stream"
|
||||||
|
import qs from "qs"
|
||||||
import {
|
import {
|
||||||
ClientFetch,
|
ClientFetch,
|
||||||
Config,
|
Config,
|
||||||
@@ -224,7 +224,9 @@ export class Client {
|
|||||||
|
|
||||||
let normalizedInput: RequestInfo | URL = input
|
let normalizedInput: RequestInfo | URL = input
|
||||||
if (input instanceof URL || typeof input === "string") {
|
if (input instanceof URL || typeof input === "string") {
|
||||||
normalizedInput = new URL(input, this.config.baseUrl)
|
const baseUrl = new URL(this.config.baseUrl)
|
||||||
|
const fullPath = `${baseUrl.pathname.replace(/\/$/, '')}/${input.toString().replace(/^\//, '')}`
|
||||||
|
normalizedInput = new URL(fullPath, baseUrl.origin)
|
||||||
if (init?.query) {
|
if (init?.query) {
|
||||||
const params = Object.fromEntries(
|
const params = Object.fromEntries(
|
||||||
normalizedInput.searchParams.entries()
|
normalizedInput.searchParams.entries()
|
||||||
|
|||||||
Reference in new issue
Block a user