Make easily chunked requests to a server.
This library allows you to make chunked requests to a server. It is useful when you want to send a large amount of data to a server, but you don't want to send it all at once. This library will send the data in chunks, and the server will receive the data in chunks.
npm install chunked-requests
const fetcher = async ({ id }) => {
const response = await axios.get(
`https://jsonplaceholder.typicode.com/posts/${id}`
)
return response.data
}
const request = async () => {
try {
const allDataFetched = await fetchChunkedRequests({
listOfPayloads: Array.from({ length: 100 }, (_, index) => ({ id: index + 1 })),
chunkSize: 10,
chunkDelay: 1000,
fetcher,
transformData: (chunks) =>
chunks.filter(({ id }) => typeof id === 'number'),
transformResponse: (response) => response,
onChunkHasFetched: (currentsChunks) => {
console.log('Chunk has been fetched', currentsChunks)
},
onChunkedRequestsFinish: (allDataFetched) => {
console.log('Chunked requests finished', allDataFetched)
}
})
console.log('All data fetched!!! 🥳', allDataFetched)
} catch (error) {
console.error('Error with fetch 😢 ', error)
}
}
request()
fetchChunkedRequests({
listOfPayloads,
chunkSize,
chunkDelay,
fetcher,
transformData,
transformResponse,
onChunkHasFetched,
onChunkedRequestsFinish
})
Type: Array
The list of payloads to send to the server.
Type: Number
The size of each chunk.
Type: Number
The delay between each chunk.
Type: Function
The function that will be called to fetch the data. It receives the payload as a parameter.
Type: Function
The function that will be called to transform the data before sending it to the server. It receives the chunk as a parameter.
Type: Function
The function that will be called to transform the response from the server. It receives the response as a parameter.
Type: Function
The function that will be called when a chunk has been fetched. It receives the current chunks as a parameter.
Type: Function
The function that will be called when all the chunks have been fetched. It receives the all data fetched as a parameter.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Sebastián Urbano - LinkedIn
MIT License
Copyright (c) 2023 Sebastián Urbano