Skip to content

Commit

Permalink
typing fix: use response or response+generic, add unknown error type …
Browse files Browse the repository at this point in the history
…on callbacks, ensure response always returns
  • Loading branch information
cooperwfloyd committed Dec 17, 2024
1 parent 943b837 commit 602c4fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ const fancyfetch: typeof Fancyfetch = async (resource, options, extras) => {

let attempts = 0;

const tryFetch = async <T>(): Promise<(Response & T) | null> => {
const tryFetch = async <T>(): Promise<Response | (Response & T)> => {
attempts++;
if (attempts > extrasToUse.maxAttempts) return null;
if (attempts > extrasToUse.maxAttempts) return new Response();

try {
const response = (await fetchToUse(resource, options)) as Response & T;
const response = await fetchToUse(resource, options);

const validResponse = extrasToUse?.validateResponse
? await extrasToUse.validateResponse(response.clone())
Expand All @@ -140,7 +140,7 @@ const fancyfetch: typeof Fancyfetch = async (resource, options, extras) => {

return response;
} else {
if (extrasToUse.maxAttempts === 1) return null;
if (extrasToUse.maxAttempts === 1) return new Response();

if (extrasToUse?.log)
console.error(
Expand All @@ -159,7 +159,7 @@ const fancyfetch: typeof Fancyfetch = async (resource, options, extras) => {
}
} catch (e) {
if (extrasToUse?.log) console.error(`Error in fancyfetch`, e);
if (extrasToUse.maxAttempts === 1) return null;
if (extrasToUse.maxAttempts === 1) return new Response();
if (extrasToUse?.log)
console.error(
`Error in fancyfetch: Failed to fetch. Retrying${
Expand All @@ -169,7 +169,7 @@ const fancyfetch: typeof Fancyfetch = async (resource, options, extras) => {
}...`,
e
);
if (extrasToUse?.onRetryError) extrasToUse.onRetryError();
if (extrasToUse?.onRetryError) extrasToUse.onRetryError(e);
if (extrasToUse?.retryDelay !== undefined)
await sleep(extrasToUse?.retryDelay);

Expand Down
18 changes: 9 additions & 9 deletions src/types/fancyfetch/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ export interface Fetch {
}

export interface FancyfetchExtras {
fetch?: <T>(...args: unknown[]) => Promise<Response<T>>;
fetch?: <T>(...args: unknown) => Promise<Response | (Response & T)>;
log?: boolean;
validateResponse?: (response: Response) => Promise<boolean> | boolean;
validateResponse?: <T>(
response: Response | (Response & T)
) => Promise<boolean> | boolean;
maxAttempts?: number;
retryDelay?: number;
/* eslint-disable @typescript-eslint/no-explicit-any */
onError?: () => any;
onRetryError?: () => any;
onRetrySuccess?: () => any;
/* eslint-enable @typescript-eslint/no-explicit-any */
onError?: (error?: unknown) => unknown;
onRetryError?: (error?: unknown) => unknown;
onRetrySuccess?: () => unknown;
}

export declare function fancyfetch(
export declare function fancyfetch<T>(
resource: Fetch['resource'],
options?: Fetch['options'],
extras?: FancyfetchExtras
): Promise<Response | null>;
): Promise<Response | (Response & T)>;

export default fancyfetch;

0 comments on commit 602c4fb

Please sign in to comment.