Skip to content

stream-axios让流式请求更简单

基于 axios 的二次封装库,提供开箱即用的流式请求接口。

stream-axios logo

为什么要使用 stream-axios?

以前 (原生 fetch)

javascript
const response = await fetch("/api/chat", {
  method: "POST",
  body: JSON.stringify({ msg: "hi" }),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  console.log(decoder.decode(value));
}
👉

现在 (stream-axios)

javascript
await request.stream({ url: "/api/chat", data: { msg: "hi" } }, (chunk) =>
  console.log(chunk),
);

Released under the MIT License.