any-data
Convert data of any type to text, json, formData, TypedArray, Blob, or
ArrayBuffer
npm install any-data
Motivation
I needed a tool for dealing with the wide variety of data types that can be
passed to new Response()
. So AnyData
allows lazy data conversion for response
bodies to simplify http server middleware such as calculating etags and
compression with Brotli.
Note that AnyData
does not support ReadableStream
even though Response
does.
Table of contents
- Example usage
- API
- Supported data types
- Changes
- Changelog
- Roadmap
- Community
- Contributing
- ISC license
Example usage
import { AnyData } from 'any-data';
await new AnyData('hello world').bytes();
await new AnyData(myArrayBuffer).bytes();
await new AnyData(myBlob).arrayBuffer();
API
import { AnyData } from 'any-data';
const hello = new AnyData('hello world');
await hello.text();
await hello.json();
await hello.arrayBuffer();
await hello.blob();
await hello.bytes();
await hello.formData();
hello.set('foo bar');
hello.clone();
hello.isEmpty();
hello.isSupported();
hello.getDataCategory();
Supported data types
data type / method | text() | json() | formData() | bytes() | blob() | arrayBuffer() | clone() |
---|
null | "" | undefined | (empty) | (empty) | (empty) | (empty) | null |
string | itself | If valid JSON | If valid | Yes | Yes | Yes | itself |
Record<string, any> | JSON | itself | Yes | JSON | JSON | JSON | deep copy |
Array | JSON | itself | as entries | JSON | JSON | JSON | deep copy |
Response | text() | json() | formData() | bytes() | blob() | arrayBuffer() | clone() |
Blob | Yes | If valid JSON | If valid JSON | Yes | itself | Yes | deep copy |
ArrayBuffer | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy |
TypedArray | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy |
DataView | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy |
FormData † | Yes | Error | itself | Yes | Yes | Yes | deep copy |
ReadableStream | Yes | If valid JSON | If valid JSON | Yes | Yes | Yes | deep copy |
URLSearchParams | URL | entries | Yes | URL | URL | URL | deep copy |
† Note: all FormData is serialized to text with multi-part form boundary
markers
Contributing
Please open a ticket or PR on GitHub. All contributions are subject to the
Code of Conduct.
ISC License
ISC License