lazypock 0.19.1 → 0.20.0

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 CHANGED
@@ -449,6 +449,23 @@ PocketBase-style service for the collections themselves (admin):
449
449
  - `getThumbUrl(baseUrl, fileId, size)` — Construct a thumbnail URL for a pre-configured thumb size
450
450
  - `getScaleUrl(baseUrl, fileId, size)` — Construct an on-demand scaled image URL (e.g. `100x100`, `400x`)
451
451
 
452
+ ### Mail (`client.mail`)
453
+
454
+ Send email through the deployment's SMTP settings, and manage its templates. Sending **queues** a
455
+ job (it does not deliver inline); failures surface on the job in `GET /api/logs`.
456
+
457
+ - `mail.send({ to, template, assigns? }, options?)` — Queue a message from a template (API-keyable)
458
+ - `mail.send({ to, subject, html?, text? }, options?)` — Queue a raw message (API-keyable)
459
+ - `mail.listTemplates(options?)` — Every template, with resolved bodies and the layer each part came from (superuser)
460
+ - `mail.getTemplate(name, options?)` — One template (superuser)
461
+ - `mail.saveTemplate(name, { subject, html, text }, options?)` — Replace all three parts; parsed before it is stored (superuser)
462
+ - `mail.deleteTemplate(name, options?)` — Remove the stored parts, falling back to the file or the built-in (superuser)
463
+ - `mail.preview(name, { assigns?, toName?, subject?, html?, text? }, options?)` — Render without storing or sending (superuser)
464
+
465
+ Defining a template is deliberately **not** API-keyable: a template is rendered with the variables of
466
+ an auth email — a verification or password-reset token — so whoever can write one can mail
467
+ themselves somebody else's token.
468
+
452
469
  ### Realtime
453
470
 
454
471
  - `realtime.connect(opts)` — Open the SSE stream (`{ url, token }`)
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ __export(index_exports, {
29
29
  FilterExpr: () => FilterExpr,
30
30
  HttpClient: () => HttpClient,
31
31
  LazypockClient: () => LazypockClient,
32
+ MailService: () => MailService,
32
33
  RealtimeService: () => RealtimeService,
33
34
  TypedClient: () => TypedClient,
34
35
  collectionTypeName: () => collectionTypeName,
@@ -393,6 +394,17 @@ var HttpClient = class {
393
394
  post(path, body, options) {
394
395
  return this.request("POST", path, body, options);
395
396
  }
397
+ /**
398
+ * HTTP PUT — a full replacement. Used where the server models a resource as a
399
+ * complete unit rather than a set of patches (e.g. a mail template, which only
400
+ * exists once all three of its parts do).
401
+ * @param path URL path.
402
+ * @param body Request body.
403
+ * @param options Optional request options.
404
+ */
405
+ put(path, body, options) {
406
+ return this.request("PUT", path, body, options);
407
+ }
396
408
  /**
397
409
  * HTTP PATCH.
398
410
  * @param path URL path.
@@ -943,6 +955,11 @@ function authorizeWithOAuth2Popup(opts) {
943
955
  }
944
956
 
945
957
  // src/collection.ts
958
+ var MAX_PER_PAGE = 500;
959
+ function clampBatch(batch) {
960
+ if (!Number.isFinite(batch)) return MAX_PER_PAGE;
961
+ return Math.min(Math.max(1, Math.floor(batch)), MAX_PER_PAGE);
962
+ }
946
963
  function stableStringify(value) {
947
964
  const seen = /* @__PURE__ */ new Set();
948
965
  const sort = (v) => {
@@ -1439,13 +1456,14 @@ var CollectionService = class _CollectionService {
1439
1456
  return result;
1440
1457
  }
1441
1458
  /**
1442
- * Fetch all records at once (auto-paginates). Mirrors PocketBase's
1459
+ * Fetch all records at once (auto-paginates; `batch` defaults to and is capped at 500, the server max `perPage`). Mirrors PocketBase's
1443
1460
  * `pb.collection(name).getFullList()`.
1444
1461
  *
1445
1462
  * @param options Query params (`sort`, `filter`, `batch`, etc.) + request options.
1446
1463
  */
1447
1464
  async getFullList(options) {
1448
- const { batch = 1e3, ...rest } = options ?? {};
1465
+ const { batch: requestedBatch = MAX_PER_PAGE, ...rest } = options ?? {};
1466
+ const batch = clampBatch(requestedBatch);
1449
1467
  if (typeof rest.filter === "function") {
1450
1468
  rest.filter = this.resolveFilter(rest.filter);
1451
1469
  }
@@ -2338,7 +2356,8 @@ var CollectionsService = class {
2338
2356
  */
2339
2357
  async getFullList(options) {
2340
2358
  if (!this.http) return [];
2341
- const { batch = 1e3, ...rest } = options ?? {};
2359
+ const { batch: requestedBatch = MAX_PER_PAGE, ...rest } = options ?? {};
2360
+ const batch = clampBatch(requestedBatch);
2342
2361
  const {
2343
2362
  requestKey: reqKey,
2344
2363
  singleFlight: _singleFlight,
@@ -2457,6 +2476,147 @@ function normalizeAction2(event) {
2457
2476
  return "update";
2458
2477
  }
2459
2478
 
2479
+ // src/mail.ts
2480
+ function templatePath(name) {
2481
+ return "/mail/templates/" + encodeURIComponent(name.trim());
2482
+ }
2483
+ var MailService = class {
2484
+ constructor(http) {
2485
+ this.http = http;
2486
+ }
2487
+ /**
2488
+ * Queue one message for delivery.
2489
+ *
2490
+ * Either a `template` or a `subject` with a body. The type requires one of
2491
+ * them — a message with neither does not compile — while the server refuses a
2492
+ * message that carries both, and a template with a raw body.
2493
+ *
2494
+ * The address is shape-checked server-side; the SMTP server remains the
2495
+ * authority on whether it exists.
2496
+ *
2497
+ * @param message The template or raw message to send.
2498
+ * @param options Optional request options (signal, custom fetch).
2499
+ * @returns The queued job (`{ queued: true, id }`).
2500
+ *
2501
+ * @example
2502
+ * ```ts
2503
+ * // A template
2504
+ * await client.mail.send({ to: 'ada@example.com', template: 'receipt', assigns: { total: 42 } });
2505
+ *
2506
+ * // A raw message
2507
+ * await client.mail.send({ to: 'ada@example.com', subject: 'Hello', text: 'Hi there' });
2508
+ * ```
2509
+ */
2510
+ async send(message, options) {
2511
+ const body = "template" in message ? {
2512
+ to: message.to,
2513
+ toName: message.toName,
2514
+ template: message.template,
2515
+ assigns: message.assigns
2516
+ } : {
2517
+ to: message.to,
2518
+ toName: message.toName,
2519
+ subject: message.subject,
2520
+ html: message.html,
2521
+ text: message.text
2522
+ };
2523
+ const data = await this.http.request("POST", "/mail", body, options);
2524
+ return data ?? { queued: false, id: "" };
2525
+ }
2526
+ /**
2527
+ * List every template the server can send, with the body each part
2528
+ * currently resolves to and the layer it came from (superuser).
2529
+ *
2530
+ * Unpaginated: there are as many templates as the deployment defined, not
2531
+ * as many as it has sent.
2532
+ */
2533
+ async listTemplates(options) {
2534
+ const data = await this.http.request(
2535
+ "GET",
2536
+ "/mail/templates",
2537
+ void 0,
2538
+ options
2539
+ );
2540
+ return data?.items ?? [];
2541
+ }
2542
+ /** Fetch one template by name (superuser). */
2543
+ async getTemplate(name, options) {
2544
+ const data = await this.http.request(
2545
+ "GET",
2546
+ templatePath(name),
2547
+ void 0,
2548
+ options
2549
+ );
2550
+ return data;
2551
+ }
2552
+ /**
2553
+ * Save all three parts of a template, replacing whatever was stored for them.
2554
+ *
2555
+ * All three at once rather than one at a time: a template is only allowed to
2556
+ * exist once it is complete, so a part-at-a-time write could leave a subject
2557
+ * saved against a body that does not parse. The server parses the bodies
2558
+ * before storing them, so a syntax error is a rejected save rather than a
2559
+ * queued email that fails where nobody is watching (superuser).
2560
+ */
2561
+ async saveTemplate(name, parts, options) {
2562
+ const data = await this.http.put(
2563
+ templatePath(name),
2564
+ { subject: parts.subject, html: parts.html, text: parts.text },
2565
+ options
2566
+ );
2567
+ return data;
2568
+ }
2569
+ /**
2570
+ * Remove the stored bodies, so the template falls back to its file or its
2571
+ * built-in (superuser).
2572
+ *
2573
+ * Deleting one that was never saved succeeds with `deleted: 0` — it is
2574
+ * already at its default — but an unknown *name* is a 404, so a typo cannot
2575
+ * look like a reset that worked.
2576
+ */
2577
+ async deleteTemplate(name, options) {
2578
+ const data = await this.http.request(
2579
+ "DELETE",
2580
+ templatePath(name),
2581
+ void 0,
2582
+ options
2583
+ );
2584
+ return data ?? { name: name.trim(), deleted: 0 };
2585
+ }
2586
+ /**
2587
+ * Render a template the way a send would, without storing or sending
2588
+ * anything (superuser).
2589
+ *
2590
+ * Any of `subject`/`html`/`text` given in `draft` is rendered in place of the
2591
+ * stored part, which is what makes this useful while a body is still being
2592
+ * written. A draft over a name the server does not know is previewed as the
2593
+ * new template it is being written as, so such a draft has to arrive
2594
+ * complete (all three parts).
2595
+ *
2596
+ * Previews share one auto-cancellation key (`POST /mail/templates/<name>/preview`),
2597
+ * so a newer preview aborts an older one: the older promise rejects with an
2598
+ * `ApiError` whose `isAbort` is `true`. That is what an editor wants when a
2599
+ * keystroke supersedes the last render, but pass `{ requestKey: null }` to
2600
+ * keep every preview if you would rather have them all.
2601
+ */
2602
+ async preview(name, draft, options) {
2603
+ const body = {
2604
+ assigns: draft?.assigns,
2605
+ toName: draft?.toName,
2606
+ subject: draft?.subject,
2607
+ html: draft?.html,
2608
+ text: draft?.text
2609
+ };
2610
+ const data = await this.http.request(
2611
+ "POST",
2612
+ templatePath(name) + "/preview",
2613
+ body,
2614
+ options
2615
+ );
2616
+ return data;
2617
+ }
2618
+ };
2619
+
2460
2620
  // src/typegen.ts
2461
2621
  function fieldTypeScriptType(field, fallback = "unknown") {
2462
2622
  const opts = field.options ?? {};
@@ -2828,6 +2988,7 @@ var LazypockClient = class {
2828
2988
  this.authStore.onChange(() => this.realtime.refresh());
2829
2989
  this.authReady.then(() => this.realtime.refresh());
2830
2990
  this.files = new FilesService(this.http);
2991
+ this.mail = new MailService(this.http);
2831
2992
  this.collections = new CollectionsService(this.http, this.realtime);
2832
2993
  if (options.types?.schemas) {
2833
2994
  this.schemaByName = new Map(
@@ -3066,6 +3227,7 @@ function createClient(options) {
3066
3227
  FilterExpr,
3067
3228
  HttpClient,
3068
3229
  LazypockClient,
3230
+ MailService,
3069
3231
  RealtimeService,
3070
3232
  TypedClient,
3071
3233
  collectionTypeName,