@hyper-fetch/core 3.3.3 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/browser/index.cjs.js +1 -1
  2. package/dist/browser/index.cjs.js.map +4 -4
  3. package/dist/browser/index.esm.js +1 -1
  4. package/dist/browser/index.esm.js.map +4 -4
  5. package/dist/index.cjs.js +1 -1
  6. package/dist/index.cjs.js.map +4 -4
  7. package/dist/index.d.ts +330 -180
  8. package/dist/index.esm.js +1 -1
  9. package/dist/index.esm.js.map +4 -4
  10. package/package.json +4 -2
  11. package/src/adapter/adapter.bindings.ts +124 -35
  12. package/src/adapter/adapter.browser.ts +29 -25
  13. package/src/adapter/adapter.server.ts +81 -81
  14. package/src/adapter/adapter.ts +1 -6
  15. package/src/adapter/adapter.types.ts +100 -12
  16. package/src/adapter/adapter.utils.ts +16 -23
  17. package/src/cache/cache.events.ts +9 -6
  18. package/src/cache/cache.ts +37 -30
  19. package/src/cache/cache.types.ts +32 -15
  20. package/src/cache/cache.utils.ts +7 -9
  21. package/src/client/client.constants.ts +6 -0
  22. package/src/client/client.ts +130 -59
  23. package/src/client/client.types.ts +11 -10
  24. package/src/client/client.utils.ts +20 -6
  25. package/src/dispatcher/dispatcher.ts +20 -26
  26. package/src/dispatcher/dispatcher.types.ts +4 -4
  27. package/src/dispatcher/dispatcher.utils.ts +2 -11
  28. package/src/effect/request.effect.ts +8 -5
  29. package/src/effect/request.effect.types.ts +14 -5
  30. package/src/index.ts +1 -0
  31. package/src/managers/logger/logger.manager.ts +8 -8
  32. package/src/managers/logger/logger.manager.types.ts +5 -8
  33. package/src/managers/logger/logger.manager.utils.ts +2 -2
  34. package/src/managers/request/request.manager.events.ts +7 -7
  35. package/src/managers/request/request.manager.types.ts +0 -1
  36. package/src/mocker/index.ts +2 -0
  37. package/src/mocker/mocker.ts +96 -0
  38. package/src/mocker/mocker.types.ts +26 -0
  39. package/src/request/request.ts +227 -224
  40. package/src/request/request.types.ts +75 -35
  41. package/src/request/request.utils.ts +61 -28
  42. package/src/types/client.types.ts +4 -0
  43. package/src/types/fetch.types.ts +16 -25
  44. package/src/types/http.types.ts +1 -0
  45. package/src/types/index.ts +1 -0
  46. package/{tsconfig.node.json → tsconfig.base.json} +3 -3
  47. package/tsconfig.json +3 -4
  48. package/dist/server/index.cjs.js +0 -2
  49. package/dist/server/index.cjs.js.map +0 -7
  50. package/dist/server/index.esm.js +0 -2
  51. package/dist/server/index.esm.js.map +0 -7
@@ -1,26 +1,100 @@
1
1
  import { RequestInstance } from "request";
2
-
3
- // Adapter
4
-
5
- export type AdapterType = (request: RequestInstance, requestId: string) => Promise<ResponseType<any, any>>;
2
+ import { HttpMethodsType, HttpStatusType } from "../types";
3
+
4
+ /**
5
+ * Base Adapter
6
+ */
7
+
8
+ export type AdapterInstance = AdapterType<any, any, any, any, any>;
9
+
10
+ export type AdapterType<
11
+ AdapterOptions = AdapterOptionsType,
12
+ MethodType = HttpMethodsType,
13
+ StatusType = HttpStatusType,
14
+ Extra extends Record<string, any> = AdapterExtraType,
15
+ QueryParams = QueryParamsType | string,
16
+ > = (
17
+ request: RequestInstance,
18
+ requestId: string,
19
+ // This is never used in the application, we pass this type to have unions extracting possibilities
20
+ DO_NOT_USE?: {
21
+ method?: MethodType;
22
+ options?: AdapterOptions;
23
+ status?: StatusType;
24
+ extra?: Extra;
25
+ queryParams?: QueryParams;
26
+ },
27
+ // [any any any] as a way to avoid circular reference that destroyed request type.
28
+ ) => Promise<ResponseReturnType<any, any, any>>;
29
+
30
+ /**
31
+ * Extractors
32
+ */
33
+
34
+ export type ExtractAdapterOptionsType<T> = T extends AdapterType<infer O, any, any, any, any> ? O : never;
35
+ export type ExtractAdapterMethodType<T> = T extends AdapterType<any, infer M, any, any, any> ? M : never;
36
+ export type ExtractAdapterStatusType<T> = T extends AdapterType<any, any, infer S, any, any> ? S : never;
37
+ export type ExtractAdapterExtraType<T> = T extends AdapterType<any, any, any, infer A, any> ? A : never;
38
+ export type ExtractAdapterQueryParamsType<T> = T extends AdapterType<any, any, any, any, infer Q> ? Q : never;
39
+ // Special type only for selecting appropriate AdapterType union version (check FirebaseAdapterType).
40
+ export type ExtractUnionAdapter<
41
+ Adapter extends AdapterInstance,
42
+ Values extends {
43
+ method?: any;
44
+ options?: any;
45
+ status?: any;
46
+ extra?: any;
47
+ queryParams?: any;
48
+ },
49
+ > = Extract<
50
+ Adapter,
51
+ AdapterType<Values["options"], Values["method"], Values["status"], Values["extra"], Values["queryParams"]>
52
+ > extends AdapterInstance
53
+ ? Extract<
54
+ Adapter,
55
+ AdapterType<Values["options"], Values["method"], Values["status"], Values["extra"], Values["queryParams"]>
56
+ >
57
+ : never;
58
+
59
+ /**
60
+ * Options
61
+ */
6
62
 
7
63
  export type AdapterOptionsType = Partial<XMLHttpRequest>;
8
64
 
65
+ export type AdapterExtraType = {
66
+ headers: Record<string, string>;
67
+ };
68
+
9
69
  export type AdapterPayloadMappingType = (data: unknown) => string | FormData;
10
70
 
11
71
  // Responses
12
72
 
13
- export type ResponseType<GenericDataType, GenericErrorType> = [
14
- GenericDataType | null,
15
- GenericErrorType | null,
16
- number | null,
17
- ];
18
- export type ResponseSuccessType<GenericDataType> = [GenericDataType, null, number | null];
19
- export type ResponseErrorType<GenericErrorType> = [null, GenericErrorType, number | null];
73
+ export type ResponseReturnType<GenericDataType, GenericErrorType, Adapter extends AdapterInstance> = {
74
+ data: GenericDataType | null;
75
+ error: GenericErrorType | null;
76
+ status: ExtractAdapterStatusType<Adapter> | null;
77
+ success: boolean;
78
+ extra: ExtractAdapterExtraType<Adapter> | null;
79
+ };
80
+ export type ResponseReturnSuccessType<GenericDataType, Adapter extends AdapterInstance> = {
81
+ data: GenericDataType;
82
+ error: null;
83
+ status: ExtractAdapterStatusType<Adapter> | null;
84
+ success: boolean;
85
+ extra: ExtractAdapterExtraType<Adapter> | null;
86
+ };
87
+ export type ResponseReturnErrorType<GenericErrorType, Adapter extends AdapterInstance> = {
88
+ data: null;
89
+ error: GenericErrorType;
90
+ status: ExtractAdapterStatusType<Adapter> | null;
91
+ success: boolean;
92
+ extra: ExtractAdapterExtraType<Adapter> | null;
93
+ };
20
94
 
21
95
  // QueryParams
22
96
 
23
- export type QueryParamValuesType = number | string | boolean | null | undefined;
97
+ export type QueryParamValuesType = number | string | boolean | null | undefined | Record<any, any>;
24
98
  export type QueryParamType = QueryParamValuesType | Array<QueryParamValuesType> | Record<string, QueryParamValuesType>;
25
99
  export type QueryParamsType = Record<string, QueryParamType>;
26
100
 
@@ -60,6 +134,15 @@ export type QueryStringifyOptionsType = {
60
134
  * Skip keys with empty string
61
135
  */
62
136
  skipEmptyString?: boolean;
137
+
138
+ /**
139
+ * Parsing function for date type query param
140
+ */
141
+ dateParser?: (value: QueryParamType) => string;
142
+ /**
143
+ * Parsing function for object type query param
144
+ */
145
+ objectParser?: (value: QueryParamType) => string;
63
146
  };
64
147
 
65
148
  // Progress
@@ -77,3 +160,8 @@ export type ProgressType = {
77
160
  loaded: number;
78
161
  startTimestamp: number;
79
162
  };
163
+
164
+ export type AdapterBindingsReturnType = {
165
+ fullUrl: string;
166
+ config;
167
+ };
@@ -13,6 +13,20 @@ export const getErrorMessage = (errorCase?: "timeout" | "abort" | "deleted") =>
13
13
  return new Error("Unexpected error");
14
14
  };
15
15
 
16
+ export const getResponseHeaders = (headersString: string): Record<string, string> => {
17
+ const arr = headersString.trim().split(/[\r\n]+/);
18
+
19
+ const headers = {};
20
+ arr.forEach((line) => {
21
+ const parts = line.split(": ");
22
+ const header = parts.shift();
23
+ const value = parts.join(": ");
24
+ headers[header] = value;
25
+ });
26
+
27
+ return headers;
28
+ };
29
+
16
30
  // Responses
17
31
 
18
32
  export const parseResponse = (response: string | unknown) => {
@@ -29,31 +43,10 @@ export const parseErrorResponse = <T extends RequestInstance>(response: unknown)
29
43
 
30
44
  // Request
31
45
 
32
- export const getUploadSize = (payload: FormData | string) => {
33
- if (payload instanceof FormData) {
34
- return Array.from(payload.values())
35
- .map((value) => (typeof value === "string" ? value.length : value.size))
36
- .reduce((a, b) => a + b, 0);
37
- }
46
+ export const getUploadSize = (payload: string) => {
38
47
  return payload.length;
39
48
  };
40
49
 
41
- export const fileToBuffer = (file: File | Blob) => {
42
- return new Promise<Uint8Array>((resolve) => {
43
- const reader = new FileReader();
44
- reader.onload = () => {
45
- resolve(new Uint8Array(reader.result as ArrayBufferLike));
46
- };
47
- reader.readAsArrayBuffer(file);
48
- });
49
- };
50
-
51
- export const getStreamPayload = (payload: FormData | string | null) => {
52
- if (payload instanceof FormData) {
53
- const parts = Array.from(payload.values()).map((value) =>
54
- typeof value === "string" ? value : fileToBuffer(value),
55
- );
56
- return Promise.all(parts);
57
- }
50
+ export const getStreamPayload = (payload: string | null) => {
58
51
  return payload;
59
52
  };
@@ -1,7 +1,7 @@
1
1
  import EventEmitter from "events";
2
2
 
3
- import { CacheValueType, getRevalidateEventKey } from "cache";
4
- import { getCacheKey } from "./cache.utils";
3
+ import { CacheValueType, getRevalidateEventKey, getCacheKey } from "cache";
4
+ import { AdapterInstance } from "adapter";
5
5
 
6
6
  export const getCacheEvents = (emitter: EventEmitter) => ({
7
7
  /**
@@ -9,7 +9,10 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
9
9
  * @param cacheKey
10
10
  * @param data
11
11
  */
12
- emitCacheData: <Response, Error>(cacheKey: string, data: CacheValueType<Response, Error>): void => {
12
+ emitCacheData: <Response, Error, Adapter extends AdapterInstance>(
13
+ cacheKey: string,
14
+ data: CacheValueType<Response, Error, Adapter>,
15
+ ): void => {
13
16
  emitter.emit(getCacheKey(cacheKey), data);
14
17
  },
15
18
  /**
@@ -18,15 +21,15 @@ export const getCacheEvents = (emitter: EventEmitter) => ({
18
21
  emitRevalidation: (cacheKey: string): void => {
19
22
  emitter.emit(getRevalidateEventKey(cacheKey));
20
23
  },
21
- /**
24
+ /** StatusType
22
25
  * Cache data listener
23
26
  * @param cacheKey
24
27
  * @param callback
25
28
  * @returns
26
29
  */
27
- onData: <Response, Error>(
30
+ onData: <Response, Error, Adapter extends AdapterInstance>(
28
31
  cacheKey: string,
29
- callback: (data: CacheValueType<Response, Error>) => void,
32
+ callback: (data: CacheValueType<Response, Error, Adapter>) => void,
30
33
  ): VoidFunction => {
31
34
  emitter.on(getCacheKey(cacheKey), callback);
32
35
  return () => emitter.removeListener(getCacheKey(cacheKey), callback);
@@ -1,7 +1,7 @@
1
1
  import EventEmitter from "events";
2
2
 
3
- import { ResponseType } from "adapter";
4
- import { ClientInstance } from "client";
3
+ import { AdapterInstance, ResponseReturnType } from "adapter";
4
+ import { ClientInstance, ExtractAdapterTypeFromClient } from "client";
5
5
  import { ResponseDetailsType, LoggerType } from "managers";
6
6
  import {
7
7
  CacheOptionsType,
@@ -11,7 +11,7 @@ import {
11
11
  getCacheEvents,
12
12
  CacheValueType,
13
13
  } from "cache";
14
- import { RequestDump, RequestInstance } from "request";
14
+ import { RequestJSON, RequestInstance } from "request";
15
15
 
16
16
  /**
17
17
  * Cache class handles the data exchange with the dispatchers.
@@ -20,7 +20,7 @@ import { RequestDump, RequestInstance } from "request";
20
20
  * Keys used to save the values are created dynamically on the Request class
21
21
  *
22
22
  */
23
- export class Cache {
23
+ export class Cache<C extends ClientInstance> {
24
24
  public emitter = new EventEmitter();
25
25
  public events: ReturnType<typeof getCacheEvents>;
26
26
 
@@ -30,7 +30,7 @@ export class Cache {
30
30
  public garbageCollectors = new Map<string, ReturnType<typeof setTimeout>>();
31
31
  private logger: LoggerType;
32
32
 
33
- constructor(public client: ClientInstance, public options?: CacheOptionsType) {
33
+ constructor(public client: C, public options?: CacheOptionsType) {
34
34
  this.storage = this.options?.storage || new Map<string, CacheValueType>();
35
35
  this.events = getCacheEvents(this.emitter);
36
36
  this.options?.onInitialization?.(this);
@@ -59,33 +59,36 @@ export class Cache {
59
59
  * @returns
60
60
  */
61
61
  set = <Response, Error>(
62
- request: RequestInstance | RequestDump<RequestInstance>,
63
- response: ResponseType<Response, Error>,
64
- details: ResponseDetailsType,
62
+ request: RequestInstance | RequestJSON<RequestInstance>,
63
+ response: ResponseReturnType<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>> &
64
+ ResponseDetailsType,
65
65
  ): void => {
66
- this.logger.debug("Processing cache response", { request, response, details });
66
+ this.logger.debug("Processing cache response", { request, response });
67
67
  const { cacheKey, cache, cacheTime, garbageCollection } = request;
68
- const cachedData = this.storage.get<Response, Error>(cacheKey);
68
+ const cachedData = this.storage.get<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey);
69
69
 
70
70
  // Once refresh error occurs we don't want to override already valid data in our cache with the thrown error
71
71
  // We need to check it against cache and return last valid data we have
72
- const data = getCacheData(cachedData?.data, response);
72
+ const data = getCacheData(cachedData, response);
73
73
 
74
- const newCacheData: CacheValueType = { data, details, cacheTime, clearKey: this.clearKey, garbageCollection };
74
+ const newCacheData: CacheValueType = { ...data, cacheTime, clearKey: this.clearKey, garbageCollection };
75
75
 
76
- this.events.emitCacheData<Response, Error>(cacheKey, newCacheData);
77
- this.logger.debug("Emitting cache response", { request, response, details });
76
+ this.events.emitCacheData<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(
77
+ cacheKey,
78
+ newCacheData,
79
+ );
80
+ this.logger.debug("Emitting cache response", { request, data });
78
81
 
79
82
  // If request should not use cache - just emit response data
80
83
  if (!cache) {
81
- return this.logger.debug("Prevented saving response to cache", { request, response, details });
84
+ return this.logger.debug("Prevented saving response to cache", { request, data });
82
85
  }
83
86
 
84
87
  // Only success data is valid for the cache store
85
- if (!details.isFailed) {
86
- this.logger.debug("Saving response to cache storage", { request, response, details });
87
- this.storage.set<Response, Error>(cacheKey, newCacheData);
88
- this.lazyStorage?.set<Response, Error>(cacheKey, newCacheData);
88
+ if (response.success) {
89
+ this.logger.debug("Saving response to cache storage", { request, data });
90
+ this.storage.set<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey, newCacheData);
91
+ this.lazyStorage?.set<Response, Error, ExtractAdapterTypeFromClient<typeof this.client>>(cacheKey, newCacheData);
89
92
  this.options?.onChange?.(cacheKey, newCacheData);
90
93
  this.scheduleGarbageCollector(cacheKey);
91
94
  }
@@ -96,9 +99,11 @@ export class Cache {
96
99
  * @param cacheKey
97
100
  * @returns
98
101
  */
99
- get = <Response, Error>(cacheKey: string): CacheValueType<Response, Error> | undefined => {
100
- this.getLazyResource<Response, Error>(cacheKey);
101
- const cachedData = this.storage.get<Response, Error>(cacheKey);
102
+ get = <Response, Error, Adapter extends AdapterInstance>(
103
+ cacheKey: string,
104
+ ): CacheValueType<Response, Error, Adapter> | undefined => {
105
+ this.getLazyResource<Response, Error, Adapter>(cacheKey);
106
+ const cachedData = this.storage.get<Response, Error, Adapter>(cacheKey);
102
107
  return cachedData;
103
108
  };
104
109
 
@@ -149,24 +154,26 @@ export class Cache {
149
154
  * Used to receive data from lazy storage
150
155
  * @param cacheKey
151
156
  */
152
- getLazyResource = async <Response, Error>(cacheKey: string): Promise<CacheValueType<Response, Error> | undefined> => {
153
- const data = await this.lazyStorage?.get<Response, Error>(cacheKey);
154
- const syncData = this.storage.get<Response, Error>(cacheKey);
157
+ getLazyResource = async <Response, Error, Adapter extends AdapterInstance>(
158
+ cacheKey: string,
159
+ ): Promise<CacheValueType<Response, Error, Adapter> | undefined> => {
160
+ const data = await this.lazyStorage?.get<Response, Error, Adapter>(cacheKey);
161
+ const syncData = this.storage.get<Response, Error, Adapter>(cacheKey);
155
162
 
156
163
  // No data in lazy storage
157
164
  const hasLazyData = this.lazyStorage && data;
158
165
  if (hasLazyData) {
159
166
  const now = +new Date();
160
- const isNewestData = syncData ? syncData.details.timestamp < data.details.timestamp : true;
161
- const isStaleData = data.cacheTime <= now - data.details.timestamp;
167
+ const isNewestData = syncData ? syncData.timestamp < data.timestamp : true;
168
+ const isStaleData = data.cacheTime <= now - data.timestamp;
162
169
  const isValidLazyData = data.clearKey === this.clearKey;
163
170
 
164
171
  if (!isValidLazyData) {
165
172
  this.lazyStorage.delete(cacheKey);
166
173
  }
167
174
  if (isNewestData && !isStaleData && isValidLazyData) {
168
- this.storage.set<Response, Error>(cacheKey, data);
169
- this.events.emitCacheData<Response, Error>(cacheKey, data);
175
+ this.storage.set<Response, Error, Adapter>(cacheKey, data);
176
+ this.events.emitCacheData<Response, Error, Adapter>(cacheKey, data);
170
177
  return data;
171
178
  }
172
179
  }
@@ -204,7 +211,7 @@ export class Cache {
204
211
 
205
212
  // Garbage collect
206
213
  if (cacheData) {
207
- const timeLeft = cacheData.garbageCollection + cacheData.details.timestamp - +new Date();
214
+ const timeLeft = cacheData.garbageCollection + cacheData.timestamp - +new Date();
208
215
  if (cacheData.garbageCollection !== null && JSON.stringify(cacheData.garbageCollection) === "null") {
209
216
  this.logger.info("Cache value is Infinite", { cacheKey });
210
217
  } else if (timeLeft >= 0) {
@@ -1,8 +1,9 @@
1
1
  import { Cache } from "cache";
2
- import { ResponseType } from "adapter";
2
+ import { AdapterInstance, ResponseReturnType } from "adapter";
3
3
  import { ResponseDetailsType } from "managers";
4
+ import { ClientInstance } from "../client";
4
5
 
5
- export type CacheOptionsType = {
6
+ export type CacheOptionsType<C extends ClientInstance = ClientInstance> = {
6
7
  /**
7
8
  * Assign your custom sync storage
8
9
  */
@@ -18,11 +19,14 @@ export type CacheOptionsType = {
18
19
  /**
19
20
  * Initialization callback
20
21
  */
21
- onInitialization?: (cache: Cache) => void;
22
+ onInitialization?: (cache: Cache<C>) => void;
22
23
  /**
23
24
  * Callback for every change in the storage
24
25
  */
25
- onChange?: <Response = any, Error = any>(key: string, data: CacheValueType<Response, Error>) => void;
26
+ onChange?: <Response = any, Error = any, Adapter extends AdapterInstance = AdapterInstance>(
27
+ key: string,
28
+ data: CacheValueType<Response, Error, Adapter>,
29
+ ) => void;
26
30
  /**
27
31
  * Callback for every delete in the storage
28
32
  */
@@ -30,25 +34,38 @@ export type CacheOptionsType = {
30
34
  };
31
35
 
32
36
  // Values
33
- export type CacheValueType<Response = any, Error = any> = {
34
- data: ResponseType<Response, Error>;
35
- details: ResponseDetailsType;
36
- cacheTime: number;
37
- clearKey: string;
38
- garbageCollection: number;
39
- };
37
+ export type CacheValueType<
38
+ Response = any,
39
+ Error = any,
40
+ Adapter extends AdapterInstance = AdapterInstance,
41
+ > = ResponseReturnType<Response, Error, Adapter> &
42
+ ResponseDetailsType & {
43
+ cacheTime: number;
44
+ clearKey: string;
45
+ garbageCollection: number;
46
+ };
40
47
 
41
48
  // Storage
42
49
  export type CacheAsyncStorageType = {
43
- set: <Response, Error>(key: string, data: CacheValueType<Response, Error>) => Promise<void>;
44
- get: <Response, Error>(key: string) => Promise<CacheValueType<Response, Error> | undefined>;
50
+ set: <Response, Error, Adapter extends AdapterInstance>(
51
+ key: string,
52
+ data: CacheValueType<Response, Error, Adapter>,
53
+ ) => Promise<void>;
54
+ get: <Response, Error, Adapter extends AdapterInstance>(
55
+ key: string,
56
+ ) => Promise<CacheValueType<Response, Error, Adapter> | undefined>;
45
57
  keys: () => Promise<string[] | IterableIterator<string> | string[]>;
46
58
  delete: (key: string) => Promise<void>;
47
59
  };
48
60
 
49
61
  export type CacheStorageType = {
50
- set: <Response, Error>(key: string, data: CacheValueType<Response, Error>) => void;
51
- get: <Response, Error>(key: string) => CacheValueType<Response, Error> | undefined;
62
+ set: <Response, Error, Adapter extends AdapterInstance>(
63
+ key: string,
64
+ data: CacheValueType<Response, Error, Adapter>,
65
+ ) => void;
66
+ get: <Response, Error, Adapter extends AdapterInstance>(
67
+ key: string,
68
+ ) => CacheValueType<Response, Error, Adapter> | undefined;
52
69
  keys: () => string[] | IterableIterator<string> | string[];
53
70
  delete: (key: string) => void;
54
71
  clear: () => void;
@@ -1,19 +1,17 @@
1
+ import { ResponseDetailsType } from "managers";
1
2
  import { RequestInstance } from "request";
2
- import { isFailedRequest } from "dispatcher";
3
3
  import { ExtractAdapterReturnType } from "types";
4
4
 
5
5
  export const getCacheData = <T extends RequestInstance>(
6
6
  previousResponse: ExtractAdapterReturnType<T> | undefined,
7
- response: ExtractAdapterReturnType<T>,
8
- ): ExtractAdapterReturnType<T> => {
9
- const isFailed = isFailedRequest(response);
7
+ response: ExtractAdapterReturnType<T> & ResponseDetailsType,
8
+ ): ExtractAdapterReturnType<T> & ResponseDetailsType => {
9
+ const { data, success } = response;
10
10
 
11
- const previousData = isFailed && previousResponse ? previousResponse[0] : null;
12
- const data = response[0] || previousData;
13
- const error = response[1];
14
- const status = response[2];
11
+ const previousData = !success && previousResponse ? previousResponse.data : null;
12
+ const computedData = data || previousData;
15
13
 
16
- return [data, error, status];
14
+ return { ...response, data: computedData };
17
15
  };
18
16
 
19
17
  export const getRevalidateEventKey = (key: string): string => {
@@ -1,3 +1,5 @@
1
+ import { AdapterExtraType } from "adapter";
2
+
1
3
  export const stringifyDefaultOptions = {
2
4
  strict: true,
3
5
  encode: true,
@@ -7,3 +9,7 @@ export const stringifyDefaultOptions = {
7
9
  skipNull: true,
8
10
  skipEmptyString: true,
9
11
  } as const;
12
+
13
+ export const xhrExtra: AdapterExtraType = {
14
+ headers: {},
15
+ };