I am having difficulty in finding a good way to generate types for Octokit rest API calls. It seems like my requirements might be a bit out of the norm.
Here's what I am trying to do. I am creating a custom action in typescript and I have a class as follows.
import {debug} from '@actions/core'
import {getOctokit} from '@actions/github'
import type {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
const octokit = getOctokit('')
type Pull = GetResponseDataTypeFromEndpointMethod<typeof octokit.rest.pulls.get>
export class GitHubService {
private readonly octokit
constructor(gitHubToken: string) {
this.octokit = getOctokit(gitHubToken)
}
async handlePullRequestChange(
repositoryOwner: string,
repositoryName: string,
pullRequestNumber: number
): Promise<Pull> {
const pull = await this.octokit.paginate(this.octokit.rest.pulls.get, {
owner: repositoryOwner,
repo: repositoryName,
pull_number: pullRequestNumber
})
debug(`Pull request ${pullRequestNumber} includes the following body: ${JSON.stringify(pull)}`)
return pull
}
}
I am creating the GitHubService
class from a main.ts
and as you can see, I am passing the token in the constructor and which is creating the octokit
object required for calling the APIs. Then I want to call handlePullRequestChange
and I want this function to return the response from the octokit api call with proper typing.
I am having trouble generating the types properly. The above codebase is something that works statically but will probably break as getOctokit('')
will fail since I am passing an empty token.
The other way could be to create the type
inside the function like
async handlePullRequestChange(
repositoryOwner: string,
repositoryName: string,
pullRequestNumber: number
): Promise<Pull> {
type Pull = GetResponseDataTypeFromEndpointMethod<typeof this.octokit.rest.pulls.get>
const pull: Pull = await this.octokit.paginate(this.octokit.rest.pulls.get, {
owner: repositoryOwner,
repo: repositoryName,
pull_number: pullRequestNumber
})
debug(`Pull request ${pullRequestNumber} includes the following body: ${JSON.stringify(pull)}`)
return pull
}
But this won't compile as the the function's return type of Promise<Pull>
doesn't have access to Pull
yet. I know I can just not give the return type explicitly but I want to maintain my linting setup and I am maintaining explicit return types on functions.
What can I do in here?