fauxbase 0.5.2 → 0.5.4
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/README.md +26 -0
- package/dist/index.cjs +31 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +31 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -137,6 +137,11 @@ 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
|
+
}): Promise<R>;
|
|
140
145
|
seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
|
|
141
146
|
getSeedVersion(): string | null;
|
|
142
147
|
setSeedVersion(version: string): void;
|
|
@@ -223,6 +228,11 @@ declare abstract class Service<T extends Entity> {
|
|
|
223
228
|
count: number;
|
|
224
229
|
}>>;
|
|
225
230
|
};
|
|
231
|
+
request<R = any>(path: string, options?: {
|
|
232
|
+
method?: string;
|
|
233
|
+
body?: any;
|
|
234
|
+
query?: Record<string, string>;
|
|
235
|
+
}): Promise<R>;
|
|
226
236
|
private emitEvent;
|
|
227
237
|
private runHooks;
|
|
228
238
|
}
|
|
@@ -253,7 +263,7 @@ declare class HttpDriver implements Driver {
|
|
|
253
263
|
private getEndpoint;
|
|
254
264
|
private buildUrl;
|
|
255
265
|
private buildHeaders;
|
|
256
|
-
private
|
|
266
|
+
private _fetch;
|
|
257
267
|
private throwMappedError;
|
|
258
268
|
list<T>(resource: string, query: QueryParams): Promise<PagedResponse<T>>;
|
|
259
269
|
get<T>(resource: string, id: string): Promise<ApiResponse<T>>;
|
|
@@ -269,6 +279,11 @@ declare class HttpDriver implements Driver {
|
|
|
269
279
|
bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
|
|
270
280
|
count: number;
|
|
271
281
|
}>>;
|
|
282
|
+
request<R = any>(resource: string, path: string, options?: {
|
|
283
|
+
method?: string;
|
|
284
|
+
body?: any;
|
|
285
|
+
query?: Record<string, string>;
|
|
286
|
+
}): Promise<R>;
|
|
272
287
|
seed(): void;
|
|
273
288
|
getSeedVersion(): string | null;
|
|
274
289
|
setSeedVersion(): void;
|
|
@@ -380,6 +395,11 @@ declare class LocalDriver implements Driver {
|
|
|
380
395
|
bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
|
|
381
396
|
count: number;
|
|
382
397
|
}>>;
|
|
398
|
+
request<R = any>(_resource: string, _path: string, _options?: {
|
|
399
|
+
method?: string;
|
|
400
|
+
body?: any;
|
|
401
|
+
query?: Record<string, string>;
|
|
402
|
+
}): Promise<R>;
|
|
383
403
|
seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
|
|
384
404
|
getSeedVersion(): string | null;
|
|
385
405
|
setSeedVersion(version: string): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -137,6 +137,11 @@ 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
|
+
}): Promise<R>;
|
|
140
145
|
seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
|
|
141
146
|
getSeedVersion(): string | null;
|
|
142
147
|
setSeedVersion(version: string): void;
|
|
@@ -223,6 +228,11 @@ declare abstract class Service<T extends Entity> {
|
|
|
223
228
|
count: number;
|
|
224
229
|
}>>;
|
|
225
230
|
};
|
|
231
|
+
request<R = any>(path: string, options?: {
|
|
232
|
+
method?: string;
|
|
233
|
+
body?: any;
|
|
234
|
+
query?: Record<string, string>;
|
|
235
|
+
}): Promise<R>;
|
|
226
236
|
private emitEvent;
|
|
227
237
|
private runHooks;
|
|
228
238
|
}
|
|
@@ -253,7 +263,7 @@ declare class HttpDriver implements Driver {
|
|
|
253
263
|
private getEndpoint;
|
|
254
264
|
private buildUrl;
|
|
255
265
|
private buildHeaders;
|
|
256
|
-
private
|
|
266
|
+
private _fetch;
|
|
257
267
|
private throwMappedError;
|
|
258
268
|
list<T>(resource: string, query: QueryParams): Promise<PagedResponse<T>>;
|
|
259
269
|
get<T>(resource: string, id: string): Promise<ApiResponse<T>>;
|
|
@@ -269,6 +279,11 @@ declare class HttpDriver implements Driver {
|
|
|
269
279
|
bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
|
|
270
280
|
count: number;
|
|
271
281
|
}>>;
|
|
282
|
+
request<R = any>(resource: string, path: string, options?: {
|
|
283
|
+
method?: string;
|
|
284
|
+
body?: any;
|
|
285
|
+
query?: Record<string, string>;
|
|
286
|
+
}): Promise<R>;
|
|
272
287
|
seed(): void;
|
|
273
288
|
getSeedVersion(): string | null;
|
|
274
289
|
setSeedVersion(): void;
|
|
@@ -380,6 +395,11 @@ declare class LocalDriver implements Driver {
|
|
|
380
395
|
bulkDelete(resource: string, ids: string[]): Promise<ApiResponse<{
|
|
381
396
|
count: number;
|
|
382
397
|
}>>;
|
|
398
|
+
request<R = any>(_resource: string, _path: string, _options?: {
|
|
399
|
+
method?: string;
|
|
400
|
+
body?: any;
|
|
401
|
+
query?: Record<string, string>;
|
|
402
|
+
}): Promise<R>;
|
|
383
403
|
seed(resource: string, data: Array<Record<string, any>>, entityClass: Function): void;
|
|
384
404
|
getSeedVersion(): string | null;
|
|
385
405
|
setSeedVersion(version: string): void;
|
package/dist/index.js
CHANGED
|
@@ -241,6 +241,9 @@ var Service = class {
|
|
|
241
241
|
}
|
|
242
242
|
};
|
|
243
243
|
}
|
|
244
|
+
async request(path, options) {
|
|
245
|
+
return this.driver.request(this.resourceName, path, options);
|
|
246
|
+
}
|
|
244
247
|
emitEvent(action, extra) {
|
|
245
248
|
if (!this._eventBus) return;
|
|
246
249
|
this._eventBus.emit({
|
|
@@ -885,6 +888,11 @@ var LocalDriver = class {
|
|
|
885
888
|
}
|
|
886
889
|
return { data: { count } };
|
|
887
890
|
}
|
|
891
|
+
async request(_resource, _path, _options) {
|
|
892
|
+
throw new Error(
|
|
893
|
+
"service.request() is only available with the HTTP driver. Local driver does not support custom endpoints."
|
|
894
|
+
);
|
|
895
|
+
}
|
|
888
896
|
// --- Seed management (synchronous) ---
|
|
889
897
|
seed(resource, data, entityClass) {
|
|
890
898
|
for (let i = 0; i < data.length; i++) {
|
|
@@ -1259,7 +1267,7 @@ var HttpDriver = class {
|
|
|
1259
1267
|
}
|
|
1260
1268
|
return headers;
|
|
1261
1269
|
}
|
|
1262
|
-
async
|
|
1270
|
+
async _fetch(url, options = {}, retryCount = 0) {
|
|
1263
1271
|
const controller = new AbortController();
|
|
1264
1272
|
const timer = setTimeout(() => controller.abort(), this.timeout);
|
|
1265
1273
|
try {
|
|
@@ -1273,7 +1281,7 @@ var HttpDriver = class {
|
|
|
1273
1281
|
if (response.status >= 500 && retryCount < this.maxRetries) {
|
|
1274
1282
|
const delay = this.baseDelay * Math.pow(2, retryCount);
|
|
1275
1283
|
await new Promise((r) => setTimeout(r, delay));
|
|
1276
|
-
return this.
|
|
1284
|
+
return this._fetch(url, options, retryCount + 1);
|
|
1277
1285
|
}
|
|
1278
1286
|
const body = await response.json().catch(() => ({}));
|
|
1279
1287
|
this.throwMappedError(response.status, body);
|
|
@@ -1311,7 +1319,7 @@ var HttpDriver = class {
|
|
|
1311
1319
|
const params = serializeQuery(query, this.preset.query);
|
|
1312
1320
|
const queryString = params.toString();
|
|
1313
1321
|
const fullUrl = queryString ? `${url}?${queryString}` : url;
|
|
1314
|
-
const raw = await this.
|
|
1322
|
+
const raw = await this._fetch(fullUrl);
|
|
1315
1323
|
const parsed = this.preset.response.list(raw);
|
|
1316
1324
|
return {
|
|
1317
1325
|
items: parsed.items,
|
|
@@ -1325,12 +1333,12 @@ var HttpDriver = class {
|
|
|
1325
1333
|
}
|
|
1326
1334
|
async get(resource, id) {
|
|
1327
1335
|
const url = this.buildUrl(resource, id);
|
|
1328
|
-
const raw = await this.
|
|
1336
|
+
const raw = await this._fetch(url);
|
|
1329
1337
|
return this.preset.response.single(raw);
|
|
1330
1338
|
}
|
|
1331
1339
|
async create(resource, data) {
|
|
1332
1340
|
const url = this.buildUrl(resource);
|
|
1333
|
-
const raw = await this.
|
|
1341
|
+
const raw = await this._fetch(url, {
|
|
1334
1342
|
method: "POST",
|
|
1335
1343
|
body: JSON.stringify(data)
|
|
1336
1344
|
});
|
|
@@ -1338,7 +1346,7 @@ var HttpDriver = class {
|
|
|
1338
1346
|
}
|
|
1339
1347
|
async update(resource, id, data) {
|
|
1340
1348
|
const url = this.buildUrl(resource, id);
|
|
1341
|
-
const raw = await this.
|
|
1349
|
+
const raw = await this._fetch(url, {
|
|
1342
1350
|
method: "PATCH",
|
|
1343
1351
|
body: JSON.stringify(data)
|
|
1344
1352
|
});
|
|
@@ -1346,7 +1354,7 @@ var HttpDriver = class {
|
|
|
1346
1354
|
}
|
|
1347
1355
|
async delete(resource, id) {
|
|
1348
1356
|
const url = this.buildUrl(resource, id);
|
|
1349
|
-
const raw = await this.
|
|
1357
|
+
const raw = await this._fetch(url, {
|
|
1350
1358
|
method: "DELETE"
|
|
1351
1359
|
});
|
|
1352
1360
|
return this.preset.response.single(raw);
|
|
@@ -1356,12 +1364,12 @@ var HttpDriver = class {
|
|
|
1356
1364
|
const params = filter ? serializeQuery({ filter }, this.preset.query) : new URLSearchParams();
|
|
1357
1365
|
const queryString = params.toString();
|
|
1358
1366
|
const fullUrl = queryString ? `${url}?${queryString}` : url;
|
|
1359
|
-
const raw = await this.
|
|
1367
|
+
const raw = await this._fetch(fullUrl);
|
|
1360
1368
|
return raw.count ?? raw.data?.count ?? 0;
|
|
1361
1369
|
}
|
|
1362
1370
|
async bulkCreate(resource, data) {
|
|
1363
1371
|
const url = `${this.buildUrl(resource)}/bulk`;
|
|
1364
|
-
const raw = await this.
|
|
1372
|
+
const raw = await this._fetch(url, {
|
|
1365
1373
|
method: "POST",
|
|
1366
1374
|
body: JSON.stringify(data)
|
|
1367
1375
|
});
|
|
@@ -1370,7 +1378,7 @@ var HttpDriver = class {
|
|
|
1370
1378
|
}
|
|
1371
1379
|
async bulkUpdate(resource, updates) {
|
|
1372
1380
|
const url = `${this.buildUrl(resource)}/bulk`;
|
|
1373
|
-
const raw = await this.
|
|
1381
|
+
const raw = await this._fetch(url, {
|
|
1374
1382
|
method: "PATCH",
|
|
1375
1383
|
body: JSON.stringify(updates)
|
|
1376
1384
|
});
|
|
@@ -1379,12 +1387,24 @@ var HttpDriver = class {
|
|
|
1379
1387
|
}
|
|
1380
1388
|
async bulkDelete(resource, ids) {
|
|
1381
1389
|
const url = `${this.buildUrl(resource)}/bulk`;
|
|
1382
|
-
const raw = await this.
|
|
1390
|
+
const raw = await this._fetch(url, {
|
|
1383
1391
|
method: "DELETE",
|
|
1384
1392
|
body: JSON.stringify({ ids })
|
|
1385
1393
|
});
|
|
1386
1394
|
return { data: { count: raw.count ?? raw.data?.count ?? ids.length } };
|
|
1387
1395
|
}
|
|
1396
|
+
async request(resource, path, options) {
|
|
1397
|
+
const endpoint = this.getEndpoint(resource);
|
|
1398
|
+
let url = `${this.baseUrl}${endpoint}${path}`;
|
|
1399
|
+
if (options?.query) {
|
|
1400
|
+
const params = new URLSearchParams(options.query);
|
|
1401
|
+
url += `?${params.toString()}`;
|
|
1402
|
+
}
|
|
1403
|
+
return this._fetch(url, {
|
|
1404
|
+
method: options?.method ?? "POST",
|
|
1405
|
+
body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1388
1408
|
// Seed methods are no-ops for HTTP — backend owns data
|
|
1389
1409
|
seed() {
|
|
1390
1410
|
}
|