futoin-request

AsyncSteps friendly wrapper of 'request' package

Usage no npm install needed!

<script type="module">
  import futoinRequest from 'https://cdn.skypack.dev/futoin-request';
</script>

README

NPM Version NPM Downloads Build Status stable

NPM

About

FutoIn AsyncSteps friendly wrapper of request.

Documentation --> FutoIn Guide

Author: Andrey Galkin

Installation for Node.js

Command line:

$ npm install futoin-request --save

or:

$ yarn add futoin-request --save

Browser installation

The module can be used with webpack or any other CommonJS packer. However, please ensure to use ES6->ES5 transpilation for older browsers.

Pre-packed UMD module is available in dist/futoin-request.js (stripped ~180KB from over 1MB).

Note: please note that pre-packed dist is heavily stripped of dependencies and most features of request library is not expected to work due not minimal use case in browsers:

  • MIME type detection based on upload file name
  • AWS request signing feature
  • HAR feature
  • HTTP Signature feature
  • HAWK feature
  • Proxy/Tunnel feature
  • HTTP digest auth (large crypto dependency)
  • OAuth
  • tough-cookie

You can always use own webpack build.

Examples

const $as = require('futoin-asyncsteps');
const $as_request = require('futoin-request');

$as().add((as) => {
    // Very basic
    $as_request(as, 'https://httpbin.org/get');
    as.add((as, rsp, body) => console.log(body));
    
    // As usual
    $as_request.post(as, {
        url: 'https://httpbin.org/post',
        json: {a: 1, b: 2},
    });
    as.add((as, rsp, body) => console.log(body));
    
    // With callback for request as stream manipulation
    $as_request.post(as, {
        url: 'https://httpbin.org/post',
        headers: { 'content-length': 4 },
    }, (req) => req.end('test') );
    as.add((as, rsp, body) => console.log(body));    
}).execute();

API documentation

API is absolutely the same as for original request package except that:

  1. The first parameter must be a reference of AsyncSteps interface type.
  2. A next step receiving (as, rsp, body) => {} must be added instead of result callback.
  3. Error is thrown through AsyncSteps#error(), if detected.
  4. HTTP status != 200 is error as well.
  5. Request object is not returned, but passed to optional callback (third argument).

Additional notes:

  • Request is properly canceled on AsyncSteps#cancel() or timeout
  • Error info details
    • error type - RequestError
    • error object should be available through standard as.state.last_exception convention
    • as.state.last_response is set with response object
  • Browser provides $as_request global reference

Members

request

window.FutoIn.request - browser-only reference to futoin-request module

$as_request

window.$as_request - browser-only reference to futoin-request module

request

window.FutoIn.request - browser-only reference to futoin-request module

Kind: global variable

$as_request

window.$as_request - browser-only reference to futoin-request module

Kind: global variable