es6 updates for helpers

This commit is contained in:
Todd Treece 2016-05-27 13:16:02 -04:00
parent 2a3d76914e
commit 730252e529
3 changed files with 54 additions and 90 deletions

View file

@ -1,36 +1,8 @@
require('dotenv').load();
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
mocha = require('gulp-mocha');
gulp.task('lint', function() {
var lint = jshint({
"curly": false,
"eqeqeq": true,
"immed": true,
"latedef": "nofunc",
"newcap": false,
"noarg": true,
"sub": true,
"undef": false,
"unused": "var",
"boss": true,
"eqnull": true,
"node": true,
"-W086": true
});
return gulp.src([
'index.js',
'lib/*.js',
'test/*.js'
]).pipe(lint)
.pipe(jshint.reporter('jshint-stylish'));
});
gulp.task('test', function() {
return gulp.src('test/*.js', {read: false})
@ -45,4 +17,4 @@ gulp.task('test', function() {
});
gulp.task('default', ['lint', 'test']);
gulp.task('default', ['test']);

View file

@ -1,62 +1,18 @@
var util = require('util');
'use strict';
/************************ PROMISIFY *****************************/
function success(resolve, response) {
const success = (resolve, response) => {
resolve(response.obj);
}
function fail(reject, response) {
reject(response);
}
exports.promisify = function(module, operation, parameters) {
return function() {
var self = this,
args = Array.prototype.slice.call(arguments);
var require_auth = parameters.find(function(param) {
return param.name === 'Authorization';
});
var promise = function(access_token) {
return new Promise(function(resolve, reject) {
if(access_token) {
if(! args[0])
args[0] = {};
args[0].Authorization = 'Bearer ' + access_token;
}
args.push(success.bind(null, resolve));
args.push(fail.bind(null, reject));
operation.apply(self, args);
});
};
if(require_auth)
return module.getAccessToken().then(promise);
return promise();
};
};
/********************* SWAGGER FIXES ****************************/
function processAPI(module, api) {
const fail = (reject, response) => {
reject(response);
};
var processed = {};
const processAPI = (module, api) => {
Object.keys(api.operations).forEach(function(operation) {
const processed = {};
Object.keys(api.operations).forEach((operation) => {
processed[operation] = exports.promisify(
module,
api[operation],
@ -66,13 +22,52 @@ function processAPI(module, api) {
return processed;
}
};
exports.processSwagger = function(module) {
const applyAuth = (operation, args, access_token) => {
var apis = {};
return new Promise((resolve, reject) => {
Object.keys(module.client.apis).forEach(function(api) {
if(access_token) {
if(! args[0])
args[0] = {};
args[0].Authorization = 'Bearer ' + access_token;
}
args.push(success.bind(null, resolve));
args.push(fail.bind(null, reject));
operation.apply(this, args);
});
};
exports.promisify = (module, operation, parameters) => {
return (...args) => {
const require_auth = parameters.find((param) => {
return param.name === 'Authorization';
});
if(require_auth)
return module.getAccessToken().then(applyAuth.bind(this, operation, args));
return applyAuth.call(this, operation, args);
};
};
exports.processSwagger = (module) => {
const apis = {};
Object.keys(module.client.apis).forEach((api) => {
if(api === 'help')
return;
@ -81,8 +76,6 @@ exports.processSwagger = function(module) {
});
util._extend(module, apis || {});
Object.assign(module, apis || {});
};
module.exports = exports;

View file

@ -32,7 +32,6 @@
},
"dependencies": {
"bunyan": "^1.4.0",
"es6-shim": "^0.32.2",
"swagger-client": "^2.1.2"
}
}