Skip to content

Commit

Permalink
Restrict safeOctokitRequest method parameter to Octokit methods
Browse files Browse the repository at this point in the history
  • Loading branch information
willsawyerrrr committed Jan 21, 2024
1 parent 01749cf commit 65fdecc
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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.
Expand All @@ -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<Method extends (...args: any[]) => any>(
method: Method,
...params: Parameters<Method>
): Promise<Awaited<ReturnType<Method>>["data"]> {
export async function safeOctokitRequest<
Method extends OctokitMethod & { (...params: Parameters<Method>): any },
>(method: Method, ...params: Parameters<Method>): Promise<Awaited<ReturnType<Method>>["data"]> {
try {
const response = await method(...params);
return response.data;
Expand Down

0 comments on commit 65fdecc

Please sign in to comment.