fauxbase 0.5.5 → 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 +31 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -50,6 +50,11 @@ interface LocalDriverConfig {
|
|
|
50
50
|
type: 'local';
|
|
51
51
|
persist?: 'memory' | 'localStorage' | 'indexeddb';
|
|
52
52
|
dbName?: string;
|
|
53
|
+
latency?: number | {
|
|
54
|
+
min: number;
|
|
55
|
+
max: number;
|
|
56
|
+
};
|
|
57
|
+
errorRate?: number;
|
|
53
58
|
}
|
|
54
59
|
interface HttpDriverConfig {
|
|
55
60
|
type: 'http';
|
|
@@ -378,7 +383,11 @@ declare class LocalDriver implements Driver {
|
|
|
378
383
|
private authProvider;
|
|
379
384
|
private _ready;
|
|
380
385
|
private _isReady;
|
|
386
|
+
private latencyMs;
|
|
387
|
+
private errorRate;
|
|
381
388
|
constructor(config: LocalDriverConfig);
|
|
389
|
+
private simulate;
|
|
390
|
+
private getLatency;
|
|
382
391
|
get ready(): Promise<void>;
|
|
383
392
|
get isReady(): boolean;
|
|
384
393
|
setAuthProvider(provider: AuthProvider): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -50,6 +50,11 @@ interface LocalDriverConfig {
|
|
|
50
50
|
type: 'local';
|
|
51
51
|
persist?: 'memory' | 'localStorage' | 'indexeddb';
|
|
52
52
|
dbName?: string;
|
|
53
|
+
latency?: number | {
|
|
54
|
+
min: number;
|
|
55
|
+
max: number;
|
|
56
|
+
};
|
|
57
|
+
errorRate?: number;
|
|
53
58
|
}
|
|
54
59
|
interface HttpDriverConfig {
|
|
55
60
|
type: 'http';
|
|
@@ -378,7 +383,11 @@ declare class LocalDriver implements Driver {
|
|
|
378
383
|
private authProvider;
|
|
379
384
|
private _ready;
|
|
380
385
|
private _isReady;
|
|
386
|
+
private latencyMs;
|
|
387
|
+
private errorRate;
|
|
381
388
|
constructor(config: LocalDriverConfig);
|
|
389
|
+
private simulate;
|
|
390
|
+
private getLatency;
|
|
382
391
|
get ready(): Promise<void>;
|
|
383
392
|
get isReady(): boolean;
|
|
384
393
|
setAuthProvider(provider: AuthProvider): void;
|
package/dist/index.js
CHANGED
|
@@ -746,7 +746,11 @@ var LocalDriver = class {
|
|
|
746
746
|
authProvider = null;
|
|
747
747
|
_ready;
|
|
748
748
|
_isReady;
|
|
749
|
+
latencyMs;
|
|
750
|
+
errorRate;
|
|
749
751
|
constructor(config) {
|
|
752
|
+
this.latencyMs = config.latency ?? 0;
|
|
753
|
+
this.errorRate = config.errorRate ?? 0;
|
|
750
754
|
if (config.persist === "indexeddb") {
|
|
751
755
|
const backend = new IndexedDBBackend(config.dbName ?? "fauxbase");
|
|
752
756
|
this.storage = backend;
|
|
@@ -760,6 +764,27 @@ var LocalDriver = class {
|
|
|
760
764
|
this._ready = Promise.resolve();
|
|
761
765
|
}
|
|
762
766
|
}
|
|
767
|
+
async simulate() {
|
|
768
|
+
if (this.errorRate > 0 && Math.random() < this.errorRate) {
|
|
769
|
+
const errors = [
|
|
770
|
+
() => new NetworkError("Simulated network failure"),
|
|
771
|
+
() => new TimeoutError("Simulated request timeout"),
|
|
772
|
+
() => new NetworkError("Simulated connection refused")
|
|
773
|
+
];
|
|
774
|
+
const delay2 = this.getLatency();
|
|
775
|
+
if (delay2 > 0) await new Promise((r) => setTimeout(r, delay2 / 2));
|
|
776
|
+
throw errors[Math.floor(Math.random() * errors.length)]();
|
|
777
|
+
}
|
|
778
|
+
const delay = this.getLatency();
|
|
779
|
+
if (delay > 0) {
|
|
780
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
getLatency() {
|
|
784
|
+
if (typeof this.latencyMs === "number") return this.latencyMs;
|
|
785
|
+
const { min, max } = this.latencyMs;
|
|
786
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
787
|
+
}
|
|
763
788
|
get ready() {
|
|
764
789
|
return this._ready;
|
|
765
790
|
}
|
|
@@ -776,12 +801,14 @@ var LocalDriver = class {
|
|
|
776
801
|
this.entityClasses.set(resource, entityClass);
|
|
777
802
|
}
|
|
778
803
|
async list(resource, query) {
|
|
804
|
+
await this.simulate();
|
|
779
805
|
const items = this.storage.getAll(resource);
|
|
780
806
|
const entityClass = this.entityClasses.get(resource);
|
|
781
807
|
const processed = entityClass ? items.map((item) => applyComputedFields(item, entityClass)) : items;
|
|
782
808
|
return executeQuery(processed, query);
|
|
783
809
|
}
|
|
784
810
|
async get(resource, id) {
|
|
811
|
+
await this.simulate();
|
|
785
812
|
const item = this.storage.getById(resource, id);
|
|
786
813
|
if (!item || item.deletedAt) {
|
|
787
814
|
throw new NotFoundError(`${resource} with id "${id}" not found`);
|
|
@@ -791,6 +818,7 @@ var LocalDriver = class {
|
|
|
791
818
|
return { data };
|
|
792
819
|
}
|
|
793
820
|
async create(resource, data) {
|
|
821
|
+
await this.simulate();
|
|
794
822
|
const entityClass = this.entityClasses.get(resource);
|
|
795
823
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
796
824
|
const authContext = this.authProvider?.();
|
|
@@ -817,6 +845,7 @@ var LocalDriver = class {
|
|
|
817
845
|
return { data: result };
|
|
818
846
|
}
|
|
819
847
|
async update(resource, id, data) {
|
|
848
|
+
await this.simulate();
|
|
820
849
|
const existing = this.storage.getById(resource, id);
|
|
821
850
|
if (!existing || existing.deletedAt) {
|
|
822
851
|
throw new NotFoundError(`${resource} with id "${id}" not found`);
|
|
@@ -843,6 +872,7 @@ var LocalDriver = class {
|
|
|
843
872
|
return { data: result };
|
|
844
873
|
}
|
|
845
874
|
async delete(resource, id) {
|
|
875
|
+
await this.simulate();
|
|
846
876
|
const existing = this.storage.getById(resource, id);
|
|
847
877
|
if (!existing || existing.deletedAt) {
|
|
848
878
|
throw new NotFoundError(`${resource} with id "${id}" not found`);
|
|
@@ -865,6 +895,7 @@ var LocalDriver = class {
|
|
|
865
895
|
return { data: record };
|
|
866
896
|
}
|
|
867
897
|
async count(resource, filter) {
|
|
898
|
+
await this.simulate();
|
|
868
899
|
let items = this.storage.getAll(resource).filter((item) => !item.deletedAt);
|
|
869
900
|
if (filter) {
|
|
870
901
|
items = applyFilters(items, filter);
|