openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

openapi-fetch: coreFetch `baseUrl` option overwrites client default `baseUrl` option

Open Rendez opened this issue 9 months ago • 0 comments

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 avatar Feb 14 '25 15:02 Rendez