Skip to content

Async securityWorker #82

@alievrenkut

Description

@alievrenkut

Hi, Could you change template code so that securityWorker will be async function. I need this so that I can check my JWT token is expired or not and update it if it is expired. I can not do this with current securityWorker.

I suggest you to change current template to below


type ApiConfig<SecurityDataType> = {
  baseUrl?: string;
  baseApiParams?: RequestParams;
  securityWorker?: (securityData: SecurityDataType) => Promise<RequestParams>;
};
  
  public request = async <T = any, E = any>(
    path: string,
    method: string,
    { secure, ...params }: RequestParams = {},
    body?: any,
    bodyType?: BodyType,
    secureByDefault?: boolean,
  ): Promise<T> =>{
    const securityRequestParams = (secureByDefault || secure) && (await this.securityWorker(this.securityData));
    return fetch(`${this.baseUrl}${path}`, {
      // @ts-ignore
      ...this.mergeRequestOptions(params, securityRequestParams),
      method,
      body: body ? this.bodyFormatters[bodyType || BodyType.Json](body) : null,
    }).then(async (response) => {
      const data = await this.safeParseResponse<T, E>(response);
      if (!response.ok) throw data;
      return data;
    });
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions