Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ const { data: app } = await requestWithAuth(
<td>
Use an <code>AbortController</code> instance to cancel a request. In node you can only cancel streamed requests.
</td>
</tr>
<th align=left>
<code>options.request.log</code>
</th>
<th>
<code>object</code>
</th>
<td>
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>
Expand Down
18 changes: 18 additions & 0 deletions src/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
77 changes: 77 additions & 0 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<https://developer.github.com/changes/2020-01-21-moving-the-team-api-endpoints/>; rel="deprecation"; type="text/html", <https://api.github.com/organizations/3430433/team/4177875>; 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'
);
});
});
});