1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Define interface for getItems options

This commit is contained in:
Merlin Danner 2023-04-03 14:34:57 +02:00
parent 06c4f0f258
commit 4fe4ed2b0a

View file

@ -1,18 +1,24 @@
import type {BaseItemDtoQueryResult} from '@jellyfin/sdk/lib/generated-client'; import type {BaseItemDtoQueryResult} from '@jellyfin/sdk/lib/generated-client';
import {ApiClient} from 'jellyfin-apiclient'; import {ApiClient} from 'jellyfin-apiclient';
interface GetItemsRequest {
Ids?: string;
Limit?: number;
}
const ITEMS_PER_REQUEST_LIMIT = 25; const ITEMS_PER_REQUEST_LIMIT = 25;
function getItemsSplit(apiClient: ApiClient, userId: string, options: any) { function getItemsSplit(apiClient: ApiClient, userId: string, options: GetItemsRequest) {
const optionsTemplate = {...options}; const optionsTemplate = {...options};
const ids = options.Ids.split(','); const ids = options.Ids!.split(',');
const results = []; const results = [];
const limit = options.Limit ?? Infinity;
let end; let end;
for (let start = 0; start < ids.length && start < options.Limit; start = end) { for (let start: number = 0; start < ids.length && start < limit; start = end) {
end = start + ITEMS_PER_REQUEST_LIMIT; end = start + ITEMS_PER_REQUEST_LIMIT;
if (end > options.Limit) { if (end > limit) {
end = options.Limit; end = limit;
} }
const idsSlice = ids.slice(start, end); const idsSlice = ids.slice(start, end);
optionsTemplate.Ids = idsSlice.join(','); optionsTemplate.Ids = idsSlice.join(',');
@ -57,8 +63,9 @@ function mergeResults(results: BaseItemDtoQueryResult[]) {
* @param options Options object to specify getItems option. This includes a possibly long Items list that will be split up. * @param options Options object to specify getItems option. This includes a possibly long Items list that will be split up.
* @returns A promise that resolves to the merged result of all getItems calls * @returns A promise that resolves to the merged result of all getItems calls
*/ */
export function getItems(apiClient: ApiClient, userId: string, options?: any) { export function getItems(apiClient: ApiClient, userId: string, options?: GetItemsRequest) {
if (options.Ids?.split(',').length <= ITEMS_PER_REQUEST_LIMIT) { const ids = options?.Ids?.split(',');
if (!options || !ids || ids.length <= ITEMS_PER_REQUEST_LIMIT) {
return apiClient.getItems(apiClient.getCurrentUserId(), options); return apiClient.getItems(apiClient.getCurrentUserId(), options);
} }
const results = getItemsSplit(apiClient, userId, options); const results = getItemsSplit(apiClient, userId, options);