Fix react query abort signal handling

This commit is contained in:
Zoe Roux 2024-01-29 00:00:19 +01:00
parent 19c5efaed0
commit b33b428d3b

View File

@ -22,6 +22,7 @@ import { ComponentType, ReactElement } from "react";
import { import {
dehydrate, dehydrate,
QueryClient, QueryClient,
QueryFunction,
QueryFunctionContext, QueryFunctionContext,
useInfiniteQuery, useInfiniteQuery,
useQuery, useQuery,
@ -39,24 +40,25 @@ const kyooUrl =
export let kyooApiUrl = kyooUrl; export let kyooApiUrl = kyooUrl;
export const queryFn = async <Data,>( export const queryFn = async <Data,>(
context: context: {
| (QueryFunctionContext & { timeout?: number; apiUrl?: string }) timeout?: number;
| { apiUrl?: string;
authenticated?: boolean;
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
} & (
| QueryFunctionContext
| ({
path: (string | false | undefined | null)[]; path: (string | false | undefined | null)[];
body?: object; body?: object;
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
authenticated?: boolean;
apiUrl?: string;
timeout?: number;
plainText?: boolean; plainText?: boolean;
}, } & Partial<QueryFunctionContext>)
),
type?: z.ZodType<Data>, type?: z.ZodType<Data>,
token?: string | null, token?: string | null,
): Promise<Data> => { ): Promise<Data> => {
const url = context.apiUrl ?? (Platform.OS === "web" ? kyooUrl : getCurrentAccount()!.apiUrl); const url = context.apiUrl ?? (Platform.OS === "web" ? kyooUrl : getCurrentAccount()!.apiUrl);
kyooApiUrl = url; kyooApiUrl = url;
// @ts-ignore
if (token === undefined && context.authenticated !== false) token = await getToken(); if (token === undefined && context.authenticated !== false) token = await getToken();
const path = [url] const path = [url]
.concat( .concat(
@ -74,15 +76,13 @@ export const queryFn = async <Data,>(
if (controller) setTimeout(() => controller.abort(), context.timeout); if (controller) setTimeout(() => controller.abort(), context.timeout);
resp = await fetch(path, { resp = await fetch(path, {
// @ts-ignore
method: context.method, method: context.method,
// @ts-ignore body: "body" in context && context.body ? JSON.stringify(context.body) : undefined,
body: context.body ? JSON.stringify(context.body) : undefined,
headers: { headers: {
...(token ? { Authorization: token } : {}), ...(token ? { Authorization: token } : {}),
...("body" in context ? { "Content-Type": "application/json" } : {}), ...("body" in context ? { "Content-Type": "application/json" } : {}),
}, },
signal: controller?.signal, signal: controller?.signal ?? context.signal,
}); });
} catch (e) { } catch (e) {
console.log("Fetch error", e, path); console.log("Fetch error", e, path);