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

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

Closed
asterikx opened this issue Aug 1, 2020 · 2 comments · Fixed by #7132
Closed

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

asterikx opened this issue Aug 1, 2020 · 2 comments · Fixed by #7132

Comments

@asterikx
Copy link
Contributor

asterikx commented Aug 1, 2020

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

@auto-labeler
Copy link

auto-labeler bot commented Aug 1, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@macjohnny
Copy link
Member

@asterikx would you like to file a PR to implement this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants