Skip to content

[Question] What do you think about change Api instance constructor params ? #155

Closed
@js2me

Description

@js2me

I guess it is not a good solution:

HttpClient.ts

  constructor(apiConfig: ApiConfig<SecurityDataType> = {}) {
    Object.assign(this, apiConfig);
  }

For example, with --modular option the field securityWorker doesn't make any sense.
Because I should send securityWorker for each modular api instance

const API_CONFIG = {
  baseUrl: `${process.env.NODE_ENV === "production" ? location.origin : process.env.API_URL}/api/v1`,
  baseApiParams: {
    headers: {
      "Content-Type": ContentType.Json,
    },
  },
  securityWorker: (token) =>
    token
      ? {
          headers: {
            Authorization: token,
          },
        }
      : {},
};

export const api = {
  auth: new Auth(API_CONFIG),
  jobs: new Jobs(API_CONFIG),
};

I think would be better if I will send HttpClient instance to each Api constructor. 🤔

const httpClient = new HttpClient<string>({
  baseUrl: `${process.env.NODE_ENV === "production" ? location.origin : process.env.API_URL}/api/v1`,
  baseApiParams: {
    headers: {
      "Content-Type": ContentType.Json,
    },
  },
  securityWorker: (token) =>
    token
      ? {
          headers: {
            Authorization: token,
          },
        }
      : {},
});

export const api = {
  http: httpClient,
  auth: new Auth(httpClient),
  jobs: new Jobs(httpClient),
};

and generated Api:

export class Auth<SecurityDataType = unknown> {
  
  constructor(private http: HttpClient<SecurityDataType>) {}
  
  // methods
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgood first issueGood for newcomersquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions