@@ -15,17 +15,15 @@ type SlackChannel = {
15
15
} ;
16
16
17
17
/**
18
- * Cloudflare workers have a maximum number of subrequests we can call (20 according to my
19
- * tests) https://developers.cloudflare.com/workers/platform/limits/#how-many-subrequests-can-i-make
20
- * TODO: Test with 50
18
+ * Cloudflare workers have a maximum number of subrequests we can call (50 on a free account)
19
+ * https://developers.cloudflare.com/workers/platform/limits/#how-many-subrequests-can-i-make
21
20
*/
22
- const maximumSubrequests = 20 ;
21
+ const maximumSubrequests = 50 ;
23
22
24
23
/**
25
- * Executes a Slack API request to fetch channels, handles pagination, then returns the merged
26
- * results.
24
+ * Helper function to fetch channels by type with pagination
27
25
*/
28
- export async function getChannelsPaginated ( context : SlackRuntimeContext ) {
26
+ async function fetchChannelsByType ( context : SlackRuntimeContext , types : string ) {
29
27
const channels : SlackChannel [ ] = [ ] ;
30
28
31
29
let response = await slackAPI < {
@@ -39,7 +37,7 @@ export async function getChannelsPaginated(context: SlackRuntimeContext) {
39
37
payload : {
40
38
limit : 1000 ,
41
39
exclude_archived : true ,
42
- types : 'public_channel,private_channel' ,
40
+ types,
43
41
} ,
44
42
} ) ;
45
43
channels . push ( ...response ?. channels ) ;
@@ -57,16 +55,34 @@ export async function getChannelsPaginated(context: SlackRuntimeContext) {
57
55
payload : {
58
56
limit : 1000 ,
59
57
exclude_archived : true ,
60
- types : 'public_channel,private_channel' ,
58
+ types,
61
59
cursor : response . response_metadata . next_cursor ,
62
60
} ,
63
61
} ) ;
64
62
channels . push ( ...response ?. channels ) ;
65
63
numberOfCalls ++ ;
66
64
}
67
65
66
+ return channels ;
67
+ }
68
+
69
+ /**
70
+ * Executes a Slack API request to fetch channels, handles pagination, then returns the merged
71
+ * results.
72
+ */
73
+ export async function getChannelsPaginated ( context : SlackRuntimeContext ) {
74
+ // Make separate calls for public and private channels due to inconsistent Slack API filtering
75
+ // when requesting multiple types together - some channels are missing from the response
76
+ const [ publicChannels , privateChannels ] = await Promise . all ( [
77
+ fetchChannelsByType ( context , 'public_channel' ) ,
78
+ fetchChannelsByType ( context , 'private_channel' ) ,
79
+ ] ) ;
80
+
81
+ // Combine results
82
+ const allChannels = [ ...publicChannels , ...privateChannels ] ;
83
+
68
84
// Remove any duplicate as the pagination API could return duplicated channels
69
- return channels . filter (
85
+ return allChannels . filter (
70
86
( value , index , self ) => index === self . findIndex ( ( t ) => t . id === value . id ) ,
71
87
) ;
72
88
}
0 commit comments