Stream and pipe resources with responseType: 'stream'
lautiamkok opened this issue · comments
Describe the feature
With axios, we can stream and pipe resources like this:
import axios from 'axios'
import fs from 'fs'
const file = fs.createWriteStream('./assets/sample.jpg')
const { data } = await axios({
url: 'https://samplesite.com/uploads/sample.jpg',
method: 'GET',
responseType: 'stream'
})
data.pipe(file)
How can we do it with ofetch? Any documentation around?
Additional information
- Would you be willing to help implement this feature?
up
Hi. You can use same responseType: "stream"
API with ofetch 👍🏼
I am getting TypeError: response.pipe is not a function, my code:
import { ofetch } from 'ofetch';
import fs from 'fs';
const response = await ofetch('https://defly.io/img/add-skins.js', {
responseType: "stream",
});
response.pipe(fs.createWriteStream('./cache/allSkins.json'));
same issue
unlike node-fetch, ofetch returns a web ReadableStream which has .pipeTo
method and accepts also a web WritableStream.
This is a working example for Node.js 18+
import { ofetch } from "ofetch";
import fs from "node:fs";
const response = await ofetch("https://defly.io/img/add-skins.js", {
responseType: "stream",
});
const fsStream = fs.createWriteStream("./add-skins.js");
response.pipeTo(
new WritableStream({
write(chunk) {
fsStream.write(chunk);
},
close() {
fsStream.close();
},
})
);