Skip to content

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

Open
@ferretwithaberet

Description

@ferretwithaberet

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions