1.0.1 • Published 5 years ago

superagent-wrapper v1.0.1

Weekly downloads
21
License
MIT
Repository
github
Last release
5 years ago

Superagent wrapper

This is a simple wrapper over superagent library.

Any pull request for optimizations and new additions is more than welcome.

Installing superagent-wrapper

npm install superagent-wrapper

Usage

import api from 'superagent-wrapper';

Methods

init

Takes apiUrl and headers as parameters.

Initialisation should be called before any get/post/put/delete request.

const API_BASE_URL = 'http://domain/api';
const defaultHeaders = {
    Accept: 'application/json',
    AppVersion: '1.0.0'
};

api.init(API_BASE_URL, defaultHeaders);

get

Takes endpoint and headers as parameters.

Headers sent to the server will extend the default headers set into the initialization call.

Returns a promise.

get('/fetch-endpoint', { Authorization: `Token 123` })
    .then((data) => {
        // do something with data
    })
    .catch((error) => {
        // check for error.data and error.status and use these values
    });

post

Takes endpoint and headers as parameters.

Headers sent to the server will extend the default headers set into the initialization call.

Returns a promise.

post('/post-endpoint', { Authorization: `Token 123` })
    .then((data) => {
        // do something with data
    })
    .catch((error) => {
        // check for error.data and error.status and use these values
    });

put

Takes endpoint and headers as parameters.

Headers sent to the server will extend the default headers set into the initialization call.

Returns a promise.

put('/put-endpoint', { Authorization: `Token 123` })
    .then((data) => {
        // do something with data
    })
    .catch((error) => {
        // check for error.data and error.status and use these values
    });

del

Takes endpoint and headers as parameters.

Headers sent to the server will extend the default headers set into the initialization call.

Returns a promise.

del('/delete-endpoint', { Authorization: `Token 123` })
    .then((data) => {
        // do something with data
    })
    .catch((error) => {
        // check for error.data and error.status and use these values
    });