fauxbase 0.5.4 → 0.5.6

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
@@ -244,6 +244,13 @@ var Service = class {
244
244
  };
245
245
  }
246
246
  async request(path, options) {
247
+ if (options?.local) {
248
+ try {
249
+ return this.driver.request(this.resourceName, path, options);
250
+ } catch {
251
+ return options.local();
252
+ }
253
+ }
247
254
  return this.driver.request(this.resourceName, path, options);
248
255
  }
249
256
  emitEvent(action, extra) {
@@ -741,7 +748,11 @@ var LocalDriver = class {
741
748
  authProvider = null;
742
749
  _ready;
743
750
  _isReady;
751
+ latencyMs;
752
+ errorRate;
744
753
  constructor(config) {
754
+ this.latencyMs = config.latency ?? 0;
755
+ this.errorRate = config.errorRate ?? 0;
745
756
  if (config.persist === "indexeddb") {
746
757
  const backend = new IndexedDBBackend(config.dbName ?? "fauxbase");
747
758
  this.storage = backend;
@@ -755,6 +766,27 @@ var LocalDriver = class {
755
766
  this._ready = Promise.resolve();
756
767
  }
757
768
  }
769
+ async simulate() {
770
+ if (this.errorRate > 0 && Math.random() < this.errorRate) {
771
+ const errors = [
772
+ () => new NetworkError("Simulated network failure"),
773
+ () => new TimeoutError("Simulated request timeout"),
774
+ () => new NetworkError("Simulated connection refused")
775
+ ];
776
+ const delay2 = this.getLatency();
777
+ if (delay2 > 0) await new Promise((r) => setTimeout(r, delay2 / 2));
778
+ throw errors[Math.floor(Math.random() * errors.length)]();
779
+ }
780
+ const delay = this.getLatency();
781
+ if (delay > 0) {
782
+ await new Promise((r) => setTimeout(r, delay));
783
+ }
784
+ }
785
+ getLatency() {
786
+ if (typeof this.latencyMs === "number") return this.latencyMs;
787
+ const { min, max } = this.latencyMs;
788
+ return Math.floor(Math.random() * (max - min + 1)) + min;
789
+ }
758
790
  get ready() {
759
791
  return this._ready;
760
792
  }
@@ -771,12 +803,14 @@ var LocalDriver = class {
771
803
  this.entityClasses.set(resource, entityClass);
772
804
  }
773
805
  async list(resource, query) {
806
+ await this.simulate();
774
807
  const items = this.storage.getAll(resource);
775
808
  const entityClass = this.entityClasses.get(resource);
776
809
  const processed = entityClass ? items.map((item) => applyComputedFields(item, entityClass)) : items;
777
810
  return executeQuery(processed, query);
778
811
  }
779
812
  async get(resource, id) {
813
+ await this.simulate();
780
814
  const item = this.storage.getById(resource, id);
781
815
  if (!item || item.deletedAt) {
782
816
  throw new NotFoundError(`${resource} with id "${id}" not found`);
@@ -786,6 +820,7 @@ var LocalDriver = class {
786
820
  return { data };
787
821
  }
788
822
  async create(resource, data) {
823
+ await this.simulate();
789
824
  const entityClass = this.entityClasses.get(resource);
790
825
  const now = (/* @__PURE__ */ new Date()).toISOString();
791
826
  const authContext = this.authProvider?.();
@@ -812,6 +847,7 @@ var LocalDriver = class {
812
847
  return { data: result };
813
848
  }
814
849
  async update(resource, id, data) {
850
+ await this.simulate();
815
851
  const existing = this.storage.getById(resource, id);
816
852
  if (!existing || existing.deletedAt) {
817
853
  throw new NotFoundError(`${resource} with id "${id}" not found`);
@@ -838,6 +874,7 @@ var LocalDriver = class {
838
874
  return { data: result };
839
875
  }
840
876
  async delete(resource, id) {
877
+ await this.simulate();
841
878
  const existing = this.storage.getById(resource, id);
842
879
  if (!existing || existing.deletedAt) {
843
880
  throw new NotFoundError(`${resource} with id "${id}" not found`);
@@ -860,6 +897,7 @@ var LocalDriver = class {
860
897
  return { data: record };
861
898
  }
862
899
  async count(resource, filter) {
900
+ await this.simulate();
863
901
  let items = this.storage.getAll(resource).filter((item) => !item.deletedAt);
864
902
  if (filter) {
865
903
  items = applyFilters(items, filter);
@@ -890,9 +928,12 @@ var LocalDriver = class {
890
928
  }
891
929
  return { data: { count } };
892
930
  }
893
- async request(_resource, _path, _options) {
931
+ async request(_resource, _path, options) {
932
+ if (options?.local) {
933
+ return options.local();
934
+ }
894
935
  throw new Error(
895
- "service.request() is only available with the HTTP driver. Local driver does not support custom endpoints."
936
+ "service.request() is only available with the HTTP driver. Provide a `local` handler or switch to HTTP driver."
896
937
  );
897
938
  }
898
939
  // --- Seed management (synchronous) ---