Skip to content
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

feat: support axios in browser can handle event stream request #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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.

None yet

1 participant