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

wassy

v2.0.1

Published

REST endpoint abstraction library

Downloads

15

Readme

Wassy

Wassy allows you to abstract RESTfull endpoints into POJOs.

Install

npm install wassy 

CDN

https://cdn.jsdelivr.net/npm/[email protected]

Get Started

The easier way to understand Wassy design is to image the ORM pattern but instead of mapping POJOs to database table record, you are mapping Http endpoint to a POJO. One endpoint, one POJO.

The first step is to define an endpoint

import Endpoint from 'wassy';

var UsersEP = Endpoint.define({
        host: 'https://api.domain.com',
        uri: '/users'
      });

After you create an endpoint instance to send your request

var user = new UsersEP();

user.get()
  .then((resp) => {
     // handle response
  }).catch((err) => {
    // handle error
  });

All the known HTTP methods (GET/POST/PUT/DELETE/HEAD) are mapped to functions get, post, put, delete, head. There is a send function if you need to send a custom method

var promise  = user.send('PING');

That's everything you need to know for a basic usage, easy right? But there's more and if you are hungry for more follow the next section

Endpoint

In REST, the resource typically refers to some object or set of objects that are exposed at an API endpoint, like /users. The same meaning applies to Wassy Endpoint class. To define an endpoint, you call the .define(options) method.

var UserEP = Endpoint.define({
        uri: '/users',
    });

The options parameter has many attributes you can configure

  • host is server host, default to http://locahost
  • uri the resource path, defaults to /
  • headers optional header fields, in the form of {'Header-Field':'value'}
  • model optional placeholder to define variables and methods that will be merged to your response object or objects in case the data returned is an array object.
  • cookies the resource path, defaults to /
  • preRequest a pre-request callback function that can be used to modify the endpoint instance before the request is sent. Use this to set custom headers, etc.
  • postRequest an optional object of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:
      postRequest: {
        404: function() {
          alert( "page not found" );
        }
      }
Extending an Endpoint

You can create a sub class from a Endpoint super class using the .extends(options) methods. Option take the same parameters as .define(options)

var BaseEP = Endpoint({
  host: 'http://api.com/v1'
});

var UsersEP = BaseEP.extends({
  uri: '/users',
});

var SchoolsP = BaseEP.extends({
  uri: '/schools',
  headers: { Accept: 'appliction/json' }
});

Both UsersEP and SchoolsEP endpoints will have their host parameter set to http://api.com/v1.

Only SchoolsEP will add Accept: application/json header to all its request

Send Request

After defining an endpoint you its instance to send your request

new UsersEP()
      .post({ name: 'wassy' })
      .then(function (resp){ })
      .catch(function (err){ })
      ;

All the known HTTP methods (GET/POST/PUT/DELETE/HEAD) are mapped to functions get, post, put, delete, head. There is a send function if you need to send a custom method

var userEP = new UsersEP()
userEP.send('PING');

Endpoint instances can take an optional object as parameter to set path variables value. See the next section to learn more about path variables

Path variables binding

An endpoint may have dynamic parameters inside the URI /users/2 or /users/232/comments/10 The bound variable is defined using curly braces {<VARIABLE_NAME>}

var UserCommentsEP = Endpoint.define({
    uri: '/users/{userId}/comments/{comId}',
  });
    
var promise = 
  new UserCommentsEP({
    userId: 43,
    comId: 100,
  }).get();

Binding parameters are not always numbers they can be string.

var promise = 
	new User({ state: 'active' }).get();

Response

The request methods returns a promise, promise callbacks .then(), .catch() and .finally() are invoked in the order they are registered. Available Promise methods are:

  • promise.then(function( response ) {});

    The response object as these attributes

    • status HTTP status code.
    • headers response headers.
    • body raw response data.
    • bodyJSON parsed json object, if the response is a JSON string.
    • request request object configuration.
  • promise.catch(function( jqXHR ) {});

    It receives the $.ajax jqHRX object.

Model

A model defines variables and methods that will be merged to your response object or objects in case the data returned is an array object.

var SchoolsEP = Endpoint.define({
  uri: '/schools',
  model: {
    year: 2018,
    getName() {
      return this.name;
    },
    getCountry() {
      return 'USA';
    }
  },
});

var school = new SchoolsEP()
  .get()
  .then(function(response) {
    const model = response.model;
    // echo: 2018
    console.log(model.year);
    
    // echo: Progress 
    console.log(model.name);
    
    console.log(model.getName());
    // echo: Progress as well
    
    // echo: Progress 
    console.log(model.getAddress());
  });