Skip to content

Commit

Permalink
Fix/request manager infinite swr requests (#306)
Browse files Browse the repository at this point in the history
* Fix RequestManager initial infinite swr requests

Functions always returned "null" as the url, which causes SWR to not send the request

 - useGetSourceLatestMangas
 - useSourceSearch
 - useSourceQuickSearch

* Use "skipRequest" for infinite SWR requests

Is easier to use then overwriting "getEndpoint" only for specific cases

* Request correct first page for infinite SWR requests

The following endpoints start at page 1
 - popular source mangas
 - latest source mangas
 - source search
 - source quick search

* Remove wrong parameter from source manga browse requests

No idea why I've added this in the first place

* Correctly request "latest" and "popular" source mangas

The "page" wasn't a param of the query but part of the endpoint
  • Loading branch information
schroda authored May 20, 2023
1 parent feac34b commit 0cd5720
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
IUpdateStatus,
Metadata,
PaginatedList,
PaginatedMangaList,
SourcePreferences,
SourceSearchResult,
UpdateCheck,
Expand Down Expand Up @@ -125,23 +126,26 @@ export class RequestManager {
});
}

public useSwrInfinite<Data = any, ErrorResponse = any>(
public useSwrInfinite<
Data = any,
ErrorResponse = any,
OptionsSWR extends SWRInfiniteOptions<Data, ErrorResponse> = SWRInfiniteOptions<Data, ErrorResponse>,
>(
getEndpoint: Required<CustomSWROptions<Data>>['getEndpoint'],
{
axiosOptions,
swrOptions,
}: { axiosOptions?: AxiosRequestConfig; swrOptions?: SWRInfiniteConfiguration<Data, ErrorResponse> } = {},
{ axiosOptions, swrOptions }: { axiosOptions?: AxiosRequestConfig; swrOptions?: OptionsSWR } = {},
): SWRInfiniteResponse<Data, ErrorResponse> {
const { skipRequest, ...swrConfig } = swrOptions ?? {};

// useSWRInfinite will (by default) revalidate the first page, to check if the other pages have to be revalidated as well
const result = useSWRInfinite<Data, ErrorResponse>(
(index, previousData) => {
const pageEndpoint = getEndpoint(index, previousData);
return pageEndpoint !== null ? this.getValidUrlFor(pageEndpoint) : null;
return pageEndpoint !== null && !skipRequest ? this.getValidUrlFor(pageEndpoint) : null;
},
{
fetcher: (path: string) =>
this.restClient.fetcher(path, { httpMethod: HttpMethod.GET, config: axiosOptions }),
...swrOptions,
...swrConfig,
},
);

Expand All @@ -163,7 +167,6 @@ export class RequestManager {
* In that case "getEndpoint" has to be passed, which gets used over "endpoint"
*
* Pass "skipRequest" to make SWR skip sending the request to the server.
* Only works for none "infinite" requests, for "infinite" requests the "getEndpoint" function can return "null".
* In case "formData" is passed, "data" gets ignored.
*/
private doRequest<
Expand Down Expand Up @@ -265,14 +268,13 @@ export class RequestManager {

public useGetSourcePopularMangas(
sourceId: string,
extension: string,
initialPages?: number,
swrOptions?: SWRInfiniteOptions<PaginatedList<IManga>>,
): SWRInfiniteResponse<PaginatedList<IManga>> {
swrOptions?: SWRInfiniteOptions<PaginatedMangaList>,
): SWRInfiniteResponse<PaginatedMangaList> {
return this.doRequest(SWRHttpMethod.SWR_GET_INFINITE, '', {
swrOptions: {
getEndpoint: (page, previousData) =>
previousData?.hasNextPage ? `source/${sourceId}/popular/${extension}?pageNum=${page}` : null,
previousData?.hasNextPage ?? true ? `source/${sourceId}/popular/${page + 1}` : null,
initialSize: initialPages,
...swrOptions,
} as typeof swrOptions,
Expand All @@ -281,14 +283,13 @@ export class RequestManager {

public useGetSourceLatestMangas(
sourceId: string,
extension: string,
initialPages?: number,
swrOptions?: SWRInfiniteOptions<PaginatedList<IManga>>,
): SWRInfiniteResponse<PaginatedList<IManga>> {
swrOptions?: SWRInfiniteOptions<PaginatedMangaList>,
): SWRInfiniteResponse<PaginatedMangaList> {
return this.doRequest(SWRHttpMethod.SWR_GET_INFINITE, '', {
swrOptions: {
getEndpoint: (page, previousData) =>
previousData?.hasNextPage ? `source/${sourceId}/latest/${extension}?pageNum=${page}` : null,
previousData?.hasNextPage ?? true ? `source/${sourceId}/latest/${page + 1}` : null,
initialSize: initialPages,
...swrOptions,
} as typeof swrOptions,
Expand Down Expand Up @@ -331,8 +332,8 @@ export class RequestManager {
return this.doRequest(HttpMethod.SWR_GET_INFINITE, '', {
swrOptions: {
getEndpoint: (page, previousData) =>
previousData?.hasNextPage
? `source/${sourceId}/search?searchTerm=${searchTerm}&pageNum=${page}`
previousData?.hasNextPage ?? true
? `source/${sourceId}/search?searchTerm=${searchTerm}&pageNum=${page + 1}`
: null,
initialSize: initialPages,
...swrOptions,
Expand All @@ -351,8 +352,8 @@ export class RequestManager {
data: filters,
swrOptions: {
getEndpoint: (page, previousData) =>
previousData?.hasNextPage
? `source/${sourceId}/quick-search?searchTerm=${searchTerm}&pageNum=${page}`
previousData?.hasNextPage ?? true
? `source/${sourceId}/quick-search?searchTerm=${searchTerm}&pageNum=${page + 1}`
: null,
initialSize: initialPages,
...swrOptions,
Expand Down
5 changes: 5 additions & 0 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ export interface PaginatedList<T> {
hasNextPage: boolean;
}

export type PaginatedMangaList = {
mangaList: IManga[];
hasNextPage: boolean;
};

export type NullAndUndefined<T> = T | null | undefined;

export type ChapterSortMode = 'fetchedAt' | 'source';
Expand Down

0 comments on commit 0cd5720

Please sign in to comment.