| 1 | import type * as ivschat from "@distilled.cloud/aws/ivschat"; |
| 2 | import type * as Duration from "effect/Duration"; |
| 3 | import type * as Effect from "effect/Effect"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import type { Room } from "./Room.ts"; |
| 6 | |
| 7 | /** |
| 8 | * The `roomIdentifier` is injected by the binding from the bound room, and |
| 9 | * the wire `sessionDurationInMinutes` is expressed as a `Duration.Input`. |
| 10 | */ |
| 11 | export interface CreateChatTokenRequest extends Omit< |
| 12 | ivschat.CreateChatTokenRequest, |
| 13 | "roomIdentifier" | "sessionDurationInMinutes" |
| 14 | > { |
| 15 | /** |
| 16 | * How long the issued token's chat session can remain active. Converted |
| 17 | * to whole minutes on the wire (`1` - `180` minutes). |
| 18 | * @default 60 minutes |
| 19 | */ |
| 20 | sessionDuration?: Duration.Input; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Mint an encrypted chat token that an end user presents to open a WebSocket |
| 25 | * connection to the bound room — the effectful call made from a deployed |
| 26 | * Lambda or Task. The `capabilities` field grants `SEND_MESSAGE`, |
| 27 | * `DELETE_MESSAGE`, and/or `DISCONNECT_USER` to the token holder (a token |
| 28 | * with no capabilities can only view chat); `attributes` attaches profile |
| 29 | * data (display name, icon, …) to every message the user sends. The returned |
| 30 | * `token` is sensitive and surfaces as a `Redacted` value. |
| 31 | * |
| 32 | * ### Minting Chat Tokens |
| 33 | * Provide the `CreateChatTokenHttp` implementation layer on the Function |
| 34 | * effect, bind the room in the init phase, then call the returned client at |
| 35 | * runtime. The binding grants `ivschat:CreateChatToken` on the room and |
| 36 | * injects its ARN as the `roomIdentifier` automatically. |
| 37 | * |
| 38 | * **Example:** Mint a token from a Lambda |
| 39 | * ```typescript |
| 40 | * // init |
| 41 | * const room = yield* IVSChat.Room("LiveChat"); |
| 42 | * const createChatToken = yield* IVSChat.CreateChatToken(room); |
| 43 | * |
| 44 | * return { |
| 45 | * fetch: Effect.gen(function* () { |
| 46 | * // runtime |
| 47 | * const { token, sessionExpirationTime } = yield* createChatToken({ |
| 48 | * userId: "user-123", |
| 49 | * capabilities: ["SEND_MESSAGE"], |
| 50 | * sessionDuration: "30 minutes", |
| 51 | * attributes: { displayName: "Sam" }, |
| 52 | * }); |
| 53 | * return HttpServerResponse.json({ |
| 54 | * token: token !== undefined ? Redacted.value(token) : undefined, |
| 55 | * sessionExpirationTime, |
| 56 | * }); |
| 57 | * }), |
| 58 | * }; |
| 59 | * // on the Function effect: |
| 60 | * // .pipe(Effect.provide(IVSChat.CreateChatTokenHttp)) |
| 61 | * ``` |
| 62 | * |
| 63 | * @binding |
| 64 | */ |
| 65 | export interface CreateChatToken extends Binding.Service< |
| 66 | CreateChatToken, |
| 67 | "AWS.IVSChat.CreateChatToken", |
| 68 | ( |
| 69 | room: Room, |
| 70 | ) => Effect.Effect< |
| 71 | ( |
| 72 | request: CreateChatTokenRequest, |
| 73 | ) => Effect.Effect< |
| 74 | ivschat.CreateChatTokenResponse, |
| 75 | ivschat.CreateChatTokenError |
| 76 | > |
| 77 | > |
| 78 | > {} |
| 79 | export const CreateChatToken = Binding.Service<CreateChatToken>( |
| 80 | "AWS.IVSChat.CreateChatToken", |
| 81 | ); |