Update Restangular definition

Add enhanced promises
Add new methods up to current 1.4
Rewrite tests to make them more realistic
This commit is contained in:
Keats
2014-04-27 10:19:26 +01:00
parent 7631e9eee4
commit 114f930fbd
2 changed files with 228 additions and 192 deletions
+127 -113
View File
@@ -1,145 +1,159 @@
/// <reference path="restangular.d.ts" />
function test_basic() {
var $scope;
Restangular.all('accounts');
Restangular.one('accounts', 1234);
Restangular.all('users').getList().then(function (users) {
$scope.user = users[0];
})
$scope.cars = $scope.user.getList('cars');
$scope.user.sendMessage();
$scope.user.one('message', 123).all('unread').getList();
var myApp = angular.module('testModule');
var baseAccounts = Restangular.all('accounts');
myApp.config((RestangularProvider: restangular.IProvider) => {
RestangularProvider.setBaseUrl('/api/v1');
RestangularProvider.setExtraFields(['name']);
RestangularProvider.setResponseExtractor(function (response, operation) {
return response.data;
});
$scope.allAccounts = baseAccounts.getList();
RestangularProvider.setDefaultHttpFields({ cache: true });
RestangularProvider.setMethodOverriders(["put", "patch"]);
var newAccount = { name: "Gonto's account" };
RestangularProvider.setErrorInterceptor(function (response) {
console.error('' + response.status + ' ' + response.data);
});
baseAccounts.post(newAccount);
RestangularProvider.setRequestSuffix('.json');
Restangular.one('accounts', 123).one('buildings', 456).get()
RestangularProvider.setRequestInterceptor(function (element, operation, route, url) {
});
Restangular.one('accounts', 123).all('buildings').getList()
RestangularProvider.addElementTransformer('accounts', false, function (elem) {
elem.accountName = 'Changed';
return elem;
});
baseAccounts.getList().then(function (accounts) {
RestangularProvider.setRestangularFields({
id: "_id",
route: "restangularRoute",
selfLink: "self.href"
});
var firstAccount = accounts[0];
$scope.buildings = firstAccount.getList("buildings");
$scope.loggedInPlaces = firstAccount.getList("places", { query: 'wuut' }, { 'x-user': 'mgonto' })
RestangularProvider.addRequestInterceptor(function(element, operation, route, url) {
delete element.name;
return element;
});
firstAccount.name = "Gonto"
var editFirstAccount = Restangular.copy(firstAccount);
firstAccount.put();
editFirstAccount.put();
firstAccount.remove();
var myBuilding = {
name: "Gonto's Building",
place: "Argentina"
};
RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
delete element.name;
return {
element: element,
params: params,
headers: headers,
httpConfig: httpConfig
};
});
});
firstAccount.post("Buildings", myBuilding).then(function () {
console.log("Object saved OK");
}, function () {
console.log("There was an error saving");
});
firstAccount.getList("users", { query: 'wuut' }).then(function (users) {
users.post({ userName: 'unknown' });
users.customGET("messages", { param: "myParam" })
var firstUser = users[0];
$scope.userFromServer = firstUser.get();
firstUser.head()
});
}, function errorCallback() {
alert("Oops error from server :(");
})
var account = Restangular.one("accounts", 123);
$scope.account = account.get({ single: true });
account.customPOST({ name: "My Message" }, "messages", { param: "myParam" }, {})
interface MyAppScope extends ng.IScope {
accounts: string[];
allAccounts: any[];
account: any;
buildings: restangular.ICollectionPromise<any>;
loggedInPlaces: restangular.ICollectionPromise<any>;
userFromServer: restangular.IPromise<any>;
}
function test_config() {
RestangularProvider.setBaseUrl('/api/v1');
RestangularProvider.setExtraFields(['name']);
RestangularProvider.setResponseExtractor(function (response, operation) {
return response.data;
myApp.controller('TestCtrl', (
$scope: MyAppScope,
Restangular: restangular.IService
) => {
var baseAccounts = Restangular.all('accounts');
baseAccounts.getList().then(function(accounts) {
$scope.allAccounts = accounts;
});
$scope.accounts = Restangular.all('accounts').getList().$object;
var newAccount = {name: "Gonto's account"};
baseAccounts.post(newAccount);
Restangular.allUrl('googlers', 'http://www.google.com/').getList();
Restangular.oneUrl('googlers', 'http://www.google.com/1').get();
Restangular.one('accounts', 123).one('buildings', 456).get();
Restangular.one('accounts', 123).getList('buildings');
baseAccounts.getList().then(function (accounts) {
var firstAccount = accounts[0];
$scope.buildings = firstAccount.getList("buildings");
$scope.loggedInPlaces = firstAccount.getList("places", {query: "param"}, {'x-user': 'mgonto'});
firstAccount.name = "Gonto";
var editFirstAccount = Restangular.copy(firstAccount);
firstAccount.put();
editFirstAccount.put();
firstAccount.save();
firstAccount.remove();
var myBuilding = {
name: "Gonto's Building",
place: "Argentina"
};
firstAccount.post("Buildings", myBuilding).then(function() {
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});
RestangularProvider.setDefaultHttpFields({ cache: true });
RestangularProvider.setMethodOverriders(["put", "patch"]);
firstAccount.getList("users", {query: "params"}).then(function(users) {
users.post({userName: 'unknown'});
users.customGET("messages", {param: "myParam"});
RestangularProvider.setErrorInterceptor(function (response) {
var firstUser = users[0];
$scope.userFromServer = firstUser.get();
firstUser.head()
});
}, function errorCallback() {
alert("Oops error from server :(");
});
var account = Restangular.one("accounts", 123);
$scope.account = account.get({single: true});
account.customPOST({name: "My Message"}, "messages", {param: "myParam"}, {});
Restangular.one('accounts', 123).withHttpConfig({timeout: 100}).getList('buildings');
$scope.account = Restangular.one('accounts', 123);
$scope.account.withHttpConfig({timeout: 100}).put();
var myRestangular = Restangular.withConfig((configurer: restangular.IProvider) => {
configurer.setBaseUrl('/api/v1');
configurer.setExtraFields(['name']);
configurer.setErrorInterceptor(function (response) {
console.error('' + response.status + ' ' + response.data);
});
configurer.setResponseExtractor(function (response, operation) {
return response.data;
});
configurer.setDefaultHttpFields({ cache: true });
configurer.setMethodOverriders(["put", "patch"]);
RestangularProvider.setRestangularFields({
configurer.setRestangularFields({
id: "_id",
route: "restangularRoute"
});
RestangularProvider.setRequestSuffix('.json');
configurer.setRequestSuffix('.json');
RestangularProvider.setRequestInterceptor(function (element, operation, route, url) {
configurer.setRequestInterceptor(function (element, operation, route, url) {
});
RestangularProvider.addElementTransformer('accounts', false, function (elem) {
configurer.addElementTransformer('accounts', false, function (elem) {
elem.accountName = 'Changed';
return elem;
});
var myRestangular = Restangular.withConfig((configurer: RestangularProvider) => {
configurer.setBaseUrl('/api/v1');
configurer.setExtraFields(['name']);
configurer.setErrorInterceptor(function (response) {
console.error('' + response.status + ' ' + response.data);
});
configurer.setResponseExtractor(function (response, operation) {
return response.data;
});
configurer.setDefaultHttpFields({ cache: true });
configurer.setMethodOverriders(["put", "patch"]);
configurer.setRestangularFields({
id: "_id",
route: "restangularRoute"
});
configurer.setRequestSuffix('.json');
configurer.setRequestInterceptor(function (element, operation, route, url) {
});
configurer.addElementTransformer('accounts', false, function (elem) {
elem.accountName = 'Changed';
return elem;
});
});
}
function test_withHttpConfig() {
var $scope;
Restangular.one('accounts', 123).withHttpConfig({timeout: 100}).getList('buildings');
$scope.account = Restangular.one('accounts', 123);
$scope.account.withHttpConfig({timeout: 100}).put();
}
});
});