Closed
Description
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
}