4.0.0 • Published 8 years ago

net.js v4.0.0

Weekly downloads
320
License
MIT
Repository
github
Last release
8 years ago

Net

A light-weight networking library for the browser.

Quick Start

Install Net like this:

npm install net.js --save

After including build/net.min.js in your project you can use it like this:

var myAPI = new Net('https://api.dhariri.com/');

myAPI
.get('articles/')
.then((response) => {
    // Got 'em!
    response.json.articles.forEach((article) => {
        console.log(article);
    });
}).catch((error) => {
    // Something bad happened...
    console.log(error);
});

HTTP Methods

Net supports the following HTTP methods: GET, PUT, POST, PATCH, and DELETE. You can call them by their lowercase Net method counterparts. Example: Net.post() or Net.delete()

Convenience

If you find yourself attaching similar headers to every request (like authorization headers, for example) you can use the constructor to set up some default headers:

const myAPI = new Net(
    'https://api.dhariri.com/', // Specify a root to prepend to all requests
    {
        'Authorization' : `Basic ${window.atob('giraffes')}`,
        'Content-Type' : 'application/json'
    }
);

myAPI
.get('articles/')
.then((response) => {
    response.json.articles.forEach((article) => {
        console.log(article.title);
    });
});

You can set these at any time (for example, when a user logs in) by calling Net.setHeaders(headers):

myAPI
.setHeaders({
    'Authorization' : `Basic ${window.atob('giraffes')}`
});

myAPI
.get('secrets/')
.then((response) => {
    // Got them secrets
});
4.0.0

8 years ago

3.0.0

8 years ago

0.2.0

8 years ago

0.1.0

9 years ago