From 730252e5297757b90feb4a97f1082c29ad6a9ff2 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Fri, 27 May 2016 13:16:02 -0400 Subject: [PATCH] es6 updates for helpers --- gulpfile.js | 30 +------------ lib/helpers.js | 113 +++++++++++++++++++++++-------------------------- package.json | 1 - 3 files changed, 54 insertions(+), 90 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 65bbc08..d9c5b39 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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']); diff --git a/lib/helpers.js b/lib/helpers.js index 7375a57..eccdb7b 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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; diff --git a/package.json b/package.json index a893f97..c079775 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ }, "dependencies": { "bunyan": "^1.4.0", - "es6-shim": "^0.32.2", "swagger-client": "^2.1.2" } }