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

rest-template

v0.7.5

Published

HTTP client library inspired by the Spring Framework's RestTemplate

Downloads

12

Readme

Rest Template

Just enough client, as you need it. Make HTTP requests from a browser or Node.js applying the only the client features you need. Configure a client once, and share it safely throughout your application. Easily extend with interceptors that wrap the request and/or response, or MIME type converters for rich data formats.

Build Status

Getting Started

Rest can be installed via NPM, or from source.

To install without source:

$ npm install rest-template

From source:

$ npm install

Rest is designed to run in a browser environment, utilizing AMD modules, or within Node.js. curl is highly recommended as an AMD loader, although any loader should work.

An ECMAScript 5 compatible environment is assumed. Older browsers, ::cough:: IE, that do not support ES5 natively can be shimmed. Any shim should work, although we've tested against cujo's poly

Usage

Using Rest is easy. The core clients provide limited functionality around the request and response lifecycle. The input and response objects are normalized to support portability between browser and server environments.

The response from a client is a promise that is resolved when the remote request finishes.

The core client behavior can be augmented with interceptors. An interceptor wraps the client and transforms the request and response. For example: an interceptor may authenticate a request, or reject the promise if an error is encountered. Interceptors may be combined to create a client with the desired behavior. A configured interceptor acts just like a client.

Making a basic request:

define(['rest'], function(client) {
    client({ path: '/' }).then(function(response) {
        console.log('response: ', response);
    });
});

In this example, you can see that the request object is very simple, it just includes the path. All of the attributes of a request are optional.

The response should look familiar as well, it contains all the fields you would expect, including the response headers (many clients ignore the headers).

Working with JSON:

If you're paying attention, you may have noticed that the response.entity in the previous example is a String. Often we need to work with more complex data types. For this, Rest supports a rich set of MIME type conversions with the mime interceptor. The correct converter will automatically be chosen based on the Content-Type response header. Custom converts can be registered for a MIME type, more on that later...

define(['rest/interceptor/mime'], function(mime) {
    var client = mime();
    client({ path: '/data.json' }).then(function(response) {
        console.log('response: ', response);
    });
});

Before an interceptor can be used, it needs to be configured. In this case, we will accept the default configuration, and obtain a client. Now when we see the response, the entity will be a JS object instead of a String.

Composing Interceptors:

define(['rest/interceptor/mime', 'rest/interceptor/errorCode'], function(mime, errorCode) {
    var client = mime();
    client = errorCode(client, { code: 500 });
    client({ path: '/data.json' }).then(
        function(response) {
            console.log('response: ', response);
        },
        function(response) {
            console.error('response error: ', response);
        }
    );
});

In this example, we take the client create by the mime interceptor, and wrap it in the errorCode interceptor. The errorCode interceptor can accept a configuration object that indicates what status codes should be considered an error. In this case we override the default value of <=400, to only reject with 500 or greater status code.

Since the errorCode interceptor can reject the response promise, we also add a second handler function to receive the response for requests in error.

Clients can continue to be composed with interceptors as needed. At any point the client as configured can be shared. It is safe to share clients and allow other parts of your application to continue to compose other clients around the shared core. Your client is protected from additional interceptors that other parts of the application may add.

Custom MIME Converters:

define(['rest/mime/registry'], function(registry) {
   registry.register('application/vnd.com.example', {
       read: function(str) {
           var obj;
           // do string to object conversions
           return obj;
       },
       write: function(obj) {
           var str;
           // do object to string conversions
           return str;
       }
   });
});

Registering a custom converter is a simple as calling the register function on the mime registry with the type and converter. A converter has just two methods: read and write. Read converts a String to a more complex Object. Write converts an Object back into a String to be sent to the server. HTTP is fundamentally a text based protocol after all.

Built in converters are available under rest/mime/type/{type}, as an example, JSON support is located at rest/mime/type/application/json. You never need to know this as a consumer, but it's a good place to find examples.

Reporting Issues

Please report issues on GitHub. Include a brief description of the error, information about the runtime (including shims) and any error messages.

Feature requests are also welcome.

Running the Tests

The test suite can be run in two different modes: in node, or in a browser. We use Buster.JS as the test driver, buster is installed automatically with other dependencies.

Before running the test suite for the first time:

$ npm install

To run the suite in node:

$ npm test

To run the suite in a browser:

$ npm start
browse to http://localhost:8282/ in the browser(s) you wish to test.  It can take a few seconds to start.

Thanks

  • Arjen Poutsma - Creator of Spring's RestTemplate
  • Brian Cavalier - cujo.js lead
  • John Hann - cujo.js lead
  • VMware - for allowing this project to be open sourced

Change Log

0.7.5

  • Initial release, everything is new