Skip to content

Commit 2e2c2b3

Browse files
Tirth GajjarTirth Gajjar
Tirth Gajjar
authored and
Tirth Gajjar
committed
feat: Add offline responses storage and retrieval
The code changes introduce the storage and retrieval of offline responses. This allows the application to save responses from API requests made while offline and retrieve them later when the device is online again. The changes include adding a new constant and implementing functions and to save and retrieve responses respectively. The function has also been updated to save the response to offline storage if a is specified in the request configuration.
1 parent 1521924 commit 2e2c2b3

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

src/index.tsx

+44-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const api = axios.create();
1717
const RETRY_LIMIT = 0;
1818
const RETRY_DELAY_MS = 1000;
1919
const API_REQUESTS_STORAGE_KEY = 'apiRequests';
20+
const OFFLINE_RESPONSES_STORAGE_KEY = 'offlineResponses';
2021

2122
import {
2223
generateUuid,
@@ -37,9 +38,10 @@ interface ApiRequest {
3738
type?: string;
3839
url: string;
3940
method: string;
40-
data?: any;
41+
data?: any | ((previousResponses: any[]) => Promise<any>);
4142
isFormdata?: boolean;
4243
retryCount?: number;
44+
dependsOn?: string;
4345
}
4446

4547
type ConfigType = {
@@ -113,7 +115,7 @@ export const OfflineSyncProvider: FC<{
113115

114116
const saveRequestToOfflineStorage = async (apiConfig: any) => {
115117
try {
116-
const storedRequests: Array<any> =
118+
const storedRequests: any =
117119
(await localForage.getItem(API_REQUESTS_STORAGE_KEY)) || [];
118120
console.log('perform stored', {
119121
req: storedRequests,
@@ -145,16 +147,53 @@ export const OfflineSyncProvider: FC<{
145147
}
146148
};
147149

150+
const saveResponseToOfflineStorage = async (type: string, response: any) => {
151+
try {
152+
const storedResponses: Record<string, any> =
153+
(await localForage.getItem(OFFLINE_RESPONSES_STORAGE_KEY)) || {};
154+
if (!storedResponses[type]) {
155+
storedResponses[type] = [];
156+
}
157+
storedResponses[type].push(response);
158+
await localForage.setItem(OFFLINE_RESPONSES_STORAGE_KEY, storedResponses);
159+
} catch (error) {
160+
console.error('Error saving response to offline storage:', error);
161+
}
162+
};
163+
164+
const getOfflineResponses = async (type: string) => {
165+
try {
166+
const storedResponses: Record<string, any> =
167+
(await localForage.getItem(OFFLINE_RESPONSES_STORAGE_KEY)) || {};
168+
return storedResponses[type] || [];
169+
} catch (error) {
170+
console.error('Error getting offline responses:', error);
171+
return [];
172+
}
173+
};
174+
148175
// Function to perform the actual API request and handle retries
149176
const performRequest = async (config: any): Promise<any> => {
150177
console.log('Inside performRequest');
151178
try {
179+
let requestData = config.data;
180+
if (typeof requestData === 'function') {
181+
const dependencyResponses = config.dependsOn
182+
? await getOfflineResponses(config.dependsOn)
183+
: [];
184+
requestData = await requestData(dependencyResponses);
185+
}
186+
152187
let response;
153-
if (config?.isFormdata && !(config?.data instanceof FormData)) {
154-
const updateConfig = { ...config, data: objectToFormData(config.data) };
188+
if (config?.isFormdata && !(requestData instanceof FormData)) {
189+
const updateConfig = { ...config, data: objectToFormData(requestData) };
155190
response = await api.request(updateConfig);
156191
} else {
157-
response = await api.request(config);
192+
response = await api.request({ ...config, data: requestData });
193+
}
194+
195+
if (config.type) {
196+
await saveResponseToOfflineStorage(config.type, response.data);
158197
}
159198

160199
onCallback && onCallback({ config, data: response, sendRequest });

0 commit comments

Comments
 (0)