Skip to content

Instantly share code, notes, and snippets.

@brachkow
Created May 14, 2023 09:48
Show Gist options
  • Save brachkow/f849f9dff2b575efcb0c09913e9931c3 to your computer and use it in GitHub Desktop.
Save brachkow/f849f9dff2b575efcb0c09913e9931c3 to your computer and use it in GitHub Desktop.
Django depaginator
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;
};
@brachkow
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment