lazypock 0.1.3 → 0.1.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/src/lazypock.ts CHANGED
@@ -21,7 +21,14 @@ import {
21
21
  type RequestOptions,
22
22
  } from "./types";
23
23
  import { RealtimeService, wsUrlFromBaseUrl } from "./realtime";
24
- import { FilesService, getFileUrl, type FileRecord } from "./files";
24
+ import {
25
+ FilesService,
26
+ getFileUrl,
27
+ getThumbUrl,
28
+ getScaleUrl,
29
+ type FileRecord,
30
+ } from "./files";
31
+ import { CollectionsService } from "./collections";
25
32
  import type { CollectionSchema, SchemaField } from "./schema";
26
33
  import { generateTypes, collectionTypeName } from "./codegen";
27
34
  import { fieldTypeScriptType, fieldTypeKind, schemaFieldType } from "./typegen";
@@ -29,11 +36,15 @@ import { fieldTypeScriptType, fieldTypeKind, schemaFieldType } from "./typegen";
29
36
  export {
30
37
  AuthStore,
31
38
  ApiError,
39
+ HttpClient,
32
40
  RealtimeService,
33
41
  wsUrlFromBaseUrl,
34
42
  FilesService,
35
43
  getFileUrl,
44
+ getThumbUrl,
45
+ getScaleUrl,
36
46
  CollectionService,
47
+ CollectionsService,
37
48
  generateTypes,
38
49
  collectionTypeName,
39
50
  fieldTypeScriptType,
@@ -88,13 +99,14 @@ export interface LazypockClientOptions {
88
99
  * ```ts
89
100
  * const client = new LazypockClient({ baseUrl: 'http://localhost:4000/api' });
90
101
  * await client.login('admin@example.com', 'password');
91
- * const posts = await client.collection('posts').list();
102
+ * const posts = await client.collection('posts').getList();
92
103
  * ```
93
104
  */
94
105
  export class LazypockClient {
95
106
  readonly http: HttpClient;
96
107
  readonly authStore: AuthStore;
97
108
  readonly realtime: RealtimeService;
109
+ readonly collections: CollectionsService;
98
110
  readonly files: FilesService;
99
111
  private collectionCache = new Map<string, CollectionService>();
100
112
  private schemaByName?: Map<string, CollectionSchema>;
@@ -114,6 +126,7 @@ export class LazypockClient {
114
126
  this.realtime.setUrl(wsUrlFromBaseUrl(baseUrl));
115
127
  }
116
128
  this.files = new FilesService(this.http);
129
+ this.collections = new CollectionsService(this.http, this.realtime);
117
130
  if (options.types?.schemas) {
118
131
  this.schemaByName = new Map(
119
132
  options.types.schemas.map((s) => [s.name, s]),
@@ -167,6 +180,41 @@ export class LazypockClient {
167
180
  return generateTypes(schemas, options);
168
181
  }
169
182
 
183
+ // ── Auto-cancellation (PocketBase `autoCancellation` parity) ──
184
+
185
+ /**
186
+ * Globally enable or disable auto-cancellation of duplicated pending requests.
187
+ *
188
+ * When enabled (default), a new request whose `requestKey` (default
189
+ * `HTTP_METHOD + path`) matches a still-pending request aborts the previous
190
+ * one — only the last duplicate executes.
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * client.autoCancellation(false); // keep every request
195
+ * ```
196
+ */
197
+ autoCancellation(enable: boolean): this {
198
+ this.http.autoCancellation(enable);
199
+ return this;
200
+ }
201
+
202
+ /**
203
+ * Abort a single pending request by its cancellation key
204
+ * (default `HTTP_METHOD + path`, e.g. `"GET /api/posts?page=1"`).
205
+ * The request rejects with an `ApiError` whose `isAbort` is `true`.
206
+ */
207
+ cancelRequest(requestKey: string): this {
208
+ this.http.cancelRequest(requestKey);
209
+ return this;
210
+ }
211
+
212
+ /** Abort all pending requests. */
213
+ cancelAllRequests(): this {
214
+ this.http.cancelAllRequests();
215
+ return this;
216
+ }
217
+
170
218
  // ── Auth ──
171
219
 
172
220
  /** Check whether any superuser exists (for login vs setup screen routing). */
@@ -318,173 +366,4 @@ export class LazypockClient {
318
366
  health(options?: RequestOptions): Promise<Record<string, unknown> | null> {
319
367
  return this.http.get<Record<string, unknown>>("/health", options);
320
368
  }
321
-
322
- // ── Collection Management (admin) ──
323
-
324
- /**
325
- * List all collections (admin).
326
- * @param q URL query string (e.g. `page=1&perPage=200`).
327
- * @param options Optional request options.
328
- */
329
- listCollections(
330
- q?: string,
331
- options?: RequestOptions,
332
- ): Promise<ListResult<ApiRecord> | null> {
333
- return this.http.get<ListResult<ApiRecord>>(
334
- "/collections" + (q ? "?" + q : ""),
335
- options,
336
- );
337
- }
338
-
339
- /**
340
- * Get a single collection by ID or name.
341
- * @param id Collection ID or name.
342
- * @param options Optional request options.
343
- */
344
- getCollection(
345
- id: string,
346
- options?: RequestOptions,
347
- ): Promise<ApiRecord | null> {
348
- return this.http.get<ApiRecord>(
349
- "/collections/" + encodeURIComponent(id),
350
- options,
351
- );
352
- }
353
-
354
- /**
355
- * Create a new collection (admin).
356
- * @param data Collection definition (name, type, fields, options, rules, etc.).
357
- * @param options Optional request options.
358
- */
359
- createCollection(
360
- data: Record<string, unknown>,
361
- options?: RequestOptions,
362
- ): Promise<ApiRecord | null> {
363
- return this.http.post<ApiRecord>("/collections", data, options);
364
- }
365
-
366
- /**
367
- * Update an existing collection (admin).
368
- * @param id Collection ID or name.
369
- * @param data Updated collection fields.
370
- * @param options Optional request options.
371
- */
372
- updateCollection(
373
- id: string,
374
- data: Record<string, unknown>,
375
- options?: RequestOptions,
376
- ): Promise<ApiRecord | null> {
377
- return this.http.patch<ApiRecord>(
378
- "/collections/" + encodeURIComponent(id),
379
- data,
380
- options,
381
- );
382
- }
383
-
384
- /**
385
- * Delete a collection (admin).
386
- * @param id Collection ID or name.
387
- * @param options Optional request options.
388
- */
389
- deleteCollection(id: string, options?: RequestOptions): Promise<null> {
390
- return this.http.delete("/collections/" + encodeURIComponent(id), options);
391
- }
392
-
393
- // ── Records (dynamic collection) ──
394
-
395
- /**
396
- * List records from a dynamic collection with optional filter/sort/pagination.
397
- *
398
- * @param coll Collection name.
399
- * @param params Query parameters including:
400
- * - `filter` — PocketBase filter syntax (e.g. `title~'hello' && published=true`)
401
- * - `sort` — Comma-separated, `-` prefix for DESC (e.g. `-created,title`)
402
- * - `page` — Page number (default: 1)
403
- * - `perPage` — Items per page (default: 30, max: 200)
404
- * - `expand` — Comma-separated relation fields (e.g. `author,category`)
405
- * @param options Optional request options.
406
- */
407
- listRecords(
408
- coll: string,
409
- params?: Record<string, string>,
410
- options?: RequestOptions,
411
- ): Promise<ListResult<ApiRecord> | null> {
412
- const qs = params ? "?" + new URLSearchParams(params).toString() : "";
413
- return this.http.get<ListResult<ApiRecord>>(
414
- "/" + encodeURIComponent(coll) + qs,
415
- options,
416
- );
417
- }
418
-
419
- /**
420
- * Get a single record by ID.
421
- * @param coll Collection name.
422
- * @param id Record ID.
423
- * @param options Optional request options.
424
- */
425
- getRecord(
426
- coll: string,
427
- id: string,
428
- options?: RequestOptions,
429
- ): Promise<ApiRecord | null> {
430
- return this.http.get<ApiRecord>(
431
- "/" + encodeURIComponent(coll) + "/" + encodeURIComponent(id),
432
- options,
433
- );
434
- }
435
-
436
- /**
437
- * Create a record in a dynamic collection.
438
- * @param coll Collection name.
439
- * @param data Record fields.
440
- * @param options Optional request options.
441
- */
442
- createRecord(
443
- coll: string,
444
- data: Record<string, unknown>,
445
- options?: RequestOptions,
446
- ): Promise<ApiRecord | null> {
447
- return this.http.post<ApiRecord>(
448
- "/" + encodeURIComponent(coll),
449
- data,
450
- options,
451
- );
452
- }
453
-
454
- /**
455
- * Update a record in a dynamic collection.
456
- * @param coll Collection name.
457
- * @param id Record ID.
458
- * @param data Updated record fields.
459
- * @param options Optional request options.
460
- */
461
- updateRecord(
462
- coll: string,
463
- id: string,
464
- data: Record<string, unknown>,
465
- options?: RequestOptions,
466
- ): Promise<ApiRecord | null> {
467
- return this.http.patch<ApiRecord>(
468
- "/" + encodeURIComponent(coll) + "/" + encodeURIComponent(id),
469
- data,
470
- options,
471
- );
472
- }
473
-
474
- /**
475
- * Delete a record from a dynamic collection.
476
- * @param coll Collection name.
477
- * @param id Record ID.
478
- * @param options Optional request options.
479
- */
480
- deleteRecord(
481
- coll: string,
482
- id: string,
483
- options?: RequestOptions,
484
- ): Promise<null> {
485
- return this.http.delete(
486
- "/" + encodeURIComponent(coll) + "/" + encodeURIComponent(id),
487
- options,
488
- );
489
- }
490
369
  }
package/src/types.ts CHANGED
@@ -65,16 +65,45 @@ export interface RequestOptions {
65
65
  signal?: AbortSignal;
66
66
  /** Custom fetch implementation (for RN or test mocking) */
67
67
  fetch?: typeof globalThis.fetch;
68
+ /**
69
+ * Request identifier used by the auto-cancellation mechanism.
70
+ *
71
+ * Pending requests sharing the same key cancel each other — only the
72
+ * last one is executed (PocketBase `requestKey` semantics).
73
+ *
74
+ * - `string` — use this exact key instead of the default `METHOD + path`.
75
+ * - `null` — disable auto-cancellation for this request (never auto-cancelled).
76
+ *
77
+ * @default `${method} ${path}`
78
+ */
79
+ requestKey?: string | null;
80
+ /**
81
+ * Disable auto-cancellation for this request.
82
+ * Alias of `requestKey: null` (PocketBase `$autoCancel: false` compat).
83
+ */
84
+ autoCancel?: boolean;
85
+ /**
86
+ * Custom request key used for auto-cancellation.
87
+ * Alias of `requestKey` (PocketBase `$cancelKey` compat).
88
+ */
89
+ cancelKey?: string;
68
90
  }
69
91
 
70
92
  export class ApiError extends Error {
71
93
  readonly data: unknown;
72
94
  readonly status: number;
95
+ /**
96
+ * `true` when this error was caused by an aborted/cancelled request
97
+ * (auto-cancelled duplicate, or manually via `cancelRequest()` /
98
+ * `cancelAllRequests()` / an external AbortSignal).
99
+ */
100
+ readonly isAbort: boolean;
73
101
 
74
- constructor(message: string, data: unknown, status: number) {
102
+ constructor(message: string, data: unknown, status: number, isAbort = false) {
75
103
  super(message);
76
104
  this.name = "ApiError";
77
105
  this.data = data;
78
106
  this.status = status;
107
+ this.isAbort = isAbort;
79
108
  }
80
109
  }