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

kwest

v0.3.6

Published

express for client requests.

Downloads

13

Readme

kwest Build Status Dependency Status

express for client side requests. Uses a middleware-like strategy to build up a http request method tailored to your specific needs.

Installation

$ npm install --save kwest

Disclaimer

This is just a prototype, use at your own risk!

Use

Rougly the same API as the request library (for now). Kwest uses promises instead of callbacks though. With the addition of a .use() method kwest can be extended with middleware. kwest aims to provide a simple core and have higher level modules handle functionality such as redirection, gzip, authentication, caching...

The most simple example is the following middleware, which is basically just the identity.

var kwest = require('kwest');
var request = kwest();
request.use(function (req, next) {
  return next(req);
});

request(url)
  .then(function (res) {
    // response is exactly the same as without the middleware
  })

Example: You can make kwest reject its result when the server responds with a bad statuscode. This functionality is actually provided in the kwest-handle-error module.

var request = kwest();
request.use(function rejectBadStatus(req, next) {
  return next(req)
    .then(function (res) {
      if (res.statusCode !== 200) throw new Error('Bad status');
      return res;
    });
});

request(url)
  .catch(function (err) {
    // this promise will be rejected when the server returns a bad statuscode
  })

Or you can augment functionality. This example is actually provided in the kwest-gzip module

var request = kwest();
request.use(function rejectBadStatus(req, next) {
  req.setHeader('accept-encoding', 'gzip');
  return next(req)
    .then(function (res) {
      // handle gzip ...
      return unzipped;
    });
});

request(url)
  .then(function (res) {
    // Can have gzipped responses now
  })

API

var request = kwest([function])

Creates a request function that can make the most basic http requests. Optionally an argument can be provided that represents the last request in the middleware chain. This is useful for e.g. mocking, browserify,... request can be called either with a string or with an options object to make the actual request. available options are:

  • uri: string or parsed url. This is mandatory
  • method: cdefaults to GET
  • headers: headers for this request
  • ... whatever extra options you want to pass down to middleware

request.use(middleware)

Adds a middleware to the chain. This is a function of the form

function (request, next) {
  return next(request);
}

request contains all request parameters and is intended to be modified by middleware. It always has following properties:

  • uri: this is a parsed url object with the current url.
  • method: current http method for this request
  • headers: map of the headers for this request
  • setHeader, getHeader, hasHeader, removeHeader: Utility methods provided by caseless.httpify.

The middleware function is expected to return a promise with a response object. next can be called with request as parameter to receive promise of the response provided kwest. This response is passed on to the next middleware and can be transformed as you wish.

request.fork()

returns a new instance based on the current one that can be used to add extra middleware.

request.get

Convenience function. Defaults to method GET.

request.post

request.put

request.del

request.head

request.patch

...