-
Notifications
You must be signed in to change notification settings - Fork 16
PM-1518 Fix 429 too many requests error #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ const baseUrl = `${EnvironmentConfig.API.V5}/projects` | |
|
||
export type ProjectsResponse = SWRResponse<Project[], Project[]> | ||
|
||
const sleep = (ms: number): Promise<()=> void> => new Promise(resolve => { setTimeout(resolve, ms) }) | ||
|
||
/** | ||
* Custom hook to fetch and manage projects data. | ||
* | ||
|
@@ -24,13 +26,26 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean, | |
const params = { name: search, ...config?.filter } | ||
const url = buildUrl(baseUrl, params) | ||
|
||
const fetcher = (): Promise<Project[]> => { | ||
if (config?.filter?.id && Array.isArray(config.filter.id)) { | ||
const chunks = chunk(config.filter.id, 20) | ||
return Promise.all( | ||
chunks.map(page => xhrGetAsync<Project[]>(buildUrl(baseUrl, { ...params, id: page }))), | ||
) | ||
.then(responses => responses.flat()) | ||
const fetcher = async (): Promise<Project[]> => { | ||
const ids = config?.filter?.id | ||
|
||
if (Array.isArray(ids)) { | ||
const idChunks = chunk(ids, 20) | ||
const allResults: Project[] = [] | ||
|
||
for (const chunkIds of idChunks) { | ||
// eslint-disable-next-line no-await-in-loop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing the |
||
const response = await xhrGetAsync<Project[]>( | ||
buildUrl(baseUrl, { ...params, id: chunkIds }), | ||
) | ||
allResults.push(...response) | ||
|
||
// Rate limit: delay 200ms between calls | ||
// eslint-disable-next-line no-await-in-loop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
await sleep(200) | ||
} | ||
|
||
return allResults | ||
} | ||
|
||
return xhrGetAsync<Project[]>(url) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type of the
sleep
function is incorrect. It should bePromise<void>
instead ofPromise<()=> void>
. The function resolves without returning any value, so the correct type should reflect that.