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

wellrested

v4.0.1

Published

A superagent wrapper library for creating preconfigured HTTP service clients

Downloads

5

Readme

WellRested

WellRested is a promise based client wrapper around superagent and path-to-regexp to create pre-configured clients for http services.

NPM version Licensed MIT ![Nodejs 4+](https://img.shields.io/badge/node.js-%3E=_4 LTS-brightgreen.svg) Downloads Build Status

Installation

WellRested requires superagent 7.1 or later be installed alongside.

npm install superagent wellrested

Example

var wellrested = require('wellrested');
var client = wellrested({
  baseUrl: 'http://example.com',
  headers: { Accepts: 'application/json' },
  endpoints: {
    'user': '/user/:username',
    'message': '/message/:messageid?',
  },
  mixins: {
    getUser (username) {
      return this.user.get({ username }).then();
    },
    createMessage (contents) {
      return this.message.post()
        .send({ contents })
        .then();
    },
    getMessage (messageid) {
      return this.message.get({ messageid }).then();
    }
  }
});

client.createMessage('Test message').then((reply) => console.log(reply));

Usage

  • client = wellrested(config) - Takes a configuration object and returns a client.

  • request = client(method, url, [parameters]): Creates a promise extended superagent request for the method and url, resolving the url against the baseUrl in the config. parameters is an object of substitution values for any url placeholders.

  • request = client.METHOD(url, [parameters]): METHOD may be any of the following methods: get, post, put, delete, patch, head. The methods available are configurable by passing your own methods array of names on the config object (see below).

  • request = client.ENDPOINT.METHOD([parameters]): ENDPOINT is the key name of any of the endpoints provided on the config object (see below).

  • request.end(): Starts the superagent request and returns a promise that will resolve with the response object. If the server responds with a 4xx or 5xx error code, the promise will reject with the superagent error.

  • request.then([resolved], [rejected]): Starts the superagent request and resolves only the parsed response from the request. If the response body is not parsable, it will resolve with the body text. The function may be invoked with no arguments to simply return a promise instead of attaching handler functions.

Config Options

  • baseUrl: The root url of the http service to interact with.

  • headers: An object hash of HTTP headers to set on every request.

  • endpoints: An object hash of name/url values for service endpoint shortcuts. These URLs may contain express style placeholders (ex: /resource/:id/) which get replaced with values at the initialization of the request.

  • mixins: The mixins object is an optional named set of functions to be mixed into the base client object. These functions have their this context hard bound to the client object at creation time.

  • auth: The auth config setting lets you define the authentication details for the Authorization header on all requests. If this value is set to an object in the form of { user, pass } then the requests will be setup for HTTP Basic Auth. If a string is provided then the requests will be setup with the string as a Bearer Token. Note, these may also be done per-request via the usual superagent methods.