@anfenn/dync 1.0.13 → 1.0.15

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.
package/dist/index.cjs CHANGED
@@ -53,6 +53,47 @@ function newLogger(base, min) {
53
53
  var SERVER_PK = "id";
54
54
  var LOCAL_PK = "_localId";
55
55
  var UPDATED_AT = "updated_at";
56
+ var ApiError = class extends Error {
57
+ isNetworkError;
58
+ cause;
59
+ constructor(message, isNetworkError2, cause) {
60
+ super(message);
61
+ this.name = "ApiError";
62
+ this.isNetworkError = isNetworkError2;
63
+ this.cause = cause;
64
+ }
65
+ };
66
+ function isNetworkError(error) {
67
+ const message = error.message.toLowerCase();
68
+ const name = error.name;
69
+ if (name === "TypeError" && (message.includes("failed to fetch") || message.includes("network request failed"))) {
70
+ return true;
71
+ }
72
+ const code = error.code;
73
+ if (code === "ERR_NETWORK" || code === "ECONNABORTED" || code === "ENOTFOUND" || code === "ECONNREFUSED") {
74
+ return true;
75
+ }
76
+ if (error.isAxiosError && error.response === void 0) {
77
+ return true;
78
+ }
79
+ if (name === "ApolloError" && error.networkError) {
80
+ return true;
81
+ }
82
+ if (message.includes("network error") || message.includes("networkerror")) {
83
+ return true;
84
+ }
85
+ return false;
86
+ }
87
+ function parseApiError(error) {
88
+ if (error instanceof ApiError) {
89
+ return error;
90
+ }
91
+ if (error instanceof Error) {
92
+ return new ApiError(error.message, isNetworkError(error), error);
93
+ }
94
+ const message = String(error);
95
+ return new ApiError(message, false);
96
+ }
56
97
  var SyncAction = /* @__PURE__ */ ((SyncAction2) => {
57
98
  SyncAction2["Create"] = "create";
58
99
  SyncAction2["Update"] = "update";
@@ -134,6 +175,7 @@ var DEFAULT_STATE = {
134
175
  var StateManager = class {
135
176
  persistedState;
136
177
  syncStatus;
178
+ apiError;
137
179
  listeners = /* @__PURE__ */ new Set();
138
180
  storageAdapter;
139
181
  hydrated = false;
@@ -174,12 +216,8 @@ var StateManager = class {
174
216
  this.persistedState = resolveNextState(this.persistedState, setterOrState);
175
217
  return this.persist();
176
218
  }
177
- /**
178
- * Set error in memory only without persisting to database.
179
- * Used when the database itself failed to open.
180
- */
181
- setErrorInMemory(error) {
182
- this.persistedState = { ...this.persistedState, error };
219
+ setApiError(error) {
220
+ this.apiError = error ? parseApiError(error) : void 0;
183
221
  this.emit();
184
222
  }
185
223
  addPendingChange(change) {
@@ -259,7 +297,7 @@ var StateManager = class {
259
297
  this.emit();
260
298
  }
261
299
  getSyncState() {
262
- return buildSyncState(this.persistedState, this.syncStatus, this.hydrated);
300
+ return buildSyncState(this.persistedState, this.syncStatus, this.hydrated, this.apiError);
263
301
  }
264
302
  subscribe(listener) {
265
303
  this.listeners.add(listener);
@@ -282,12 +320,13 @@ function resolveNextState(current, setterOrState) {
282
320
  }
283
321
  return { ...current, ...setterOrState };
284
322
  }
285
- function buildSyncState(state, status, hydrated) {
323
+ function buildSyncState(state, status, hydrated, apiError) {
286
324
  const persisted = clonePersistedState(state);
287
325
  const syncState = {
288
326
  ...persisted,
289
327
  status,
290
- hydrated
328
+ hydrated,
329
+ apiError
291
330
  };
292
331
  deleteKeyIfEmptyObject(syncState, "conflicts");
293
332
  return syncState;
@@ -1495,10 +1534,7 @@ var DyncBase = class {
1495
1534
  this.emitMutation({ type: "pull", tableName });
1496
1535
  }
1497
1536
  this.syncStatus = "idle";
1498
- await this.state.setState((syncState) => ({
1499
- ...syncState,
1500
- error: pullResult.error ?? firstPushSyncError
1501
- }));
1537
+ this.state.setApiError(pullResult.error ?? firstPushSyncError);
1502
1538
  if (this.mutationsDuringSync) {
1503
1539
  this.mutationsDuringSync = false;
1504
1540
  this.syncOnce().catch(() => {