Created
May 14, 2023 09:48
-
-
Save brachkow/f849f9dff2b575efcb0c09913e9931c3 to your computer and use it in GitHub Desktop.
Django depaginator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; | |
function paramsToObject(searchParams: URLSearchParams): { [key: string]: string } { | |
const params: { [key: string]: string } = {}; | |
searchParams.forEach((value: string, key: string) => { | |
params[key] = value; | |
}); | |
return params; | |
} | |
export const depaginate = async <T>(url: string, config?: AxiosRequestConfig): Promise<T[]> => { | |
const allItems = []; | |
let nextUrl = undefined; | |
while (nextUrl !== null) { | |
let nextParams = { | |
page_size: 100, | |
}; | |
if (nextUrl) { | |
nextParams = { ...nextParams, ...paramsToObject(new URL(nextUrl).searchParams) }; | |
} | |
const response: AxiosResponse<any> = await axios.get(url, { | |
...config, | |
params: { ...config?.params, ...nextParams }, | |
}); | |
allItems.push(...(response.data.results as T[])); | |
nextUrl = response.data.next; | |
} | |
return allItems; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I definitely need to rewrite it into something more versatile. But it does work, and I don't want to miss it and write again, so publishing it as is