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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tonybadguy/call-me-maybe

v2.1.1

Published

A Node.js module for creating REST clients with easy request model templating and straightforward extensibility

Downloads

2,647

Readme

@tonybadguy/call-me-maybe

npm version Build Status codecov

A Node.js module for creating REST clients with easy request model templating and straightforward extensibility

A simple GET request :+1:

const send = require('@tonybadguy/call-me-maybe');

send({
  url: 'https://httpbin.org/get'
}).then(response => {
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
  console.log(response.jsonBody.origin);
}).catch(error => {
  console.log(error);
});

POST with body :sparkling_heart:

send({
  url: 'https://httpbin.org/post',
  method: 'POST',
  body: 'my data'
}).then(response => {
  console.log(response.body);
  console.log(response.jsonBody.data); // 'my data'
});

POST with object as json body :sparkles::sparkling_heart::sparkles:

send({
  url: 'https://httpbin.org/post',
  method: 'POST',
  jsonBody: {
    foo: 'bar'
  }
}).then(response => {
  console.log(response.body);
  console.log(response.jsonBody.json.foo); // 'bar'
});

Content-Type header is automatically set to application/json.

POST with object as urlencoded body :sparkles::scream::sparkles:

send({
  url: 'https://httpbin.org/post',
  method: 'POST',
  urlencodedBody: {
    foo: 'bar'
  }
}).then(response => {
  console.log(response.jsonBody.form.foo); // 'bar'
});

Content-Type header is automatically set to application/x-www-form-urlencoded.

Make it fancy with urlParams :sparkles::sparkling_heart::scream::sparkling_heart::sparkles:

send({
  url: 'https://httpbin.org/{foo}',  // 'https://httpbin.org/get'
  urlParams:{
    foo:'get'
  }
}).then(response => {
  console.log(response.body);
});

Or with a query :hand::dollar::dollar::dollar:

send({
  url: 'https://httpbin.org/get',  // 'https://httpbin.org/get?foo=bar%20baz'
  query:{
    foo:'bar baz'
  }
}).then(response => {
  console.log(response.body);
});

Set headers :collision::dizzy_face::collision:

send({
  url: 'https://httpbin.org/get',
  headers:{
    'x-my-header':'oh hai'
  }
}).then(response => {
  console.log(response.body);
});

Set bearer token authorization header :collision::revolving_hearts::dizzy_face::revolving_hearts::collision:

This is a shortcut for setting the Authorization header.

send({
  url: 'https://httpbin.org/get',
  bearerToken: 'mytoken'
}).then(response => {
  console.log(response.body);
});

Authorization header is set to Bearer mytoken.

Handle non-200 status :fire::poop::fire::ok_hand:

send({
  url: 'https://httpbin.org/status/500'
}).then(response => {
  // won't be called
}).catch(error => {
  console.log(error.response.statusCode); // 500
});

All features above are enabled by default

  • They are implemented using pluggable filter modules on request / response
  • You can customize which filters to use via optional params of the send() function
  • You can write your own filters

Advanced: Overriding default filters :rocket:

'use strict';

const send = require('@tonybadguy/call-me-maybe');
const jsonBodyFilter = require('@tonybadguy/call-me-maybe/lib/response-filters/json-body');

const request = {
  url: 'https://httpbin.org/get'
};

const requestFilters = []; // don't use any of the default request filters
const responseFilters = [jsonBodyFilter]; // only use the json body filter

send(request, requestFilters, responseFilters).then(response => {
  console.log(response);
});

Advanced: Example custom request filter :rocket::rocket:

'use strict';

// a filter that always sets the request body to 'hello world!'
module.exports = {
  filter: (request) => {
    request.body = 'hello world!';
  
    return request;
  }
};

Advanced: Custom response filters are exactly the same :rocket::rocket:

'use strict';

// a filter that always sets the response body to 'hello world!'
module.exports = {
  filter: (response) => {
    response.body = 'hello world!';
  
    return request;
  }
};