-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: `octokit.registerEndpoints()` has been removed. Instead of ```js await octokit.registerEndpoints({ misc: { getRoot: { method: "GET", url: "/" } } }); ``` do ```js octokit.misc.getRoot = octokit.request.defaults({ method: "GET", url: "/" }); ``` or ```js Object.assign(octokit.misc, { getRoot: octokit.request.defaults({ method: "GET", url: "/" }) }) ```
- Loading branch information
Showing
8 changed files
with
2,069 additions
and
6,924 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Octokit } from "@octokit/core"; | ||
import { | ||
EndpointOptions, | ||
RequestParameters, | ||
RequestMethod, | ||
Route, | ||
Url | ||
} from "@octokit/types"; | ||
import { EndpointsDefaultsAndDecorations, EndpointDecorations } from "./types"; | ||
import { RestEndpointMethods } from "./generated/rest-endpoint-methods-types"; | ||
|
||
type EndpointMethods = { | ||
[methodName: string]: typeof Octokit.prototype.request; | ||
}; | ||
|
||
export function endpointsToMethods( | ||
octokit: Octokit, | ||
endpointsMap: EndpointsDefaultsAndDecorations | ||
) { | ||
const newMethods = {} as { [key: string]: object }; | ||
|
||
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 (!newMethods[scope]) { | ||
newMethods[scope] = {}; | ||
} | ||
|
||
const scopeMethods = newMethods[scope] as EndpointMethods; | ||
|
||
if (decorations) { | ||
scopeMethods[methodName] = decorate( | ||
octokit, | ||
scope, | ||
methodName, | ||
endpointDefaults, | ||
decorations | ||
); | ||
continue; | ||
} | ||
|
||
scopeMethods[methodName] = octokit.request.defaults(endpointDefaults); | ||
} | ||
} | ||
|
||
return newMethods as RestEndpointMethods; | ||
} | ||
|
||
function decorate( | ||
octokit: Octokit, | ||
scope: string, | ||
methodName: string, | ||
defaults: EndpointOptions, | ||
decorations: EndpointDecorations | ||
) { | ||
const requestWithDefaults = octokit.request.defaults(defaults); | ||
|
||
function withDeprecations( | ||
...args: [Route, RequestParameters?] | [EndpointOptions] | ||
) { | ||
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(withDeprecations, requestWithDefaults); | ||
} |
Oops, something went wrong.