@@ -17,6 +17,7 @@ const api = axios.create();
17
17
const RETRY_LIMIT = 0 ;
18
18
const RETRY_DELAY_MS = 1000 ;
19
19
const API_REQUESTS_STORAGE_KEY = 'apiRequests' ;
20
+ const OFFLINE_RESPONSES_STORAGE_KEY = 'offlineResponses' ;
20
21
21
22
import {
22
23
generateUuid ,
@@ -37,9 +38,10 @@ interface ApiRequest {
37
38
type ?: string ;
38
39
url : string ;
39
40
method : string ;
40
- data ?: any ;
41
+ data ?: any | ( ( previousResponses : any [ ] ) => Promise < any > ) ;
41
42
isFormdata ?: boolean ;
42
43
retryCount ?: number ;
44
+ dependsOn ?: string ;
43
45
}
44
46
45
47
type ConfigType = {
@@ -113,7 +115,7 @@ export const OfflineSyncProvider: FC<{
113
115
114
116
const saveRequestToOfflineStorage = async ( apiConfig : any ) => {
115
117
try {
116
- const storedRequests : Array < any > =
118
+ const storedRequests : any =
117
119
( await localForage . getItem ( API_REQUESTS_STORAGE_KEY ) ) || [ ] ;
118
120
console . log ( 'perform stored' , {
119
121
req : storedRequests ,
@@ -145,16 +147,53 @@ export const OfflineSyncProvider: FC<{
145
147
}
146
148
} ;
147
149
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
+
148
175
// Function to perform the actual API request and handle retries
149
176
const performRequest = async ( config : any ) : Promise < any > => {
150
177
console . log ( 'Inside performRequest' ) ;
151
178
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
+
152
187
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 ) } ;
155
190
response = await api . request ( updateConfig ) ;
156
191
} 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 ) ;
158
197
}
159
198
160
199
onCallback && onCallback ( { config, data : response , sendRequest } ) ;
0 commit comments