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 09:55:40 +01:00
parent 7631e9eee4
commit 114f930fbd
2 changed files with 228 additions and 192 deletions

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();
}
});
});

View File

@@ -1,4 +1,4 @@
// Type definitions for Restangular v1.2.2
// Type definitions for Restangular v1.4.0
// Project: https://github.com/mgonto/restangular
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
@@ -6,7 +6,23 @@
/// <reference path="../angularjs/angular.d.ts" />
interface RestangularRequestConfig {
declare module restangular {
interface IPromise<T> extends ng.IPromise<T> {
call(methodName: string, params?: any): IPromise<T>;
get(fieldName: string): IPromise<T>;
$object: T;
}
interface ICollectionPromise<T> extends ng.IPromise<T> {
push(object: any): ICollectionPromise<T>;
call(methodName: string, params?: any): ICollectionPromise<T>;
get(fieldName: string): ICollectionPromise<T>;
$object: T[];
}
interface IRequestConfig {
params?: any;
headers?: any;
cache?: any;
@@ -15,81 +31,9 @@ interface RestangularRequestConfig {
transformRequest?: any;
transformResponse?: any;
timeout?: any; // number | promise
}
}
interface Restangular extends RestangularCustom {
one(route: string, id?: number): RestangularElement;
one(route: string, id?: string): RestangularElement;
oneUrl(route: string, url: string): RestangularElement;
all(route: string): RestangularCollection;
allUrl(route: string, url: string): RestangularCollection;
copy(fromElement: any): RestangularElement;
withConfig(configurer: (RestangularProvider: RestangularProvider) => any): Restangular;
restangularizeElement(parent: any, element: any, route: string, collection?: any, reqParams?: any): RestangularElement;
restangularizeCollection(parent: any, element: any, route: string): RestangularCollection;
stripRestangular(element: any): any;
}
interface RestangularElement extends Restangular {
get(queryParams?: any, headers?: any): ng.IPromise<any>;
getList(subElement: any, queryParams?: any, headers?: any): ng.IPromise<any>;
put(queryParams?: any, headers?: any): ng.IPromise<any>;
post(subElement: any, elementToPost: any, queryParams?: any, headers?: any): ng.IPromise<any>;
remove(queryParams?: any, headers?: any): ng.IPromise<any>;
head(queryParams?: any, headers?: any): ng.IPromise<any>;
trace(queryParams?: any, headers?: any): ng.IPromise<any>;
options(queryParams?: any, headers?: any): ng.IPromise<any>;
patch(queryParams?: any, headers?: any): ng.IPromise<any>;
withHttpConfig(httpConfig: RestangularRequestConfig): RestangularElement;
getRestangularUrl(): string;
}
interface RestangularCollection extends Restangular {
getList(queryParams?: any, headers?: any): ng.IPromise<any>;
post(elementToPost: any, queryParams?: any, headers?: any): ng.IPromise<any>;
head(queryParams?: any, headers?: any): ng.IPromise<any>;
trace(queryParams?: any, headers?: any): ng.IPromise<any>;
options(queryParams?: any, headers?: any): ng.IPromise<any>;
patch(queryParams?: any, headers?: any): ng.IPromise<any>;
putElement(idx: any, params: any, headers: any): ng.IPromise<any>;
withHttpConfig(httpConfig: RestangularRequestConfig): RestangularCollection;
getRestangularUrl(): string;
}
interface RestangularCustom {
customGET(path: string, params?: any, headers?: any): ng.IPromise<any>;
customGETLIST(path: string, params?: any, headers?: any): ng.IPromise<any>;
customDELETE(path: string, params?: any, headers?: any): ng.IPromise<any>;
customPOST(elem?: any, path?: string, params?: any, headers?: any): ng.IPromise<any>;
customPUT(elem?: any, path?: string, params?: any, headers?: any): ng.IPromise<any>;
customOperation(operation: string, path: string, params?: any, headers?: any, elem?: any): ng.IPromise<any>;
addRestangularMethod(name: string, operation: string, path?: string, params?: any, headers?: any, elem?: any): ng.IPromise<any>;
}
interface RestangularProvider {
setBaseUrl(baseUrl: string): void;
setExtraFields(fields: string[]): void;
setParentless(parentless: boolean, routes: string[]): void;
setDefaultHttpFields(httpFields: any): void;
addElementTransformer(route: string, transformer: Function): void;
addElementTransformer(route: string, isCollection: boolean, transformer: Function): void;
setOnElemRestangularized(callback: (elem: any, isCollection: boolean, what: string, restangular: Restangular) => any): void;
setResponseInterceptor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: RestangularResponse, deferred: ng.IDeferred<any>) => any): void;
setResponseExtractor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: RestangularResponse, deferred: ng.IDeferred<any>) => any): void;
setRequestInterceptor(requestInterceptor: (element: any, operation: string, what: string, url: string) => any): void;
setFullRequestInterceptor(fullRequestInterceptor: (element: any, operation: string, what: string, url: string, headers: any, params: any) => {element: any; headers: any; params: any}): void;
setErrorInterceptor(errorInterceptor: (response: RestangularResponse) => any): void;
setRestangularFields(fields: {[fieldName: string]: string}): void;
setMethodOverriders(overriders: string[]): void;
setDefaultRequestParams(params: any): void;
setDefaultRequestParams(methods: any, params: any): void;
setFullResponse(fullResponse: boolean): void;
setDefaultHeaders(headers: any): void;
setRequestSuffix(suffix: string): void;
setUseCannonicalId(useCannonicalId: boolean): void;
}
interface RestangularResponse {
interface IResponse {
status: number;
data: any;
config: {
@@ -97,7 +41,85 @@ interface RestangularResponse {
url: string;
params: any;
}
}
}
declare var Restangular: Restangular;
declare var RestangularProvider: RestangularProvider;
interface IProvider {
setBaseUrl(baseUrl: string): void;
setExtraFields(fields: string[]): void;
setParentless(parentless: boolean, routes: string[]): void;
setDefaultHttpFields(httpFields: any): void;
addElementTransformer(route: string, transformer: Function): void;
addElementTransformer(route: string, isCollection: boolean, transformer: Function): void;
setTransformOnlyServerElements(active: boolean): void;
setOnElemRestangularized(callback: (elem: any, isCollection: boolean, what: string, restangular: IService) => any): void;
setResponseInterceptor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: IResponse, deferred: ng.IDeferred<any>) => any): void;
setResponseExtractor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: IResponse, deferred: ng.IDeferred<any>) => any): void;
addResponseInterceptor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: IResponse, deferred: ng.IDeferred<any>) => any): void;
setRequestInterceptor(requestInterceptor: (element: any, operation: string, what: string, url: string) => any): void;
addRequestInterceptor(requestInterceptor: (element: any, operation: string, what: string, url: string) => any): void;
setFullRequestInterceptor(fullRequestInterceptor: (element: any, operation: string, what: string, url: string, headers: any, params: any, httpConfig: IRequestConfig) => {element: any; headers: any; params: any}): void;
addFullRequestInterceptor(requestInterceptor: (element: any, operation: string, what: string, url: string, headers: any, params: any, httpConfig: IRequestConfig) => {headers: any; params: any; element: any; httpConfig: IRequestConfig}): void;
setErrorInterceptor(errorInterceptor: (response: IResponse, deferred: ng.IDeferred<any>) => any): void;
setRestangularFields(fields: {[fieldName: string]: string}): void;
setMethodOverriders(overriders: string[]): void;
setJsonp(jsonp: boolean): void;
setDefaultRequestParams(params: any): void;
setDefaultRequestParams(method: string, params: any): void;
setDefaultRequestParams(methods: string[], params: any): void;
setFullResponse(fullResponse: boolean): void;
setDefaultHeaders(headers: any): void;
setRequestSuffix(suffix: string): void;
setUseCannonicalId(useCannonicalId: boolean): void;
setEncodeIds(encode: boolean): void;
}
interface ICustom {
customGET(path: string, params?: any, headers?: any): IPromise<any>;
customGETLIST(path: string, params?: any, headers?: any): ICollectionPromise<any>;
customDELETE(path: string, params?: any, headers?: any): IPromise<any>;
customPOST(elem?: any, path?: string, params?: any, headers?: any): IPromise<any>;
customPUT(elem?: any, path?: string, params?: any, headers?: any): IPromise<any>;
customOperation(operation: string, path: string, params?: any, headers?: any, elem?: any): IPromise<any>;
addRestangularMethod(name: string, operation: string, path?: string, params?: any, headers?: any, elem?: any): IPromise<any>;
}
interface IService extends ICustom {
one(route: string, id?: number): IElement;
one(route: string, id?: string): IElement;
oneUrl(route: string, url: string): IElement;
all(route: string): IElement;
allUrl(route: string, url: string): IElement;
copy(fromElement: any): IElement;
withConfig(configurer: (RestangularProvider: IProvider) => any): IService;
restangularizeElement(parent: any, element: any, route: string, collection?: any, reqParams?: any): IElement;
restangularizeCollection(parent: any, element: any, route: string): ICollection;
stripRestangular(element: any): any;
}
interface IElement extends IService {
get(queryParams?: any, headers?: any): IPromise<any>;
getList(subElement?: any, queryParams?: any, headers?: any): ICollectionPromise<any>;
put(queryParams?: any, headers?: any): IPromise<any>;
post(subElement: any, elementToPost: any, queryParams?: any, headers?: any): IPromise<any>;
post(elementToPost: any, queryParams?: any, headers?: any): IPromise<any>;
remove(queryParams?: any, headers?: any): IPromise<any>;
head(queryParams?: any, headers?: any): IPromise<any>;
trace(queryParams?: any, headers?: any): IPromise<any>;
options(queryParams?: any, headers?: any): IPromise<any>;
patch(queryParams?: any, headers?: any): IPromise<any>;
withHttpConfig(httpConfig: IRequestConfig): IElement;
getRestangularUrl(): string;
}
interface ICollection extends IService {
getList(queryParams?: any, headers?: any): ICollectionPromise<any>;
post(elementToPost: any, queryParams?: any, headers?: any): IPromise<any>;
head(queryParams?: any, headers?: any): IPromise<any>;
trace(queryParams?: any, headers?: any): IPromise<any>;
options(queryParams?: any, headers?: any): IPromise<any>;
patch(queryParams?: any, headers?: any): IPromise<any>;
putElement(idx: any, params: any, headers: any): IPromise<any>;
withHttpConfig(httpConfig: IRequestConfig): ICollection;
getRestangularUrl(): string;
}
}