From 2573c477fff787c8b4261b498ddf7593bf3747b8 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Fri, 7 Jul 2023 02:30:11 +0200 Subject: [PATCH 01/13] add asStream Option --- src/fetch-wrapper.ts | 29 ++++++++++++++++++++--------- test/request.test.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 61e0cac48..6f615bdaf 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -4,11 +4,14 @@ import type { EndpointInterface } from "@octokit/types"; import getBuffer from "./get-buffer-response"; -export default function fetchWrapper( - requestOptions: ReturnType & { - redirect?: "error" | "follow" | "manual"; - }, -) { +type RequestOptions = ReturnType & { + redirect?: "error" | "follow" | "manual"; + request?: { + asStream?: boolean; + }; +}; + +export default function fetchWrapper(requestOptions: RequestOptions) { const log = requestOptions.request && requestOptions.request.log ? requestOptions.request.log @@ -101,14 +104,14 @@ export default function fetchWrapper( url, status, headers, - data: await getResponseData(response), + data: await getResponseData(requestOptions, response), }, request: requestOptions, }); } if (status >= 400) { - const data = await getResponseData(response); + const data = await getResponseData(requestOptions, response); const error = new RequestError(toErrorMessage(data), status, { response: { @@ -123,7 +126,7 @@ export default function fetchWrapper( throw error; } - return getResponseData(response); + return getResponseData(requestOptions, response); }) .then((data) => { return { @@ -143,8 +146,16 @@ export default function fetchWrapper( }); } -async function getResponseData(response: Response) { +async function getResponseData( + requestOptions: RequestOptions, + response: Response, +) { + if (requestOptions.request?.asStream) { + return response.body; + } + const contentType = response.headers.get("content-type"); + if (/application\/json/.test(contentType!)) { return response.json(); } diff --git a/test/request.test.ts b/test/request.test.ts index f43415104..3f532384e 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -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"; @@ -1040,4 +1040,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: { + asStream: true, + 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); + }); + }); }); From a4a6c4c01e91e255ffc487d12e3cb9ed729aff28 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sat, 8 Jul 2023 08:27:53 +0200 Subject: [PATCH 02/13] adapt requested changes --- src/fetch-wrapper.ts | 24 +++++++++++------------- test/request.test.ts | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 6f615bdaf..96a012d3d 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -7,7 +7,7 @@ import getBuffer from "./get-buffer-response"; type RequestOptions = ReturnType & { redirect?: "error" | "follow" | "manual"; request?: { - asStream?: boolean; + parseResponse?: boolean; }; }; @@ -16,6 +16,10 @@ export default function fetchWrapper(requestOptions: RequestOptions) { requestOptions.request && requestOptions.request.log ? requestOptions.request.log : console; + const parseResponse = + requestOptions.request && typeof requestOptions.request.parseResponse === "boolean" + ? requestOptions.request.parseResponse + : true if ( isPlainObject(requestOptions.body) || @@ -104,14 +108,14 @@ export default function fetchWrapper(requestOptions: RequestOptions) { url, status, headers, - data: await getResponseData(requestOptions, response), + data: parseResponse ? await getResponseData(response) : response.body, }, request: requestOptions, }); } if (status >= 400) { - const data = await getResponseData(requestOptions, response); + const data = parseResponse ? await getResponseData(response) : response.body; const error = new RequestError(toErrorMessage(data), status, { response: { @@ -126,7 +130,9 @@ export default function fetchWrapper(requestOptions: RequestOptions) { throw error; } - return getResponseData(requestOptions, response); + return parseResponse + ? await getResponseData(response) + : response.body; }) .then((data) => { return { @@ -146,16 +152,8 @@ export default function fetchWrapper(requestOptions: RequestOptions) { }); } -async function getResponseData( - requestOptions: RequestOptions, - response: Response, -) { - if (requestOptions.request?.asStream) { - return response.body; - } - +async function getResponseData(response: Response) { const contentType = response.headers.get("content-type"); - if (/application\/json/.test(contentType!)) { return response.json(); } diff --git a/test/request.test.ts b/test/request.test.ts index 3f532384e..262cf3bac 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -1061,7 +1061,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== repo: "release-assets", branch: "main", request: { - asStream: true, + parseResponse: true, fetch: mock, }, }).then((response) => { From 60688289fb64e792d9b64bb4e2200c2e2709bbe2 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 8 Jul 2023 08:29:14 +0200 Subject: [PATCH 03/13] fix linting --- src/fetch-wrapper.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 96a012d3d..30b28583a 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -17,9 +17,10 @@ export default function fetchWrapper(requestOptions: RequestOptions) { ? requestOptions.request.log : console; const parseResponse = - requestOptions.request && typeof requestOptions.request.parseResponse === "boolean" + requestOptions.request && + typeof requestOptions.request.parseResponse === "boolean" ? requestOptions.request.parseResponse - : true + : true; if ( isPlainObject(requestOptions.body) || @@ -108,14 +109,18 @@ export default function fetchWrapper(requestOptions: RequestOptions) { url, status, headers, - data: parseResponse ? await getResponseData(response) : response.body, + data: parseResponse + ? await getResponseData(response) + : response.body, }, request: requestOptions, }); } if (status >= 400) { - const data = parseResponse ? await getResponseData(response) : response.body; + const data = parseResponse + ? await getResponseData(response) + : response.body; const error = new RequestError(toErrorMessage(data), status, { response: { @@ -130,9 +135,7 @@ export default function fetchWrapper(requestOptions: RequestOptions) { throw error; } - return parseResponse - ? await getResponseData(response) - : response.body; + return parseResponse ? await getResponseData(response) : response.body; }) .then((data) => { return { From 38c67c7219acac5241fe232da812c2f991080228 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 8 Jul 2023 08:31:55 +0200 Subject: [PATCH 04/13] fix test --- test/request.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/request.test.ts b/test/request.test.ts index 262cf3bac..1fe85a9d4 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -1061,7 +1061,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== repo: "release-assets", branch: "main", request: { - parseResponse: true, + parseResponse: false, fetch: mock, }, }).then((response) => { From 5eb4ab5f5fe17fbd673e521ae63ead3ff7c8048c Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Sat, 8 Jul 2023 12:35:09 -0700 Subject: [PATCH 05/13] Update src/fetch-wrapper.ts Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> --- src/fetch-wrapper.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index b19c74982..830612967 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -16,11 +16,7 @@ export default function fetchWrapper(requestOptions: RequestOptions) { requestOptions.request && requestOptions.request.log ? requestOptions.request.log : console; - const parseResponse = - requestOptions.request && - typeof requestOptions.request.parseResponse === "boolean" - ? requestOptions.request.parseResponse - : true; + const parseResponse = requestOptions.request?.parseResponse !== false if ( isPlainObject(requestOptions.body) || From cd8253ac18d56815a2f093e313dcc1da8432001f Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sat, 8 Jul 2023 22:15:44 +0200 Subject: [PATCH 06/13] Apply suggestions from code review --- src/fetch-wrapper.ts | 12 ++++-------- test/request.test.ts | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 830612967..dcff7263c 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -7,7 +7,7 @@ import getBuffer from "./get-buffer-response"; type RequestOptions = ReturnType & { redirect?: "error" | "follow" | "manual"; request?: { - parseResponse?: boolean; + parseSuccessResponseBody?: boolean; }; }; @@ -16,7 +16,7 @@ export default function fetchWrapper(requestOptions: RequestOptions) { requestOptions.request && requestOptions.request.log ? requestOptions.request.log : console; - const parseResponse = requestOptions.request?.parseResponse !== false + const parseSuccessResponseBody = requestOptions.request?.parseSuccessResponseBody !== false if ( isPlainObject(requestOptions.body) || @@ -97,18 +97,14 @@ export default function fetchWrapper(requestOptions: RequestOptions) { url, status, headers, - data: parseResponse - ? await getResponseData(response) - : response.body, + data: await getResponseData(response), }, request: requestOptions, }); } if (status >= 400) { - const data = parseResponse - ? await getResponseData(response) - : response.body; + const data = await getResponseData(response); const error = new RequestError(toErrorMessage(data), status, { response: { diff --git a/test/request.test.ts b/test/request.test.ts index 2fc7e44ab..86e79bbdb 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -1042,7 +1042,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== repo: "release-assets", branch: "main", request: { - parseResponse: false, + parseSuccessResponseBody: false, fetch: mock, }, }).then((response) => { From 64c3ab522747edde6f8a6f805dda0990ffafa51e Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sat, 8 Jul 2023 23:14:48 +0200 Subject: [PATCH 07/13] add documentation --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index c034e311c..3645274ae 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,18 @@ const { data: app } = await requestWithAuth( Used for internal logging. Defaults to console. + + + options.request.parseSuccessResponseBody + + + boolean + + + If set to false the request will return a Stream as response.data for successful requests (e.g. 200 OK status code). + Defaults to true. + + All other options except `options.request.*` will be passed depending on the `method` and `url` options. From 4cb03966215967fb666fb2999351dbb4922a9675 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sun, 9 Jul 2023 02:53:29 +0200 Subject: [PATCH 08/13] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3645274ae..02a501835 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,7 @@ const { data: app } = await requestWithAuth( If set to false the request will return a Stream as response.data for successful requests (e.g. 200 OK status code). + This option may be useful when downloading files from the GitHub API. Defaults to true. From cc1841ab9a97be07e5ed95d317cf9d9ed362be47 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sun, 9 Jul 2023 02:54:26 +0200 Subject: [PATCH 09/13] Apply suggestions from code review --- src/fetch-wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index dcff7263c..b23b34505 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -119,7 +119,7 @@ export default function fetchWrapper(requestOptions: RequestOptions) { throw error; } - return parseResponse ? await getResponseData(response) : response.body; + return parseSuccessResponseBody ? await getResponseData(response) : response.body; }) .then((data) => { return { From 6bf077f1bd18ba3aec7b3475dac0def1728b9fbf Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 9 Jul 2023 02:55:22 +0200 Subject: [PATCH 10/13] lint --- src/fetch-wrapper.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index b23b34505..0f92b5b5c 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -16,7 +16,8 @@ export default function fetchWrapper(requestOptions: RequestOptions) { requestOptions.request && requestOptions.request.log ? requestOptions.request.log : console; - const parseSuccessResponseBody = requestOptions.request?.parseSuccessResponseBody !== false + const parseSuccessResponseBody = + requestOptions.request?.parseSuccessResponseBody !== false; if ( isPlainObject(requestOptions.body) || @@ -119,7 +120,9 @@ export default function fetchWrapper(requestOptions: RequestOptions) { throw error; } - return parseSuccessResponseBody ? await getResponseData(response) : response.body; + return parseSuccessResponseBody + ? await getResponseData(response) + : response.body; }) .then((data) => { return { From 947ed4f8b34036ceb29037e7acc55fb7db54ec91 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Sun, 9 Jul 2023 15:54:09 +0200 Subject: [PATCH 11/13] Update README.md Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com> --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 02a501835..d290d3432 100644 --- a/README.md +++ b/README.md @@ -352,9 +352,7 @@ const { data: app } = await requestWithAuth( boolean - If set to false the request will return a Stream as response.data for successful requests (e.g. 200 OK status code). - This option may be useful when downloading files from the GitHub API. - Defaults to true. + If set to false the returned `response` will be passed through from `fetch`. This is useful to stream response.body when downloading files from the GitHub API. From d9e7f73cbbb979947018321296c5abef64ff72ae Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Sun, 9 Jul 2023 17:23:45 -0400 Subject: [PATCH 12/13] fix bad merge --- src/fetch-wrapper.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 02301b456..93b81913f 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -4,14 +4,13 @@ import type { EndpointInterface } from "@octokit/types"; import getBuffer from "./get-buffer-response"; -type RequestOptions = & { +type RequestOptions = ReturnType & { request?: { parseSuccessResponseBody?: boolean; }; }; export default function fetchWrapper(requestOptions: RequestOptions) { - const log = requestOptions.request && requestOptions.request.log ? requestOptions.request.log From 1b4977f03bfee7893630ca9481eaa3ed6a416520 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 11 Jul 2023 10:36:37 +0200 Subject: [PATCH 13/13] bump types, revert type in fetch-wrapper.ts --- package-lock.json | 8 ++++---- package.json | 2 +- src/fetch-wrapper.ts | 10 +++------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 58c8e0223..8e0dca4c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@octokit/endpoint": "^9.0.0", "@octokit/request-error": "^5.0.0", - "@octokit/types": "^11.0.0", + "@octokit/types": "^11.1.0", "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, @@ -2013,9 +2013,9 @@ "dev": true }, "node_modules/@octokit/types": { - "version": "11.0.0", - "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.0.0.tgz", - "integrity": "sha512-h4iyfMpQUdub1itwTn6y7z2a3EtPuer1paKfsIbZErv0LBbZYGq6haiPUPJys/LetPqgcX3ft33O16XuS03Anw==", + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.1.0.tgz", + "integrity": "sha512-Fz0+7GyLm/bHt8fwEqgvRBWwIV1S6wRRyq+V6exRKLVWaKGsuy6H9QFYeBVDV7rK6fO3XwHgQOPxv+cLj2zpXQ==", "dependencies": { "@octokit/openapi-types": "^18.0.0" } diff --git a/package.json b/package.json index 509113dc1..8c43062cd 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "dependencies": { "@octokit/endpoint": "^9.0.0", "@octokit/request-error": "^5.0.0", - "@octokit/types": "^11.0.0", + "@octokit/types": "^11.1.0", "is-plain-object": "^5.0.0", "universal-user-agent": "^6.0.0" }, diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 93b81913f..0e6987176 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -4,13 +4,9 @@ import type { EndpointInterface } from "@octokit/types"; import getBuffer from "./get-buffer-response"; -type RequestOptions = ReturnType & { - request?: { - parseSuccessResponseBody?: boolean; - }; -}; - -export default function fetchWrapper(requestOptions: RequestOptions) { +export default function fetchWrapper( + requestOptions: ReturnType, +) { const log = requestOptions.request && requestOptions.request.log ? requestOptions.request.log