flamenco/web/app/src/api-client.js
Sybren A. Stüvel 1add6bfc8a Webapp: avoid browser JS errors about forbidden 'User-Agent' header
Brave (and maybe other browseres) refuse to set the 'User-Agent' header
in XMLHTTPRequests, and are vocal about this in the debug log. Since the
OpenAPI code generator always outputs a custom 'User-Agent' header, I've
added some JS code to strip that off when constructing an API client.
2023-02-21 11:08:48 +01:00

40 lines
1.1 KiB
JavaScript

import { ApiClient } from "@/manager-api";
import { CountingApiClient } from "@/stores/api-query-count";
import { api as apiURL } from '@/urls'
/**
* Scrub the custom User-Agent header from the API client, for those webbrowsers
* who do not want to have it set.
*
* It's actually scrubbed for all webbrowsers, as those privacy-first
* webbrowsers also make it hard to fingerprint which browser you're using (for
* good reason).
*
* @param {ApiClient} apiClient
*/
export function scrubAPIClient(apiClient) {
delete apiClient.defaultHeaders['User-Agent'];
}
/**
* @returns {ApiClient} Bare API client that is not connected to the UI in any way.
*/
export function newBareAPIClient() {
const apiClient = new ApiClient(apiURL());
scrubAPIClient(apiClient);
return apiClient;
}
let apiClient = null;
/**
* @returns {ApiClient} API client that updates the UI to show long-running queries.
*/
export function getAPIClient() {
if (apiClient == null) {
apiClient = new CountingApiClient(apiURL());
scrubAPIClient(apiClient);
}
return apiClient;
}