minireqdeprecated

A minimal request library for the browser

Usage no npm install needed!

<script type="module">
  import minireq from 'https://cdn.skypack.dev/minireq';
</script>

README

minireq

build docs codecov code style: prettier

A minimal request library built on XMLHTTPRequest for the browser.

Documentation on Github Pages

Why not fetch, axios or superagent

fetch is too bare bones and also does not support features like progress indication. axios and superagent are neither minimal nor are they written with ES modules with makes them awkward to bundle.

Also I want a request library with better types than currently available.

Example

import { makeRequest } from 'minireq';

const request = makeRequest();

const { promise, abort } = request({
    method: 'GET',
    url: '/api/users'
});

promise.then(({ status, data }) => {
    if (status === 200) {
        console.log(data.name);
    }
});

Making a post request, with a timeout on 500ms

import { makeRequest } from 'minireq';

const request = makeRequest();

const { promise } = request({
    method: 'POST',
    url: '/api/users',
    send: {
        name: 'Peter',
        age: 50,
        children: []
    }
});

promise.then(({ status, data }) => {
    if (status === 201) {
        console.log(data.id);
    }
});

Using a custom content type

import { makeRequest, defaultSerializers } from 'minireq';

const serializer = {
    parse: (data: string) => data.split('\n').map(x => JSON.parse(x)),
    convert: (data: any) => {
        if (!Array.isArray(data)) {
            return [JSON.stringify(data)];
        } else {
            return data.map(x => JSON.stringify(x)).join('\n');
        }
    }
};

const { request } = makeRequest({
    ...defaultSerializers,
    'application/ndjson': serializer
});

const { promise, abort } = request({
    method: 'GET',
    url: '/api/users',
    accept: 'application/ndjson'
});

const { status, data } = await promise;

if (status === 200) {
    console.log(data.length);
}