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

Suggestion: improve readability of data type #2022

Open
1 task done
OliverJAsh opened this issue Nov 27, 2024 · 0 comments
Open
1 task done

Suggestion: improve readability of data type #2022

OliverJAsh opened this issue Nov 27, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library

Comments

@OliverJAsh
Copy link

Description

When inspecting data returned by openapi-fetch, the type can be quite difficult to read, and the appearance seems to vary. For example:

index.ts:

import createClient from "openapi-fetch";
import type { paths } from "./schema";

const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });

const x = await client.GET("/photos/random", {
  params: { query: { count: 10 } },
});
x.data;
/*
SuccessResponse<{
    200: {
        headers: {
            [name: string]: unknown;
        };
        content: {
            "application/json": components["schemas"]["Photo"] | components["schemas"]["Photo"][];
        };
    };
}, `${string}/${string}`> | undefined
*/

const y = await client.GET("/photos", {});
y.data;
/*
{
    updated_at: string;
    id: string;
}[] | undefined
 */

Reproduction

In case it helps for testing, here's the schema JSON I used.

Expected result

In both of these examples I think it would be better if the data simply appeared as Photo, as per the component name in the schema.

I believe this would be possible if openapi-typescript generated individual interfaces for each component, rather than one interface with components as nested objects. When TypeScript prints the type e.g. inside hover tooltips and error messages, TypeScript will preserve the name of interfaces rather than inlining their contents.

Furthermore, this change is required to force TypeScript to compute the type in the first example:

+type Prettify<T> = { [K in keyof T]: T[K]; } & {};
 
 export type FetchResponse<T extends Record<string | number, any>, Options, Media extends MediaType> =
   | {
-      data: ParseAsResponse<SuccessResponse<ResponseObjectMap<T>, Media>, Options>;
+      data: Prettify<ParseAsResponse<SuccessResponse<ResponseObjectMap<T>, Media>, Options>>;

With I tested both of those changes, this is what I was able to achieve:

const x = await client.GET("/photos/random", {
  params: { query: { count: 10 } },
});
x.data;
/*
Photo[] | {
    id: string;
} | undefined
*/

const y = await client.GET("/photos", {});
y.data;
/*
Photo[] | undefined
 */

(I still need to figure out why Photo is still being inlined in the first example.)

Checklist

@OliverJAsh OliverJAsh added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Nov 27, 2024
@kerwanp kerwanp added enhancement New feature or request and removed bug Something isn't working labels Nov 27, 2024
@drwpow drwpow self-assigned this Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

3 participants