refactor: switch from Fetch API to Axios (#1520)
* refactor: switch from Fetch API to Axios * fix: remove unwanted changes * fix: rewrite error handling for Axios and remove IPv4 first setting * style: run prettier * style: run prettier * fix: add back custom proxy agent * fix: add back custom proxy agent * fix: correct rebase issue * fix: resolve review comments
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import type { AxiosError, AxiosResponse } from 'axios';
|
||||
import axios from 'axios';
|
||||
|
||||
interface JellyfinAuthenticationResult {
|
||||
Id: string;
|
||||
AccessToken: string;
|
||||
@@ -15,34 +18,30 @@ class JellyAPI {
|
||||
resolve: (result: JellyfinAuthenticationResult) => void,
|
||||
reject: (e: Error) => void
|
||||
) => {
|
||||
fetch(Hostname + '/Users/AuthenticateByName', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Emby-Authorization':
|
||||
'MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NDsgcnY6ODUuMCkgR2Vja28vMjAxMDAxMDEgRmlyZWZveC84NS4wfDE2MTI5MjcyMDM5NzM1", Version="10.8.0"',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
Username: Username,
|
||||
Pw: Password,
|
||||
}),
|
||||
})
|
||||
.then((res) => {
|
||||
if (!res.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
axios
|
||||
.post(
|
||||
Hostname + '/Users/AuthenticateByName',
|
||||
{
|
||||
Username: Username,
|
||||
Pw: Password,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'X-Emby-Authorization':
|
||||
'MediaBrowser Client="Jellyfin Web", Device="Firefox", DeviceId="TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NDsgcnY6ODUuMCkgR2Vja28vMjAxMDAxMDEgRmlyZWZveC84NS4wfDE2MTI5MjcyMDM5NzM1", Version="10.8.0"',
|
||||
},
|
||||
}
|
||||
return res.json();
|
||||
})
|
||||
.then((data) => {
|
||||
)
|
||||
.then((resp: AxiosResponse) => {
|
||||
const response: JellyfinAuthenticationResult = {
|
||||
Id: data.User.Id,
|
||||
AccessToken: data.AccessToken,
|
||||
ServerId: data.ServerId,
|
||||
Id: resp.data.User.Id,
|
||||
AccessToken: resp.data.AccessToken,
|
||||
ServerId: resp.data.ServerId,
|
||||
};
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
.catch((e: AxiosError) => {
|
||||
reject(e);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user