Skip to content

Commit 110f3c4

Browse files
authored
feat: add parseSuccessResponseBody Option (#602)
1 parent 59ef35f commit 110f3c4

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,17 @@ const { data: app } = await requestWithAuth(
346346
Used for internal logging. Defaults to <a href="https://developer.mozilla.org/en-US/docs/Web/API/console"><code>console</code></a>.
347347
</td>
348348
</tr>
349+
</tr>
350+
<th align=left>
351+
<code>options.request.parseSuccessResponseBody</code>
352+
</th>
353+
<td>
354+
<code>boolean</code>
355+
</td>
356+
<td>
357+
If set to <code>false</code> the returned `response` will be passed through from `fetch`. This is useful to stream response.body when downloading files from the GitHub API.
358+
</td>
359+
</tr>
349360
</table>
350361

351362
All other options except `options.request.*` will be passed depending on the `method` and `url` options.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"dependencies": {
2525
"@octokit/endpoint": "^9.0.0",
2626
"@octokit/request-error": "^5.0.0",
27-
"@octokit/types": "^11.0.0",
27+
"@octokit/types": "^11.1.0",
2828
"is-plain-object": "^5.0.0",
2929
"universal-user-agent": "^6.0.0"
3030
},

src/fetch-wrapper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default function fetchWrapper(
1111
requestOptions.request && requestOptions.request.log
1212
? requestOptions.request.log
1313
: console;
14+
const parseSuccessResponseBody =
15+
requestOptions.request?.parseSuccessResponseBody !== false;
1416

1517
if (
1618
isPlainObject(requestOptions.body) ||
@@ -113,7 +115,9 @@ export default function fetchWrapper(
113115
throw error;
114116
}
115117

116-
return getResponseData(response);
118+
return parseSuccessResponseBody
119+
? await getResponseData(response)
120+
: response.body;
117121
})
118122
.then((data) => {
119123
return {

test/request.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from "fs";
2-
import stream from "stream";
2+
import stream, { Stream } from "stream";
33

44
import { getUserAgent } from "universal-user-agent";
55
import fetchMock from "fetch-mock";
@@ -1021,4 +1021,35 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
10211021
expect(error.name).toEqual("AbortError");
10221022
});
10231023
});
1024+
1025+
it("request should pass the stream in the response", () => {
1026+
const mock = fetchMock.sandbox().get(
1027+
"https://api.github.com/repos/octokit-fixture-org/release-assets/tarball/main",
1028+
{
1029+
status: 200,
1030+
headers: {
1031+
"content-type": "application/x-gzip",
1032+
},
1033+
body: fs.createReadStream(__filename),
1034+
},
1035+
{
1036+
sendAsJson: false,
1037+
},
1038+
);
1039+
1040+
return request("GET /repos/{owner}/{repo}/tarball/{branch}", {
1041+
owner: "octokit-fixture-org",
1042+
repo: "release-assets",
1043+
branch: "main",
1044+
request: {
1045+
parseSuccessResponseBody: false,
1046+
fetch: mock,
1047+
},
1048+
}).then((response) => {
1049+
expect(response.status).toEqual(200);
1050+
expect(response.headers["content-type"]).toEqual("application/x-gzip");
1051+
expect(response.data).toBeInstanceOf(Stream);
1052+
expect(mock.done()).toBe(true);
1053+
});
1054+
});
10241055
});

0 commit comments

Comments
 (0)