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

Refactor queries to use non-null assert

This commit is contained in:
viown 2025-02-25 17:18:50 +03:00
parent 3df39d659c
commit 2ce9e9f1e0
28 changed files with 47 additions and 127 deletions

View file

@ -7,26 +7,23 @@ import { useApi } from './useApi';
import type { ItemDto } from 'types/base/models/item-dto';
const fetchItem = async (
api?: Api,
userId?: string,
itemId?: string,
api: Api,
itemId: string,
userId: string,
options?: AxiosRequestConfig
) => {
if (!api) throw new Error('No API instance available');
if (!itemId) throw new Error('No item ID provided');
const response = await getUserLibraryApi(api)
.getItem({ userId, itemId }, options);
return response.data as ItemDto;
};
export const getItemQuery = (
api?: Api,
userId?: string,
itemId?: string
api: Api | undefined,
itemId?: string,
userId?: string
) => queryOptions({
queryKey: [ 'User', userId, 'Items', itemId ],
queryFn: ({ signal }) => fetchItem(api, userId, itemId, { signal }),
queryFn: ({ signal }) => fetchItem(api!, itemId!, userId!, { signal }),
staleTime: 1000, // 1 second
enabled: !!api && !!userId && !!itemId
});
@ -36,5 +33,5 @@ export const useItem = (
) => {
const apiContext = useApi();
const { api, user } = apiContext;
return useQuery(getItemQuery(api, user?.Id, itemId));
return useQuery(getItemQuery(api, itemId, user?.Id));
};

View file

@ -6,14 +6,9 @@ import type { AxiosRequestConfig } from 'axios';
import { useApi } from './useApi';
const fetchSyncPlayGroups = async (
api?: Api,
api: Api,
options?: AxiosRequestConfig
) => {
if (!api) {
console.warn('[fetchSyncPlayGroups] No API instance available');
return;
}
const response = await getSyncPlayApi(api)
.syncPlayGetGroups(options);
return response.data;
@ -23,7 +18,7 @@ export const useSyncPlayGroups = () => {
const { api } = useApi();
return useQuery({
queryKey: [ 'SyncPlay', 'Groups' ],
queryFn: ({ signal }) => fetchSyncPlayGroups(api, { signal }),
queryFn: ({ signal }) => fetchSyncPlayGroups(api!, { signal }),
enabled: !!api
});
};

View file

@ -6,14 +6,9 @@ import type { AxiosRequestConfig } from 'axios';
import { useApi } from './useApi';
const fetchSystemInfo = async (
api?: Api,
api: Api,
options?: AxiosRequestConfig
) => {
if (!api) {
console.warn('[fetchSystemInfo] No API instance available');
return;
}
const response = await getSystemApi(api)
.getSystemInfo(options);
return response.data;
@ -23,7 +18,7 @@ export const getSystemInfoQuery = (
api?: Api
) => queryOptions({
queryKey: [ 'SystemInfo' ],
queryFn: ({ signal }) => fetchSystemInfo(api, { signal, headers: { 'Cache-Control': 'no-cache' } }),
queryFn: ({ signal }) => fetchSystemInfo(api!, { signal, headers: { 'Cache-Control': 'no-cache' } }),
// Allow for query reuse in legacy javascript.
staleTime: 1000, // 1 second
enabled: !!api

View file

@ -7,14 +7,11 @@ import type { AxiosRequestConfig } from 'axios';
import { useApi } from './useApi';
const fetchUserViews = async (
api?: Api,
userId?: string,
api: Api,
userId: string,
params?: UserViewsApiGetUserViewsRequest,
options?: AxiosRequestConfig
) => {
if (!api) throw new Error('No API instance available');
if (!userId) throw new Error('No User ID provided');
const response = await getUserViewsApi(api)
.getUserViews({ ...params, userId }, options);
return response.data;
@ -26,7 +23,7 @@ export const getUserViewsQuery = (
params?: UserViewsApiGetUserViewsRequest
) => queryOptions({
queryKey: [ 'User', userId, 'Views', params ],
queryFn: ({ signal }) => fetchUserViews(api, userId, params, { signal }),
queryFn: ({ signal }) => fetchUserViews(api!, userId!, params, { signal }),
// On initial page load we request user views 3x. Setting a 1 second stale time
// allows a single request to be made to resolve all 3.
staleTime: 1000, // 1 second

View file

@ -9,15 +9,10 @@ import { useApi } from './useApi';
export type UsersRecords = Record<string, UserDto>;
const fetchUsers = async (
api?: Api,
api: Api,
requestParams?: UserApiGetUsersRequest,
options?: AxiosRequestConfig
) => {
if (!api) {
console.warn('[fetchUsers] No API instance available');
return;
}
const response = await getUserApi(api).getUsers(requestParams, {
signal: options?.signal
});
@ -30,7 +25,7 @@ export const useUsers = (requestParams?: UserApiGetUsersRequest) => {
return useQuery({
queryKey: ['Users'],
queryFn: ({ signal }) =>
fetchUsers(api, requestParams, { signal }),
fetchUsers(api!, requestParams, { signal }),
enabled: !!api
});
};