Merge pull request #81 from standardnotes/no-rest

remove restangular as dependency, use xmlhttprequest
This commit is contained in:
Mo Bitar
2017-03-01 18:07:52 -06:00
committed by GitHub
7 changed files with 134 additions and 93 deletions

View File

@@ -85,7 +85,6 @@ module.exports = function(grunt) {
'vendor/assets/bower_components/angular/angular.js',
'vendor/assets/bower_components/angular-ui-router/release/angular-ui-router.js',
'vendor/assets/bower_components/lodash/dist/lodash.min.js',
'vendor/assets/bower_components/restangular/dist/restangular.js',
'vendor/assets/javascripts/crypto/*.js'
],
dest: 'vendor/assets/javascripts/lib.js',

View File

@@ -14,24 +14,5 @@ if(!IEOrEdge && (window.crypto && window.crypto.subtle)) {
}
angular.module('app.frontend', [
'ui.router',
'restangular'
'ui.router'
])
.config(function (RestangularProvider, authManagerProvider) {
RestangularProvider.setDefaultHeaders({"Content-Type": "application/json"});
RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
var token = localStorage.getItem("jwt");
if(token) {
headers = _.extend(headers, {Authorization: "Bearer " + localStorage.getItem("jwt")});
}
return {
element: element,
params: params,
headers: headers,
httpConfig: httpConfig
};
});
})

View File

@@ -7,11 +7,11 @@ angular.module('app.frontend')
return domain;
}
this.$get = function($rootScope, Restangular, modelManager) {
return new AuthManager($rootScope, Restangular, modelManager);
this.$get = function($rootScope, httpManager, modelManager) {
return new AuthManager($rootScope, httpManager, modelManager);
}
function AuthManager($rootScope, Restangular, modelManager) {
function AuthManager($rootScope, httpManager, modelManager) {
var userData = localStorage.getItem("user");
if(userData) {
@@ -34,12 +34,10 @@ angular.module('app.frontend')
this.getAuthParamsForEmail = function(url, email, callback) {
var requestUrl = url + "/auth/params";
var request = Restangular.oneUrl(requestUrl, requestUrl);
request.get({email: email}).then(function(response){
callback(response.plain());
})
.catch(function(response){
console.log("Error getting auth params", response);
httpManager.getAbsolute(requestUrl, {email: email}, function(response){
callback(response);
}, function(response){
console.error("Error getting auth params", response);
callback(null);
})
}
@@ -74,17 +72,15 @@ angular.module('app.frontend')
Neeto.crypto.computeEncryptionKeysForUser(_.merge({password: password}, authParams), function(keys){
var requestUrl = url + "/auth/sign_in";
var request = Restangular.oneUrl(requestUrl, requestUrl);
var params = {password: keys.pw, email: email};
_.merge(request, params);
request.post().then(function(response){
httpManager.postAbsolute(requestUrl, params, function(response){
this.handleAuthResponse(response, email, url, authParams, keys.mk, keys.pw);
callback(response);
}.bind(this))
.catch(function(response){
console.log("Error logging in", response);
callback(response.data);
}.bind(this), function(response){
console.error("Error logging in", response);
callback(response);
})
}.bind(this));
}.bind(this))
}
@@ -93,7 +89,7 @@ angular.module('app.frontend')
if(url) {
localStorage.setItem("server", url);
}
localStorage.setItem("user", JSON.stringify(response.plain().user));
localStorage.setItem("user", JSON.stringify(response.user));
localStorage.setItem("auth_params", JSON.stringify(_.omit(authParams, ["pw_nonce"])));
localStorage.setItem("mk", mk);
localStorage.setItem("pw", pw);
@@ -103,40 +99,35 @@ angular.module('app.frontend')
this.register = function(url, email, password, callback) {
Neeto.crypto.generateInitialEncryptionKeysForUser({password: password, email: email}, function(keys, authParams){
var requestUrl = url + "/auth";
var request = Restangular.oneUrl(requestUrl, requestUrl);
var params = _.merge({password: keys.pw, email: email}, authParams);
_.merge(request, params);
request.post().then(function(response){
httpManager.postAbsolute(requestUrl, params, function(response){
this.handleAuthResponse(response, email, url, authParams, keys.mk, keys.pw);
callback(response);
}.bind(this), function(response){
console.error("Registration error", response);
callback(response);
}.bind(this))
.catch(function(response){
console.log("Registration error", response);
callback(response.data);
})
}.bind(this));
}
this.changePassword = function(email, new_password, callback) {
Neeto.crypto.generateInitialEncryptionKeysForUser({password: new_password, email: email}, function(keys, authParams){
var requestUrl = localStorage.getItem("server") + "/auth/change_pw";
var request = Restangular.oneUrl(requestUrl, requestUrl);
var params = _.merge({new_password: keys.pw}, authParams);
_.merge(request, params);
request.post().then(function(response){
httpManager.postAbsolute(requestUrl, params, function(response){
this.handleAuthResponse(response, email, null, authParams, keys.mk, keys.pw);
callback(response.plain());
}.bind(this))
.catch(function(response){
var error = response.data;
callback(response);
}.bind(this), function(response){
var error = response;
if(!error) {
error = {message: "Something went wrong while changing your password. Your password was not changed. Please try again."}
}
console.log("Change pw error", response);
console.error("Change pw error", response);
callback({error: error});
})
}.bind(this));
})
}
this.staticifyObject = function(object) {

View File

@@ -1,7 +1,7 @@
class ExtensionManager {
constructor(Restangular, modelManager, authManager, syncManager) {
this.Restangular = Restangular;
constructor(httpManager, modelManager, authManager, syncManager) {
this.httpManager = httpManager;
this.modelManager = modelManager;
this.authManager = authManager;
this.enabledRepeatActionUrls = JSON.parse(localStorage.getItem("enabledRepeatActionUrls")) || [];
@@ -77,11 +77,11 @@ class ExtensionManager {
relevant just to this item. The response extension is not saved, just displayed as a one-time thing.
*/
loadExtensionInContextOfItem(extension, item, callback) {
this.Restangular.oneUrl(extension.url, extension.url).customGET("", {content_type: item.content_type, item_uuid: item.uuid}).then(function(response){
var scopedExtension = new Extension(response.plain());
this.httpManager.getAbsolute(extension.url, {content_type: item.content_type, item_uuid: item.uuid}, function(response){
var scopedExtension = new Extension(response);
callback(scopedExtension);
}.bind(this))
.catch(function(response){
}, function(response){
console.log("Error loading extension", response);
callback(null);
})
@@ -91,14 +91,13 @@ class ExtensionManager {
Registers new extension and saves it to user's account
*/
retrieveExtensionFromServer(url, callback) {
this.Restangular.oneUrl(url, url).get().then(function(response){
var ext = this.handleExtensionLoadExternalResponseItem(url, response.plain());
this.httpManager.getAbsolute(url, {}, function(response){
var ext = this.handleExtensionLoadExternalResponseItem(url, response);
if(callback) {
callback(ext);
}
}.bind(this))
.catch(function(response){
console.log("Error registering extension", response);
}.bind(this), function(response){
console.error("Error registering extension", response);
callback(null);
})
}
@@ -150,7 +149,8 @@ class ExtensionManager {
switch (action.verb) {
case "get": {
this.Restangular.oneUrl(action.url, action.url).get().then(function(response){
this.httpManager.getAbsolute(action.url, {}, function(response){
action.error = false;
var items = response.items || [response.item];
EncryptionHelper.decryptMultipleItems(items, localStorage.getItem("mk"));
@@ -160,8 +160,7 @@ class ExtensionManager {
}
this.syncManager.sync(null);
customCallback({items: items});
}.bind(this))
.catch(function(response){
}.bind(this), function(response){
action.error = true;
customCallback(null);
})
@@ -170,13 +169,14 @@ class ExtensionManager {
}
case "render": {
this.Restangular.oneUrl(action.url, action.url).get().then(function(response){
this.httpManager.getAbsolute(action.url, {}, function(response){
action.error = false;
EncryptionHelper.decryptItem(response.item, localStorage.getItem("mk"));
var item = this.modelManager.createItem(response.item);
customCallback({item: item});
}.bind(this))
.catch(function(response){
}.bind(this), function(response){
action.error = true;
customCallback(null);
})
@@ -310,19 +310,17 @@ class ExtensionManager {
}
performPost(action, extension, params, callback) {
var request = this.Restangular.oneUrl(action.url, action.url);
if(this.extensionUsesEncryptedData(extension)) {
request.auth_params = this.authManager.getAuthParams();
}
_.merge(request, params);
request.post().then(function(response){
if(this.extensionUsesEncryptedData(extension)) {
params.auth_params = this.authManager.getAuthParams();
}
this.httpManager.postAbsolute(action.url, params, function(response){
action.error = false;
if(callback) {
callback(response.plain());
callback(response);
}
})
.catch(function(response){
}.bind(this), function(response){
action.error = true;
console.log("Action error response:", response);
if(callback) {

View File

@@ -0,0 +1,73 @@
class HttpManager {
constructor($timeout) {
// calling callbacks in a $timeout allows angular UI to update
this.$timeout = $timeout;
}
setAuthHeadersForRequest(request) {
var token = localStorage.getItem("jwt");
if(token) {
request.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("jwt"));
}
}
postAbsolute(url, params, onsuccess, onerror) {
this.httpRequest("post", url, params, onsuccess, onerror);
}
getAbsolute(url, params, onsuccess, onerror) {
this.httpRequest("get", url, params, onsuccess, onerror);
}
httpRequest(verb, url, params, onsuccess, onerror) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
if(response) {
response = JSON.parse(response);
}
if(xmlhttp.status >= 200 && xmlhttp.status <= 299){
this.$timeout(function(){
onsuccess(response);
})
} else {
console.error("Request error:", response);
this.$timeout(function(){
onerror(response)
})
}
}
}.bind(this)
if(verb == "get" && Object.keys(params).length > 0) {
url = url + this.formatParams(params);
}
xmlhttp.open(verb, url, true);
this.setAuthHeadersForRequest(xmlhttp);
xmlhttp.setRequestHeader('Content-type', 'application/json');
if(verb == "post") {
xmlhttp.send(JSON.stringify(params));
} else {
xmlhttp.send();
}
}
formatParams(params) {
return "?" + Object
.keys(params)
.map(function(key){
return key+"="+params[key]
})
.join("&")
}
}
angular.module('app.frontend').service('httpManager', HttpManager);

View File

@@ -1,10 +1,10 @@
class SyncManager {
constructor($rootScope, modelManager, authManager, dbManager, Restangular) {
constructor($rootScope, modelManager, authManager, dbManager, httpManager) {
this.$rootScope = $rootScope;
this.httpManager = httpManager;
this.modelManager = modelManager;
this.authManager = authManager;
this.Restangular = Restangular;
this.dbManager = dbManager;
this.syncStatus = {};
}
@@ -161,18 +161,18 @@ class SyncManager {
this.syncStatus.current = 0;
}
var request = this.Restangular.oneUrl(this.syncURL, this.syncURL);
request.limit = 150;
request.items = _.map(subItems, function(item){
var params = {};
params.limit = 150;
params.items = _.map(subItems, function(item){
var itemParams = new ItemParams(item, localStorage.getItem("mk"));
itemParams.additionalFields = options.additionalFields;
return itemParams.paramsForSync();
}.bind(this));
request.sync_token = this.syncToken;
request.cursor_token = this.cursorToken;
params.sync_token = this.syncToken;
params.cursor_token = this.cursorToken;
request.post().then(function(response) {
this.httpManager.postAbsolute(this.syncURL, params, function(response){
this.modelManager.clearDirtyItems(subItems);
this.syncStatus.error = null;
@@ -210,10 +210,9 @@ class SyncManager {
this.callQueuedCallbacksAndCurrent(callback, response);
}
}.bind(this))
.catch(function(response){
}.bind(this), function(response){
console.log("Sync error: ", response);
var error = response.data ? response.data.error : {message: "Could not connect to server."};
var error = response ? response.error : {message: "Could not connect to server."};
this.syncStatus.syncOpInProgress = false;
this.syncStatus.error = error;
@@ -222,7 +221,7 @@ class SyncManager {
this.$rootScope.$broadcast("sync:error", error);
this.callQueuedCallbacksAndCurrent(callback, {error: "Sync error"});
}.bind(this))
}.bind(this));
}
handleUnsavedItemsResponse(unsaved) {

View File

@@ -9,7 +9,7 @@
"dependencies": {
"angular": "1.6.1",
"angular-ui-router": "^0.3.2",
"restangular": "^1.6.1"
"lodash" : "^4.17.4"
},
"resolutions": {
"angular": "1.6.1"