Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi-fetch: coreFetch baseUrl option overwrites client default baseUrl option #2156

Open
Rendez opened this issue Feb 14, 2025 · 0 comments · May be fixed by #2157
Open

openapi-fetch: coreFetch baseUrl option overwrites client default baseUrl option #2156

Rendez opened this issue Feb 14, 2025 · 0 comments · May be fixed by #2157

Comments

@Rendez
Copy link

Rendez commented Feb 14, 2025

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

The problem is that if any baseUrl option is passed to a client's coreFetch (GET/POST/...) call, the client's default baseUrl option gets overwritten for the rest of the requests. This leads to issues where the a url prefix change is necessary for some requests, but unnecessary for others.

Here is the diff that solved my problem:

diff --git a/node_modules/openapi-fetch/dist/index.js b/node_modules/openapi-fetch/dist/index.js
index 56c4826..e7d4bba 100644
--- a/node_modules/openapi-fetch/dist/index.js
+++ b/node_modules/openapi-fetch/dist/index.js
@@ -44,8 +44,9 @@ export default function createClient(clientOptions) {
       body,
       ...init
     } = fetchOptions || {};
+    let finalBaseUrl = baseUrl;
     if (localBaseUrl) {
-      baseUrl = removeTrailingSlash(localBaseUrl);
+      finalBaseUrl = removeTrailingSlash(localBaseUrl) ?? baseUrl;
     }
 
     let querySerializer =
@@ -84,7 +85,7 @@ export default function createClient(clientOptions) {
 
     let id;
     let options;
-    let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl, params, querySerializer }), requestInit);
+    let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer }), requestInit);
 
     /** Add custom parameters to Request object */
     for (const key in init) {
@@ -98,7 +99,7 @@ export default function createClient(clientOptions) {
 
       // middleware (request)
       options = Object.freeze({
-        baseUrl,
+        baseUrl: finalBaseUrl,
         fetch,
         parseAs,
         querySerializer,

This issue body was partially generated by patch-package.

@Rendez Rendez changed the title coreFetch baseUrl option overwrites client default baseUrl option openapi-fetch: coreFetch baseUrl option overwrites client default baseUrl option Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant