Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT]: Export friendly types for use in TS #2598

Open
1 task done
JoshMcCullough opened this issue Dec 21, 2023 · 10 comments
Open
1 task done

[FEAT]: Export friendly types for use in TS #2598

JoshMcCullough opened this issue Dec 21, 2023 · 10 comments
Labels
Type: Feature New feature or request

Comments

@JoshMcCullough
Copy link

JoshMcCullough commented Dec 21, 2023

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

  • I agree to follow this project's Code of Conduct
@JoshMcCullough JoshMcCullough added Status: Triage This is being looked at and prioritized Type: Feature New feature or request labels Dec 21, 2023
Copy link

👋 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! 🚀

@wolfy1339
Copy link
Member

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
}

@JoshMcCullough
Copy link
Author

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 ];

@nickfloyd nickfloyd moved this from 🆕 Triage to 🔥 Backlog in 🧰 Octokit Active Jan 4, 2024
@kfcampbell kfcampbell removed the Status: Triage This is being looked at and prioritized label Jan 5, 2024
@mjfwebb

This comment was marked as off-topic.

@wolfy1339

This comment was marked as off-topic.

@mjfwebb

This comment was marked as off-topic.

@wolfy1339

This comment was marked as off-topic.

@mjfwebb

This comment was marked as off-topic.

@wolfy1339

This comment was marked as off-topic.

@wolfy1339
Copy link
Member

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];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
Status: 🔥 Backlog
Development

No branches or pull requests

4 participants