diff --git a/.changeset/modern-schools-hug.md b/.changeset/modern-schools-hug.md new file mode 100644 index 00000000..01f393f4 --- /dev/null +++ b/.changeset/modern-schools-hug.md @@ -0,0 +1,5 @@ +--- +"livekit-server-sdk": major +--- + +Make `WebhookEvent` names type safe diff --git a/src/WebhookReceiver.test.ts b/src/WebhookReceiver.test.ts index 8d988bbf..2a614347 100644 --- a/src/WebhookReceiver.test.ts +++ b/src/WebhookReceiver.test.ts @@ -1,7 +1,6 @@ import { describe, expect, it } from 'vitest'; import { AccessToken } from './AccessToken.js'; -import { WebhookEvent } from './proto/livekit_webhook_pb.js'; -import { WebhookReceiver } from './WebhookReceiver.js'; +import { WebhookReceiver, WebhookEvent } from './WebhookReceiver.js'; const testApiKey = 'abcdefg'; const testSecret = 'abababa'; diff --git a/src/WebhookReceiver.ts b/src/WebhookReceiver.ts index 4a93a1ed..9352e185 100644 --- a/src/WebhookReceiver.ts +++ b/src/WebhookReceiver.ts @@ -1,8 +1,43 @@ +import { BinaryReadOptions, JsonValue, JsonReadOptions } from '@bufbuild/protobuf'; import { TokenVerifier } from './AccessToken.js'; -import { WebhookEvent } from './proto/livekit_webhook_pb.js'; +import { WebhookEvent as ProtoWebhookEvent } from './proto/livekit_webhook_pb.js'; export const authorizeHeader = 'Authorize'; +export class WebhookEvent extends ProtoWebhookEvent { + event: WebhookEventNames = ''; + + static fromBinary(bytes: Uint8Array, options?: Partial): WebhookEvent { + return new WebhookEvent().fromBinary(bytes, options); + } + + static fromJson(jsonValue: JsonValue, options?: Partial): WebhookEvent { + return new WebhookEvent().fromJson(jsonValue, options); + } + + static fromJsonString(jsonString: string, options?: Partial): WebhookEvent { + return new WebhookEvent().fromJsonString(jsonString, options); + } +} + +export type WebhookEventNames = + | 'room_started' + | 'room_finished' + | 'participant_joined' + | 'participant_left' + | 'track_published' + | 'track_unpublished' + | 'egress_started' + | 'egress_updated' + | 'egress_ended' + | 'ingress_started' + | 'ingress_ended' + /** + * @internal + * @note only used as a default value, not a valid webhook event + */ + | ''; + export class WebhookReceiver { private verifier: TokenVerifier; diff --git a/src/index.ts b/src/index.ts index f2cf6fb3..426fe9fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,4 +36,3 @@ export { TrackInfo, TrackType, } from './proto/livekit_models_pb.js'; -export type { WebhookEvent } from './proto/livekit_webhook_pb.js';