Skip to content

Commit

Permalink
request: fix/improve typesafety of TData (ohcnetwork#6324)
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad authored Sep 25, 2023
1 parent b2076e3 commit 9fb1a9f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Utils/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import handleResponse from "./handleResponse";
import { RequestOptions, RequestResult, Route } from "./types";
import { makeHeaders, makeUrl } from "./utils";

interface Options extends RequestOptions {
interface Options<TData> extends RequestOptions<TData> {
controller?: AbortController;
}

Expand All @@ -16,7 +16,7 @@ export default async function request<TData>(
onResponse,
silent,
reattempts = 3,
}: Options = {}
}: Options<TData> = {}
): Promise<RequestResult<TData>> {
const signal = controller?.signal;
const url = makeUrl(path, query, pathParams);
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/request/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { QueryRoute, RequestOptions, RequestResult } from "./types";
import request from "./request";
import { mergeRequestOptions } from "./utils";

export interface QueryOptions extends RequestOptions {
export interface QueryOptions<TData> extends RequestOptions<TData> {
prefetch?: boolean;
refetchOnWindowFocus?: boolean;
}

export default function useQuery<TData>(
route: QueryRoute<TData>,
options?: QueryOptions
options?: QueryOptions<TData>
) {
const [response, setResponse] = useState<RequestResult<TData>>();
const [loading, setLoading] = useState(false);

const controllerRef = useRef<AbortController>();

const runQuery = useCallback(
async (overrides?: QueryOptions) => {
async (overrides?: QueryOptions<TData>) => {
controllerRef.current?.abort();

const controller = new AbortController();
Expand Down
8 changes: 4 additions & 4 deletions src/Utils/request/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export function getAuthorizationHeader() {
return null;
}

export function mergeRequestOptions(
options: RequestOptions,
overrides: RequestOptions
): RequestOptions {
export function mergeRequestOptions<TData>(
options: RequestOptions<TData>,
overrides: RequestOptions<TData>
): RequestOptions<TData> {
return {
...options,
...overrides,
Expand Down

0 comments on commit 9fb1a9f

Please sign in to comment.