Skip to content

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

@Rendez

Description

@Rendez

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch openapi-fetch@0.12.2 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions