Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #7105
Closed
Open
Issue created Aug 01, 2020 by Administrator@rootContributor

[typescript-axios] accept promises for bearer tokens

Created by: 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

Assignee
Assign to
Time tracking