Skip to content

Commit 9a98c4b

Browse files
committed
feat: fetch function now returns the browser native Response object
1 parent 6d1c262 commit 9a98c4b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/openapi-typescript/fetch-factory.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ function fetchFactory<Paths>(options?: InitParameters) {
3030

3131
const fetchInit = buildInit(defaultInit, options);
3232
const response = await fetchMethod(url, fetchInit);
33-
34-
return {
35-
...response,
36-
ok: response.ok,
33+
return new Response(response.body, {
3734
status: response.status,
38-
} as Omit<Response, "json"> & ResponseByStatus<Paths[Path][Method]>;
35+
headers: response.headers,
36+
statusText: response.statusText,
37+
}) as Omit<Response, "json"> & ResponseByStatus<Paths[Path][Method]>;
3938
}
4039

4140
return fetcher;

test/openapi-typescript/fetch-factory.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ import { FetchFactory } from "../../src/openapi-typescript";
22
import { paths, components } from "./test-data/petstore-openapi3";
33
import { IsEqual } from "../test-tools";
44
import { Immutable } from "../../src/types/utilities";
5+
import { Readable } from "stream";
56

67
const mockedJson = jest.fn(() => ({} as any));
7-
const mockedFetch = jest.fn(() => ({ ok: true, json: mockedJson } as any));
8+
const mockedFetch = jest.fn(
9+
() =>
10+
({
11+
ok: true,
12+
body: Readable.from([`{"hello": "world"}`]),
13+
} as any)
14+
);
815
global.fetch = jest.fn(() => ({ ok: true, json: mockedJson } as any));
916

1017
const defaultFetch = FetchFactory.build<paths>();
@@ -268,9 +275,9 @@ describe("Generated fetch request", () => {
268275
describe("Generated fetch response", () => {
269276
it("calls json() of response", async () => {
270277
const response = await customFetch("/store/inventory", { method: "get" });
271-
response.json();
278+
const payload = await response.json();
272279

273-
expect(mockedJson.mock.calls).toHaveLength(1);
280+
expect(payload.hello).toBe("world");
274281
});
275282

276283
it("can be discriminated based on ok property", async () => {

0 commit comments

Comments
 (0)