UNPKG

vmagic

Version:

vMagic is a RESTFul framework for NodeJS applications.

149 lines (130 loc) 3.62 kB
"use strict"; import request from "request"; class Request { /** * It send a http request as GET. * @param {string} uri address * @param {Object} qs query * @param {Object} headers settings * @return {Promise}. */ get(uri, qs = {}, headers) { const that = this; if (headers) { that.headers = headers; } else { that.headers = { "content-type": "application/json", }; } return new Promise((resolve, reject) => { const options = { method: "GET", uri: uri, headers: that.headers, json: true, qs: qs, }; request(options, function (error, response, body) { if (error) { reject(error); } else { resolve(body); } }); }); } /** * It send a http request as POST. * @param {string} uri address * @param {Object} body payload * @param {Object} headers settings * @return {Promise} . */ post(uri, body = {}, headers) { const that = this; if (headers) { that.headers = headers; } else { that.headers = { "content-type": "application/json", }; } return new Promise((resolve, reject) => { const options = { method: "POST", uri: uri, headers: that.headers, json: true, body: body, }; request(options, function (error, response, rBody) { if (error) { reject(error); } else { resolve(rBody); } }); }); } /** * It send a http request as PUT. * @param {string} uri address * @param {Object} body payload * @param {Object} headers settings * @return {Promise} . */ put(uri, body = {}, headers) { const that = this; if (headers) { that.headers = headers; } else { that.headers = { "content-type": "application/json", }; } return new Promise((resolve, reject) => { const options = { method: "PUT", uri: uri, headers: that.headers, json: true, body: body, }; request(options, function (error, response, rBody) { if (error) { reject(error); } else { resolve(rBody); } }); }); } delete(uri, body = {}, headers) { const that = this; if (headers) { that.headers = headers; } else { that.headers = { "content-type": "application/json", }; } return new Promise((resolve, reject) => { const options = { method: "DELETE", uri: uri, headers: that.headers, json: true, body: body, }; request(options, function (error, response, rBody) { if (error) { reject(error); } else { resolve(rBody); } }); }); } } export default Request;