Skip to content

Latest commit

 

History

History
61 lines (48 loc) · 2.04 KB

README.md

File metadata and controls

61 lines (48 loc) · 2.04 KB

rapport-http CircleCI Coverage Status

NPM

Adds HTTP style methods to Rapport, intended for use with rapport-router.

Installation

Node: Install the plugin via NPM: npm install --save rapport-http

Browser: Attach rapport.http.min.js to your HTML page

Then add the plugin to rapport:

// Globally
Rapport.use(require('rapport-http')); // In Node.js
Rapport.use(RapportHttp); // In the browser

// Or to a instance
Rapport(wsImplementation).use(require('rapport-http')); // In Node.js
Rapport(wsImplementation).use(RapportHttp); // In the browser

Usage

This plugin adds methods to the Rapport socket. To use it, simply call them:

const ws = rapport.create('url');

// Create a request on /users
ws.get('/users');
ws.post('/users');
ws.put('/users');
ws.patch('/users');
ws.delete('/users');
ws.http('get', '/users');

// Add query parameters (each call appends to the query string)
ws.get('/users').query({ key: value });
ws.get('/users').query('key=value');
ws.get('/users').query(['key=value', 'key2=value2']);

// Add a body
ws.get('/users').body({ key: value }); // Merged with any existing body objects
ws.get('/users').body('some string');  // Overwrites an existing body
ws.get('/users').body([1, 2, 3]); // Also overwrites an existing body

// Add a request timeout
ws.get('/users').timeout(3000); // Times out in 3 seconds

// Send it!
ws.get('/users').send().then((response) => {}); // With a promise
ws.get('/users').send((response, err) => {}); // Or a callback

// All together now
ws.get('/users')
    .query({ id: 1 })
    .timeout(3000)
    .send()
    .then((response) => {
        console.log(`Got users: ${JSON.stringify(response)}`);    
    });