sa-kws-node-sdk
Advanced tools
Comparing version 0.2.4 to 1.0.0
@@ -8,2 +8,3 @@ 'use strict'; | ||
var crypto = require('crypto'); | ||
var _ = require('lodash'); | ||
@@ -39,7 +40,26 @@ function KwsSdk(opts) { | ||
var endpoints = [ | ||
'app.user.getMap', | ||
'app.getStatistics', | ||
'app.notify' | ||
]; | ||
var endpoints = { | ||
'v1': { | ||
'app.user.getMap': { | ||
'alias': null | ||
}, | ||
'app.getStatistics': { | ||
'alias': null | ||
}, | ||
'app.notify': { | ||
'alias': null | ||
} | ||
}, | ||
'v2': { | ||
'app.user.getAppPoints': { | ||
alias: null | ||
}, | ||
'app.user.getScore': { | ||
alias: null | ||
}, | ||
'user.getPoints': { | ||
alias: 'app.user.getGlobalPoints' | ||
} | ||
} | ||
}; | ||
@@ -53,2 +73,3 @@ var singularList = []; | ||
this.opts.language = this.opts.language || 'en'; | ||
this.currentApiVersion = 2; | ||
@@ -59,3 +80,3 @@ this.bearerToken = null; | ||
var endpointList = endpoints.concat(this.opts.endpoints || []); | ||
var endpointList = _.merge(endpoints, this.opts.endpoints || {}); | ||
singularList = singularList.concat(this.opts.singularList || []); | ||
@@ -67,6 +88,15 @@ this.apiFunctionFactory(endpointList, singularList); | ||
//********************************************************************************* | ||
KwsSdk.prototype.createApiFunction = function (endpoint, singularList) { | ||
KwsSdk.prototype.createApiFunction = function (version, endpoint, options, singularList) { | ||
var self = this; | ||
//for old version we prepend the version number to the sdk function name | ||
if (version !== 'v' + self.currentApiVersion) { | ||
endpoint = version + '.' + endpoint; | ||
if (options.alias !== null) { | ||
options.alias = version + '.' + options.alias; | ||
} | ||
} | ||
var parts = endpoint.split('.'); | ||
var optionsPart = options.alias ? options.alias.split('.') : null; | ||
var action = parts[parts.length - 1]; | ||
@@ -82,3 +112,3 @@ var method = utils.getMethodFromAction(action); | ||
// This is the final function that is created | ||
lastLevel[action] = function (params) { | ||
var endpointCall = function (params) { | ||
params = params || {}; | ||
@@ -88,3 +118,3 @@ | ||
.then(function (token) { | ||
var url = self.opts.kwsApiHost + utils.createPathFromEndpoint('v1', endpoint, singularList, params, token); | ||
var url = self.opts.kwsApiHost + utils.createPathFromEndpoint(version, endpoint, singularList, params, token); | ||
@@ -99,2 +129,16 @@ var opts = { | ||
}; | ||
lastLevel[action] = endpointCall; | ||
if (optionsPart !== null) { | ||
var lastLevelAlias = self; | ||
for (var j = 0; j < optionsPart.length - 1; j++) { | ||
lastLevelAlias[optionsPart[j]] = lastLevelAlias[optionsPart[j]] || {}; | ||
lastLevelAlias = lastLevelAlias[optionsPart[j]]; | ||
} | ||
var aliasAction = optionsPart[optionsPart.length -1]; | ||
lastLevelAlias[aliasAction] = endpointCall; | ||
} | ||
}; | ||
@@ -105,5 +149,8 @@ | ||
endpointList.forEach(function (endpoint) { | ||
self.createApiFunction(endpoint, singularList); | ||
}); | ||
for(var version in endpointList) { | ||
for(var endpoint in endpointList[version]) { | ||
var options = endpointList[version][endpoint]; | ||
self.createApiFunction(version, endpoint, options, singularList); | ||
} | ||
} | ||
}; | ||
@@ -110,0 +157,0 @@ //********************************************************************************* |
{ | ||
"name": "sa-kws-node-sdk", | ||
"version": "0.2.4", | ||
"version": "1.0.0", | ||
"description": "KWS Node App SDK", | ||
@@ -13,7 +13,7 @@ "main": "./lib/kwsSdk.js", | ||
"inflected": "^1.1.7", | ||
"request-promise": "~0.4.0" | ||
"lodash": "^4.17.2", | ||
"request-promise": "^0.4.3" | ||
}, | ||
"devDependencies": { | ||
"jsonwebtoken": "~5.0.5", | ||
"lodash": "~3.10.1", | ||
"should": "~7.1.0", | ||
@@ -20,0 +20,0 @@ "sinon": "~1.16.1" |
@@ -1,2 +0,2 @@ | ||
/* globals describe, it, before, after */ | ||
/* globals describe, it, beforeEach, afterEach */ | ||
@@ -33,3 +33,3 @@ 'use strict'; | ||
return expectedFunction; | ||
return expectedFunction; | ||
} | ||
@@ -59,5 +59,5 @@ | ||
stubs.post = sandbox.stub(rp, 'post', function (url, opts) { | ||
stubs.post = sandbox.stub(rp, 'post', function (url) { | ||
if (url === kwsSdkOpts.kwsApiHost + '/oauth/token') { | ||
return BPromise.resolve({access_token: stubData.token}); | ||
return BPromise.resolve({access_token: stubData.token}); // jshint ignore:line | ||
} else { | ||
@@ -68,11 +68,11 @@ return BPromise.resolve(stubData.resp); | ||
stubs.get = sandbox.stub(rp, 'get', function (url, opts) { | ||
stubs.get = sandbox.stub(rp, 'get', function () { | ||
return BPromise.resolve(stubData.resp); | ||
}); | ||
stubs.put = sandbox.stub(rp, 'put', function (url, opts) { | ||
stubs.put = sandbox.stub(rp, 'put', function () { | ||
return BPromise.resolve(stubData.resp); | ||
}); | ||
stubs.del = sandbox.stub(rp, 'del', function (url, opts) { | ||
stubs.del = sandbox.stub(rp, 'del', function () { | ||
return BPromise.resolve(stubData.resp); | ||
@@ -92,5 +92,8 @@ }); | ||
_.each([ | ||
'app.user.getMap', | ||
'app.getStatistics', | ||
'app.notify' | ||
'v1.app.user.getMap', | ||
'v1.app.getStatistics', | ||
'v1.app.notify', | ||
'app.user.getScore', | ||
'app.user.getAppPoints', | ||
'app.user.getGlobalPoints' | ||
], function (functionName) { | ||
@@ -106,5 +109,5 @@ it('should create ' + functionName, function (done) { | ||
describe('get token', function () { | ||
it('should request a token in the first call', function (done) { | ||
kwsSdk.app.user.getMap() | ||
kwsSdk.v1.app.user.getMap() | ||
.finally(function () { | ||
@@ -131,3 +134,3 @@ should(stubs.post.callCount).eql(1); | ||
{ | ||
functionName: 'app.user.getMap', | ||
functionName: 'v1.app.user.getMap', | ||
expectedPath: '/v1/apps/' + appId + '/users/map', | ||
@@ -140,3 +143,3 @@ expectedMethod: 'get', | ||
{ | ||
functionName: 'app.getStatistics', | ||
functionName: 'v1.app.getStatistics', | ||
expectedPath: '/v1/apps/' + appId + '/statistics', | ||
@@ -149,3 +152,3 @@ expectedMethod: 'get', | ||
{ | ||
functionName: 'app.notify', | ||
functionName: 'v1.app.notify', | ||
expectedPath: '/v1/apps/' + appId + '/notify', | ||
@@ -155,6 +158,6 @@ expectedMethod: 'post', | ||
expectedJson: {attribute: 'whatever'}, | ||
expectedQs: undefined | ||
expectedQs: undefined | ||
} | ||
], function (item) { | ||
it('should make a ' + item.expectedMethod + ' request to ' + item.expectedPath + | ||
it('should make a ' + item.expectedMethod + ' request to ' + item.expectedPath + | ||
' when calling ' + item.functionName, function (done) { | ||
@@ -183,15 +186,27 @@ | ||
var userId = 222; | ||
var customEndpoints = [ | ||
'user.get', | ||
'user.update', | ||
'user.app.list', | ||
'user.app.create', | ||
'app.user.list' | ||
]; | ||
var customEndpoints = { | ||
'v1': { | ||
'user.get': { | ||
'alias': null | ||
}, | ||
'user.update': { | ||
'alias': null | ||
}, | ||
'user.app.list': { | ||
'alias': null | ||
}, | ||
'user.app.create': { | ||
'alias': null | ||
}, | ||
'app.user.list': { | ||
'alias': null | ||
} | ||
} | ||
}; | ||
var customOpts = _.extend(kwsSdkOpts, { | ||
endpoints: customEndpoints | ||
}); | ||
var customKwsApi = new KwsSdk(customOpts); | ||
var customKwsApi = new KwsSdk(kwsSdkOpts); | ||
beforeEach(function (done) { | ||
@@ -207,3 +222,3 @@ stubData.token = jwt.sign({ | ||
{ | ||
functionName: 'user.get', | ||
functionName: 'v1.user.get', | ||
expectedPath: '/v1/users/' + userId, | ||
@@ -216,3 +231,3 @@ expectedMethod: 'get', | ||
{ | ||
functionName: 'user.update', | ||
functionName: 'v1.user.update', | ||
expectedPath: '/v1/users/' + userId, | ||
@@ -222,6 +237,6 @@ expectedMethod: 'put', | ||
expectedJson: {firstName: 'whatever'}, | ||
expectedQs: undefined | ||
expectedQs: undefined | ||
}, | ||
{ | ||
functionName: 'user.app.list', | ||
functionName: 'v1.user.app.list', | ||
expectedPath: '/v1/users/' + userId + '/apps', | ||
@@ -231,6 +246,6 @@ expectedMethod: 'get', | ||
expectedJson: true, | ||
expectedQs: {} | ||
expectedQs: {} | ||
}, | ||
{ | ||
functionName: 'user.app.create', | ||
functionName: 'v1.user.app.create', | ||
expectedPath: '/v1/users/' + userId + '/apps', | ||
@@ -240,6 +255,6 @@ expectedMethod: 'post', | ||
expectedJson: {appId: appId}, | ||
expectedQs: undefined | ||
expectedQs: undefined | ||
}, | ||
{ | ||
functionName: 'app.user.list', | ||
functionName: 'v1.app.user.list', | ||
expectedPath: '/v1/apps/' + appId + '/users', | ||
@@ -252,3 +267,3 @@ expectedMethod: 'get', | ||
], function (item) { | ||
it('should make a ' + item.expectedMethod + ' request to ' + item.expectedPath + | ||
it('should make a ' + item.expectedMethod + ' request to ' + item.expectedPath + | ||
' when calling ' + item.functionName, function (done) { | ||
@@ -287,9 +302,12 @@ | ||
externalUserIds: true, | ||
endpoints: [ | ||
'user.app.create' | ||
] | ||
endpoints: { | ||
'v1': { | ||
'user.app.create': { | ||
'alias': null | ||
} | ||
} | ||
} | ||
}); | ||
var expectedFunction = getFunctionFromName(kwsSdk, 'v1.user.app.create'); | ||
var expectedFunction = getFunctionFromName(kwsSdk, 'user.app.create'); | ||
expectedFunction({userId: userId, appId: appId}) | ||
@@ -350,5 +368,5 @@ .then(function (resp) { | ||
done(); | ||
}) | ||
}); | ||
}); | ||
}); | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
52705
3
1408
1
8
+ Addedlodash@^4.17.2
+ Addedlodash@4.17.21(transitive)
Updatedrequest-promise@^0.4.3