import type { ApiGatewayManagementApiClient } from '@aws-sdk/client-apigatewaymanagementapi' import type { ChannelStore, EventHubService } from '@pikku/core/channel' import type { EventHubStore } from '@pikku/core/channel' import { getApiGatewayManagementApiClient, sendMessages } from './utils.js' import type { Logger } from '@pikku/core/services' import type { APIGatewayEvent } from 'aws-lambda' export class LambdaEventHubService< EventTopics extends Record = {}, > implements EventHubService { private callbackAPI: ApiGatewayManagementApiClient constructor( private logger: Logger, event: APIGatewayEvent, private channelStore: ChannelStore, private eventHubStore: EventHubStore ) { this.callbackAPI = getApiGatewayManagementApiClient(logger, event) } async subscribe( topic: T, channelId: string ): Promise { await this.eventHubStore.subscribe(topic, channelId) } async unsubscribe( topic: T, channelId: string ): Promise { await this.eventHubStore.unsubscribe(topic, channelId) } async publish( topic: T, channelId: string | null, data: EventTopics[T], isBinary?: boolean ): Promise { const channelIds = await this.eventHubStore.getChannelIdsForTopic( topic as string ) if (channelId) { await this.sendMessages(channelIds, channelId, data, isBinary) } } private async sendMessages( channelIds: string[], fromChannelId: string, data: EventTopics[keyof EventTopics], isBinary?: boolean ): Promise { await sendMessages( this.logger, this.channelStore, this.callbackAPI, fromChannelId, channelIds, data, isBinary ) } }