fauxbase 0.5.3 → 0.5.5

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.d.cts CHANGED
@@ -137,6 +137,12 @@ interface Driver {
137
137
  bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
138
138
  count: number;
139
139
  }>>;
140
+ request<R = any>(resource: string, path: string, options?: {
141
+ method?: string;
142
+ body?: any;
143
+ query?: Record<string, string>;
144
+ local?: () => R | Promise<R>;
145
+ }): Promise<R>;
140
146
  seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
141
147
  getSeedVersion(): string | null;
142
148
  setSeedVersion(version: string): void;
@@ -223,6 +229,12 @@ declare abstract class Service<T extends Entity> {
223
229
  count: number;
224
230
  }>>;
225
231
  };
232
+ request<R = any>(path: string, options?: {
233
+ method?: string;
234
+ body?: any;
235
+ query?: Record<string, string>;
236
+ local?: () => R | Promise<R>;
237
+ }): Promise<R>;
226
238
  private emitEvent;
227
239
  private runHooks;
228
240
  }
@@ -253,7 +265,7 @@ declare class HttpDriver implements Driver {
253
265
  private getEndpoint;
254
266
  private buildUrl;
255
267
  private buildHeaders;
256
- private request;
268
+ private _fetch;
257
269
  private throwMappedError;
258
270
  list<T>(resource: string, query: QueryParams): Promise<PagedResponse<T>>;
259
271
  get<T>(resource: string, id: string): Promise<ApiResponse<T>>;
@@ -269,6 +281,12 @@ declare class HttpDriver implements Driver {
269
281
  bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
270
282
  count: number;
271
283
  }>>;
284
+ request<R = any>(resource: string, path: string, options?: {
285
+ method?: string;
286
+ body?: any;
287
+ query?: Record<string, string>;
288
+ local?: () => R | Promise<R>;
289
+ }): Promise<R>;
272
290
  seed(): void;
273
291
  getSeedVersion(): string | null;
274
292
  setSeedVersion(): void;
@@ -380,6 +398,12 @@ declare class LocalDriver implements Driver {
380
398
  bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
381
399
  count: number;
382
400
  }>>;
401
+ request<R = any>(_resource: string, _path: string, options?: {
402
+ method?: string;
403
+ body?: any;
404
+ query?: Record<string, string>;
405
+ local?: () => R | Promise<R>;
406
+ }): Promise<R>;
383
407
  seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
384
408
  getSeedVersion(): string | null;
385
409
  setSeedVersion(version: string): void;
package/dist/index.d.ts CHANGED
@@ -137,6 +137,12 @@ interface Driver {
137
137
  bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
138
138
  count: number;
139
139
  }>>;
140
+ request<R = any>(resource: string, path: string, options?: {
141
+ method?: string;
142
+ body?: any;
143
+ query?: Record<string, string>;
144
+ local?: () => R | Promise<R>;
145
+ }): Promise<R>;
140
146
  seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
141
147
  getSeedVersion(): string | null;
142
148
  setSeedVersion(version: string): void;
@@ -223,6 +229,12 @@ declare abstract class Service<T extends Entity> {
223
229
  count: number;
224
230
  }>>;
225
231
  };
232
+ request<R = any>(path: string, options?: {
233
+ method?: string;
234
+ body?: any;
235
+ query?: Record<string, string>;
236
+ local?: () => R | Promise<R>;
237
+ }): Promise<R>;
226
238
  private emitEvent;
227
239
  private runHooks;
228
240
  }
@@ -253,7 +265,7 @@ declare class HttpDriver implements Driver {
253
265
  private getEndpoint;
254
266
  private buildUrl;
255
267
  private buildHeaders;
256
- private request;
268
+ private _fetch;
257
269
  private throwMappedError;
258
270
  list<T>(resource: string, query: QueryParams): Promise<PagedResponse<T>>;
259
271
  get<T>(resource: string, id: string): Promise<ApiResponse<T>>;
@@ -269,6 +281,12 @@ declare class HttpDriver implements Driver {
269
281
  bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
270
282
  count: number;
271
283
  }>>;
284
+ request<R = any>(resource: string, path: string, options?: {
285
+ method?: string;
286
+ body?: any;
287
+ query?: Record<string, string>;
288
+ local?: () => R | Promise<R>;
289
+ }): Promise<R>;
272
290
  seed(): void;
273
291
  getSeedVersion(): string | null;
274
292
  setSeedVersion(): void;
@@ -380,6 +398,12 @@ declare class LocalDriver implements Driver {
380
398
  bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
381
399
  count: number;
382
400
  }>>;
401
+ request<R = any>(_resource: string, _path: string, options?: {
402
+ method?: string;
403
+ body?: any;
404
+ query?: Record<string, string>;
405
+ local?: () => R | Promise<R>;
406
+ }): Promise<R>;
383
407
  seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
384
408
  getSeedVersion(): string | null;
385
409
  setSeedVersion(version: string): void;
package/dist/index.js CHANGED
@@ -241,6 +241,16 @@ var Service = class {
241
241
  }
242
242
  };
243
243
  }
244
+ async request(path, options) {
245
+ if (options?.local) {
246
+ try {
247
+ return this.driver.request(this.resourceName, path, options);
248
+ } catch {
249
+ return options.local();
250
+ }
251
+ }
252
+ return this.driver.request(this.resourceName, path, options);
253
+ }
244
254
  emitEvent(action, extra) {
245
255
  if (!this._eventBus) return;
246
256
  this._eventBus.emit({
@@ -885,6 +895,14 @@ var LocalDriver = class {
885
895
  }
886
896
  return { data: { count } };
887
897
  }
898
+ async request(_resource, _path, options) {
899
+ if (options?.local) {
900
+ return options.local();
901
+ }
902
+ throw new Error(
903
+ "service.request() is only available with the HTTP driver. Provide a `local` handler or switch to HTTP driver."
904
+ );
905
+ }
888
906
  // --- Seed management (synchronous) ---
889
907
  seed(resource, data, entityClass) {
890
908
  for (let i = 0; i < data.length; i++) {
@@ -1259,7 +1277,7 @@ var HttpDriver = class {
1259
1277
  }
1260
1278
  return headers;
1261
1279
  }
1262
- async request(url, options = {}, retryCount = 0) {
1280
+ async _fetch(url, options = {}, retryCount = 0) {
1263
1281
  const controller = new AbortController();
1264
1282
  const timer = setTimeout(() => controller.abort(), this.timeout);
1265
1283
  try {
@@ -1273,7 +1291,7 @@ var HttpDriver = class {
1273
1291
  if (response.status >= 500 && retryCount < this.maxRetries) {
1274
1292
  const delay = this.baseDelay * Math.pow(2, retryCount);
1275
1293
  await new Promise((r) => setTimeout(r, delay));
1276
- return this.request(url, options, retryCount + 1);
1294
+ return this._fetch(url, options, retryCount + 1);
1277
1295
  }
1278
1296
  const body = await response.json().catch(() => ({}));
1279
1297
  this.throwMappedError(response.status, body);
@@ -1311,7 +1329,7 @@ var HttpDriver = class {
1311
1329
  const params = serializeQuery(query, this.preset.query);
1312
1330
  const queryString = params.toString();
1313
1331
  const fullUrl = queryString ? `${url}?${queryString}` : url;
1314
- const raw = await this.request(fullUrl);
1332
+ const raw = await this._fetch(fullUrl);
1315
1333
  const parsed = this.preset.response.list(raw);
1316
1334
  return {
1317
1335
  items: parsed.items,
@@ -1325,12 +1343,12 @@ var HttpDriver = class {
1325
1343
  }
1326
1344
  async get(resource, id) {
1327
1345
  const url = this.buildUrl(resource, id);
1328
- const raw = await this.request(url);
1346
+ const raw = await this._fetch(url);
1329
1347
  return this.preset.response.single(raw);
1330
1348
  }
1331
1349
  async create(resource, data) {
1332
1350
  const url = this.buildUrl(resource);
1333
- const raw = await this.request(url, {
1351
+ const raw = await this._fetch(url, {
1334
1352
  method: "POST",
1335
1353
  body: JSON.stringify(data)
1336
1354
  });
@@ -1338,7 +1356,7 @@ var HttpDriver = class {
1338
1356
  }
1339
1357
  async update(resource, id, data) {
1340
1358
  const url = this.buildUrl(resource, id);
1341
- const raw = await this.request(url, {
1359
+ const raw = await this._fetch(url, {
1342
1360
  method: "PATCH",
1343
1361
  body: JSON.stringify(data)
1344
1362
  });
@@ -1346,7 +1364,7 @@ var HttpDriver = class {
1346
1364
  }
1347
1365
  async delete(resource, id) {
1348
1366
  const url = this.buildUrl(resource, id);
1349
- const raw = await this.request(url, {
1367
+ const raw = await this._fetch(url, {
1350
1368
  method: "DELETE"
1351
1369
  });
1352
1370
  return this.preset.response.single(raw);
@@ -1356,12 +1374,12 @@ var HttpDriver = class {
1356
1374
  const params = filter ? serializeQuery({ filter }, this.preset.query) : new URLSearchParams();
1357
1375
  const queryString = params.toString();
1358
1376
  const fullUrl = queryString ? `${url}?${queryString}` : url;
1359
- const raw = await this.request(fullUrl);
1377
+ const raw = await this._fetch(fullUrl);
1360
1378
  return raw.count ?? raw.data?.count ?? 0;
1361
1379
  }
1362
1380
  async bulkCreate(resource, data) {
1363
1381
  const url = `${this.buildUrl(resource)}/bulk`;
1364
- const raw = await this.request(url, {
1382
+ const raw = await this._fetch(url, {
1365
1383
  method: "POST",
1366
1384
  body: JSON.stringify(data)
1367
1385
  });
@@ -1370,7 +1388,7 @@ var HttpDriver = class {
1370
1388
  }
1371
1389
  async bulkUpdate(resource, updates) {
1372
1390
  const url = `${this.buildUrl(resource)}/bulk`;
1373
- const raw = await this.request(url, {
1391
+ const raw = await this._fetch(url, {
1374
1392
  method: "PATCH",
1375
1393
  body: JSON.stringify(updates)
1376
1394
  });
@@ -1379,12 +1397,24 @@ var HttpDriver = class {
1379
1397
  }
1380
1398
  async bulkDelete(resource, ids) {
1381
1399
  const url = `${this.buildUrl(resource)}/bulk`;
1382
- const raw = await this.request(url, {
1400
+ const raw = await this._fetch(url, {
1383
1401
  method: "DELETE",
1384
1402
  body: JSON.stringify({ ids })
1385
1403
  });
1386
1404
  return { data: { count: raw.count ?? raw.data?.count ?? ids.length } };
1387
1405
  }
1406
+ async request(resource, path, options) {
1407
+ const endpoint = this.getEndpoint(resource);
1408
+ let url = `${this.baseUrl}${endpoint}${path}`;
1409
+ if (options?.query) {
1410
+ const params = new URLSearchParams(options.query);
1411
+ url += `?${params.toString()}`;
1412
+ }
1413
+ return this._fetch(url, {
1414
+ method: options?.method ?? "POST",
1415
+ body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0
1416
+ });
1417
+ }
1388
1418
  // Seed methods are no-ops for HTTP — backend owns data
1389
1419
  seed() {
1390
1420
  }