Last active
October 31, 2024 14:15
-
-
Save Wanchai/1c4354e83f1fdb7b6d361f684a6a3228 to your computer and use it in GitHub Desktop.
Reading through a HTTP stream with RxJs. It needs to be improved but this is a working solution to decode a stream of data.
This file contains hidden or 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
| from( | |
| fetch('https://dev.com', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| Authorization: | |
| 'Bearer XXX', | |
| }, | |
| body: JSON.stringify({}), | |
| }), | |
| ) | |
| .pipe( | |
| map((resp) => { | |
| return resp.body?.pipeThrough(new TextDecoderStream()); | |
| }), | |
| tap((stream) => { | |
| if (!stream) { | |
| return; | |
| } | |
| const rd = stream.getReader(); | |
| of(rd) | |
| .pipe( | |
| switchMap((rd) => rd.read()), | |
| repeat(), | |
| tap((rd) => { | |
| console.log(rd); | |
| }), | |
| takeWhile((rd) => !rd.done), | |
| ) | |
| .subscribe(); | |
| }), | |
| ) | |
| .subscribe(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment