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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ const { data: app } = await requestWithAuth(
Used for internal logging. Defaults to <a href="https://developer.mozilla.org/en-US/docs/Web/API/console"><code>console</code></a>.
</td>
</tr>
</tr>
<th align=left>
<code>options.request.parseSuccessResponseBody</code>
</th>
<td>
<code>boolean</code>
</td>
<td>
If set to <code>false</code> the request will return a Stream as response.data for successful requests (e.g. <code>200 OK</code> status code).
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
This option may be useful when downloading files from the GitHub API.
Defaults to <code>true</code>.
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
</td>
</tr>
</table>

All other options except `options.request.*` will be passed depending on the `method` and `url` options.
Expand Down
19 changes: 13 additions & 6 deletions src/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ 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?: {
parseSuccessResponseBody?: boolean;
};
};

export default function fetchWrapper(requestOptions: RequestOptions) {
const log =
requestOptions.request && requestOptions.request.log
? requestOptions.request.log
: console;
const parseSuccessResponseBody =
requestOptions.request?.parseSuccessResponseBody !== false;

if (
isPlainObject(requestOptions.body) ||
Expand Down Expand Up @@ -115,7 +120,9 @@ export default function fetchWrapper(
throw error;
}

return getResponseData(response);
return parseSuccessResponseBody
? await getResponseData(response)
: response.body;
})
.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: {
parseSuccessResponseBody: false,
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);
});
});
});