Skip to content

Is there a plan to support cookie parameters? #1689

@zaru

Description

@zaru

Description

Hello! I’m using openapi-fetch conveniently. Thank you. I would be delighted if openapi-fetch supported cookie parameters. Currently, I am sending cookies from the headers as follows:

const { data, error } = await fetchClient.GET("/me", {
  params: {
    header: {
      Cookie: "_session_id=foobar",
    },
  },
});

Proposal

I propose that we add support for specifying cookie parameters directly. For example:

const { data, error } = await fetchClient.GET("/me", {
  params: {
    cookie: {
      _session_id: "foobar",
    },
  },
});

I found that adding the following code to the index.js of openapi-fetch works, but I’m not sure if this is the correct approach:

diff --git a/packages/openapi-fetch/src/index.js b/packages/openapi-fetch/src/index.js
index e0cb43c..e612304 100644
--- a/packages/openapi-fetch/src/index.js
+++ b/packages/openapi-fetch/src/index.js
@@ -76,6 +76,14 @@ export default function createClient(clientOptions) {
       ...init,
       headers: mergeHeaders(baseHeaders, headers, params.header),
     };
+
+    if (params.cookie) {
+      const cookieValue = Object.entries(params.cookie)
+        .map(([key, value]) => `${key}=${value}`)
+        .join("; ");
+      requestInit.headers.set("Cookie", cookieValue);
+    }
+
     if (requestInit.body) {
       requestInit.body = bodySerializer(requestInit.body);
     }

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs welcomePRs are welcome to solve this issue!enhancementNew feature or requestopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions