-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathendpoints-to-methods.ts
177 lines (157 loc) · 5.3 KB
/
endpoints-to-methods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { Octokit } from "@octokit/core";
import type { EndpointOptions, RequestParameters, Route } from "@octokit/types";
import ENDPOINTS from "./generated/endpoints.js";
import type { RestEndpointMethods } from "./generated/method-types.js";
import type { EndpointDecorations } from "./types.js";
// The following code was refactored in: https://github.com/octokit/plugin-rest-endpoint-methods.js/pull/622
// to optimise the runtime performance of Octokit initialization.
//
// This optimization involves two key changes:
// 1. Pre-Computation: The endpoint methods are pre-computed once at module load
// time instead of each invocation of `endpointsToMethods()`.
// 2. Lazy initialization and caching: We use a Proxy for each scope to only
// initialize methods that are actually called. This reduces runtime overhead
// as the initialization involves deep merging of objects. The initialized
// methods are then cached for future use.
const endpointMethodsMap = 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 (!endpointMethodsMap.has(scope)) {
endpointMethodsMap.set(scope, new Map());
}
endpointMethodsMap.get(scope).set(methodName, {
scope,
methodName,
endpointDefaults,
decorations,
});
}
}
type ProxyTarget = {
octokit: Octokit;
scope: string;
cache: Record<string, (...args: any[]) => any>;
};
const handler = {
has({ scope }: ProxyTarget, methodName: string) {
return endpointMethodsMap.get(scope).has(methodName);
},
getOwnPropertyDescriptor(target: ProxyTarget, methodName: string) {
return {
value: this.get(target, methodName), // ensures method is in the cache
configurable: true,
writable: true,
enumerable: true,
};
},
defineProperty(
target: ProxyTarget,
methodName: string,
descriptor: PropertyDescriptor,
) {
Object.defineProperty(target.cache, methodName, descriptor);
return true;
},
deleteProperty(target: ProxyTarget, methodName: string) {
delete target.cache[methodName];
return true;
},
ownKeys({ scope }: ProxyTarget) {
return [...endpointMethodsMap.get(scope).keys()];
},
set(target: ProxyTarget, methodName: string, value: any) {
return (target.cache[methodName] = value);
},
get({ octokit, scope, cache }: ProxyTarget, methodName: string) {
if (cache[methodName]) {
return cache[methodName];
}
const method = endpointMethodsMap.get(scope).get(methodName);
if (!method) {
return undefined;
}
const { endpointDefaults, decorations } = method;
if (decorations) {
cache[methodName] = decorate(
octokit,
scope,
methodName,
endpointDefaults,
decorations,
);
} else {
cache[methodName] = octokit.request.defaults(endpointDefaults);
}
return cache[methodName];
},
};
export function endpointsToMethods(octokit: Octokit): RestEndpointMethods {
const newMethods = {} as { [key: string]: object };
for (const scope of endpointMethodsMap.keys()) {
newMethods[scope] = new Proxy({ octokit, scope, cache: {} }, handler);
}
return newMethods as RestEndpointMethods;
}
function decorate(
octokit: Octokit,
scope: string,
methodName: string,
defaults: EndpointOptions,
decorations: EndpointDecorations,
) {
const requestWithDefaults = octokit.request.defaults(defaults);
/* istanbul ignore next */
function withDecorations(
...args: [Route, RequestParameters?] | [EndpointOptions]
) {
// @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
let options = requestWithDefaults.endpoint.merge(...args);
// There are currently no other decorations than `.mapToData`
if (decorations.mapToData) {
options = Object.assign({}, options, {
data: options[decorations.mapToData],
[decorations.mapToData]: undefined,
});
return requestWithDefaults(options);
}
if (decorations.renamed) {
const [newScope, newMethodName] = decorations.renamed;
octokit.log.warn(
`octokit.${scope}.${methodName}() has been renamed to octokit.${newScope}.${newMethodName}()`,
);
}
if (decorations.deprecated) {
octokit.log.warn(decorations.deprecated);
}
if (decorations.renamedParameters) {
// @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
const options = requestWithDefaults.endpoint.merge(...args);
for (const [name, alias] of Object.entries(
decorations.renamedParameters,
)) {
if (name in options) {
octokit.log.warn(
`"${name}" parameter is deprecated for "octokit.${scope}.${methodName}()". Use "${alias}" instead`,
);
if (!(alias in options)) {
options[alias] = options[name];
}
delete options[name];
}
}
return requestWithDefaults(options);
}
// @ts-ignore https://github.com/microsoft/TypeScript/issues/25488
return requestWithDefaults(...args);
}
return Object.assign(withDecorations, requestWithDefaults);
}