Skip to content

Commit

Permalink
Make WebhookEvent names type safe (#125)
Browse files Browse the repository at this point in the history
* Make WebhookEventNames type safe

* Create modern-schools-hug.md
  • Loading branch information
lukasIO authored Dec 20, 2023
1 parent 5d41df0 commit 43f540d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-schools-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-server-sdk": major
---

Make `WebhookEvent` names type safe
3 changes: 1 addition & 2 deletions src/WebhookReceiver.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
37 changes: 36 additions & 1 deletion src/WebhookReceiver.ts
Original file line number Diff line number Diff line change
@@ -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<BinaryReadOptions>): WebhookEvent {
return new WebhookEvent().fromBinary(bytes, options);
}

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): WebhookEvent {
return new WebhookEvent().fromJson(jsonValue, options);
}

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): 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;

Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,3 @@ export {
TrackInfo,
TrackType,
} from './proto/livekit_models_pb.js';
export type { WebhookEvent } from './proto/livekit_webhook_pb.js';

0 comments on commit 43f540d

Please sign in to comment.