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';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-16 19:07:49 +00:00
|
|
|
export type TMethods = 'POST' | 'GET' | 'DELETE' | 'PUT' | 'post' | 'get' | 'delete' | 'put';
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
interface CustomHeaders {
|
2022-03-16 19:07:49 +00:00
|
|
|
'User-Agent'?: string;
|
2022-01-12 12:54:04 +00:00
|
|
|
Authorization?: string;
|
2022-03-16 19:07:49 +00:00
|
|
|
'Content-Type'?: string;
|
|
|
|
'X-Auth-Token'?: string;
|
|
|
|
'X-User-Id'?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IOptions {
|
|
|
|
headers?: CustomHeaders;
|
|
|
|
signal?: AbortSignal;
|
|
|
|
method?: TMethods;
|
|
|
|
body?: any;
|
2022-01-12 12:54:04 +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"
|
2022-01-12 12:54:04 +00:00
|
|
|
export const headers: CustomHeaders = {
|
2021-09-13 20:41:05 +00:00
|
|
|
'User-Agent': `RC Mobile; ${
|
|
|
|
Platform.OS
|
|
|
|
} ${DeviceInfo.getSystemVersion()}; v${DeviceInfo.getVersion()} (${DeviceInfo.getBuildNumber()})`
|
2020-02-20 12:58:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let _basicAuth;
|
2023-10-19 13:38:57 +00:00
|
|
|
export const setBasicAuth = (basicAuth: string | null): void => {
|
2020-02-20 12:58:13 +00:00
|
|
|
_basicAuth = basicAuth;
|
|
|
|
if (basicAuth) {
|
2021-09-13 20:41:05 +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
|
|
|
|
2022-03-16 19:07:49 +00:00
|
|
|
export default (url: string, options: IOptions = {}): Promise<Response> => {
|
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
|
|
|
}
|
2022-05-13 14:52:37 +00:00
|
|
|
// TODO: Check if this really works and if anyone else has complained about this problem.
|
|
|
|
// if (RocketChat.controller) {
|
|
|
|
// // @ts-ignore
|
|
|
|
// const { signal } = RocketChat.controller;
|
|
|
|
// customOptions = { ...customOptions, signal };
|
|
|
|
// }
|
2019-10-18 16:20:01 +00:00
|
|
|
return fetch(url, customOptions);
|
|
|
|
};
|