octokit.js
octokit.js copied to clipboard
[FEAT]: Export friendly types for use in TS
Describe the need
Perhaps I'm missing something, but If I want to write a function that takes in a result of an API call, I can't currently type that parameter. For example:
const workflow = await octokit.rest.actions
.listWorkflowRun(...)
.then(r => r.data.workflows[0]);
doSomethingWithWorkflow(workflow);
function doSomethingWithWorkflow(workflow: ???) {
// do something with the workflow
}
How can I type the workflow
parameter? Looking in the types, I could do:
workflow: RestEndpointMethodTypes["actions"]["listWorkflowRuns"]["response"]["data"]["workflows"][0]
But RestEndpointMethodTypes
is not exported. And it'd be far better to have a type alias e.g. workflow: typeof ListWorkflowRunsResponsePayload[0]
.
For now, I can get around this by indicating the things I want from the workflow:
workflow: { id: number, name: string }
But I assume I'm missing something here...
SDK Version
3.1.2
API Version
No response
Relevant log output
No response
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labled with Status: Up for grabs
. You & others like you are the reason all of this works! So thank you & happy coding! 🚀
The types and methods are automatically generated from the OpenAPI spec.
You can already get the types for a specific endpoint via @octokit/types
import { Endpoints } from "@octokit/types";
type ListWorkflowRunResponsePayload = Endpoints["GET /repos/{owner}/{repo}/actions/runs"]["response"];
function doSomethingWithWorkflow(workflow: ListWorkflowRunResponsePayload) {
//Do something
}
Thanks, I didn't know about that package. It'd still be nice to have concise wrapper types, as in your example, but this will work for now.
Or perhaps RestEndpointMethodTypes
should be exported so we can do:
type Workflow: RestEndpointMethodTypes[ "actions" ][ "listWorkflowRuns" ][ "response" ][ "data" ][ "workflows" ][ 0 ];
Instead of:
type Workflow = Endpoints[ "GET /repos/{owner}/{repo}/actions/workflows" ][ "response" ][ "data" ][ "workflows" ][ 0 ];
The types and methods are automatically generated from the OpenAPI spec.
You can already get the types for a specific endpoint via
@octokit/types
import { Endpoints } from "@octokit/types"; type ListWorkflowRunResponsePayload = Endpoints["GET /repos/{owner}/{repo}/actions/runs"]["response"]; function doSomethingWithWorkflow(workflow: ListWorkflowRunResponsePayload) { //Do something }
How do we do this in version 4?
It's the same. The endpoints have changed but not the method to get the types
It's the same. The endpoints have changed but not the method to get the types
I am not sure what the cause of the problem is, but I'm experiencing an issue here.
@octokit/types
is no longer available for me after 4.0.0, I get this error: Cannot find module '@octokit' or its corresponding type declarations.
Some relevant information, as it seems the types
directory is missing in 4.0.0:
The node_modules/@oktokit
contents on 3.1.0:
$ ls
app/ graphql/ plugin-throttling/
auth-app/ oauth-app/ request/
auth-oauth-app/ oauth-authorization-url/ request-error/
auth-oauth-device/ oauth-methods/ types/
auth-oauth-user/ openapi-types/ webhooks/
auth-token/ plugin-paginate-graphql/ webhooks-methods/
auth-unauthenticated/ plugin-paginate-rest/ webhooks-types/
core/ plugin-rest-endpoint-methods/
endpoint/ plugin-retry/
The node_modules/@octokit
contents after 4.0.0:
$ ls
app/ auth-oauth-device/ oauth-app/ openapi-webhooks-types/
auth-app/ auth-oauth-user/ oauth-authorization-url/ webhooks/
auth-oauth-app/ auth-unauthenticated/ oauth-methods/ webhooks-methods/
My package.json entries between upgrading to 4.0.0 (most lines have been removed for the sake of brevity):
Before:
"type": "module",
"dependencies": {
"octokit": "^3.1.0",
},
After:
"type": "module",
"dependencies": {
"octokit": "^4.0.0",
},
I can assure you that the types are still there, https://github.com/octokit/octokit.js/blob/0f148204943280e232ce1c12293194d5675d5ea3/package.json#L33
Have you updated your tsconfig.json
as mentioned in the Usage section of the README.md?
I can assure you that the types are still there,
https://github.com/octokit/octokit.js/blob/0f148204943280e232ce1c12293194d5675d5ea3/package.json#L33
Have you updated your
tsconfig.json
as mentioned in the Usage section of the README.md?
I appreciate the quick replies!
Yes, I have the correct configuration in tsconfig.json
.
I've made a small reproduction showing this issue: https://codesandbox.io/p/devbox/4wls6z?file=%2Fsrc%2Findex.ts%3A13%2C1
Let's make another issue, what you are experiencing is different than this one.
There is always this way to do it as well
import { Api } from "@octokit/plugin-rest-endpoint-methods";
type workflow = Awaited<ReturnType<Api["rest"]["actions"]["listRepoWorkflows"]>>['data']['workflows'][0];
or even
import type { Octokit } from "octokit";
type workflow = Awaited<ReturnType<InstanceType<typeof Octokit>["rest"]["actions"]["listRepoWorkflows"]>>['data']['workflows'][0];