2019-10-18 16:20:01 +00:00
|
|
|
import { Platform } from 'react-native';
|
|
|
|
import DeviceInfo from 'react-native-device-info';
|
2020-02-20 12:58:13 +00:00
|
|
|
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
|
2020-05-05 13:11:28 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2019-10-18 16:20:01 +00:00
|
|
|
|
2020-01-28 13:24:29 +00:00
|
|
|
// this form is required by Rocket.Chat's parser in "app/statistics/server/lib/UAParserCustom.js"
|
2020-02-20 12:58:13 +00:00
|
|
|
export const headers = {
|
|
|
|
'User-Agent': `RC Mobile; ${ Platform.OS } ${ DeviceInfo.getSystemVersion() }; v${ DeviceInfo.getVersion() } (${ DeviceInfo.getBuildNumber() })`
|
|
|
|
};
|
|
|
|
|
|
|
|
let _basicAuth;
|
|
|
|
export const setBasicAuth = (basicAuth) => {
|
|
|
|
_basicAuth = basicAuth;
|
|
|
|
if (basicAuth) {
|
2020-04-01 20:32:24 +00:00
|
|
|
RocketChatSettings.customHeaders = { ...headers, Authorization: `Basic ${ _basicAuth }` };
|
2020-02-20 12:58:13 +00:00
|
|
|
} else {
|
|
|
|
RocketChatSettings.customHeaders = headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
export const BASIC_AUTH_KEY = 'BASIC_AUTH_KEY';
|
|
|
|
|
|
|
|
RocketChatSettings.customHeaders = headers;
|
2019-10-18 16:20:01 +00:00
|
|
|
|
|
|
|
export default (url, options = {}) => {
|
2020-02-20 12:58:13 +00:00
|
|
|
let customOptions = { ...options, headers: RocketChatSettings.customHeaders };
|
2019-10-18 16:20:01 +00:00
|
|
|
if (options && options.headers) {
|
2020-02-20 12:58:13 +00:00
|
|
|
customOptions = { ...customOptions, headers: { ...options.headers, ...customOptions.headers } };
|
2019-10-18 16:20:01 +00:00
|
|
|
}
|
2020-05-05 13:11:28 +00:00
|
|
|
if (RocketChat.controller) {
|
|
|
|
const { signal } = RocketChat.controller;
|
|
|
|
customOptions = { ...customOptions, signal };
|
|
|
|
}
|
2019-10-18 16:20:01 +00:00
|
|
|
return fetch(url, customOptions);
|
|
|
|
};
|