Comparing version 0.0.8 to 1.0.0
12
index.js
@@ -9,3 +9,13 @@ | ||
module.exports = require( './lib/wialon' ); | ||
var Wialon = require( './lib/wialon' ); | ||
/** | ||
* Initialise a new API instance | ||
* | ||
* @param {object} opts - Options | ||
*/ | ||
module.exports = function ( opts ) { | ||
return new Wialon( opts ); | ||
}; | ||
@@ -31,5 +31,6 @@ | ||
module.exports = { | ||
session : inject( './core/session' ), | ||
search : inject( './core/search' ) | ||
geofences : inject( './resources/zones' ), | ||
search : inject( './core/search' ), | ||
zones : inject( './resources/zones' ) | ||
}; | ||
'use strict'; | ||
var debug = require( 'debug' )( 'core/search' ); | ||
var debug = require( 'debug' )( 'wialon:core:search' ); | ||
var extend = require( 'extend' ); | ||
@@ -92,3 +92,3 @@ | ||
/** | ||
* Search for geofences using 'avl_resource' item type. Flags are set | ||
* Search for zones using 'avl_resource' item type. Flags are set | ||
* to 0x00001001. | ||
@@ -99,3 +99,3 @@ * | ||
*/ | ||
Search.prototype.geofences = function ( filter, callback ) { | ||
Search.prototype.zones = function ( filter, callback ) { | ||
@@ -116,2 +116,11 @@ var params = { | ||
/** | ||
* Alias for {@link Search#zones} | ||
* | ||
* @method | ||
* @see Search#zones | ||
*/ | ||
Search.prototype.geofences = Search.prototype.zones; | ||
/** | ||
* Search for retranslators using 'avl_retranslator' item type. Flags are | ||
@@ -163,3 +172,3 @@ * set to 0x00000301. | ||
var session = this.wialon.session(); | ||
var session = this.wialon.session; | ||
session.request( 'core/search_items', params, function ( err, data ) { | ||
@@ -166,0 +175,0 @@ if ( err ) { |
'use strict'; | ||
var debug = require( 'debug' )( 'core/session' ); | ||
var debug = require( 'debug' )( 'wialon:core:session' ); | ||
var request = require( 'request' ); | ||
@@ -15,3 +15,19 @@ | ||
var Session = function ( opts ) { | ||
this._session = {}; | ||
this.options( opts ); | ||
var self = this; | ||
if ( self._options.credentials ) { | ||
debug( 'creadentials found, initialising session...' ); | ||
self._session = new Promise( function ( resolve, reject ) { | ||
self.start( self._options.credentials , function ( err, data ) { | ||
if ( err ) { | ||
return reject( err ); | ||
} | ||
resolve( data ); | ||
} ); | ||
} ); | ||
} | ||
}; | ||
@@ -38,3 +54,2 @@ | ||
var url = this._options.url || | ||
this.wialon._options.url || | ||
'https://hst-api.wialon.com/wialon/ajax.html'; | ||
@@ -78,4 +93,4 @@ | ||
self.wialon._session = data; | ||
callback( err, self.wialon._session ); | ||
self._session = data; | ||
callback( err, self._session ); | ||
} ); | ||
@@ -109,7 +124,30 @@ | ||
if ( svc !== 'core/login' && (! this.wialon._session.eid ) ) { | ||
return callback( new Error( 'Invalid session' ) ); | ||
var self = this; | ||
if ( self._session instanceof Promise ) { | ||
debug( 'waiting for session promise...' ); | ||
self._session | ||
.then( function () { | ||
self._request( svc, params, callback ); | ||
} ) | ||
.catch( callback ); | ||
} else { | ||
self._request( svc, params, callback ); | ||
} | ||
}; | ||
/** | ||
* Internal helper method to send API requests | ||
* | ||
* @private | ||
* @param {svc} string - API command (e.g. 'core/search_items') | ||
* @param {object} params - API parameters for command execution | ||
* @param {callback} callback - Callback method to handle the response | ||
*/ | ||
Session.prototype._request = function ( svc, params, callback ) { | ||
var url = this.endpoint() + '?svc=' + svc; | ||
@@ -119,3 +157,6 @@ var data = { params : JSON.stringify( params ) }; | ||
if ( svc !== 'core/login' ) { | ||
data.sid = this.wialon._session.eid; | ||
if (! this._session.eid ) { | ||
return callback( new Error( 'Invalid session' ) ); | ||
} | ||
data.sid = this._session.eid; | ||
} | ||
@@ -122,0 +163,0 @@ |
'use strict'; | ||
var debug = require( 'debug' )( 'wialon' ); | ||
var apis = require( './apis' ); | ||
var debug = require( 'debug' )( 'wialon' ); | ||
var apis = require( './apis' ); | ||
var Session = require( './core/session' ); | ||
@@ -14,6 +15,8 @@ /** | ||
var W = function ( opts ) { | ||
this._session = {}; | ||
this.options( opts ); | ||
this.bindAPIs( apis ); | ||
this.session = new Session( opts ); | ||
}; | ||
@@ -44,10 +47,3 @@ | ||
var wialon = new W(); | ||
module.exports = W; | ||
/** | ||
* Exports a Wailon API wrapper object | ||
* | ||
* @type {W} | ||
*/ | ||
module.exports = wialon; | ||
{ | ||
"name": "wialon", | ||
"version": "0.0.8", | ||
"version": "1.0.0", | ||
"description": "NodeJS wrapper for Wialon Remote API", | ||
"main": "index.js", | ||
"engines": { | ||
"iojs": ">=2.0", | ||
"node": ">=0.12" | ||
}, | ||
"scripts": { | ||
@@ -25,14 +29,14 @@ "coverage": "istanbul cover _mocha -- --reporter spec", | ||
"dependencies": { | ||
"debug": "^2.1.3", | ||
"extend": "^2.0.0", | ||
"request": "^2.54.0" | ||
"debug": "^2.2.0", | ||
"extend": "^2.0.1", | ||
"request": "^2.58.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^2.2.0", | ||
"chai": "^3.0.0", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.13", | ||
"jshint": "^2.6.3", | ||
"mocha": "^2.2.4", | ||
"nock": "^1.6.0" | ||
"istanbul": "^0.3.15", | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.2.5", | ||
"nock": "^2.6.0" | ||
} | ||
} |
@@ -22,23 +22,32 @@ node-wialon | ||
Send API requests using session object: | ||
Initialising a session: | ||
``` js | ||
var session = require( 'wialon' ).session(); | ||
var wialon = require( 'wialon' ); | ||
// login credentials | ||
var credentials = { | ||
username : 'wialon_test', | ||
password : 'test' | ||
// using session.start() | ||
var session = wialon().session; | ||
session.start( { username : 'user', password : 'pass' }, function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
// auto-initialise a session by passing in credentials to the factory method | ||
var opts = { | ||
// login credentials | ||
credentials : { | ||
username : 'wialon_test', | ||
password : 'test' | ||
} | ||
}; | ||
// start a session | ||
session.start( credentials, function ( err, session ) { | ||
console.log( session ); | ||
} ); | ||
var session = wialon( opts ).session; | ||
``` | ||
// send a request | ||
Send API requests using session object: | ||
``` js | ||
session.request( 'core/search_items', params, function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
// close session | ||
// close the session | ||
session.end( function ( err, data ) { | ||
@@ -49,3 +58,13 @@ console.log( data ); | ||
Using search helpers: | ||
``` js | ||
// grab a new search instance | ||
var search = wialon( opts ).search(); | ||
// search for units | ||
search.units( '*', function ( err, data ) { | ||
console.log( data ); | ||
} ); | ||
``` | ||
## API Documentation | ||
@@ -52,0 +71,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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12715
9
389
1
77
Updateddebug@^2.2.0
Updatedextend@^2.0.1
Updatedrequest@^2.58.0