Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused fetchhelper and ajax call functions #696

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions src/components/__tests__/jellyfinApi.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, beforeAll, beforeEach, test, expect } from 'vitest';
import { JellyfinApi } from '../jellyfinApi';
import { version } from '../../../package.json';

const setupMockCastSenders = (): void => {
const getSenders = (): any[] => [{ id: 'thisIsSenderId' }]; // eslint-disable-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -141,70 +140,3 @@ describe('creating image urls', () => {
expect(result).toEqual(correct);
});
});

describe('test authenticated user ajax', () => {
test('should return rejected promise when server info is undefined', async () => {
// Linting requires this weird spacing.
JellyfinApi.setServerInfo(undefined, '', '');

const resultUserIdIsNull = JellyfinApi.authAjaxUser('', {});

JellyfinApi.setServerInfo('', undefined, '');

const resultAccessTokenIsNull = JellyfinApi.authAjaxUser('', {});

JellyfinApi.setServerInfo('', '');

const resultServerAddressIsNull = JellyfinApi.authAjaxUser('', {});

await expect(resultUserIdIsNull).rejects.toEqual(
'no server info present'
);
await expect(resultAccessTokenIsNull).rejects.toEqual(
'no server info present'
);
await expect(resultServerAddressIsNull).rejects.toEqual(
'no server info present'
);
});
});

describe('getting security headers', () => {
beforeAll(() => {
setupMockCastSenders();
});

test('should return correct auth header with all server details set', () => {
JellyfinApi.setServerInfo(
'thisIsUserId',
'thisIsAccessToken',
'thisIsServerAddress',
'thisIsReceiverName'
);

// @ts-expect-error Since the method is private.
const result = JellyfinApi.getSecurityHeaders();
const correctAuth = `MediaBrowser Client="Chromecast", Version="${version}", Token="thisIsAccessToken", DeviceId="${btoa(
'thisIsReceiverName'
)}", Device="thisIsReceiverName"`;

expect(result).toHaveProperty('Authorization');
expect(result.Authorization).toMatch(correctAuth);
});

test('should return correct auth header with minimal server details set', () => {
JellyfinApi.setServerInfo(
undefined,
'thisIsAccessToken',
'thisIsServerAddress'
);

// @ts-expect-error Since the method is private.
const result = JellyfinApi.getSecurityHeaders();
const correct = {
Authorization: `MediaBrowser Client="Chromecast", Version="${version}", Token="thisIsAccessToken", DeviceId="thisIsSenderId", Device="Google%20Cast"`
};

expect(result).toMatchObject(correct);
});
});
141 changes: 0 additions & 141 deletions src/components/fetchhelper.ts

This file was deleted.

57 changes: 0 additions & 57 deletions src/components/jellyfinApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Api, Jellyfin } from '@jellyfin/sdk';
import axios from 'axios';
import { version as packageVersion } from '../../package.json';
import { ajax } from './fetchhelper';

axios.interceptors.request.use((request) => {
console.log(`requesting url: ${request.url}`);
Expand Down Expand Up @@ -109,39 +108,6 @@ export abstract class JellyfinApi {
}
}

// create the necessary headers for authentication
private static getSecurityHeaders(): { Authorization?: string } {
const parameters: Record<string, string> = {
Client: 'Chromecast',
Version: packageVersion
};

if (this.accessToken) {
parameters.Token = this.accessToken;
}

if (this.deviceId) {
parameters.DeviceId = this.deviceId;
}

if (this.deviceName) {
parameters.Device = this.deviceName;
}

let header = 'MediaBrowser';

for (const [key, value] of Object.entries(parameters)) {
header += ` ${key}="${encodeURIComponent(value)}", `;
}

// Remove last comma
header = header.substring(0, header.length - 2);

return {
Authorization: header
};
}

// Create a basic url.
// Cannot start with /.
public static createUrl(path: string): string {
Expand Down Expand Up @@ -191,27 +157,4 @@ export abstract class JellyfinApi {
`Items/${itemId}/Images/${imgType}/${imgIdx.toString()}?tag=${imgTag}`
);
}

// Authenticated ajax
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static authAjaxUser(path: string, args: any): Promise<any> {
if (
this.userId === undefined ||
this.accessToken === undefined ||
this.serverAddress === undefined
) {
console.error(
'JellyfinApi.authAjaxUser: No userid/accesstoken/serverAddress present. Skipping request'
);

return Promise.reject('no server info present');
}

const params = {
headers: this.getSecurityHeaders(),
url: this.createUserUrl(path)
};

return ajax({ ...params, ...args });
}
}