Skip to content

Commit 40f1f92

Browse files
authored
feat: add get hook event list (#1586)
## CLA - [x] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required). - [x] Code changes are tested ## Description of the changes, What, Why and How? ## Changelog ``` /** * getHookEvents - Get available events for hooks (webhook, SQS, and SNS) * * @param {Product[]} [products] Optional array of products to filter events by (e.g., [Product.Chat, Product.Video]) * @returns {Promise<GetHookEventsResponse>} Response containing available hook events */ async getHookEvents(products?: Product[]) { const params = products && products.length > 0 ? { product: products.join(',') } : {}; return await this.get<GetHookEventsResponse>(this.baseURL + '/hook/events', params); } ```
1 parent bb33c1e commit 40f1f92

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/client.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import type {
106106
GetCampaignOptions,
107107
GetChannelTypeResponse,
108108
GetCommandResponse,
109+
GetHookEventsResponse,
109110
GetImportResponse,
110111
GetMessageAPIResponse,
111112
GetMessageOptions,
@@ -148,6 +149,7 @@ import type {
148149
PollVote,
149150
PollVoteData,
150151
PollVotesAPIResponse,
152+
Product,
151153
PushPreference,
152154
PushProvider,
153155
PushProviderConfig,
@@ -2164,6 +2166,17 @@ export class StreamChat {
21642166
});
21652167
}
21662168

2169+
/**
2170+
* getHookEvents - Get available events for hooks (webhook, SQS, and SNS)
2171+
*
2172+
* @param {Product[]} [products] Optional array of products to filter events by (e.g., [Product.Chat, Product.Video])
2173+
* @returns {Promise<GetHookEventsResponse>} Response containing available hook events
2174+
*/
2175+
async getHookEvents(products?: Product[]) {
2176+
const params = products && products.length > 0 ? { product: products.join(',') } : {};
2177+
return await this.get<GetHookEventsResponse>(this.baseURL + '/hook/events', params);
2178+
}
2179+
21672180
_addChannelConfig({ cid, config }: ChannelResponse) {
21682181
if (this._cacheEnabled()) {
21692182
this.configs[cid] = config;

src/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,23 @@ export type GetRateLimitsResponse = APIResponse & {
576576
web?: RateLimitsMap;
577577
};
578578

579+
export enum Product {
580+
Chat = 'chat',
581+
Video = 'video',
582+
Moderation = 'moderation',
583+
Feeds = 'feeds',
584+
}
585+
586+
export type HookEvent = {
587+
name: string;
588+
description: string;
589+
products: Product[];
590+
};
591+
592+
export type GetHookEventsResponse = APIResponse & {
593+
events: HookEvent[];
594+
};
595+
579596
export type GetReactionsAPIResponse = APIResponse & {
580597
reactions: ReactionResponse[];
581598
};

test/unit/client.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,4 +1128,66 @@ describe('X-Stream-Client header', () => {
11281128

11291129
expect(userAgent).toMatchInlineSnapshot(`"deprecated"`);
11301130
});
1131+
1132+
describe('getHookEvents', () => {
1133+
let clientGetSpy;
1134+
1135+
beforeEach(() => {
1136+
clientGetSpy = vi.spyOn(client, 'get').mockResolvedValue({});
1137+
});
1138+
1139+
it('should call get with correct URL and no params when no products specified', async () => {
1140+
await client.getHookEvents();
1141+
1142+
expect(clientGetSpy).toHaveBeenCalledTimes(1);
1143+
expect(clientGetSpy).toHaveBeenCalledWith(`${client.baseURL}/hook/events`, {});
1144+
});
1145+
1146+
it('should call get with correct URL and empty params when empty products array specified', async () => {
1147+
await client.getHookEvents([]);
1148+
1149+
expect(clientGetSpy).toHaveBeenCalledTimes(1);
1150+
expect(clientGetSpy).toHaveBeenCalledWith(`${client.baseURL}/hook/events`, {});
1151+
});
1152+
1153+
it('should call get with product params when products specified', async () => {
1154+
await client.getHookEvents(['chat', 'video']);
1155+
1156+
expect(clientGetSpy).toHaveBeenCalledTimes(1);
1157+
expect(clientGetSpy).toHaveBeenCalledWith(`${client.baseURL}/hook/events`, {
1158+
product: 'chat,video',
1159+
});
1160+
});
1161+
1162+
it('should call get with single product param', async () => {
1163+
await client.getHookEvents(['chat']);
1164+
1165+
expect(clientGetSpy).toHaveBeenCalledTimes(1);
1166+
expect(clientGetSpy).toHaveBeenCalledWith(`${client.baseURL}/hook/events`, {
1167+
product: 'chat',
1168+
});
1169+
});
1170+
1171+
it('should return the response from get', async () => {
1172+
const mockResponse = {
1173+
events: [
1174+
{
1175+
name: 'message.new',
1176+
description: 'When a new message is added',
1177+
products: ['chat'],
1178+
},
1179+
{
1180+
name: 'call.created',
1181+
description: 'The call was created',
1182+
products: ['video'],
1183+
},
1184+
],
1185+
};
1186+
clientGetSpy.mockResolvedValue(mockResponse);
1187+
1188+
const result = await client.getHookEvents(['chat', 'video']);
1189+
1190+
expect(result).toEqual(mockResponse);
1191+
});
1192+
});
11311193
});

0 commit comments

Comments
 (0)