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.cjs CHANGED
@@ -243,6 +243,16 @@ var Service = class {
243
243
  }
244
244
  };
245
245
  }
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
+ }
254
+ return this.driver.request(this.resourceName, path, options);
255
+ }
246
256
  emitEvent(action, extra) {
247
257
  if (!this._eventBus) return;
248
258
  this._eventBus.emit({
@@ -887,6 +897,14 @@ var LocalDriver = class {
887
897
  }
888
898
  return { data: { count } };
889
899
  }
900
+ async request(_resource, _path, options) {
901
+ if (options?.local) {
902
+ return options.local();
903
+ }
904
+ throw new Error(
905
+ "service.request() is only available with the HTTP driver. Provide a `local` handler or switch to HTTP driver."
906
+ );
907
+ }
890
908
  // --- Seed management (synchronous) ---
891
909
  seed(resource, data, entityClass) {
892
910
  for (let i = 0; i < data.length; i++) {
@@ -1261,7 +1279,7 @@ var HttpDriver = class {
1261
1279
  }
1262
1280
  return headers;
1263
1281
  }
1264
- async request(url, options = {}, retryCount = 0) {
1282
+ async _fetch(url, options = {}, retryCount = 0) {
1265
1283
  const controller = new AbortController();
1266
1284
  const timer = setTimeout(() => controller.abort(), this.timeout);
1267
1285
  try {
@@ -1275,7 +1293,7 @@ var HttpDriver = class {
1275
1293
  if (response.status >= 500 && retryCount < this.maxRetries) {
1276
1294
  const delay = this.baseDelay * Math.pow(2, retryCount);
1277
1295
  await new Promise((r) => setTimeout(r, delay));
1278
- return this.request(url, options, retryCount + 1);
1296
+ return this._fetch(url, options, retryCount + 1);
1279
1297
  }
1280
1298
  const body = await response.json().catch(() => ({}));
1281
1299
  this.throwMappedError(response.status, body);
@@ -1313,7 +1331,7 @@ var HttpDriver = class {
1313
1331
  const params = serializeQuery(query, this.preset.query);
1314
1332
  const queryString = params.toString();
1315
1333
  const fullUrl = queryString ? `${url}?${queryString}` : url;
1316
- const raw = await this.request(fullUrl);
1334
+ const raw = await this._fetch(fullUrl);
1317
1335
  const parsed = this.preset.response.list(raw);
1318
1336
  return {
1319
1337
  items: parsed.items,
@@ -1327,12 +1345,12 @@ var HttpDriver = class {
1327
1345
  }
1328
1346
  async get(resource, id) {
1329
1347
  const url = this.buildUrl(resource, id);
1330
- const raw = await this.request(url);
1348
+ const raw = await this._fetch(url);
1331
1349
  return this.preset.response.single(raw);
1332
1350
  }
1333
1351
  async create(resource, data) {
1334
1352
  const url = this.buildUrl(resource);
1335
- const raw = await this.request(url, {
1353
+ const raw = await this._fetch(url, {
1336
1354
  method: "POST",
1337
1355
  body: JSON.stringify(data)
1338
1356
  });
@@ -1340,7 +1358,7 @@ var HttpDriver = class {
1340
1358
  }
1341
1359
  async update(resource, id, data) {
1342
1360
  const url = this.buildUrl(resource, id);
1343
- const raw = await this.request(url, {
1361
+ const raw = await this._fetch(url, {
1344
1362
  method: "PATCH",
1345
1363
  body: JSON.stringify(data)
1346
1364
  });
@@ -1348,7 +1366,7 @@ var HttpDriver = class {
1348
1366
  }
1349
1367
  async delete(resource, id) {
1350
1368
  const url = this.buildUrl(resource, id);
1351
- const raw = await this.request(url, {
1369
+ const raw = await this._fetch(url, {
1352
1370
  method: "DELETE"
1353
1371
  });
1354
1372
  return this.preset.response.single(raw);
@@ -1358,12 +1376,12 @@ var HttpDriver = class {
1358
1376
  const params = filter ? serializeQuery({ filter }, this.preset.query) : new URLSearchParams();
1359
1377
  const queryString = params.toString();
1360
1378
  const fullUrl = queryString ? `${url}?${queryString}` : url;
1361
- const raw = await this.request(fullUrl);
1379
+ const raw = await this._fetch(fullUrl);
1362
1380
  return raw.count ?? raw.data?.count ?? 0;
1363
1381
  }
1364
1382
  async bulkCreate(resource, data) {
1365
1383
  const url = `${this.buildUrl(resource)}/bulk`;
1366
- const raw = await this.request(url, {
1384
+ const raw = await this._fetch(url, {
1367
1385
  method: "POST",
1368
1386
  body: JSON.stringify(data)
1369
1387
  });
@@ -1372,7 +1390,7 @@ var HttpDriver = class {
1372
1390
  }
1373
1391
  async bulkUpdate(resource, updates) {
1374
1392
  const url = `${this.buildUrl(resource)}/bulk`;
1375
- const raw = await this.request(url, {
1393
+ const raw = await this._fetch(url, {
1376
1394
  method: "PATCH",
1377
1395
  body: JSON.stringify(updates)
1378
1396
  });
@@ -1381,12 +1399,24 @@ var HttpDriver = class {
1381
1399
  }
1382
1400
  async bulkDelete(resource, ids) {
1383
1401
  const url = `${this.buildUrl(resource)}/bulk`;
1384
- const raw = await this.request(url, {
1402
+ const raw = await this._fetch(url, {
1385
1403
  method: "DELETE",
1386
1404
  body: JSON.stringify({ ids })
1387
1405
  });
1388
1406
  return { data: { count: raw.count ?? raw.data?.count ?? ids.length } };
1389
1407
  }
1408
+ async request(resource, path, options) {
1409
+ const endpoint = this.getEndpoint(resource);
1410
+ let url = `${this.baseUrl}${endpoint}${path}`;
1411
+ if (options?.query) {
1412
+ const params = new URLSearchParams(options.query);
1413
+ url += `?${params.toString()}`;
1414
+ }
1415
+ return this._fetch(url, {
1416
+ method: options?.method ?? "POST",
1417
+ body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0
1418
+ });
1419
+ }
1390
1420
  // Seed methods are no-ops for HTTP — backend owns data
1391
1421
  seed() {
1392
1422
  }