Skip to content

Commit b62d5c7

Browse files
Merge pull request #1170 from topcoder-platform/PM-1518
PM-1518 Fix 429 too many requests error
2 parents 75c5608 + 2383f98 commit b62d5c7

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/apps/copilots/src/services/projects.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const baseUrl = `${EnvironmentConfig.API.V5}/projects`
1111

1212
export type ProjectsResponse = SWRResponse<Project[], Project[]>
1313

14+
const sleep = (ms: number): Promise<()=> void> => new Promise(resolve => { setTimeout(resolve, ms) })
15+
1416
/**
1517
* Custom hook to fetch and manage projects data.
1618
*
@@ -24,13 +26,26 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean,
2426
const params = { name: search, ...config?.filter }
2527
const url = buildUrl(baseUrl, params)
2628

27-
const fetcher = (): Promise<Project[]> => {
28-
if (config?.filter?.id && Array.isArray(config.filter.id)) {
29-
const chunks = chunk(config.filter.id, 20)
30-
return Promise.all(
31-
chunks.map(page => xhrGetAsync<Project[]>(buildUrl(baseUrl, { ...params, id: page }))),
32-
)
33-
.then(responses => responses.flat())
29+
const fetcher = async (): Promise<Project[]> => {
30+
const ids = config?.filter?.id
31+
32+
if (Array.isArray(ids)) {
33+
const idChunks = chunk(ids, 20)
34+
const allResults: Project[] = []
35+
36+
for (const chunkIds of idChunks) {
37+
// eslint-disable-next-line no-await-in-loop
38+
const response = await xhrGetAsync<Project[]>(
39+
buildUrl(baseUrl, { ...params, id: chunkIds }),
40+
)
41+
allResults.push(...response)
42+
43+
// Rate limit: delay 200ms between calls
44+
// eslint-disable-next-line no-await-in-loop
45+
await sleep(200)
46+
}
47+
48+
return allResults
3449
}
3550

3651
return xhrGetAsync<Project[]>(url)

0 commit comments

Comments
 (0)