diff --git a/src/utils.ts b/src/utils.ts index e11d698..9107643 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,4 @@ +import { RestEndpointMethods } from "@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types.js"; import { RequestError } from "octokit"; function camelCaseToSentenceCase(str: string) { @@ -7,8 +8,11 @@ function camelCaseToSentenceCase(str: string) { ); } -// TODO: figure out how to type this to restrict the method to only -// those which are available on the `octokit.rest` instance +type OctokitMethod = { + [Key in keyof RestEndpointMethods]: { + [Subkey in keyof RestEndpointMethods[Key]]: RestEndpointMethods[Key][Subkey]; + }[keyof RestEndpointMethods[Key]]; +}[keyof RestEndpointMethods]; /** * Calls an Octokit method, logging any errors that occur. @@ -20,10 +24,9 @@ function camelCaseToSentenceCase(str: string) { * * @todo figure out how to type this to restrict the method to only those which are available on the `octokit.rest` instance */ -export async function safeOctokitRequest any>( - method: Method, - ...params: Parameters -): Promise>["data"]> { +export async function safeOctokitRequest< + Method extends OctokitMethod & { (...params: Parameters): any }, +>(method: Method, ...params: Parameters): Promise>["data"]> { try { const response = await method(...params); return response.data;