openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

OpenAPI.ts `Resolver` typing is annoying, should use a `Maybe` type

Open ferretwithaberet opened this issue 2 years ago • 3 comments

Currently, the Resolver is defined as

type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;

and keys like TOKEN are defined like:

export type OpenAPIConfig = {
  TOKEN?: string | Resolver<string> | undefined;
};

this is annoying in cases where you want to fetch the token from somewhere else, but it might be undefined as maybe it was not fetched yet or the user is not logged in.

Example

OpenAPI.TOKEN = () => {
  const user = getKeycloakUser();

  if (!user) return;

  return user.access_token;
};

I propose the following typing changes:

Resolver

type Maybe<T> = T | Promise<T>;
type Resolver<T> = (options: ApiRequestOptions) => Maybe<T>;

Keys like TOKEN

export type OpenAPIConfig = {
  TOKEN?: string | Resolver<string | undefined>;
};

Another note would be that, at the moment keys like TOKEN contain ? which marks them as optional, so | undefined is not required. I do not know if this is intentional as to make the typing more clear thought.

ferretwithaberet avatar Jan 26 '24 14:01 ferretwithaberet

I'm +1 on this, I've similar pb with building HEADERS where using a regular function is easier.

sthenault avatar May 06 '24 15:05 sthenault

@sthenault if you're interested in continuing this thread, please migrate to https://github.com/hey-api/openapi-ts and open an issue there

mrlubos avatar May 06 '24 15:05 mrlubos

noticed, thx

sthenault avatar May 07 '24 08:05 sthenault