Skip to content

[typescript-axios] accept promises for bearer tokens #7105

@asterikx

Description

@asterikx

Is your feature request related to a problem? Please describe.

I want to use Bearer authentication with AWS Amliply. Amplify returns tokens as promises (as it checks whether the current token has expired and attempts to refresh it).

const bearerToken = (await Auth.currentSession()).getIdToken().getJwtToken();

However, the generated API only accepts plain strings or functions returning plain strings for accessToken.

export interface ConfigurationParameters {
    apiKey?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
    username?: string;
    password?: string;
    accessToken?: string | ((name?: string, scopes?: string[]) => string);
    basePath?: string;
    baseOptions?: any;
}

Describe the solution you'd like

Just as for apiKey, extend the signature of accessToken to accept promises:

accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);

In the generated code, only the keyword await must be added in two places

// authentication ApiKeyAuth required
if (configuration && configuration.apiKey) {
  const localVarApiKeyValue = typeof configuration.apiKey === 'function'
      ? await configuration.apiKey("X-API-Key")
      : await configuration.apiKey;
  localVarHeaderParameter["X-API-Key"] = localVarApiKeyValue;
}

// authentication BearerAuth required
// http bearer authentication required
if (configuration && configuration.accessToken) {
  const accessToken = typeof configuration.accessToken === 'function'
      ? configuration.accessToken()                                      // <-- add "await" here
      : configuration.accessToken;                                       // <-- add "await" here
  localVarHeaderParameter["Authorization"] = "Bearer " + accessToken;
}

Describe alternatives you've considered

I'm not aware of any alternative solution

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions