Require client and device info

This commit is contained in:
Bill Thornton 2021-09-16 17:32:35 -04:00
parent 52f101d649
commit e05b93d613
3 changed files with 49 additions and 14 deletions

View File

@ -8,7 +8,16 @@ A TypeScript SDK for Jellyfin.
```js
// Create a new instance of the SDK
const jellyfin = new Jellyfin();
const jellyfin = new Jellyfin({
clientInfo: {
name: 'My Client Application',
version: '1.0.0'
},
deviceInfo: {
name: 'Device Name',
id: 'unique-device-id'
}
});
const api = jellyfin.createApi('https://demo.jellyfin.org/stable');
// Each API endpoint is exposed via a getter on the SDK instance using

View File

@ -8,18 +8,34 @@ import { Jellyfin } from '..';
// FIXME: These tests should be mocked and not calling an actual server
const SERVER_URL = 'https://demo.jellyfin.org/stable';
const TEST_CLIENT = {
name: 'sdk-test-client',
version: '0.0.0'
};
const TEST_DEVICE = {
name: 'device-name',
id: 'device-id'
};
describe('Test the Base SDK', () => {
it('create an api instance', () => {
const jellyfin = new Jellyfin();
const jellyfin = new Jellyfin({
clientInfo: TEST_CLIENT,
deviceInfo: TEST_DEVICE
});
expect(jellyfin).not.toBeNull();
const api = jellyfin.createApi(SERVER_URL);
expect(api.basePath).toEqual(SERVER_URL);
expect(api.authorizationHeader).toEqual('MediaBrowser Client="jellyfin-sdk-typescript", Device="device-name", DeviceId="device-id", Version="v0.1.0", Token=""');
expect(api.authorizationHeader).toEqual('MediaBrowser Client="sdk-test-client", Device="device-name", DeviceId="device-id", Version="0.0.0", Token=""');
});
it('public users api', async () => {
const jellyfin = new Jellyfin();
const jellyfin = new Jellyfin({
clientInfo: TEST_CLIENT,
deviceInfo: TEST_DEVICE
});
const api = jellyfin.createApi(SERVER_URL);
const users = await api.userApi.getPublicUsers();
@ -28,7 +44,10 @@ describe('Test the Base SDK', () => {
});
it('public system info api', async () => {
const jellyfin = new Jellyfin();
const jellyfin = new Jellyfin({
clientInfo: TEST_CLIENT,
deviceInfo: TEST_DEVICE
});
const api = jellyfin.createApi(SERVER_URL);
const info = await api.systemApi.getPublicSystemInfo();
@ -37,7 +56,10 @@ describe('Test the Base SDK', () => {
});
it('user login api', async () => {
const jellyfin = new Jellyfin();
const jellyfin = new Jellyfin({
clientInfo: TEST_CLIENT,
deviceInfo: TEST_DEVICE
});
const api = jellyfin.createApi(SERVER_URL);
const auth = await api.authenticateUserByName({ Username: 'demo', Pw: '' });
@ -46,7 +68,10 @@ describe('Test the Base SDK', () => {
});
it('library api', async () => {
const jellyfin = new Jellyfin();
const jellyfin = new Jellyfin({
clientInfo: TEST_CLIENT,
deviceInfo: TEST_DEVICE
});
const api = jellyfin.createApi(SERVER_URL);
await api.authenticateUserByName({ Username: 'demo', Pw: '' });

View File

@ -8,6 +8,11 @@ import { AxiosInstance } from 'axios';
import { Api } from './api';
import { ClientInfo, DeviceInfo } from './models';
interface JellyfinParameters {
clientInfo: ClientInfo,
deviceInfo: DeviceInfo
}
/**
* The minimum supported server version.
*/
@ -20,13 +25,9 @@ export class Jellyfin {
clientInfo;
deviceInfo;
constructor(
clientInfo: ClientInfo = { name: 'jellyfin-sdk-typescript', version: 'v0.1.0' },
// FIXME: The device info should always be required.
deviceInfo: DeviceInfo = { name: 'device-name', id: 'device-id' }
) {
this.clientInfo = clientInfo;
this.deviceInfo = deviceInfo;
constructor(parameters: JellyfinParameters) {
this.clientInfo = parameters.clientInfo;
this.deviceInfo = parameters.deviceInfo;
}
createApi(basePath: string, accessToken?: string, axiosInstance?: AxiosInstance): Api {