sa-kws-node-sdk
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -33,2 +33,26 @@ 'use strict'; | ||
* - language: 2 letter ISO code ("en" by default) * | ||
* - hooks: Object where key is hook name and value is function which will be executed | ||
hooks.beforeRequest({ | ||
method: method, | ||
path: path, | ||
}) | ||
hooks.afterRequest({ | ||
method: method, | ||
path: path, | ||
requestTime: time milis | ||
}, response) | ||
hooks.beforeEachRequest({ | ||
method: method, | ||
path: path, | ||
}) | ||
hooks.afterEachRequest({ | ||
method: method, | ||
path: path, | ||
requestTime: time milis | ||
}, response) | ||
* | ||
*/ | ||
@@ -72,2 +96,3 @@ | ||
this.opts.language = this.opts.language || 'en'; | ||
this.opts.hooks = this.opts.hooks || {}; | ||
this.currentApiVersion = 2; | ||
@@ -129,3 +154,3 @@ | ||
var lastLevelAlias = self; | ||
for (var j = 0; j < optionsPart.length - 1; j++) { | ||
@@ -176,3 +201,3 @@ lastLevelAlias[optionsPart[j]] = lastLevelAlias[optionsPart[j]] || {}; | ||
return rp.post(tokenPath, opts) | ||
return this.requestApi('post', tokenPath, opts) | ||
.then(function (resp) { | ||
@@ -186,2 +211,39 @@ /*jshint camelcase: false */ | ||
var beforeEachRequestHook = function(context, method, path) { | ||
if (context.opts.hooks.beforeEachRequest && typeof context.opts.hooks.beforeEachRequest === 'function') { | ||
context.opts.hooks.beforeEachRequest({ | ||
method: method, | ||
path: path, | ||
}); | ||
} | ||
}; | ||
var afterEachRequest = function(context, resp, method, path, startTime) { | ||
if (context.opts.hooks.afterRequest && typeof context.opts.hooks.afterRequest === 'function') { | ||
var endTime = Date.now(); | ||
context.opts.hooks.afterEachRequest({ | ||
method: method, | ||
path: path, | ||
requestTime: endTime - startTime | ||
}, resp); | ||
} | ||
return resp; | ||
}; | ||
KwsSdk.prototype.requestApi = function(method, path, opts) { | ||
var self = this; | ||
var startTime = Date.now(); | ||
beforeEachRequestHook(this, method, path); | ||
return rp[method](path, opts) | ||
.then(function (resp) { | ||
afterEachRequest(self, resp, method, path, startTime); | ||
return resp; | ||
}) | ||
.catch(function(err){ | ||
afterEachRequest(self, err, method, path, startTime); | ||
throw err; | ||
}); | ||
}; | ||
KwsSdk.prototype.getClientCredentialsToken = function () { | ||
@@ -200,3 +262,16 @@ var self = this; | ||
opts = opts || {}; | ||
var startTime = Date.now(); | ||
var afterRequestHook = function(resp) { | ||
if (self.opts.hooks.afterRequest && typeof self.opts.hooks.afterRequest === 'function') { | ||
var endTime = Date.now(); | ||
self.opts.hooks.afterRequest({ | ||
method: method, | ||
path: path, | ||
requestTime: endTime - startTime | ||
}, resp); | ||
} | ||
return resp; | ||
}; | ||
var returnOpts = { | ||
@@ -213,4 +288,21 @@ json: opts.json, | ||
return rp[method](path, returnOpts) | ||
.catch(function () { | ||
if (this.opts.hooks.beforeRequest && typeof this.opts.hooks.beforeRequest === 'function') { | ||
this.opts.hooks.beforeRequest({ | ||
method: method, | ||
path: path, | ||
}); | ||
} | ||
var requestSucceded = false; | ||
return this.requestApi(method, path, returnOpts) | ||
.then(function(resp) { | ||
requestSucceded = true; | ||
afterRequestHook(resp); | ||
return resp; | ||
}) | ||
.catch(function (err) { | ||
//Throw error if it raised because of the hook actions | ||
if (requestSucceded) { | ||
throw err; | ||
} | ||
// If call fails, try getting the token again | ||
@@ -223,3 +315,7 @@ delete self.bearerToken; | ||
opts.headers['Accept-Language'] = self.opts.language; | ||
return rp[method](path, opts); | ||
return self.requestApi(method, path, opts); | ||
}) | ||
.finally(function(resp) { | ||
afterRequestHook(resp); | ||
return resp; | ||
}); | ||
@@ -226,0 +322,0 @@ }); |
{ | ||
"name": "sa-kws-node-sdk", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "KWS Node App SDK", | ||
@@ -5,0 +5,0 @@ "main": "./lib/kwsSdk.js", |
@@ -5,17 +5,16 @@ /* globals describe, it, beforeEach, afterEach */ | ||
var KwsSdk = require('../lib/kwsSdk'); | ||
var BPromise = require('bluebird'); | ||
var jwt = require('jsonwebtoken'); | ||
var _ = require('lodash'); | ||
var should = require('should'); | ||
var sinon = require('sinon'); | ||
var rp = require('request-promise'); | ||
var crypto = require('crypto'); | ||
const KwsSdk = require('../lib/kwsSdk'); | ||
const BPromise = require('bluebird'); | ||
const jwt = require('jsonwebtoken'); | ||
const _ = require('lodash'); | ||
const should = require('should'); | ||
const sinon = require('sinon'); | ||
const rp = require('request-promise'); | ||
const crypto = require('crypto'); | ||
describe('KwsSdk', function () { | ||
var sandbox; | ||
var stubs = {}; | ||
var stubData; | ||
var kwsSdkOpts = { | ||
let sandbox; | ||
let stubs = {}; | ||
let stubData; | ||
let kwsSdkOpts = { | ||
saAppId: 'test_app', | ||
@@ -25,8 +24,8 @@ saAppApiKey: 'test_key', | ||
}; | ||
var kwsSdk = new KwsSdk(kwsSdkOpts); | ||
let kwsSdk = new KwsSdk(kwsSdkOpts); | ||
function getFunctionFromName(api, functionName) { | ||
var parts = functionName.split('.'); | ||
var expectedFunction = api; | ||
for (var i = 0; i < parts.length; i++) { | ||
let parts = functionName.split('.'); | ||
let expectedFunction = api; | ||
for (let i = 0; i < parts.length; i++) { | ||
expectedFunction = expectedFunction[parts[i]]; | ||
@@ -156,5 +155,5 @@ } | ||
it('should make a ' + item.expectedMethod + ' request to ' + item.expectedPath + | ||
' when calling ' + item.functionName, function (done) { | ||
' when calling ' + item.functionName + ' and execute hooks', function (done) { | ||
var expectedFunction = getFunctionFromName(kwsSdk, item.functionName); | ||
let expectedFunction = getFunctionFromName(kwsSdk, item.functionName); | ||
@@ -177,2 +176,128 @@ expectedFunction(item.params) | ||
describe('extending with hooks', function () { | ||
let appId = 1234; | ||
let hooksOpts = _.clone(kwsSdkOpts); | ||
hooksOpts.hooks = { | ||
beforeRequest: function() {}, //yes it's needed | ||
afterRequest: function() {}, //yes it's needed | ||
afterEachRequest: function() {}, //yes it's needed | ||
beforeEachRequest: function() {} //yes it's needed | ||
}; | ||
let kwsApiWithHooks = new KwsSdk(hooksOpts); | ||
let spies = {}; | ||
spies.beforeRequest = sinon.spy(hooksOpts.hooks, 'beforeRequest'); | ||
spies.afterRequest = sinon.spy(hooksOpts.hooks, 'afterRequest'); | ||
spies.afterEachRequest = sinon.spy(hooksOpts.hooks, 'afterEachRequest'); | ||
spies.beforeEachRequest = sinon.spy(hooksOpts.hooks, 'beforeEachRequest'); | ||
beforeEach(function (done) { | ||
stubData.token = jwt.sign({ | ||
appId: appId, | ||
clientId: hooksOpts.saAppId | ||
}, 'whatever', {}); | ||
stubData.resp = {example: 'whatever'}; | ||
done(); | ||
}); | ||
afterEach(function(done){ | ||
spies.beforeRequest.reset(); | ||
spies.afterRequest.reset(); | ||
spies.afterEachRequest.reset(); | ||
spies.beforeEachRequest.reset(); | ||
done(); | ||
}); | ||
_.each([ | ||
{ | ||
functionName: 'v1.app.user.getMap', | ||
expectedPath: '/v1/apps/' + appId + '/users/map', | ||
expectedMethod: 'get', | ||
params: {}, | ||
expectedJson: true, | ||
expectedQs: {}, | ||
expectedBeforeHooksCalls: 2, | ||
expectedAfterHooksCalls: 2 | ||
}, | ||
{ | ||
functionName: 'v1.app.getStatistics', | ||
expectedPath: '/v1/apps/' + appId + '/statistics', | ||
expectedMethod: 'get', | ||
params: {}, | ||
expectedJson: true, | ||
expectedQs: {}, | ||
expectedBeforeHooksCalls: 1, | ||
expectedAfterHooksCalls: 1 | ||
}, | ||
{ | ||
functionName: 'v1.app.notify', | ||
expectedPath: '/v1/apps/' + appId + '/notify', | ||
expectedMethod: 'post', | ||
params: {attribute: 'whatever'}, | ||
expectedJson: {attribute: 'whatever'}, | ||
expectedQs: undefined, | ||
expectedBeforeHooksCalls: 1, | ||
expectedAfterHooksCalls: 1 | ||
} | ||
], function (item) { | ||
it('should make a ' + item.expectedMethod + ' request to ' + item.expectedPath + | ||
' when calling ' + item.functionName + ' and execute hooks', function (done) { | ||
let expectedFunction = getFunctionFromName(kwsApiWithHooks, item.functionName); | ||
expectedFunction(item.params) | ||
.then(function (resp) { | ||
should(resp).eql(stubData.resp); | ||
should(stubs[item.expectedMethod].callCount).eql(1); | ||
should(stubs[item.expectedMethod].lastCall.args[0]) | ||
.eql(hooksOpts.kwsApiHost + item.expectedPath); | ||
should(stubs[item.expectedMethod].lastCall.args[1].json) | ||
.eql(item.expectedJson); | ||
should(stubs[item.expectedMethod].lastCall.args[1].qs) | ||
.eql(item.expectedQs); | ||
should(spies.beforeRequest.callCount).eql(1); | ||
should(spies.beforeRequest.lastCall.args[0]).eql({ | ||
method: item.expectedMethod, | ||
path: hooksOpts.kwsApiHost + item.expectedPath | ||
}); | ||
should(spies.afterRequest.callCount).eql(1); | ||
should(typeof spies.afterRequest.lastCall.args[0]).eql('object'); | ||
should(typeof spies.afterRequest.lastCall.args[0].requestTime).eql('number'); | ||
delete spies.afterRequest.lastCall.args[0].requestTime; | ||
should(spies.afterRequest.lastCall.args[0]).eql({ | ||
method: item.expectedMethod, | ||
path: hooksOpts.kwsApiHost + item.expectedPath | ||
}); | ||
should(spies.afterRequest.lastCall.args[1]).eql(stubData.resp); | ||
should(spies.beforeEachRequest.callCount).eql(item.expectedBeforeHooksCalls); | ||
if (item.expectedBeforeHooksCalls > 1) { | ||
should(spies.beforeEachRequest.firstCall.args[0]).eql({ | ||
method: 'post', | ||
path: hooksOpts.kwsApiHost + '/oauth/token' | ||
}); | ||
} | ||
should(spies.beforeEachRequest.lastCall.args[0]).eql({ | ||
method: item.expectedMethod, | ||
path: hooksOpts.kwsApiHost + item.expectedPath | ||
}); | ||
should(spies.afterEachRequest.callCount).eql(item.expectedAfterHooksCalls); | ||
should(typeof spies.afterEachRequest.lastCall.args[0]).eql('object'); | ||
should(typeof spies.afterEachRequest.lastCall.args[0].requestTime).eql('number'); | ||
delete spies.afterEachRequest.lastCall.args[0].requestTime; | ||
should(spies.afterEachRequest.lastCall.args[0]).eql({ | ||
method: item.expectedMethod, | ||
path: hooksOpts.kwsApiHost + item.expectedPath | ||
}); | ||
should(spies.afterEachRequest.lastCall.args[1]).eql(stubData.resp); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('overriding endpoints', function () { | ||
@@ -179,0 +304,0 @@ var appId = 1234; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
61435
1608
9
1