Skip to content

Commit

Permalink
feat: Add cloneResponse option to createFetchClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-Zayne committed Aug 1, 2024
1 parent ec9ae65 commit 6ec8f12
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-rocks-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zayne-labs/callapi": minor
---

Add cloneResponse Option to callApi
22 changes: 11 additions & 11 deletions src/createFetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const createFetchClient = <
retryCodes: defaultRetryCodes,
retryMethods: defaultRetryMethods,
defaultErrorMessage: "Failed to fetch data from server!",
cancelRedundantRequests: true,
...baseExtraOptions,
...extraOptions,
} satisfies ExtraOptions;
Expand Down Expand Up @@ -157,11 +156,18 @@ export const createFetchClient = <

if (!response.ok) {
const errorData = await getResponseData<TErrorData>(
response.clone(),
options.cloneResponse ? response.clone() : response,
options.responseType,
options.responseParser
);

await options.onResponseError?.({
response: options.cloneResponse ? response.clone() : response,
errorData,
request: requestInit,
options,
});

// == Pushing all error handling responsibilities to the catch block
throw new HTTPError({
errorData,
Expand All @@ -171,7 +177,7 @@ export const createFetchClient = <
}

const successData = await getResponseData<TData>(
response.clone(),
options.cloneResponse ? response.clone() : response,
options.responseType,
options.responseParser
);
Expand All @@ -181,8 +187,8 @@ export const createFetchClient = <
: successData;

await options.onResponse?.({
// == Workaround as opposed to using the spread operator, as it doesn't work on the response object. So using Object.assign instead on a clone of the response object.
response: Object.assign(response.clone(), { data: validSuccessData }),
response: options.cloneResponse ? response.clone() : response,
data: validSuccessData,
request: requestInit,
options,
});
Expand Down Expand Up @@ -212,12 +218,6 @@ export const createFetchClient = <
if (isHTTPErrorInstance<TErrorData>(error)) {
const { errorData, response } = error;

await options.onResponseError?.({
response: Object.assign(response.clone(), { errorData }),
request: requestInit,
options,
});

return resolveErrorResult({
errorData,
message: (errorData as PossibleErrorObject)?.message,
Expand Down
24 changes: 22 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ export interface ExtraOptions<
*/
cancelRedundantRequests?: boolean;

// /**
// * @description Defines the deduplication strategy for the request, can be set to "none" | "defer" | "cancel".
// *
// * - If set to "none", deduplication is disabled.
// *
// * - If set to "defer", no new requests to the same URL will be allowed through, until the previous one is completed.
// *
// * - If set to "cancel"(default), the previous pending request to the same URL will be cancelled and lets the new request through.
// * @default "cancel"
// */
// dedupeStrategy?: "none" | "defer" | "cancel";

/**
* @description Base URL to be prepended to all request URLs
*/
Expand All @@ -83,6 +95,12 @@ export interface ExtraOptions<
*/
defaultErrorMessage?: string;

/**
* @description Whether to clone the response, so response.json and the like can used in the interceptors.
* @default false
*/
cloneResponse?: boolean;

/**
* If true or the function returns true, throws errors instead of returning them
* The function is passed the error object and can be used to conditionally throw the error
Expand Down Expand Up @@ -166,15 +184,17 @@ export interface ExtraOptions<

export type ResponseContext<TData> = {
_: {
response: Response & { data: TData };
response: Response;
data: TData;
request: $RequestOptions;
options: ExtraOptions;
};
}["_"];

export type ResponseErrorContext<TErrorData> = {
_: {
response: Response & { errorData: TErrorData };
response: Response;
errorData: TErrorData;
request: $RequestOptions;
options: ExtraOptions;
};
Expand Down

0 comments on commit 6ec8f12

Please sign in to comment.