Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parseSuccessResponseBody Option #602

Merged
merged 18 commits into from
Jul 11, 2023
Merged
24 changes: 16 additions & 8 deletions src/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import type { EndpointInterface } from "@octokit/types";

import getBuffer from "./get-buffer-response";

export default function fetchWrapper(
requestOptions: ReturnType<EndpointInterface> & {
redirect?: "error" | "follow" | "manual";
},
) {
type RequestOptions = ReturnType<EndpointInterface> & {
redirect?: "error" | "follow" | "manual";
request?: {
parseResponse?: boolean;
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
};
};

export default function fetchWrapper(requestOptions: RequestOptions) {
const log =
requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
const parseResponse = requestOptions.request?.parseResponse !== false
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

if (
isPlainObject(requestOptions.body) ||
Expand Down Expand Up @@ -93,14 +97,18 @@ export default function fetchWrapper(
url,
status,
headers,
data: await getResponseData(response),
data: parseResponse
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
? await getResponseData(response)
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
: response.body,
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
},
request: requestOptions,
});
}

if (status >= 400) {
const data = await getResponseData(response);
const data = parseResponse
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
? await getResponseData(response)
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
: response.body;
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved

const error = new RequestError(toErrorMessage(data), status, {
response: {
Expand All @@ -115,7 +123,7 @@ export default function fetchWrapper(
throw error;
}

return getResponseData(response);
return parseResponse ? await getResponseData(response) : response.body;
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
})
.then((data) => {
return {
Expand Down
33 changes: 32 additions & 1 deletion test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from "fs";
import stream from "stream";
import stream, { Stream } from "stream";

import { getUserAgent } from "universal-user-agent";
import fetchMock from "fetch-mock";
Expand Down Expand Up @@ -1021,4 +1021,35 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
expect(error.name).toEqual("AbortError");
});
});

it("request should pass the stream in the response", () => {
const mock = fetchMock.sandbox().get(
"https://api.github.com/repos/octokit-fixture-org/release-assets/tarball/main",
{
status: 200,
headers: {
"content-type": "application/x-gzip",
},
body: fs.createReadStream(__filename),
},
{
sendAsJson: false,
},
);

return request("GET /repos/{owner}/{repo}/tarball/{branch}", {
owner: "octokit-fixture-org",
repo: "release-assets",
branch: "main",
request: {
parseResponse: false,
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
fetch: mock,
},
}).then((response) => {
expect(response.status).toEqual(200);
expect(response.headers["content-type"]).toEqual("application/x-gzip");
expect(response.data).toBeInstanceOf(Stream);
expect(mock.done()).toBe(true);
});
});
});