This repository has been archived by the owner on Dec 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathConnectAction.ts
51 lines (44 loc) · 1.84 KB
/
ConnectAction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import ApiClient from 'jellyfin-apiclient/dist/apiclient';
import { getApiClient, setApiClient } from '../utilities/api-client';
import { connectSuccessful, connectFailed } from "../reducers/connectionStatus";
import { ThunkAction } from "redux-thunk";
import { RootState } from "../utilities/storage/store";
import { Action } from "redux";
function replaceAll(originalString: string, strReplace: string, strWith: string): string {
const reg = new RegExp(strReplace, "ig");
return originalString.replace(reg, strWith);
}
function normalizeAddress(serverAddress: string): string {
// attempt to correct bad input
serverAddress = serverAddress.trim();
if (serverAddress.toLowerCase().indexOf("http") !== 0) {
serverAddress = `http://${serverAddress}`;
}
// Seeing failures in iOS when protocol isn't lowercase
serverAddress = replaceAll(serverAddress, "Http:", "http:");
serverAddress = replaceAll(serverAddress, "Https:", "https:");
return serverAddress;
}
/**
* Connect to Jellyfin given a server address. A request to a public endpoint is attempted
* to verify connection.
*
* @param serverAddress The server address to connect to
* @returns true if connection success, false otherwise
*/
export default function connectToServer(
serverAddress: string
): ThunkAction<Promise<boolean>, RootState, null, Action<string>> {
return async (dispatch) => {
const normalizedAddress = normalizeAddress(serverAddress);
setApiClient(new ApiClient(null, normalizedAddress || '-', "Jellyfin WebNG", "0.0.1", "WebNG", "WebNG", ""));
try {
await getApiClient().getPublicSystemInfo()
dispatch(connectSuccessful({ serverAddress }));
return true
} catch (err) {
dispatch(connectFailed({ serverAddress }));
return false
}
};
}