diff --git a/README.md b/README.md index 7ed04dc14d..3ef0be551c 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ List of Definitions * [Restify](https://github.com/mcavage/node-restify) (by [Bret Little](https://github.com/blittle)) * [Rx.js](http://rx.codeplex.com/) (by [gsino](http://www.codeplex.com/site/users/view/gsino)) * [Raphael](http://raphaeljs.com/) (by [CheCoxshall](https://github.com/CheCoxshall)) +* [Restangular](https://github.com/mgonto/restangular/) (by [Boris Yankov](https://github.com/borisyankov)) * [require.js](http://requirejs.org/) (by [Josh Baldwin](https://github.com/jbaldwin/)) * [Sammy.js](http://sammyjs.org/) (by [Boris Yankov](https://github.com/borisyankov)) * [Select2](http://ivaynberg.github.com/select2/) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/restangular/restangular-tests.ts b/restangular/restangular-tests.ts new file mode 100644 index 0000000000..0a2ff42a76 --- /dev/null +++ b/restangular/restangular-tests.ts @@ -0,0 +1,107 @@ +/// + +function test_basic() { + var $scope; + Restangular.all('accounts'); + Restangular.one('accounts', 1234); + + Restangular.one('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 baseAccounts = Restangular.all('accounts'); + + $scope.allAccounts = baseAccounts.getList(); + + var newAccount = { name: "Gonto's account" }; + + baseAccounts.post(newAccount); + + Restangular.one('accounts', 123).one('buildings', 456).get() + + Restangular.one('accounts', 123).all('buildings').getList() + + baseAccounts.getList().then(function (accounts) { + + var firstAccount = accounts[0]; + $scope.buildings = firstAccount.getList("buildings"); + $scope.loggedInPlaces = firstAccount.getList("places", { query: 'wuut' }, { 'x-user': 'mgonto' }) + + firstAccount.name = "Gonto" + + var editFirstAccount = Restangular.copy(firstAccount); + + firstAccount.put(); + editFirstAccount.put(); + + 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"); + }); + + + 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("messages", { param: "myParam" }, {}, { name: "My Message" }) +} + +function test_config() { + RestangularProvider.setBaseUrl('/api/v1'); + RestangularProvider.setExtraFields(['name']); + RestangularProvider.setResponseExtractor(function (response, operation) { + return response.data; + }); + + RestangularProvider.setDefaultHttpFields({ cache: true }); + RestangularProvider.setMethodOverriders(["put", "patch"]); + + RestangularProvider.setListTypeIsArray(true); + + RestangularProvider.setRestangularFields({ + id: "_id", + route: "restangularRoute" + }); + + RestangularProvider.setRequestSuffix('.json'); + + RestangularProvider.setRequestInterceptor(function (element, operation, route, url) { + }); + + RestangularProvider.addElementTransformer('accounts', false, function (elem) { + elem.accountName = 'Changed'; + return elem; + }); +} \ No newline at end of file diff --git a/restangular/restangular.d.ts b/restangular/restangular.d.ts new file mode 100644 index 0000000000..573a442801 --- /dev/null +++ b/restangular/restangular.d.ts @@ -0,0 +1,69 @@ +// Type definitions for Restangular 0.6.9 +// Project: https://github.com/mgonto/restangular +// Definitions by: Boris Yankov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +/// + +interface Restangular extends RestangularCustom { + one(route: string, id?: number): RestangularElement; + all(route: string): RestangularCollection; + copy(fromElement: any): RestangularElement; +} + +interface RestangularElement extends Restangular { + get (): ng.IPromise; + get (params: any): ng.IPromise; + get (params: any, headers: any): ng.IPromise; + + getList(): ng.IPromise; + getList(subElement: any): ng.IPromise; + getList(subElement: any, queryParams: string, headers: any): ng.IPromise; + + put(queryParams?: string, headers?: any): ng.IPromise; + + post(subElement, elementToPost, queryParams?, headers?): ng.IPromise; + + remove(queryParams?, headers?): ng.IPromise; + head(queryParams?, headers?): ng.IPromise; + trace(queryParams?, headers?): ng.IPromise; + options(queryParams?, headers?): ng.IPromise; + patch(queryParams?, headers?): ng.IPromise; +} + +interface RestangularCollection extends Restangular { + getList(queryParams?, headers?): ng.IPromise; + post(elementToPost, queryParams?, headers?): ng.IPromise; + head(queryParams?, headers?): ng.IPromise; + trace(queryParams?, headers?): ng.IPromise; + options(queryParams?, headers?): ng.IPromise; + patch(queryParams?, headers?): ng.IPromise; + putElement(idx, params, headers): ng.IPromise; +} + +interface RestangularCustom { + customGET(path, params?, headers?): ng.IPromise; + customGETLIST(path, params?, headers?): ng.IPromise; + customDELETE(path, params?, headers?): ng.IPromise; + customPOST(path, params?, headers?, elem?): ng.IPromise; + customPUT(path, params?, headers?, elem?): ng.IPromise; + customOperation(operation, path, params?, headers?, elem?): ng.IPromise; + addRestangularMethod(name, operation, path?, params?, headers?, elem?): ng.IPromise; +} + +interface RestangularProvider { + setBaseUrl(newValue: string): void; + setExtraFields(newValues: string[]): void; + setDefaultHttpFields(newValue: any): void; + setMethodOverriders(newValue: any): void; + setResponseExtractor(newValue: any): void; + setRequestInterceptor(newValue: any): void; + setListTypeIsArray(newValue: bool): void; + setRestangularFields(newValue: any): void; + setRequestSuffix(newValue: any): void; + addElementTransformer(type, secondArg, thirdArg): void; +} + +declare var Restangular: Restangular; +declare var RestangularProvider: RestangularProvider; \ No newline at end of file