npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

polymorphic-request

v0.1.1

Published

Use a modern request library, but continue to have a working request interface

Downloads

5

Readme

polymorphic-request

use your spiffy new network request library, but continue to have a working request interface for compatibility. No dependencies.

Ding Dong! The witch is dead! All hail the glorious new day, which will all lead us to use a common interface, creating library consistency and reduced bundle sizes everywhere! And that interface is: Fetch, no... wait... Axios err... um...

The main lesson I've taken away from this deprecation in favor of the "next-gen" is request is an excellent interface, stable, uniform and in 2020.... dead as a doornail.

Still, writing test suits for the backend in fetch leads huge blocks of promise boilerplate for anything but the simplest of cases and still leaves plenty of room for devs to not be using their preferred solution. Due to the time scales of network requests, indirection overhead is no big deal, but there's a lack of uniformity of interface, and extra warts everywhere for no marginal benefit. The main argument for it being it's "compatibility" between client and server; told by people who believe it's not "best practice" to write cross compatible modules. It's bikeshedding, just like promises (how many codebases have been refactored for the aesthetics of people who principally enjoy other languages?).

This is where polymorphic-request comes in. Put it in place and the frontend guys can use lib A the backend guys can use lib B and modules that run in both places use a request syntax, allowing pluggable libs (as long as your use cases aren't too much of an edge case) everywhere. The only supported syntaxes are in the Test Suite so please check these use cases before using and, should you need more, we accept PRs.

In addition it enables some development features for writing tests, allowing simple mocking of an external system, without network requests, keeping test suites fast and lean and giving you an easy mechanism to capture and prevent regressions.

Install

npm install polymorphic-request

Fetch Usage

Supports:

ES5 Node.js

var fetch = require('node-fetch');
var formData = require('form-data')
var request = require('polymorphic-request').fetch(fetch, formData);

ES5 Browser (Webpack/Browserify)

var request = require('polymorphic-request').fetch(Fetch, FormData);

ES6 Node.js

import poly from "polymorphic-request/implementations/node-fetch";
import fetch from "node-fetch";
import formData from "form-data";
const request = poly(fetch, formData);

ES6 Browser (Via Babel)

import poly from "polymorphic-request/implementations/node-fetch";
import fetch from "node-fetch";
import formData from "form-data";
const request = poly(fetch, formData);

Axios Usage

Supports:

ES5 Node.js

var axios = require('axios');
var formData = require('form-data')
var request = require('polymorphic-request').axios(axios, formData);

ES5 Browser (Webpack/Browserify)

var axios = require('axios');
var request = require('polymorphic-request').axios(axios, FormData);

ES6 Node.js

import poly from "polymorphic-request/implementations/axios";
import axios from "axios";
import formData from "form-data";
const request = poly(axios, formData);

ES6 Browser (Via Babel)

import poly from "polymorphic-request/implementations/axios";
import axios from "axios";
import formData from "form-data";
const request = poly(axios, formData);

request Usage

Supports:

ES5 Node.js

var request = require('request');
var formData = require('form-data');
var request = require('polymorphic-request').request(request, formData);

ES5 Browser (Webpack/Browserify)

var request = require('request');
request = require('polymorphic-request').request(request, FormData);

ES6 Node.js

import poly from "polymorphic-request/implementations/request";
import requestLib from "request";
import formData from "form-data";
const request = poly(requestLib, formData);

ES6 Browser (Via Babel)

import poly from "polymorphic-request/implementations/request";
import requestLib from "request";
import formData from "form-data";
const request = poly(requestLib, formData);

Testing Extensions[TBD]

var testRequest = request.testing(<instance>) //return an instance of request with testing features

testRequest.mock(<url or selectorFn>, <[err, res, data]>) //hardcode specific request configs

testRequest.record(<handlerFn or dirPath>) //record requests

testRequest.events()[TBD] //event control, first ref causes event prop

testRequest.stats()[TBD] //returns current stats, first ref activates

Writing Test Suites that support many request libraries (in Mocha)

    // before you start, install your dev-dependencies:
    // node-fetch, request, axios, form-data, express & polymorhpic-request

    //include some dependencies
    var poly = require('polymorhpic-request');
    var util = require('polymorhpic-request/util');

    //setup some variables
    var supportedModules = ['node-fetch', 'request', 'axios'];
    var testPort = 8081;
    var makeRequestFunction = util.makeRequestFunctionGenerator(
        supportedModules,
        { formData : require('form-data') },
        poly
    );
    var testRoot = 'http://localhost:'+testPort;

    describe('my-awesome-module', function(){
        supportedModules.forEach(function(moduleName){
            describe('uses the '+moduleName+' module', function(){
                var server;
                var getRequest = makeRequestFunction(moduleName);

                before(function(done){
                    server = util.makeServer(
                        done,
                        require('express'),
                        testPort
                    );
                });


                it('does something with the module', function(done){
                    var request = getRequest();
                    request = poly.testing(request);
                    //do your stuff
                });

                //more tests here


                after(function(done){
                    server.close(function(){
                        done();
                    });
                });
            });
        });
    });

This gives you a uniform suite executing across all fetch methods, and if you are writing a library you want to be compatible with many methods you'd set on your library at runtime with instance.setRequest('axios', axios) which you implement yourself as part of your library's API.

Testing

mocha