Socket
Book a DemoInstallSign in
Socket

basic-browser-request

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

basic-browser-request

Yet another XHR abstraction. Supports canceling, JSON, and crude chunking.

latest
Source
npmnpm
Version
10.0.2
Version published
Maintainers
1
Created
Source

basic-browser-request

Another lightweight wrapper around XHR that — according to me — does just enough. Supports chunking, canceling, and JSON.

Installation

npm install basic-browser-request

Usage

var request = require('basic-browser-request');

var requestHandle = request(
  {
    url: 'http://something.whatever/yeah',
    method: 'GET',
    mimeType: 'text/plain',
    onData: function onData(data) {
      console.log(data);
      chunksReceived += 1;
    }
  },
  done
);

function done(error, response, text) {
  if (error) {
    console.log(error);
  }
  else {
    useCompleteDownloadedText(text);
  }
}

To cancel:

requestHandle.cancelRequest();

If you don't specify a mimeType, it defaults to application/json and done() will be passed a parsed JSON object.

Here's a working example.

In the interest of sort-of compatibility with request, the callback will be passed three parameters:

  • error: An error object, if there was an error while making the request.
  • response: An object containing the statusCode, the statusMessage , rawResponse, and xhr: XMLHttpRequest.response. This is not at all the same as a Node response, though, so proceed with caution. The xhr is the XMLHttpRequest used to run the request operation.
  • body: This is going to be a string or, if the mimeType was application/json, an object.

Tests

Run in Chrome and Firefox with make test.

License

MIT.

TODO

Add JSON test.

Keywords

module

FAQs

Package last updated on 13 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts