From 686de59909fff37bf43a183182886bdaddb8e2fe Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Mon, 31 May 2021 17:46:05 -0700 Subject: [PATCH 1/3] docs(README): `options.request.log` --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 514eb6e28..45ae5fcad 100644 --- a/README.md +++ b/README.md @@ -343,6 +343,16 @@ const { data: app } = await requestWithAuth( Use an AbortController instance to cancel a request. In node you can only cancel streamed requests. + + + options.request.log + + + object + + + Used for internal logging. Defaults to console. + From c8240db39b5715393b1ef31c3b6ed7818b1b4490 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Fri, 4 Jun 2021 14:27:55 -0700 Subject: [PATCH 2/3] test: logs deprecation warning if `deprecation` header is present --- test/request.test.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/test/request.test.ts b/test/request.test.ts index a8ecf2d21..8bf92acd9 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -774,4 +774,81 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== ); }); }); + + it("logs deprecation warning if `deprecation` header is present", function () { + const mock = fetchMock.sandbox().mock( + "https://api.github.com/teams/123", + { + body: { + id: 123, + }, + headers: { + deprecation: "Sat, 01 Feb 2020 00:00:00 GMT", + sunset: "Mon, 01 Feb 2021 00:00:00 GMT", + link: '; rel="deprecation"; type="text/html", ; rel="alternate"', + }, + }, + { + headers: { + accept: "application/vnd.github.v3+json", + authorization: "token 0000000000000000000000000000000000000001", + "user-agent": userAgent, + }, + } + ); + + const warn = jest.fn(); + + return request("GET /teams/{team_id}", { + headers: { + authorization: "token 0000000000000000000000000000000000000001", + }, + team_id: 123, + request: { fetch: mock, log: { warn } }, + }).then((response) => { + expect(response.data).toEqual({ id: 123 }); + expect(warn).toHaveBeenCalledTimes(1); + expect(warn).toHaveBeenCalledWith( + '[@octokit/request] "GET https://api.github.com/teams/123" is deprecated. It is scheduled to be removed on Mon, 01 Feb 2021 00:00:00 GMT. See https://developer.github.com/changes/2020-01-21-moving-the-team-api-endpoints/' + ); + }); + }); + + it("deprecation header without deprecation link", function () { + const mock = fetchMock.sandbox().mock( + "https://api.github.com/teams/123", + { + body: { + id: 123, + }, + headers: { + deprecation: "Sat, 01 Feb 2020 00:00:00 GMT", + sunset: "Mon, 01 Feb 2021 00:00:00 GMT", + }, + }, + { + headers: { + accept: "application/vnd.github.v3+json", + authorization: "token 0000000000000000000000000000000000000001", + "user-agent": userAgent, + }, + } + ); + + const warn = jest.fn(); + + return request("GET /teams/{team_id}", { + headers: { + authorization: "token 0000000000000000000000000000000000000001", + }, + team_id: 123, + request: { fetch: mock, log: { warn } }, + }).then((response) => { + expect(response.data).toEqual({ id: 123 }); + expect(warn).toHaveBeenCalledTimes(1); + expect(warn).toHaveBeenCalledWith( + '[@octokit/request] "GET https://api.github.com/teams/123" is deprecated. It is scheduled to be removed on Mon, 01 Feb 2021 00:00:00 GMT' + ); + }); + }); }); From 89dd83452bf3ff5c2ab2b865bd6a4aa76150f23c Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Fri, 4 Jun 2021 14:28:52 -0700 Subject: [PATCH 3/3] feat: logs deprecation warning if `deprecation` header is present --- src/fetch-wrapper.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/fetch-wrapper.ts b/src/fetch-wrapper.ts index 2f7c98abd..3d22df1cf 100644 --- a/src/fetch-wrapper.ts +++ b/src/fetch-wrapper.ts @@ -10,6 +10,11 @@ export default function fetchWrapper( redirect?: "error" | "follow" | "manual"; } ) { + const log = + requestOptions.request && requestOptions.request.log + ? requestOptions.request.log + : console; + if ( isPlainObject(requestOptions.body) || Array.isArray(requestOptions.body) @@ -46,6 +51,19 @@ export default function fetchWrapper( headers[keyAndValue[0]] = keyAndValue[1]; } + if ("deprecation" in headers) { + const matches = + headers.link && headers.link.match(/<([^>]+)>; rel="deprecation"/); + const deprecationLink = matches && matches.pop(); + log.warn( + `[@octokit/request] "${requestOptions.method} ${ + requestOptions.url + }" is deprecated. It is scheduled to be removed on ${headers.sunset}${ + deprecationLink ? `. See ${deprecationLink}` : "" + }` + ); + } + if (status === 204 || status === 205) { return; }