Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
raw request function for advanced usage
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed May 23, 2018
1 parent ea74a38 commit ec18ba6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.put = put;
exports.patch = patch;
exports.del = del;
exports.parse = parse;
exports.request = request;

var _languageCommon = require('language-common');

Expand Down Expand Up @@ -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);
};
}
15 changes: 15 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
24 changes: 23 additions & 1 deletion src/Adaptor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @module Adaptor */
import { req } from './Client';
import { req, rawRequest } from './Client';
import { setAuth, setUrl } from './Utils';
import {
execute as commonExecute,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down

0 comments on commit ec18ba6

Please sign in to comment.