-
-
Notifications
You must be signed in to change notification settings - Fork 594
Closed
Labels
PRs welcomePRs are welcome to solve this issue!PRs are welcome to solve this issue!enhancementNew feature or requestNew feature or requestopenapi-fetchRelevant to the openapi-fetch libraryRelevant to the openapi-fetch library
Description
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
- I’m willing to open a PR for this (see CONTRIBUTING.md)
drwpow
Metadata
Metadata
Assignees
Labels
PRs welcomePRs are welcome to solve this issue!PRs are welcome to solve this issue!enhancementNew feature or requestNew feature or requestopenapi-fetchRelevant to the openapi-fetch libraryRelevant to the openapi-fetch library