Skip to content

feat: legacyRestEndpointMethods export #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2021
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: 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);
});
});