@kabobcompany/lift 0.2.0 → 0.4.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
@@ -30,6 +30,14 @@ and `storage` are the rest. An app reaches only its own files.
30
30
  `handleJob(request, handlers)` answers Lift's call when a job or a schedule from the app's
31
31
  `lift.json` is due; `jobRuns`, `jobRun`, `cancelJob` and `jobs` show how they went.
32
32
 
33
+ `sendEmail({ to, subject, text, html })` sends an email from the app's own address and
34
+ `sendSms({ to, text })` a text message to one phone, both on Kabob's account and within
35
+ the limits Lift sets; `email()` and `sms()` say what those are and what was sent today.
36
+
37
+ `channelTicket({ channels, client })` lets one client of the app listen to channels over a
38
+ long connection to Lift, and `publish(channel, event, data)` says something to everyone
39
+ listening; `listeners` and `channels` show who is connected.
40
+
33
41
  A refusal is thrown as a `LiftError` with `status`, `code`, `message` and, on a 422,
34
42
  `issues`.
35
43
 
package/dist/index.d.ts CHANGED
@@ -427,4 +427,147 @@ export declare const jobs: () => Promise<{
427
427
  * is tried again. Make handlers safe to repeat.
428
428
  */
429
429
  export declare function handleJob(request: Request, handlers: Record<string, JobHandler>): Promise<Response>;
430
+ export type SentEmail = {
431
+ id: string;
432
+ from: string;
433
+ to: string[];
434
+ };
435
+ /**
436
+ * Send an email:
437
+ *
438
+ * await sendEmail({ to: customer.email, subject: "Your try-on", html, text }, { tag: `brand-${brand.id}` })
439
+ *
440
+ * It comes from the app's own address (`<app>@lift.kabob.io`) under the
441
+ * app's name; set `replyTo` to where answers should go. Give `text`, `html`
442
+ * or both (256 KB together at most), to 10 addresses at most. There are no
443
+ * attachments: put the file in storage and link to it with `fileUrl`.
444
+ *
445
+ * Throws a `LiftError`: 429 `daily_limit` (the app sent its day's worth),
446
+ * 429 `recipient_limit` (that address got enough from this app for now),
447
+ * 422 `refused` (the mail service won't send this one; `message` says why).
448
+ */
449
+ export declare function sendEmail(message: {
450
+ to: string | string[];
451
+ subject: string;
452
+ text?: string;
453
+ html?: string;
454
+ replyTo?: string | string[];
455
+ }, options?: Paid): Promise<SentEmail>;
456
+ /** The app's address, how many emails it sent today, and its limits. */
457
+ export declare const email: (options?: Paid) => Promise<{
458
+ from: string;
459
+ sent_today: number;
460
+ limits: {
461
+ app_per_day: number;
462
+ max_recipients: number;
463
+ max_bytes: number;
464
+ };
465
+ }>;
466
+ export type SentSms = {
467
+ id: string;
468
+ to: string;
469
+ segments: number;
470
+ };
471
+ /**
472
+ * Send a text message to one phone:
473
+ *
474
+ * await sendSms({ to: "+886912345678", text: `Your code is ${code}` }, { tag })
475
+ *
476
+ * `to` has its country code; Lift only texts the countries it lists
477
+ * (`sms()`), Taiwan unless set otherwise. Every message costs money by
478
+ * segment: 160 plain letters, or 70 with Chinese or emoji, and three
479
+ * segments at most. Send one only when a person asked for it, and never let
480
+ * a visitor choose the text.
481
+ *
482
+ * Throws a `LiftError`: 403 `country_not_allowed`, 413 `too_long`, 429
483
+ * `daily_limit`, 429 `recipient_limit` (a number gets a few an hour), 422
484
+ * `refused` (the network won't send this one; `message` says why).
485
+ */
486
+ export declare function sendSms(message: {
487
+ to: string;
488
+ text: string;
489
+ }, options?: Paid): Promise<SentSms>;
490
+ /** How many segments the app sent today, its limits, and the countries Lift texts. */
491
+ export declare const sms: (options?: Paid) => Promise<{
492
+ sent_today: number;
493
+ limits: {
494
+ app_per_day: number;
495
+ max_segments: number;
496
+ countries: string[];
497
+ };
498
+ }>;
499
+ export type ChannelTicket = {
500
+ /** What the client presents. */
501
+ ticket: string;
502
+ /** Where the client connects with it: `new EventSource(url)`. */
503
+ url: string;
504
+ expires_at: string;
505
+ };
506
+ /**
507
+ * A ticket for one client, naming the channels it may listen to:
508
+ *
509
+ * app.get("/api/ticket", requireCompany, async (c) =>
510
+ * c.json(await channelTicket({ channels: [`company-${c.get("company").id}`], client: c.get("session").user.id })))
511
+ *
512
+ * Give one only to a client the app has recognised (a session, a paired
513
+ * device), and only for channels that client should hear: whoever holds the
514
+ * ticket hears them. `client` is the app's own word for who this is and
515
+ * `meta` a small object, both shown back in `listeners`. It works for `ttl`
516
+ * seconds (an hour unless said, a day at most); the stream ends then with a
517
+ * `lift.expired` event and the client asks for a new one. 20 channels a
518
+ * ticket at most.
519
+ */
520
+ export declare const channelTicket: (ask: {
521
+ channels: string[];
522
+ client?: string | number;
523
+ meta?: Record<string, unknown>;
524
+ ttl?: number;
525
+ }) => Promise<ChannelTicket>;
526
+ /**
527
+ * Say something to everyone listening to a channel:
528
+ *
529
+ * await publish(`brand-${brand.id}`, "content", { version: 12 }, { retain: true })
530
+ *
531
+ * `event` is one word for what happened and `data` small JSON (16 KB): say
532
+ * what changed and let the client fetch it. With `retain` the message is
533
+ * also kept as the channel's latest, and whoever connects later gets it
534
+ * first. Answers with the message's id and how many were listening.
535
+ *
536
+ * A channel needs no creating: any name of letters, digits, dashes, dots,
537
+ * colons and underscores. A message reaches who is connected now; it is not
538
+ * a queue, so what must not be lost also goes in the app's database.
539
+ */
540
+ export declare const publish: (channel: string, event: string, data?: unknown, options?: {
541
+ retain?: boolean;
542
+ }) => Promise<{
543
+ id: number;
544
+ listeners: number;
545
+ }>;
546
+ export type Listener = {
547
+ client: string | null;
548
+ meta: Record<string, unknown>;
549
+ since: string;
550
+ };
551
+ /** Who is connected to a channel right now, longest first. */
552
+ export declare const listeners: (channel: string) => Promise<Listener[]>;
553
+ /** The app's channels that somebody listens to or that keep a message, its open connections, and the limits. */
554
+ export declare const channels: () => Promise<{
555
+ connections: number;
556
+ channels: {
557
+ name: string;
558
+ listeners: number;
559
+ kept: {
560
+ event: string;
561
+ at: string;
562
+ } | null;
563
+ }[];
564
+ limits: {
565
+ connections_per_app: number;
566
+ channels_per_ticket: number;
567
+ message_bytes: number;
568
+ publishes_per_minute: number;
569
+ };
570
+ }>;
571
+ /** Forget what a channel keeps as its latest. */
572
+ export declare const forgetKept: (channel: string) => Promise<void>;
430
573
  export {};
package/dist/index.js CHANGED
@@ -421,3 +421,96 @@ export async function handleJob(request, handlers) {
421
421
  return reply(500, { error: "job_failed", message: error instanceof Error ? error.message : String(error) });
422
422
  }
423
423
  }
424
+ // Email and text messages, on Kabob's account. Lift decides who they come
425
+ // from and limits how much an app sends and how many one person gets.
426
+ // Needs the app's Email or Text messages switch on Lift.
427
+ const sending = async (service, method, path, body, options) => paidAnswer(await paid(service, method, path, body, { timeoutMs: 25_000, ...options }), "Lift");
428
+ /**
429
+ * Send an email:
430
+ *
431
+ * await sendEmail({ to: customer.email, subject: "Your try-on", html, text }, { tag: `brand-${brand.id}` })
432
+ *
433
+ * It comes from the app's own address (`<app>@lift.kabob.io`) under the
434
+ * app's name; set `replyTo` to where answers should go. Give `text`, `html`
435
+ * or both (256 KB together at most), to 10 addresses at most. There are no
436
+ * attachments: put the file in storage and link to it with `fileUrl`.
437
+ *
438
+ * Throws a `LiftError`: 429 `daily_limit` (the app sent its day's worth),
439
+ * 429 `recipient_limit` (that address got enough from this app for now),
440
+ * 422 `refused` (the mail service won't send this one; `message` says why).
441
+ */
442
+ export async function sendEmail(message, options = {}) {
443
+ const { replyTo, ...rest } = message;
444
+ const body = { ...rest, ...(replyTo != null && { reply_to: replyTo }) };
445
+ return (await sending("email", "POST", "messages", body, options)).message;
446
+ }
447
+ /** The app's address, how many emails it sent today, and its limits. */
448
+ export const email = (options = {}) => sending("email", "GET", "", undefined, options);
449
+ /**
450
+ * Send a text message to one phone:
451
+ *
452
+ * await sendSms({ to: "+886912345678", text: `Your code is ${code}` }, { tag })
453
+ *
454
+ * `to` has its country code; Lift only texts the countries it lists
455
+ * (`sms()`), Taiwan unless set otherwise. Every message costs money by
456
+ * segment: 160 plain letters, or 70 with Chinese or emoji, and three
457
+ * segments at most. Send one only when a person asked for it, and never let
458
+ * a visitor choose the text.
459
+ *
460
+ * Throws a `LiftError`: 403 `country_not_allowed`, 413 `too_long`, 429
461
+ * `daily_limit`, 429 `recipient_limit` (a number gets a few an hour), 422
462
+ * `refused` (the network won't send this one; `message` says why).
463
+ */
464
+ export async function sendSms(message, options = {}) {
465
+ return (await sending("sms", "POST", "messages", message, options)).message;
466
+ }
467
+ /** How many segments the app sent today, its limits, and the countries Lift texts. */
468
+ export const sms = (options = {}) => sending("sms", "GET", "", undefined, options);
469
+ // Channels: telling many clients something at once. Clients (screens,
470
+ // kiosks, phones, open pages) each hold one long connection to Lift and
471
+ // listen; the app's server says who may listen to what (`channelTicket`) and
472
+ // what to say (`publish`). The app holds no connection itself, so it can
473
+ // sleep and be deployed without anyone dropping. Needs the app's Channels
474
+ // switch on Lift.
475
+ const channelsCall = async (method, path, body) => paidAnswer(await paid("channels", method, path, body, { timeoutMs: 25_000 }), "Lift");
476
+ /**
477
+ * A ticket for one client, naming the channels it may listen to:
478
+ *
479
+ * app.get("/api/ticket", requireCompany, async (c) =>
480
+ * c.json(await channelTicket({ channels: [`company-${c.get("company").id}`], client: c.get("session").user.id })))
481
+ *
482
+ * Give one only to a client the app has recognised (a session, a paired
483
+ * device), and only for channels that client should hear: whoever holds the
484
+ * ticket hears them. `client` is the app's own word for who this is and
485
+ * `meta` a small object, both shown back in `listeners`. It works for `ttl`
486
+ * seconds (an hour unless said, a day at most); the stream ends then with a
487
+ * `lift.expired` event and the client asks for a new one. 20 channels a
488
+ * ticket at most.
489
+ */
490
+ export const channelTicket = (ask) => channelsCall("POST", "tickets", ask);
491
+ /**
492
+ * Say something to everyone listening to a channel:
493
+ *
494
+ * await publish(`brand-${brand.id}`, "content", { version: 12 }, { retain: true })
495
+ *
496
+ * `event` is one word for what happened and `data` small JSON (16 KB): say
497
+ * what changed and let the client fetch it. With `retain` the message is
498
+ * also kept as the channel's latest, and whoever connects later gets it
499
+ * first. Answers with the message's id and how many were listening.
500
+ *
501
+ * A channel needs no creating: any name of letters, digits, dashes, dots,
502
+ * colons and underscores. A message reaches who is connected now; it is not
503
+ * a queue, so what must not be lost also goes in the app's database.
504
+ */
505
+ export const publish = (channel, event, data, options = {}) => channelsCall("POST", "publish", {
506
+ channel,
507
+ event,
508
+ ...(data !== undefined && { data }),
509
+ ...(options.retain && { retain: true }),
510
+ });
511
+ /** Who is connected to a channel right now, longest first. */
512
+ export const listeners = async (channel) => (await channelsCall("GET", `${encodeURIComponent(channel)}/listeners`)).listeners;
513
+ /** The app's channels that somebody listens to or that keep a message, its open connections, and the limits. */
514
+ export const channels = () => channelsCall("GET", "");
515
+ /** Forget what a channel keeps as its latest. */
516
+ export const forgetKept = (channel) => channelsCall("DELETE", `${encodeURIComponent(channel)}/kept`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kabobcompany/lift",
3
- "version": "0.2.0",
4
- "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, run public actions, use OpenAI and Seedance on Kabob's account, keep files in Lift's storage, and run background jobs.",
3
+ "version": "0.4.0",
4
+ "description": "For apps hosted on Lift: call Kabob products (Lookr) as the signed-in person, run public actions, use OpenAI and Seedance on Kabob's account, keep files in Lift's storage, run background jobs, send email and text messages, and publish to long-lived channels.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
7
7
  "main": "./dist/index.js",