This is the YAWP! Framework javascript client.
It streamlines the access to your REST APIs from Node.js or a browser.
Contents
- Installation
- Setup
- Repository Actions
- Custom Actions
- Query
- Transformers
- Instance Methods
- Class Extension
- ES5 Prototypes
Installation
Web
NodeJs
npm install yawp --save
Note: if your environemnt doesn't support ES6 promises, you'll need to use a polyfill like this one.
Setup
By default the client routes all API calls to the path /api of the current app's host. You can override this setting as following:
yawp;
Repository Actions
// create;// update;// patch;// destroy;// fetch;// list;
Custom Actions
// @GET("me") over collection action;// @PUT("reverse-name") single entity action;
Query
// where + list;// where + first;// limit;// order;
Transfomers
// transform + where + list;// transform + first;
Instance Methods
All objects returned by the yawp query methods are wrapped inside an instance of the class Yawp. This class gives us some methods that operate over those instances:
;
The complete API methods of the Yawp class can be found here.
Class Extension
All yawp client features can be extendend by subclassing the base Yawp class for a given endpoint. And this can be done either with the ES6 class syntax or with ES5 prototypes.
For instance, to create a Person class to add and encapsulate some new methods to the endpoint /people, we can do something this:
'/people'
Now to add static methods to this endpoint model, we can do:
'/people'static {return this;}
Note that now all the objects returned by the API calls using Person will be wrapped inside an instance of the Person class. With this, it is also possible to add methods that operate over instances of that class:
'/people'static {return this;}{return this;}
And use then in our application code:
Personinative
Finally, we can override methods:
'/people'{console;return super;}
ES5 Prototypes
If we are running our app in an environment that doesn't support ES6 class syntax, we have two options. The first is to transpile our ES6 code to ES5 using the Babel JS. The other is to use some convenience YAWP! methods. To create the same Person class as above but in ES5 we can do:
var Person = ;Person {return this;}Personprototype {return this;}
If we want to override methods, there is a small difference from the ES6 version. With ES5 we have to access the super methods using the syntax this.super, like this:
Personprototype {console;return thissuper;}