JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q38490F
  • License MIT

Http Requests with ES6 Promise capability

Package Exports

  • http-promise-api

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (http-promise-api) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

#http-promise-api

NPM Version

A simple nodejs application for http requests with Promise wrapper

npm install http-promise-api

How to use

var HttpRequestAPI = require('http-promise-api')
HttpRequestAPI = new HttpRequestAPI('your base url',  {header object});

//get request
HttpRequestAPI.get('/posts').then(function(data){
    //Successful
}, function(error){
    //Error
})

All the required headers like Content-Type, Tokens etc.. need to be inserted while constructing HttpRequestAPI.

#Response/Error Formats

successful response

{
    headers:{
        //All headers from servers
    },
    status:{
        code:200,
        message:'OK'
    },
    data: {
        //desired data
    }
}

Error Response

    {
        headers:{ },
        status: {},
        error: {
            //Any error responses from server
        }
    }

#Methods

get(resource, query)

//url will be http://example.com/?id=123
HttpRequestAPI.get('/posts', {id:'123'}).then(function(data){
    //Successful
}, function(error){
    //Error
})

post(resource, postbody, query)

//url will be http://example.com/
HttpRequestAPI.get('/posts', {id:'123', name:'Anand', profession:'developer'}, {optional}).then(function(data){
    //Successful
}, function(error){
    //Error
})

delete(resource, query, body)

//url will be http://example.com/?id='123'
HttpRequestAPI.delete('/posts', {id:'123'}, {optional -body }).then(function(data){
    //Successful
}, function(error){
    //Error
})

put(resource, postbody, query)

//url will be http://example.com/
HttpRequestAPI.put('/posts', {id:'123', name:'Anand', profession:'developer'}, {optional}).then(function(data){
    //Successful
}, function(error){
    //Error
})

#Generic Method

A part from above requests there is a generic request makeRequest(resource, method, query, body)

HttpRequestAPI.makeRequest('/posts', 'ANY METHOD', {}, {}).then(function(data){
    //Successful
}, function(error){
    //Error
})