kriszyp / cbor-x

Ultra-fast CBOR encoder/decoder with extensions for records and structural cloning
MIT License
276 stars 32 forks source link

How to use with Axios? #71

Closed ivanjaros closed 1 year ago

ivanjaros commented 1 year ago

How do I use this with Axios? I am fiddling with response type setting to stream, arraybuffer, blob and trying do decode/encode in transformRequest and transformResponse methods of Axios client but cbor is not taking the data in.

kriszyp commented 1 year ago

Here are some Axios examples that I have tested (and work for me):

import axios from 'axios';
import { decode, DecoderStream } from 'cbor-x';

let response = await axios({
    url,
    method: 'GET',
    responseType: 'arraybuffer',
    headers,
});
console.log('decoded arraybuffer data:', decode(response.data));

response = await axios({
    url,
    method: 'GET',
    responseType: 'arraybuffer',
    headers,
    transformResponse: [decode],
});
console.log('decoded arraybuffer data with transformResponse:', response.data);

response = await axios({
    url,
    method: 'GET',
    responseType: 'stream',
    headers,
});
let decoderStream = new DecoderStream({ mapsAsObjects: true });
response.data.pipe(decoderStream);
decoderStream.on('data', (data) => console.log('decoded data from stream:', data));
await new Promise(r => decoderStream.on('end', r));

Are you also using cbor-x on the server side, or another package?

ivanjaros commented 1 year ago

Thanks, but i have decided to not use cbor after all.