| 1 | import * as sns from "@distilled.cloud/aws/sns"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { Topic } from "./Topic.ts"; |
| 5 | |
| 6 | export interface PublishRequest extends Omit< |
| 7 | sns.PublishInput, |
| 8 | "TopicArn" | "TargetArn" | "PhoneNumber" |
| 9 | > {} |
| 10 | |
| 11 | /** |
| 12 | * Runtime binding for `sns:Publish`. |
| 13 | * |
| 14 | * Bind this operation to a {@link Topic} inside a function runtime to get a |
| 15 | * callable that automatically injects the `TopicArn`. The binding grants the |
| 16 | * host function `sns:Publish` on the topic. Provide the `PublishHttp` layer |
| 17 | * on the Function to implement the binding. |
| 18 | * ### Publishing Messages |
| 19 | * **Example:** Publish from a Lambda Function |
| 20 | * ```typescript |
| 21 | * export class ApiFunction extends Lambda.Function<Lambda.Function>()( |
| 22 | * "ApiFunction", |
| 23 | * ) {} |
| 24 | * |
| 25 | * export default ApiFunction.make( |
| 26 | * { main: import.meta.url, functionUrl: true }, |
| 27 | * Effect.gen(function* () { |
| 28 | * const topic = yield* SNS.Topic("Events"); |
| 29 | * |
| 30 | * // init: bind the operation to the topic (grants sns:Publish) |
| 31 | * const publish = yield* SNS.Publish(topic); |
| 32 | * |
| 33 | * return { |
| 34 | * fetch: Effect.gen(function* () { |
| 35 | * // runtime: TopicArn is injected automatically |
| 36 | * const response = yield* publish({ |
| 37 | * Message: "order shipped", |
| 38 | * Subject: "order-update", |
| 39 | * }); |
| 40 | * return yield* HttpServerResponse.json({ |
| 41 | * messageId: response.MessageId, |
| 42 | * }); |
| 43 | * }).pipe(Effect.orDie), |
| 44 | * }; |
| 45 | * }).pipe(Effect.provide(SNS.PublishHttp)), |
| 46 | * ); |
| 47 | * ``` |
| 48 | * |
| 49 | * @binding |
| 50 | */ |
| 51 | export interface Publish extends Binding.Service< |
| 52 | Publish, |
| 53 | "AWS.SNS.Publish", |
| 54 | ( |
| 55 | topic: Topic, |
| 56 | ) => Effect.Effect< |
| 57 | ( |
| 58 | request: PublishRequest, |
| 59 | ) => Effect.Effect<sns.PublishResponse, sns.PublishError> |
| 60 | > |
| 61 | > {} |
| 62 | |
| 63 | export const Publish = Binding.Service<Publish>("AWS.SNS.Publish"); |