Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ export default function createClient(clientOptions) {
if (parseAs === "stream") {
return { data: response.body, response };
}
// only parse as JSON if content-type indicates JSON
if (parseAs === "json") {
const contentType = response.headers.get("content-type");
if (contentType?.includes("application/json")) {
return { data: await response.json(), response };
}
return { data: await response.text(), response };
}
return { data: await response[parseAs](), response };
}

Expand Down
5 changes: 3 additions & 2 deletions packages/openapi-fetch/test/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ describe("request", () => {
function createCustomFetch(data: any) {
const response = {
clone: () => ({ ...response }),
headers: new Headers(),
headers: new Headers({ "content-type": "application/json" }),
json: async () => data,
text: async () => JSON.stringify(data),
status: 200,
ok: true,
} as Response;
Expand All @@ -353,7 +354,7 @@ describe("request", () => {

const customFetch = createCustomFetch({});
const client = createClient<paths>({ baseUrl: "https://fakeurl.example", fetch: customFetch });
client.GET("/resources", {
await client.GET("/resources", {
customProperty: "value",
});
});
Expand Down
65 changes: 65 additions & 0 deletions packages/openapi-fetch/test/common/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,71 @@ describe("response", () => {
});
});

describe("content-type handling", () => {
test("parses JSON when content-type is application/json", async () => {
const client = createObservedClient<paths>({}, async () =>
Response.json([{ id: "1", name: "Test" }], { status: 200 }),
);
const { data, error } = await client.GET("/resources");
expect(error).toBeUndefined();
expect(data).toEqual([{ id: "1", name: "Test" }]);
});

test("returns text when content-type is not application/json", async () => {
const client = createObservedClient<paths>({}, async () =>
new Response("plain text response", {
status: 200,
headers: { "Content-Type": "text/plain" },
}),
);
const { data, error } = await client.GET("/resources");
expect(error).toBeUndefined();
expect(data).toBe("plain text response");
});

test("returns text when content-type header is missing", async () => {
const client = createObservedClient<paths>({}, async () =>
new Response("no content type", { status: 200 }),
);
const { data, error } = await client.GET("/resources");
expect(error).toBeUndefined();
expect(data).toBe("no content type");
});

test("parses error as JSON when content-type is application/json", async () => {
const client = createObservedClient<paths>({}, async () =>
Response.json({ code: 404, message: "Not found" }, { status: 404 }),
);
const { data, error } = await client.GET("/resources");
expect(data).toBeUndefined();
expect(error).toEqual({ code: 404, message: "Not found" });
});

test("keeps error as text when content-type is not application/json", async () => {
const client = createObservedClient<paths>({}, async () =>
new Response("Error: something went wrong", {
status: 500,
headers: { "Content-Type": "text/plain" },
}),
);
const { data, error } = await client.GET("/resources");
expect(data).toBeUndefined();
expect(error).toBe("Error: something went wrong");
});

test("handles application/json with charset", async () => {
const client = createObservedClient<paths>({}, async () =>
new Response(JSON.stringify([{ id: "1", name: "Test" }]), {
status: 200,
headers: { "Content-Type": "application/json; charset=utf-8" },
}),
);
const { data, error } = await client.GET("/resources");
expect(error).toBeUndefined();
expect(data).toEqual([{ id: "1", name: "Test" }]);
});
});

describe("parseAs", () => {
const client = createObservedClient<paths>({}, async () => Response.json({}));

Expand Down
Loading
Loading