Skip to content

Conversation

geekeren
Copy link

@geekeren geekeren commented Dec 20, 2023

axios in browser use XHR to send the request, not support handling the event stream response.
to solve this issue: axios/axios#479

@geekeren
Copy link
Author

Usage:

const httpClient = axios.create({
 adapter: fetchAdapter
});

const response = awat httpClient.post('/api/event-stream');
const responseStream = response.data as Response['body'];
const reader = responseStream!.getReader();
 const readChunk = () => {
            // Read a chunk from the reader
            reader.read()
                .then(({
                    value,
                    done
                }) => {
                    // Check if the stream is done
                    if (done) {
                        // Log a message
                        console.log('Stream finished');
                        // Return from the function
                        return;
                    }
                    // Convert the chunk value to a string
                    const chunkString = new TextDecoder().decode(value);
                    // Log the chunk string
                    console.log(chunkString);
                    // Read the next chunk
                    readChunk();
                })
                .catch(error => {
                    // Log the error
                    console.error(error);
                });
        };
        // Start reading the first chunk
        readChunk();

@geekeren
Copy link
Author

@vespaiach bro, is this package still in maintenance? could you merge this request

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant