Skip to content

Non-nullish data field #2109

Open
Open
@musjj

Description

@musjj

Description

I'm using openapi-fetch with ky

import ky from "ky";
import createFetchClient from "openapi-fetch";

const fetchClient = createFetchClient<paths>({
  baseUrl: "https://example.com",
  fetch(input) {
    return ky(input);
  },
});

One of the nice things about ky is that it raises an exception on non-2XX responses. This means that if a fetch call succeeds, response data is always guaranteed.

But the type system doesn't know this:

const response = await fetchClient.GET("/foo/{id}", {
  params: { path: { id: "foo" } },
});
response.data?.id // the types thinks that data may not be present

Proposal

I'm not sure what's the best way to go about it, but maybe there could be an option?

import ky from "ky";
import createFetchClient from "openapi-fetch";

const fetchClient = createFetchClient<paths>({
  baseUrl: "https://example.com",
  nonNullData: true, // <-- `data` will always be non-null
  fetch(input) {
    return ky(input);
  },
});

But it does feel a bit footgun-ish. Maybe we could also have another mini-library, something like openapi-ky as a wrapper.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions