Static & Private Class Fields
What security benefit does this implementation provide?
class APIClient {
static #instance;
#token;
#baseURL;
constructor(config) {
if (APIClient.#instance) {
return APIClient.#instance;
}
this.#baseURL = config.baseURL;
this.#token = config.token;
APIClient.#instance = this;
}
async #request(endpoint, options) {
const response = await fetch(`${this.#baseURL}${endpoint}`, {
...options,
headers: {
'Authorization': `Bearer ${this.#token}`,
...options?.headers
}
});
return response.json();
}
}