1 import { getAppVersionHeaders, getUIDHeaders } from './fetch/headers';
3 const addClientSecret = ['core/v4/auth', 'core/v4/auth/info', 'auth/v4/sessions'];
6 * Create a function that can call the API with the correct parameters.
7 * @param {function} xhr - Fetch function
8 * @param {String} API_URL - The URL to the API
9 * @param {String} APP_VERSION - The app version
10 * @param {String} CLIENT_ID - The id of the client
11 * @param {String} [CLIENT_SECRET] - Optional client secret
12 * @param {Object} defaultHeaders - This help to override parameters in the default headers
22 defaultHeaders: otherDefaultHeaders = {},
24 let authHeaders = UID ? getUIDHeaders(UID) : undefined;
25 const appVersionHeaders = getAppVersionHeaders(clientID, APP_VERSION);
27 const defaultHeaders = {
28 accept: 'application/vnd.protonmail.v1+json',
30 ...otherDefaultHeaders,
33 const cb = ({ url, data, headers, ...rest }) => {
34 // Special case for the admin panel
35 const dataWithClientSecret =
36 CLIENT_SECRET && addClientSecret.includes(url) ? { ...data, ClientSecret: CLIENT_SECRET } : data;
38 url: /^https?:\/\//.test(url) ? url : `${API_URL}/${url}`,
39 data: dataWithClientSecret,
49 Object.defineProperties(cb, {
52 authHeaders = value ? getUIDHeaders(value) : undefined;