The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { sanitizeKey, type RuntimeContext } from "alchemy/RuntimeContext"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import type * as Effect from "effect/Effect"; |
| 4 | import type { StripeEventClass, StripeEventInstance } from "./Events.ts"; |
| 5 | |
| 6 | export interface ConsumeEventsProps< |
| 7 | E extends readonly StripeEventClass[] = readonly StripeEventClass[], |
| 8 | > { |
| 9 | /** |
| 10 | * Event classes to subscribe to. The handler `event` parameter is the |
| 11 | * union of these classes. |
| 12 | */ |
| 13 | events: E; |
| 14 | /** |
| 15 | * Path on the host. Defaults to `/webhooks/stripe`. |
| 16 | */ |
| 17 | path?: string; |
| 18 | } |
| 19 | |
| 20 | export type SelectedStripeEvent<E extends readonly StripeEventClass[]> = InstanceType<E[number]>; |
| 21 | |
| 22 | export const webhookPath = (path?: string): string => path ?? "/webhooks/stripe"; |
| 23 | |
| 24 | export const webhookSecretEnvName = (path?: string): string => |
| 25 | `STRIPE_WEBHOOK_SECRET_${webhookPath(path).replaceAll(/[^a-zA-Z0-9]/g, "_")}`; |
| 26 | |
| 27 | /** |
| 28 | * Deterministic logical id for the {@link WebhookEndpoint} created by |
| 29 | * {@link consumeEvents} when the caller does not pass an explicit id. |
| 30 | * Derived from the delivery path so two subscriptions on different paths |
| 31 | * don't collide. |
| 32 | */ |
| 33 | export const webhookEndpointLogicalId = (path?: string): string => |
| 34 | `WebhookEndpoint${sanitizeKey(webhookPath(path))}`; |
| 35 | |
| 36 | /** |
| 37 | * Subscribe to Stripe webhook events on the host. |
| 38 | * |
| 39 | * `consumeEvents` provisions the {@link WebhookEndpoint} itself: it derives |
| 40 | * the delivery URL from the host's `url`, enables the selected event |
| 41 | * types, and binds the endpoint's minted signing secret onto the host so |
| 42 | * deliveries are verified. No separate endpoint declaration is needed. |
| 43 | * |
| 44 | * Provide `Stripe.ConsumeEventsLive` on the host's Effect, e.g. a Cloudflare |
| 45 | * Worker. |
| 46 | * |
| 47 | * ### Handling events |
| 48 | * **Example:** Customer created and invoice paid |
| 49 | * ```typescript |
| 50 | * yield* Stripe.consumeEvents("Events", { |
| 51 | * events: [Stripe.CustomerCreated, Stripe.InvoicePaid], |
| 52 | * }, Effect.fn(function* (event) { |
| 53 | * // event: CustomerCreated | InvoicePaid |
| 54 | * yield* Effect.log(event.type); |
| 55 | * })); |
| 56 | * ``` |
| 57 | * |
| 58 | * @binding |
| 59 | * @product Webhook |
| 60 | */ |
| 61 | export function consumeEvents<const E extends readonly StripeEventClass[], Req = never>( |
| 62 | props: ConsumeEventsProps<E>, |
| 63 | process: (event: SelectedStripeEvent<E>) => Effect.Effect<void, never, Req | RuntimeContext>, |
| 64 | ): Effect.Effect<void, never, EventSource>; |
| 65 | export function consumeEvents<const E extends readonly StripeEventClass[], Req = never>( |
| 66 | id: string, |
| 67 | props: ConsumeEventsProps<E>, |
| 68 | process: (event: SelectedStripeEvent<E>) => Effect.Effect<void, never, Req | RuntimeContext>, |
| 69 | ): Effect.Effect<void, never, EventSource>; |
| 70 | export function consumeEvents( |
| 71 | idOrProps: string | ConsumeEventsProps, |
| 72 | propsOrProcess: |
| 73 | | ConsumeEventsProps |
| 74 | | ((event: StripeEventInstance) => Effect.Effect<void, never, any>), |
| 75 | maybeProcess?: (event: StripeEventInstance) => Effect.Effect<void, never, any>, |
| 76 | ): Effect.Effect<void, never, EventSource> { |
| 77 | const [id, props, process] = |
| 78 | typeof idOrProps === "string" |
| 79 | ? [ |
| 80 | idOrProps, |
| 81 | propsOrProcess as ConsumeEventsProps, |
| 82 | maybeProcess as (event: StripeEventInstance) => Effect.Effect<void, never, any>, |
| 83 | ] |
| 84 | : [ |
| 85 | undefined, |
| 86 | idOrProps as ConsumeEventsProps, |
| 87 | propsOrProcess as (event: StripeEventInstance) => Effect.Effect<void, never, any>, |
| 88 | ]; |
| 89 | return EventSource.use((source) => |
| 90 | source(id ?? webhookEndpointLogicalId(props.path), props, process), |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | export type EventSourceService = <E extends readonly StripeEventClass[], Req = never>( |
| 95 | id: string, |
| 96 | props: ConsumeEventsProps<E>, |
| 97 | process: (event: SelectedStripeEvent<E>) => Effect.Effect<void, never, Req>, |
| 98 | ) => Effect.Effect<void, never, never>; |
| 99 | |
| 100 | export class EventSource extends Context.Service<EventSource, EventSourceService>()( |
| 101 | "Stripe.EventSource", |
| 102 | ) {} |