node-axosoft
Advanced tools
Comparing version 2.0.0 to 2.1.0
106
lib/api.js
var _ = require('lodash'); | ||
var rest = require('restler'); | ||
var required_credentials = ['username','password','client_id','client_secret']; | ||
var required_password_credentials = ['username','password','client_id','client_secret']; | ||
var required_authorization_code_credentials = ['client_id','client_secret','redirect_uri']; | ||
@@ -15,16 +16,23 @@ var _axosoft_api_version = 'v6'; | ||
function _register_credentials (credentials) { | ||
if (credentials.access_token) { | ||
_access_token = credentials.access_token; | ||
} else { | ||
_.each(required_credentials, function (credentialName) { | ||
if (!_.has(credentials, credentialName)) { | ||
console.log(credentials); | ||
throw new Error('When not providing access_token, you must specify "' | ||
+ credentialName + '" within your credentials'); | ||
_.each(required_password_credentials, function (credentialName) { | ||
if (!_.has(credentials, credentialName)) { | ||
console.log(credentials); | ||
throw new Error('When not providing access_token, you must specify "' | ||
+ credentialName + '" within your credentials'); | ||
} | ||
}); | ||
} | ||
_credentials = credentials; | ||
} | ||
function _oauth_flow (credentials) { | ||
_.each(required_authorization_code_credentials, function (credentialName) { | ||
if (!_.has(credentials, credentialName)) { | ||
console.log(credentials); | ||
throw new Error('When not providing access_token, you must specify "' | ||
+ credentialName + '" within your credentials'); | ||
} | ||
}); | ||
_credentials = credentials; | ||
} | ||
function _register_base_url (base_url) { | ||
@@ -36,7 +44,33 @@ _base_url = base_url; | ||
function register (base_url, credentials) { | ||
if (!base_url) { | ||
throw new Error('The parameter "base_url" is required.'); | ||
} | ||
if (!credentials) { | ||
throw new Error('The parameter "credentials" is required.'); | ||
} | ||
_register_base_url(base_url); | ||
_register_credentials(credentials); | ||
if(credentials.access_token){ | ||
_access_token = credentials.access_token; | ||
} else if (credentials.grant_type === 'password') { | ||
_register_credentials(credentials); | ||
} else if (credentials.grant_type === 'authorization_code'){ | ||
_oauth_flow(credentials); | ||
} else { | ||
throw new Error("Must set 'grant_type' to either 'password' or 'authorization_code'."); | ||
} | ||
} | ||
function getLoginUrl (callback) { | ||
var authUrl = _base_url + '/auth' | ||
+ '?response_type=code' | ||
+ `&client_id=${_credentials.client_id}` | ||
+ `&redirect_uri=${encodeURIComponent(_credentials.redirect_uri)}` | ||
+ `&scope=${_credentials.scope ? _credentials.scope : 'read'}` | ||
+ `&state=${_credentials.state ? _credentials.state : ''}` | ||
+ `&expiring=${_credentials.expiring ? _credentials.expiring : ''}` | ||
callback(null, authUrl); | ||
} | ||
/** | ||
@@ -92,2 +126,46 @@ * Wrapper for REST requests. | ||
function exchangeCodeForToken(code, callback) { | ||
_credentials.code = code; | ||
_authenticateCredentails(function (err) { | ||
if (!err) { | ||
callback(null, _access_token); | ||
} else { | ||
callback(err); | ||
} | ||
}) | ||
} | ||
function setAccessToken(access_token, callback) { | ||
_access_token = access_token; | ||
_authenticateCredentails(function (err) { | ||
if (!err) { | ||
callback(null, _access_token); | ||
} else { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
function getAccessToken(callback) { | ||
if (_access_token) { | ||
callback(null, _access_token); | ||
} | ||
else { | ||
_authenticateCredentails(function (err) { | ||
if (!err) { | ||
callback(null, _access_token); | ||
} else { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
} | ||
function deleteAccessToken(callback) { | ||
_access_token = ''; | ||
callback(null); | ||
} | ||
/** | ||
@@ -122,3 +200,2 @@ * Called on completion of a REST reqwuest, calling the callback with either | ||
function _authenticateCredentails (callback) { | ||
_credentials.grant_type = 'password'; | ||
@@ -148,1 +225,6 @@ if (!_access_token) { | ||
exports.handleRestRequest = handleRestRequest; | ||
exports.getLoginUrl = getLoginUrl; | ||
exports.exchangeCodeForToken = exchangeCodeForToken; | ||
exports.getAccessToken = getAccessToken; | ||
exports.setAccessToken = setAccessToken; | ||
exports.deleteAccessToken = deleteAccessToken; |
@@ -5,8 +5,5 @@ var pkg = require('../package.json'); | ||
var api = require('./api'); | ||
api.register(base_url, credentials); | ||
var node_axosoft = { | ||
VERSION: pkg.version, | ||
Api: require('./api'), | ||
Activity: require('./activity.js'), | ||
@@ -40,2 +37,3 @@ Attachments: require('./attachments.js'), | ||
node_axosoft.Api.register(base_url, credentials); | ||
@@ -42,0 +40,0 @@ return node_axosoft; |
{ | ||
"name": "node-axosoft", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A node.js client for accessing the Axosoft API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# node-axosoft | ||
A node.js client for the Axosoft API | ||
This library is built to support v6 of the Axosoft REST API and has been tested with both axosoft.com hosted accounts and Axosoft installed on premise. Any questions can be sent to api@axosoft.com. | ||
[Here's](http://developer.axosoft.com/) a link to our full documentation for the Axosoft REST API. | ||
## Installation | ||
Install with the node package manager [npm](http://npmjs.org): | ||
$ npm install --save node-axosoft | ||
## Examples | ||
### Create the Axosoft Connection | ||
var nodeAxosoft = require('node-axosoft'); | ||
var credentials = {}; | ||
var axosoftUrl = 'https://example.axosoft.com; | ||
//Populate Credentials (See Below) | ||
var axo = nodeAxosoft(axosoftUrl, credentials); | ||
### Populate Credentials | ||
####Via User Name and Password | ||
credentials.client_id = 'your client id'; | ||
credentials.client_secret = 'your client secret'; | ||
credentials.grant_type = 'password'; | ||
credentials.username = 'your username'; | ||
credentials.password = 'secret'; | ||
#### Via Authorization Code (required for public apps or if using Windows authentication) | ||
credentials.client_id = 'your client id'; | ||
credentials.client_secret = 'your client secret'; | ||
credentials.grant_type = 'authorization_code'; | ||
credentials.redirect_uri = 'https://exampleredirect.com'; | ||
var axo = nodeAxosoft(axosoftUrl, credentials); | ||
axo.Api.getLoginUrl(function(url) { | ||
// open browser using authorizationUrl and get code parameter from redirected Url after login | ||
var code = 'code received from redirect'; | ||
axo.Api.exchangeCodeForToken(code); | ||
}); | ||
#### Via Non-Expiring Token | ||
//Create Non Expiring Token by logging into Axosoft account, Clicking on Tools/System Options/Axosoft API Settings/Manage Tokens, and make non-expiring token. | ||
credentials.access_token = 'your non-expiring token'; | ||
var axo = nodeAxosoft(axosoftUrl, credentials); | ||
### Get Work Item (feature) | ||
//optional parameters | ||
var params = {columns: 'name'}; | ||
axo.Features.get(params, function(error, response){ | ||
console.log(response.data); | ||
}); | ||
### Add Work Item (feature) | ||
params = {}; | ||
item = {}; | ||
item.project = {id: 1}; //required | ||
item.name = 'Test Item Name'; | ||
params.item = item; | ||
//create new feature | ||
axo.Features.add(params, function(error, response){ | ||
console.log(response); | ||
}); | ||
### API Promises | ||
node-axosoft also offers promise versions of its API functions. To use them, just require `node-axosoft/promise` instead of `node-axosoft`. | ||
var nodeAxosoft = require('node-axosoft/promise'); | ||
var axo = nodeAxosoft(axosoftUrl, credentials); | ||
// get items | ||
axo.Features.get() | ||
.then(function(response) { | ||
console.log(response); | ||
}); | ||
## RunKit | ||
Test features on our [RunKit](https://runkit.com/brettgaxosoft/axosoft). Just Clone the notebook and update the Axosoft Url and Access Token to test calls with your account. |
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
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
95572
35
1729
96
0