Skip to content

Commit

Permalink
feat: legacyRestEndpointMethods export (#367)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: No methods are set on `octokit.*` any more, only on `octokit.rest.*`. You can use `Octokit.plugin(legacyRestEndpointMethods)` to recover the previous behavior for the forseeable time, but eventually it will be deprecated and removed
  • Loading branch information
gr2m committed Mar 26, 2021
1 parent 6cb8fa8 commit 734d7a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import { endpointsToMethods } from "./endpoints-to-methods";
export function restEndpointMethods(octokit: Octokit): Api {
const api = endpointsToMethods(octokit, ENDPOINTS);
return {
...api,
rest: api,
};
}
restEndpointMethods.VERSION = VERSION;

export function legacyRestEndpointMethods(octokit: Octokit): Api["rest"] & Api {
const api = endpointsToMethods(octokit, ENDPOINTS);
return {
...api,
rest: api,
};
}
legacyRestEndpointMethods.VERSION = VERSION;
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { Route, RequestParameters } from "@octokit/types";

import { RestEndpointMethods } from "./generated/method-types";

export type Api = RestEndpointMethods & {
rest: RestEndpointMethods;
};
export type Api = { rest: RestEndpointMethods };

export type EndpointDecorations = {
mapToData?: string;
Expand Down
20 changes: 12 additions & 8 deletions test/rest-endpoint-methods.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetchMock from "fetch-mock";
import { Octokit } from "@octokit/core";

import { restEndpointMethods } from "../src";
import { restEndpointMethods, legacyRestEndpointMethods } from "../src";

describe("REST API endpoint methods", () => {
it("README example", async () => {
Expand Down Expand Up @@ -179,11 +179,11 @@ describe("REST API endpoint methods", () => {
return octokit.rest.apps.listInstallations();
});

// besides setting `octokit.rest.*`, the plugin is also setting the same methods
// directly on `octokit.*` for legacy reasons. We will deprecate the `octokit.*`
// methods in future, but for now we just make sure they are set
// besides setting `octokit.rest.*`, the plugin exports legacyRestEndpointMethods
// which is also setting the same methods directly on `octokit.*` for legacy reasons.
// We will deprecate the `octokit.*` methods in future, but for now we just make sure they are set

it("octokit.repos.createForAuthenticatedUser()", async () => {
it("legacyRestEndpointMethods", async () => {
const mock = fetchMock.sandbox().post(
"path:/user/repos",
{ id: 1 },
Expand All @@ -194,7 +194,7 @@ describe("REST API endpoint methods", () => {
}
);

const MyOctokit = Octokit.plugin(restEndpointMethods);
const MyOctokit = Octokit.plugin(legacyRestEndpointMethods);
const octokit = new MyOctokit({
auth: "secret123",
request: {
Expand All @@ -203,10 +203,14 @@ describe("REST API endpoint methods", () => {
});

// See https://developer.github.com/v3/repos/#create
const { data } = await octokit.repos.createForAuthenticatedUser({
const response1 = await octokit.repos.createForAuthenticatedUser({
name: "my-new-repo",
});
const response2 = await octokit.rest.repos.createForAuthenticatedUser({
name: "my-new-repo",
});

expect(data.id).toStrictEqual(1);
expect(response1.data.id).toStrictEqual(1);
expect(response2.data.id).toStrictEqual(1);
});
});

0 comments on commit 734d7a3

Please sign in to comment.