Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 59 additions & 39 deletions src/endpoints-to-methods.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,72 @@
import { Octokit } from "@octokit/core";
import {
EndpointOptions,
RequestParameters,
RequestMethod,
Route,
Url,
} from "@octokit/types";
import { EndpointsDefaultsAndDecorations, EndpointDecorations } from "./types";
import { EndpointOptions, RequestParameters, Route } from "@octokit/types";
import { EndpointDecorations } from "./types";
import { RestEndpointMethods } from "./generated/method-types";
import ENDPOINTS from "./generated/endpoints";

type EndpointMethods = {
[methodName: string]: typeof Octokit.prototype.request;
const enpointMethodsMap = new Map();
for (const [scope, endpoints] of Object.entries(ENDPOINTS)) {
for (const [methodName, endpoint] of Object.entries(endpoints)) {
const [route, defaults, decorations] = endpoint;
const [method, url] = route.split(/ /);
const endpointDefaults = Object.assign(
{
method,
url,
},
defaults
);

if (!enpointMethodsMap.has(scope)) {
enpointMethodsMap.set(scope, new Map());
}

enpointMethodsMap.get(scope).set(methodName, {
scope,
methodName,
endpointDefaults,
decorations,
});
}
}

type ProxyTarget = {
octokit: Octokit;
scope: string;
cache: Record<string, (...args: any[]) => any>;
};

export function endpointsToMethods(
octokit: Octokit,
endpointsMap: EndpointsDefaultsAndDecorations
) {
const newMethods = {} as { [key: string]: object };
const handler = {
get({ octokit, scope, cache }: ProxyTarget, methodName: string) {
if (cache[methodName]) {
return cache[methodName];
}

const { decorations, endpointDefaults } = enpointMethodsMap
.get(scope)
.get(methodName);

for (const [scope, endpoints] of Object.entries(endpointsMap)) {
for (const [methodName, endpoint] of Object.entries(endpoints)) {
const [route, defaults, decorations] = endpoint;
const [method, url] = route.split(/ /) as [RequestMethod, Url];
const endpointDefaults: EndpointOptions = Object.assign(
{ method, url },
defaults
if (decorations) {
cache[methodName] = decorate(
octokit,
scope,
methodName,
endpointDefaults,
decorations
);
} else {
cache[methodName] = octokit.request.defaults(endpointDefaults);
}

if (!newMethods[scope]) {
newMethods[scope] = {};
}
return cache[methodName];
},
};

const scopeMethods = newMethods[scope] as EndpointMethods;

if (decorations) {
scopeMethods[methodName] = decorate(
octokit,
scope,
methodName,
endpointDefaults,
decorations
);
continue;
}
export function endpointsToMethods(octokit: Octokit): RestEndpointMethods {
const newMethods = {} as { [key: string]: object };

scopeMethods[methodName] = octokit.request.defaults(endpointDefaults);
}
for (const scope of enpointMethodsMap.keys()) {
newMethods[scope] = new Proxy({ octokit, scope, cache: {} }, handler);
}

return newMethods as RestEndpointMethods;
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Octokit } from "@octokit/core";

import ENDPOINTS from "./generated/endpoints";
export { RestEndpointMethodTypes } from "./generated/parameters-and-response-types";
import { VERSION } from "./version";
import { Api } from "./types";
import { endpointsToMethods } from "./endpoints-to-methods";

export function restEndpointMethods(octokit: Octokit): Api {
const api = endpointsToMethods(octokit, ENDPOINTS);
const api = endpointsToMethods(octokit);
return {
rest: api,
};
}
restEndpointMethods.VERSION = VERSION;

export function legacyRestEndpointMethods(octokit: Octokit): Api["rest"] & Api {
const api = endpointsToMethods(octokit, ENDPOINTS);
const api = endpointsToMethods(octokit);
return {
...api,
rest: api,
Expand Down