Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Request / Response progress #277

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into pr/picunada/277
pi0 committed Oct 26, 2023
commit f159250a5f1917dab6527f8a157c3fa2b505edad
83 changes: 6 additions & 77 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -8,83 +8,12 @@ import {
detectResponseType,
mergeFetchOptions,
} from "./utils";

export interface CreateFetchOptions {
// eslint-disable-next-line no-use-before-define
defaults?: FetchOptions;
fetch?: Fetch;
Headers?: typeof Headers;
AbortController?: typeof AbortController;
}

export type FetchRequest = RequestInfo;
export interface FetchResponse<T> extends Response {
_data?: T;
}
export interface SearchParameters {
[key: string]: any;
}

export interface FetchContext<T = any, R extends ResponseType = ResponseType> {
request: FetchRequest;
// eslint-disable-next-line no-use-before-define
options: FetchOptions<R>;
response?: FetchResponse<T>;
error?: Error;
}

export interface FetchOptions<R extends ResponseType = ResponseType>
extends Omit<RequestInit, "body"> {
baseURL?: string;
body?: RequestInit["body"] | Record<string, any>;
ignoreResponseError?: boolean;
params?: SearchParameters;
query?: SearchParameters;
parseResponse?: (responseText: string) => any;
responseType?: R;

/**
* @experimental Set to "half" to enable duplex streaming.
* Will be automatically set to "half" when using a ReadableStream as body.
* https://fetch.spec.whatwg.org/#enumdef-requestduplex
*/
duplex?: "half" | undefined;

/** timeout in milliseconds */
timeout?: number;

retry?: number | false;
/** Delay between retries in milliseconds. */
retryDelay?: number;
/** Default is [408, 409, 425, 429, 500, 502, 503, 504] */
retryStatusCodes?: number[];

onRequest?(context: FetchContext): Promise<void> | void;
onRequestError?(
context: FetchContext & { error: Error }
): Promise<void> | void;
onRequestProgress?(progress: number): Promise<void> | void;
onResponse?(
context: FetchContext & { response: FetchResponse<R> }
): Promise<void> | void;
onResponseError?(
context: FetchContext & { response: FetchResponse<R> }
): Promise<void> | void;
onResponseProgress?(progress: number): Promise<void> | void;
}

export interface $Fetch {
<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<MappedType<R, T>>;
raw<T = any, R extends ResponseType = "json">(
request: FetchRequest,
options?: FetchOptions<R>
): Promise<FetchResponse<MappedType<R, T>>>;
native: Fetch;
create(defaults: FetchOptions): $Fetch;
}
import type {
CreateFetchOptions,
FetchResponse,
FetchContext,
$Fetch,
} from "./types";

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
const retryStatusCodes = new Set([
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -67,6 +67,9 @@ export interface FetchOptions<R extends ResponseType = ResponseType>
onResponseError?(
context: FetchContext & { response: FetchResponse<R> }
): Promise<void> | void;

onRequestProgress?(progress: number): Promise<void> | void;
onResponseProgress?(progress: number): Promise<void> | void;
}

export interface CreateFetchOptions {
You are viewing a condensed version of this merge commit. You can view the full changes here.