New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tol-api

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tol-api - npm Package Compare versions

Comparing version

to
0.7.0

3

index.js

@@ -21,3 +21,4 @@ var api = require('./lib/api');

getClientCredentialsToken: api.getClientCredentialsToken,
getResourceOwnerPasswordCredentialsToken: api.getResourceOwnerPasswordCredentialsToken
getResourceOwnerPasswordCredentialsToken: api.getResourceOwnerPasswordCredentialsToken,
handleRefreshToken: api.handleRefreshToken
};

@@ -10,2 +10,3 @@ var request = require('request');

this.tokenPromise = null;
this.refreshToken = null;
this.settings = {};

@@ -33,8 +34,22 @@ this.defaultConfiguration();

api.getToken = function() {
this.tokenPromise = this.tokenPromise || this.settings.tokenFetcher();
api.getTokens = function() {
this.tokenPromise = this.tokenPromise || this.settings.tokenFetcher(this.refreshToken).then(function(tokens){
if (tokens.refresh_token) {
this.refreshToken = tokens.refresh_token;
}
return tokens;
});
return this.tokenPromise;
};
api.setTokens = function(tokens) {
if (tokens.refresh_token) {
this.refreshToken = tokens.refresh_token;
}
this.tokenPromise = q.fulfill(tokens);
};
api.get = function(resource, id, parameters) {

@@ -47,4 +62,4 @@ var deferred = q.defer();

return this.getToken().then(_.bind(function(token) {
var req = {url: this.urlFor(resource, id, parameters), headers: {'Authorization': 'Bearer ' + token}, json: true};
return this.getTokens().then(_.bind(function(tokens) {
var req = {url: this.urlFor(resource, id, parameters), headers: {'Authorization': 'Bearer ' + tokens.access_token}, json: true};

@@ -62,5 +77,5 @@ request(req, resolveResponse(deferred, this, req));

api.index = function(resource, parameters) {
return this.getToken().then(_.bind(function(token) {
return this.getTokens().then(_.bind(function(tokens) {
var deferred = q.defer();
var req = {url: this.urlFor(resource, null, parameters), headers: {'Authorization': 'Bearer ' + token}, json: true};
var req = {url: this.urlFor(resource, null, parameters), headers: {'Authorization': 'Bearer ' + tokens.access_token}, json: true};

@@ -90,3 +105,3 @@ request(req, resolveResponse(deferred, this, req));

return this.getToken().then(_.bind(function(token) {
return this.getTokens().then(_.bind(function(tokens) {
var req = {

@@ -96,3 +111,3 @@ url: this.urlFor(resource),

json: parameters,
headers: {'Authorization': 'Bearer ' + token}
headers: {'Authorization': 'Bearer ' + tokens.access_token}
};

@@ -106,2 +121,35 @@

api.put = function(resource, id, parameters) {
var deferred = q.defer();
return this.getTokens().then(_.bind(function(tokens) {
var req = {
url: this.urlFor(resource, id),
method: 'PUT',
json: parameters,
headers: {'Authorization': 'Bearer ' + tokens.access_token}
};
request(req, resolveResponse(deferred, this, req));
return deferred.promise;
}, this));
}
api.delete = function(resource, id) {
var deferred = q.defer();
return this.getTokens().then(_.bind(function(tokens) {
var req = {
url: this.urlFor(resource, id),
method: 'DELETE',
headers: {'Authorization': 'Bearer ' + tokens.access_token}
};
request(req, resolveResponse(deferred, this, req));
return deferred.promise;
}, this));
}
api.set = function(setting, value) {

@@ -122,6 +170,8 @@ if (arguments.length == 1) {

api.tokenPromise = null;
api.getToken().then(_.bind(function(token) {
req.headers.Authorization = 'Bearer ' + token;
api.getTokens().then(_.bind(function(tokens) {
req.headers.Authorization = 'Bearer ' + tokens.access_token;
request(req, resolveResponse(deferred, api, req));
}, api));
}, api), function(error) {
deferred.reject({res: error.res, body: error.body});
});
} else if (res.statusCode >= 400) {

@@ -155,2 +205,15 @@ deferred.reject({res: res, body: body});

api.handleRefreshToken = function(tokenUrl, clientId, clientSecret, scope, refreshToken) {
var deferred = q.defer();
var form = {grant_type: 'refresh_token', client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken};
if (scope !== undefined) {
form.scope = scope;
}
request({url: tokenUrl, method: 'POST', form: form, json: true}, resolveResponse(deferred));
return deferred.promise.then(parseTokenResponse);
};
api.getResourceOwnerPasswordCredentialsToken = function(tokenUrl, clientId, clientSecret, username, password, scope) {

@@ -179,3 +242,8 @@ var deferred = q.defer();

if (res.body.access_token) {
return res.body.access_token;
var tokens = {access_token: res.body.access_token};
if (res.body.refresh_token) {
tokens.refresh_token = res.body.refresh_token;
}
return tokens;
} else {

@@ -182,0 +250,0 @@ throw { error: "Invalid response: " + res.body }

{
"name": "tol-api",
"version": "0.6.1",
"version": "0.7.0",
"description": "TraderOnline api client",

@@ -5,0 +5,0 @@ "dependencies": {