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
  • #10065
Closed
Open
Issue created Jul 31, 2021 by Administrator@rootContributor

[BUG] Change configuration parameters for typescript-fetch

Created by: kpldvnpne

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

Yes. Previously, we used to be able to specify how username and password are fetched during API calls. The ConfigurationParameters was defined as:

interface ConfigurationParameters {
  // other fields
  username?: string | () => string;
  password?: string | () => string;
}

In this previous interface, we used to be able to specify configuration as follows:

const configuration = new Configuration({
  username: () => getUsernameFromLocalStorage(),
  password: () => getPasswordFromLocalStorage(),
});

But in the new versions, the ConfigurationParameters interface has been changed, where it only accepts a string for username and password:

interface ConfigurationParameters {
  // other fields
  username?: string;
  password?: string;
}

Link to the generated code

This has made it harder to login and logout the users as login means we specify new username and password and logout means we delete any username and password.

Describe the solution you'd like

A solution would be to revert back to the old ConfigurationParameters interface.

Another solution would be to generate Configuration like it is on angular's generated code:

export interface ConfigurationParameters {
    /**
     *  @deprecated Since 5.0. Use credentials instead
     */
    apiKeys?: {[ key: string ]: string};
    username?: string;
    password?: string;
    /**
     *  @deprecated Since 5.0. Use credentials instead
     */
    accessToken?: string | (() => string);
    basePath?: string;
    withCredentials?: boolean;
    encoder?: HttpParameterCodec;
    /**
     * The keys are the names in the securitySchemes section of the OpenAPI
     * document. They should map to the value used for authentication
     * minus any standard prefixes such as 'Basic' or 'Bearer'.
     */
    credentials?: {[ key: string ]: string | (() => string | undefined)};
}

Here, username and password have been deprecated and use only string, but credentials can use functions, which allows us to specify username and password when api is being called.

Describe alternatives you've considered

I have been using this hack to get around this problem:

const httpBasicInterceptor = new (class implements Middleware {
  async pre(context: RequestContext): Promise<FetchParams | void> {
    const username = AuthStorage.getUsername();
    const password = AuthStorage.getPassword();

    const oldHeaders = context.init.headers;
    let newHeaders;

    const AUTHORIZATION_KEY = 'Authorization';
    const basicAuth = 'Basic ' + btoa(`${username}:${password}`);

    if (oldHeaders !== undefined) {
      if (typeof oldHeaders == 'object') {
        newHeaders = {
          ...oldHeaders,
          [AUTHORIZATION_KEY]: basicAuth,
        };
      } else if (Array.isArray(oldHeaders)) {
        newHeaders = oldHeaders as string[][];
        newHeaders.push([AUTHORIZATION_KEY, basicAuth]);
      } else {
        newHeaders = oldHeaders as Headers;
        newHeaders.append(AUTHORIZATION_KEY, basicAuth);
      }
    }

    return {
      url: context.url,
      init: {
        ...context.init,
        headers: newHeaders,
      },
    };
  }
})();
const privateConfigParameters: ConfigurationParameters = {
  ...basicConfigParameters,
  middleware: [httpBasicInterceptor],
  username: '',
  password: '',
};
const privateConfig = new Configuration(privateConfigParameters);

But this is complex and harder to read. Also, there might be some circumstances where this solution might fail to work.

Additional context

No other context required to understand this problem.

Assignee
Assign to
Time tracking