kubb-labs / kubb

The ultimate toolkit for working with APIs.
https://kubb.dev
MIT License
711 stars 62 forks source link

Support named tuple output in the swagger ts plugin #1089

Closed Downchuck closed 1 week ago

Downchuck commented 3 months ago

What is the problem this feature would solve?

Named tuples may be used for easier to read function parameters, or for other structured tuple data.

Example:

export type MyfuncParams = [id: number, my_interesting_field: number, optional_field?: string | undefined];

There are no utility functions in Typescript to support conversion from a struct:

export type MyfuncRequest= { id: number; my_interesting_field: number; optional_field?: string | undefined };

What is the feature you are proposing to solve the problem?

Named parameters are usable for functions:

function myFunc(...params: MyFuncParams) {}

Named parameters are useful for tuples in an IDE,:

const a: MyFuncParams = result.nextRow();
const test = a[0];

In Visual Studio Code, the test variable will hover with: (property) 0: number (id), as the type annotation carries through.

While this is a request for an extra option on the swagger ts plugin to just export alongside the generated Request structure for json-body style inputs.

What alternatives have you considered?

Request / Response pairs could be written out as a function:

type Myfunc = (id: number, my_interesting_field: number, optional_field?: string): MyfuncResponse
type MyfuncParams = Parameters<Myfunc>

OAS has Tuple support with a description field. It's a bit of a chore with min and max array lengths. Adding tuples to all input types could be a bit verbose, in trying to include query and path params.

This is intended as an optional feature, with the default of false.

Downchuck commented 3 months ago

This link is a great resource for actually getting aliases to show up when they sometimes don't in type hints in your IDE:

https://stackoverflow.com/questions/57683303/how-can-i-see-the-full-expanded-contract-of-a-typescript-type

This one in particular will expand tuples, especially useful for return type:

type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;

For:

type myfunc = (myarg: number) => [col1: string, cold2: number, col3: string][]

export interface example {
    myfunc(...args: Parameters<myfunc>): Promise<Expand<ReturnType<myfunc>>>
}

Again this is very VS Code hinting-oriented, but it was nice to see that interface work on an include. I'm in a JSDoc centric world so that's picked up in javascript file via:

/** @typedef {import("./api.d.ts").example} example */
/** @type {example} */
const exampleImpl = /** @type {const} */ {
    async myfunc(myarg) {
        return ...
    }
}

Some extra const in there to allow more type testing to flow through.

stijnvanhulle commented 1 month ago

Not sure what the use cases are here, do you want to have function that will be used by React-Query be typed as tuples or do you want to have tuples that are named(in our ts plugin)? Do you have an example(based on the Swagger/OpenApi Petstore) of what you want to see as result?

stijnvanhulle commented 1 week ago

This will be resolved with https://github.com/kubb-labs/kubb/issues/1195

Downchuck commented 1 week ago

I would like named/labeled tuples support in play; https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#labeled-tuple-elements

These are poorly supported by OpenAPI by using min and max element counts for an array of values.