From ec18ba6606e7e067918fa02711fe9169e076b4f2 Mon Sep 17 00:00:00 2001 From: Taylor Downs Date: Wed, 23 May 2018 16:13:42 +0000 Subject: [PATCH] raw request function for advanced usage --- lib/Adaptor.js | 19 +++++++++++++++++++ lib/Client.js | 15 +++++++++++++++ package.json | 2 +- src/Adaptor.js | 24 +++++++++++++++++++++++- src/Client.js | 14 ++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) diff --git a/lib/Adaptor.js b/lib/Adaptor.js index 0fc4179..32bc960 100644 --- a/lib/Adaptor.js +++ b/lib/Adaptor.js @@ -15,6 +15,7 @@ exports.put = put; exports.patch = patch; exports.del = del; exports.parse = parse; +exports.request = request; var _languageCommon = require('language-common'); @@ -357,3 +358,21 @@ function parse(body, script) { } }; } + +/** + * Make a request using the 'request' node module. + * @public + * @example + * request(params); + * @function + * @param {object} params - Query, Headers and Authentication parameters + * @returns {Operation} + */ +function request(params) { + return function (state) { + + var expanded = typeof params === 'string' ? params : (0, _languageCommon.expandReferences)(params)(state); + + return (0, _Client.rawRequest)(expanded); + }; +} diff --git a/lib/Client.js b/lib/Client.js index e5ca834..6f16996 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; +exports.rawRequest = rawRequest; exports.req = req; var _request = require('request'); @@ -16,6 +17,20 @@ var _Utils = require('./Utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } +function rawRequest(params) { + return new Promise(function (resolve, reject) { + (0, _request2.default)(params, function (error, response, body) { + error = (0, _Utils.assembleError)({ error: error, response: response }); + error && reject(error); + + console.log('\u2713 Request succeeded.'); + console.log('Server responded with: \n' + JSON.stringify(response, null, 2)); + var resp = (0, _Utils.tryJson)(body); + resolve(resp); + }); + }); +} + function req(method, params) { var url = params.url, headers = params.headers, diff --git a/package.json b/package.json index b1ef44c..9a36223 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "language-http", - "version": "2.1.2", + "version": "2.2.0", "description": "An HTTP request language package for use with Open Function", "main": "lib/index.js", "scripts": { diff --git a/src/Adaptor.js b/src/Adaptor.js index b3e3472..588312c 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -1,5 +1,5 @@ /** @module Adaptor */ -import { req } from './Client'; +import { req, rawRequest } from './Client'; import { setAuth, setUrl } from './Utils'; import { execute as commonExecute, @@ -262,6 +262,28 @@ export function parse(body, script) { } } +/** + * Make a request using the 'request' node module. + * @public + * @example + * request(params); + * @function + * @param {object} params - Query, Headers and Authentication parameters + * @returns {Operation} + */ +export function request(params) { + return state => { + + const expanded = ( + typeof params === 'string' ? + params : + expandReferences(params)(state) + ); + + return rawRequest(expanded) + } +} + export { alterState, dataPath, diff --git a/src/Client.js b/src/Client.js index c5d7f37..ee5be00 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1,6 +1,20 @@ import request from 'request' import { assembleError, tryJson } from './Utils'; +export function rawRequest(params) { + return new Promise((resolve, reject) => { + request(params, (error, response, body) => { + error = assembleError({error, response}) + error && reject(error); + + console.log(`✓ Request succeeded.`); + console.log(`Server responded with: \n${JSON.stringify(response, null, 2)}`); + const resp = tryJson(body); + resolve(resp); + }) + }) +} + export function req( method, params ) { const { url, headers, body, auth, query, rest } = params; return new Promise((resolve, reject) => {