Comparing version 1.1.1 to 2.0.0
@@ -48,8 +48,8 @@ | ||
* @param {number} flags - Search flags | ||
* @param {callback} callback - Callback method to handle the response | ||
* @param {number=} force - Force a search (0 - if such search has been done, | ||
* then return cached result, 1 - to do a new search ) | ||
* @returns {Promise} | ||
* @see {@link http://sdk.wialon.com/wiki/en/kit/remoteapi/apiref/core/search_items} | ||
*/ | ||
Search.prototype.search = function ( type, filter, flags, callback, force ) { | ||
Search.prototype.search = function ( type, filter, flags, force ) { | ||
@@ -66,3 +66,3 @@ var params = { | ||
this._search( params, callback ); | ||
return this._search( params ); | ||
@@ -76,5 +76,5 @@ }; | ||
* @param {string} filter - Search filter | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Search.prototype.units = function ( filter, callback ) { | ||
Search.prototype.units = function ( filter ) { | ||
@@ -89,3 +89,3 @@ var params = { | ||
this._search( params, callback ); | ||
return this._search( params ); | ||
@@ -100,5 +100,5 @@ }; | ||
* @param {string} filter - Search filter | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Search.prototype.zones = function ( filter, callback ) { | ||
Search.prototype.zones = function ( filter ) { | ||
@@ -113,3 +113,3 @@ var params = { | ||
this._search( params, callback ); | ||
return this._search( params ); | ||
@@ -133,5 +133,5 @@ }; | ||
* @param {string} filter - Search filter | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Search.prototype.retranslators = function ( filter, callback ) { | ||
Search.prototype.retranslators = function ( filter ) { | ||
@@ -146,3 +146,3 @@ var params = { | ||
this._search( params, callback ); | ||
return this._search( params ); | ||
@@ -153,10 +153,2 @@ }; | ||
/** | ||
* Internal callback method | ||
* | ||
* @private | ||
*/ | ||
Search.prototype.dummycb = function ( err, data ) {}; | ||
/** | ||
* Internal search helper | ||
@@ -166,21 +158,20 @@ * | ||
* @param {object} params - Search parameters | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Search.prototype._search = function ( params, callback ) { | ||
Search.prototype._search = function ( params ) { | ||
if (! callback ) { | ||
callback = this.dummycb; | ||
} | ||
var _params = this._params; | ||
var session = this.wialon.session; | ||
var _params = this._params; | ||
params = extend( true, _params, params ); | ||
debug( params ); | ||
var session = this.wialon.session; | ||
session.request( 'core/search_items', params, function ( err, data ) { | ||
if ( err ) { | ||
return callback( err ); | ||
} | ||
callback( err, data.items ); | ||
return new Promise( function ( resolve, reject ) { | ||
session.request( 'core/search_items', params ) | ||
.then( function ( data ) { | ||
resolve( data.items ); | ||
} ) | ||
.catch( reject ); | ||
} ); | ||
@@ -187,0 +178,0 @@ |
@@ -7,2 +7,7 @@ | ||
/** | ||
* Zone resources API | ||
* | ||
* @constructor | ||
*/ | ||
var Zones = function () { | ||
@@ -13,6 +18,13 @@ | ||
Zones.prototype.data = function ( resourceId, ids, callback ) { | ||
/** | ||
* Get detailed information for a set of zone resources | ||
* | ||
* @param {string} resourceId - Resource ID | ||
* @param {array|string} ids - Zone IDs | ||
* @returns {Promise} | ||
* | ||
* @see {@link http://sdk.wialon.com/wiki/en/kit/remoteapi/apiref/resource/get_zone_data} | ||
*/ | ||
Zones.prototype.data = function ( resourceId, ids ) { | ||
// FIXME: validate args | ||
if (! Array.isArray( ids ) ) { | ||
@@ -27,3 +39,3 @@ ids = [ids]; | ||
this.wialon.session.request( 'resource/get_zone_data', params, callback ); | ||
return this.wialon.session.request( 'resource/get_zone_data', params ); | ||
@@ -30,0 +42,0 @@ }; |
@@ -22,9 +22,5 @@ | ||
self._session = new Promise( function ( resolve, reject ) { | ||
self.start( self._options.authz, function ( err, data ) { | ||
if ( err ) { | ||
return reject( err ); | ||
} | ||
resolve( data ); | ||
} ); | ||
self.start( self._options.authz ) | ||
.then( resolve ) | ||
.catch( reject ); | ||
} ); | ||
@@ -67,29 +63,28 @@ } | ||
* @param {string} authz.operateAs - User to use this session as | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Session.prototype.start = function ( authz, callback ) { | ||
Session.prototype.start = function ( authz ) { | ||
if (! callback ) { | ||
callback = this.dummycb; | ||
} | ||
var self = this; | ||
// validate authorization parameters | ||
if ( typeof authz !== 'object' || | ||
(! authz.token ) ) { | ||
return callback( new Error( 'Invalid authorization parameters' ) ); | ||
} | ||
return new Promise( function ( resolve, reject ) { | ||
var self = this; | ||
var params = { | ||
token : authz.token, | ||
operateAs : authz.operateAs | ||
}; | ||
self.request( 'token/login', params, function ( err, data ) { | ||
if ( err ) { | ||
return callback( err ); | ||
// validate authorization parameters | ||
if ( typeof authz !== 'object' || | ||
(! authz.token ) ) { | ||
return reject( new Error( 'Invalid authorization parameters' ) ); | ||
} | ||
self._session = data; | ||
callback( err, self._session ); | ||
var params = { | ||
token : authz.token, | ||
operateAs : authz.operateAs | ||
}; | ||
self._request( 'token/login', params ) | ||
.then( function ( data ) { | ||
self._session = data; | ||
resolve( self._session ); | ||
} ) | ||
.catch( reject ); | ||
} ); | ||
@@ -105,24 +100,28 @@ | ||
* @param {object} params - API parameters for command execution | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Session.prototype.request = function ( svc, params, callback ) { | ||
Session.prototype.request = function ( svc, params ) { | ||
if (! callback ) { | ||
callback = this.dummycb; | ||
} | ||
var self = this; | ||
return new Promise( function ( resolve, reject ) { | ||
var self = this; | ||
if ( self._session instanceof Promise ) { | ||
debug( 'waiting for session promise...' ); | ||
if ( self._session instanceof Promise ) { | ||
debug( 'waiting for session promise...' ); | ||
self._session | ||
.then( function () { | ||
self._request( svc, params, callback ); | ||
} ) | ||
.catch( callback ); | ||
self._session | ||
.then( function () { | ||
self._request( svc, params ) | ||
.then( resolve ) | ||
.catch( reject ); | ||
} ) | ||
.catch( reject ); | ||
} else { | ||
debug( 'no waiting' ); | ||
self._request( svc, params ) | ||
.then( resolve ) | ||
.catch( reject ); | ||
} | ||
} else { | ||
self._request( svc, params, callback ); | ||
} | ||
} ); | ||
@@ -138,35 +137,41 @@ }; | ||
* @param {object} params - API parameters for command execution | ||
* @param {callback} callback - Callback method to handle the response | ||
* @returns {Promise} | ||
*/ | ||
Session.prototype._request = function ( svc, params, callback ) { | ||
Session.prototype._request = function ( svc, params ) { | ||
var url = this.endpoint() + '?svc=' + svc; | ||
var self = this; | ||
var url = self.endpoint() + '?svc=' + svc; | ||
var data = { params : JSON.stringify( params ) }; | ||
if ( svc !== 'token/login' ) { | ||
if (! this._session.eid ) { | ||
return callback( new Error( 'Invalid session' ) ); | ||
} | ||
data.sid = this._session.eid; | ||
} | ||
return new Promise( function ( resolve, reject ) { | ||
request.post( { | ||
url : url, | ||
form : data | ||
}, function ( err, res, data ) { | ||
if ( err ) { | ||
return callback( err ); | ||
if ( svc !== 'token/login' ) { | ||
if (! self._session.eid ) { | ||
return reject( new Error( 'Invalid session' ) ); | ||
} | ||
data.sid = self._session.eid; | ||
} | ||
try { | ||
data = JSON.parse( data ); | ||
} catch ( e ) { | ||
return callback( e ); | ||
} | ||
request | ||
.post( { | ||
url : url, | ||
form : data | ||
} ) | ||
.on( 'error', reject ) | ||
.on( 'data', function ( buffer ) { | ||
var data = buffer.toString(); | ||
if ( data.error ) { | ||
return callback( new Error( 'API error: ' + data.error ) ); | ||
} | ||
try { | ||
data = JSON.parse( data ); | ||
} catch ( e ) { | ||
return reject( e ); | ||
} | ||
callback( err, data ); | ||
if ( data.error ) { | ||
return reject( new Error( 'API error: ' + data.error ) ); | ||
} | ||
resolve( data ); | ||
} ); | ||
} ); | ||
@@ -177,11 +182,4 @@ | ||
/** | ||
* Internal callback method | ||
* | ||
* @private | ||
*/ | ||
Session.prototype.dummycb = function ( err, data ) { }; | ||
module.exports = Session; | ||
{ | ||
"name": "wialon", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "NodeJS wrapper for Wialon Remote API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,5 +31,9 @@ node-wialon | ||
var session = wialon().session; | ||
session.start( { token : '<access token>' }, function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
session.start( { token : '<access token>' } ) | ||
.then( function ( data ) { | ||
console.log( data ); | ||
} ) | ||
.catch( function ( err ) { | ||
console.log( err ); | ||
} ); | ||
@@ -51,10 +55,10 @@ | ||
``` js | ||
session.request( 'core/search_items', params, function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
session.request( 'core/search_items', params ) | ||
.then( function ( data ) { | ||
console.log( data ); | ||
} ) | ||
.catch( function ( err ) { | ||
console.log( err ); | ||
} ); | ||
// close the session | ||
session.end( function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
``` | ||
@@ -68,5 +72,9 @@ | ||
// search for units | ||
search.units( '*', function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
search.units( '*' ) | ||
.then( function ( data ) { | ||
console.log( data ); | ||
} ) | ||
.catch( function ( err ) { | ||
console.log( err ) | ||
} ); | ||
``` | ||
@@ -73,0 +81,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
383
88
12551