UNPKG

@jellyfin/sdk

Version:
56 lines (53 loc) 2.22 kB
import globalAxios from 'axios'; import { Configuration } from './generated-client/configuration.js'; import { getAuthorizationHeader } from './utils/authentication.js'; import { getSessionApi } from './utils/api/session-api.js'; import { getUserApi } from './utils/api/user-api.js'; /** The authorization header field name. */ const AUTHORIZATION_HEADER = 'Authorization'; /** Class representing the Jellyfin API. */ class Api { constructor(basePath, clientInfo, deviceInfo, accessToken = '', axiosInstance = globalAxios) { // Remove trailing slash if present this.basePath = basePath.endsWith('/') ? basePath.slice(0, -1) : basePath; this.clientInfo = clientInfo; this.deviceInfo = deviceInfo; this.accessToken = accessToken; this.axiosInstance = axiosInstance; } get configuration() { return new Configuration({ basePath: this.basePath, apiKey: this.authorizationHeader }); } /** * Convenience method for authenticating a user by name and updating the internal state. * @param username The username. * @param password The user password if required. */ authenticateUserByName(username, password) { return getUserApi(this).authenticateUserByName( // The axios client does some strange wrapping of the param object { authenticateUserByName: { Username: username, Pw: password } }, // The authorization header is required for the request to succeed { headers: { [AUTHORIZATION_HEADER]: this.authorizationHeader } }).then(response => { // Update the current token and configuration object this.accessToken = response.data.AccessToken || ''; return response; }); } /** * Convenience method for logging out and updating the internal state. */ logout() { return getSessionApi(this).reportSessionEnded().then(response => { this.accessToken = ''; return response; }); } get authorizationHeader() { return getAuthorizationHeader(this.clientInfo, this.deviceInfo, this.accessToken); } } export { AUTHORIZATION_HEADER, Api };