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

@coldbrewjs/router

v0.1.9

Published

API Router for Readable Code

Readme

Router Build Status

API Router for Readable Code.

Table of Contents

Features

  • mostly covered basic HTTP method
  • provide method which make payload resource for HTTP client
  • easily set and override HTTP client configuration with less code
  • modify partially basic routing configuration with builder pattern

Browser Support

Chrome | Firefox | Safari | Opera | Edge | IE | --- | --- | --- | --- | --- | --- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |

Installing

Using npm:

$ npm install @coldbrewjs/router

Using yarn:

$ yarn add @coldbrewjs/router

Example

Setting up a routing environment using Configure

  • Use Configure to set a global environment for api routing
  • All instance of Router inherit routing environment from Configure
  • Recommend you use Configure at the top of file directory such as app.tsx, index.ts
import { Configure } from '@coldbrewjs/router';

Configure.getInstance.baseURL('http://api-test.com/v1');
Configure.getInstance.header({
  accessToken: '1234',
  transactionId: '5678'
});
Configure.getInstance.config({
  timeout: 100000,
  maxContentLength: 100000
})

Performing a GET request

import { Router, RouterResponse, RouterError } from '@coldbrewjs/router';

const router = new Router();
/** 
 *  already have string value 'http://http://api-test.com/v1' as prefix uri
 *  it always routes with header { accessToken: '1234', transactionId: '5678' }
 */

// Make a request with callback pattern
router.uri('/user?id=12345')
  .get((err: RouterError, response: RouterResponse) => {
        if (err) {
          console.log('err:', err);
        }

        console.log(response);
  });

// Make a request with promise pattern
router.uri('/user?id=12345')
  .get()
  .then((response: RouterResponse) => {
          console.log(response);
  })
  .catch((err: RouterError) => {
          console.log(err);
  });

// Make a request with async/await pattern
async function getUser() {
  try {
    const results: RouterResponse = await router.uri('/user?id=12345').get();
  } catch (err) {
    console.log(err);
  }
}

NOTE: async/await is part of ECMAScript 2017 and is not supported in Internet Explorer and older browsers.

Performing a POST request

import { Router, RouterResponse, RouterError } from '@coldbrewjs/router';

const router = new Router();

// Make a request with callback pattern
router.uri('/user')
      .payload({
                id: 'abcdef',
                password: 'qwerty',
              })
      .post((err: RouterError, response: RouterResponse) => {
              if (err) {
                  console.log(err);
              }

              console.log(response);
      });

// Make a request with promise pattern
router.uri('/user')
      .payload({
                id: 'abcdef',
                password: 'qwerty',
              })
      .post()
      .then((response: RouterResponse) => {
            console.log(response);
      })
      .catch((err: RouterError) => {
            console.log(err);
      })
      .finally(() => {
            console.log('done...!');    
      });

// Make a request with async/await pattern
try {
  const response = 
      await router.uri('/user')
                  .payload({
                            id: 'abcdef',
                            password: 'qwerty',
                          })
                  .post();

  console.log(response);
} catch(err) {
  console.log(err);
}

Override routing environment dynamically

import { Configure, Router } from '@coldbrewjs/router';

Configure.getInstance.baseURL('https://dev.api.com/v1');
Configure.getInstance.header({
  accessToken: '1234',
  transactionId: '5678'
});

async function dynamicRouting(isDev: boolean) {
  const router = new Router():

  if (isDev) {
    /**
     * Url: https://dev.api.com/v1/ping
     * Header: { accessToken: '1234', transactionId: '5678' }
    */
    return await router.uri('/ping').get();
  } else {
     /**
     * Url: https://stage.api.com/v1/ping
     * Header: { accessToken: '1234', transactionId: '8765', customToken: '0000' }
    */
    return await router.overrideHeader({
      transactionId: '8765',
      customToken: '0000'
    }).overrideUrl('https://stage.api.com/v1/ping').get();
  }
}

try {
  const response = await dynamicRouting(process.env.NODE_ENV === 'development' ? true : false);
} catch(err) {
  console.log(err);
}

NOTE: You must use overrideHeader at every single router instance if you custom http header without Configure.

Performing multiple concurrent requests

import { Router, Configure } from '@coldbrewjs/router';

const baseUri = 'http://api.service.com';

Configure.getInstance.baseURL(baseUri);

const products = '/products';
const categories = '/categories';
const sellers = '/sellers';

const firstRouter = new Router().uri(products).get();
const secondRouter = new Router().uri(categories).get();
const thirdRouter = new Router().uri(sellers).get();

Promise.all([firstRouter, secondRouter, thirdRouter]).then(
    (response) => {
        console.log(response[0]);
        console.log(response[1]);
        console.log(response[2]);
    },
);

Performing a Form data request

import { Configure, Router, RouterResponse, RouterError } from '@coldbrewjs/router';
import * as fs from 'fs';
import path from 'path';
import FormData from 'form-data';

Configure.getInstance.baseURL('https://dev.api.com/upload');
Configure.getInstance.config({
  timeout: 100000,
  maxContentLength: 100000
})

const router = new Router();
const formData = new FormData();
formData.append('file', JSON.stringify(temp));

// Make a request with FormData from Browser
try {
    const result = await router
        .uri('/v1')
        .payload(formData)
        .form();

    console.log(result.data);
} catch (err) {
    console.log(err);
}

// Make a request FormData from Node.js  
const file = fs.createReadStream(path.resolve('./sample.pdf'));
const formData_2 = new FormData();
formData.append('file', file);

const router_2 = new Router();

try {
    const result_2 = await router
        .overrideHeader({
            ...formData.getHeaders(),
        })
        .uri('/v2')
        .payload(formData)
        .form();

    console.log(result.data);
} catch (err) {
    console.log(err);
}

License

MIT