From c4609f2715637711718898b022e9934fb5ad43b0 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Fri, 15 Dec 2023 12:44:36 +0100 Subject: [PATCH 1/2] Make WebhookEventNames type safe --- src/WebhookReceiver.test.ts | 3 +-- src/WebhookReceiver.ts | 37 ++++++++++++++++++++++++++++++++++++- src/index.ts | 1 - 3 files changed, 37 insertions(+), 4 deletions(-) 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'; From d1cda6f9327870a0c0536d5a750ab85b1b0a0bf5 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Fri, 15 Dec 2023 12:45:53 +0100 Subject: [PATCH 2/2] Create modern-schools-hug.md --- .changeset/modern-schools-hug.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/modern-schools-hug.md 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