diff --git a/restangular/restangular-tests.ts b/restangular/restangular-tests.ts index 9643c9e6b3..9d5aa9fb4f 100644 --- a/restangular/restangular-tests.ts +++ b/restangular/restangular-tests.ts @@ -1,145 +1,159 @@ /// -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; + loggedInPlaces: restangular.ICollectionPromise; + userFromServer: restangular.IPromise; } -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(); -} + }); +}); diff --git a/restangular/restangular.d.ts b/restangular/restangular.d.ts index 0cc4321afb..9fd397bf57 100644 --- a/restangular/restangular.d.ts +++ b/restangular/restangular.d.ts @@ -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 // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -6,7 +6,23 @@ /// -interface RestangularRequestConfig { + +declare module restangular { + + interface IPromise extends ng.IPromise { + call(methodName: string, params?: any): IPromise; + get(fieldName: string): IPromise; + $object: T; +} + + interface ICollectionPromise extends ng.IPromise { + push(object: any): ICollectionPromise; + call(methodName: string, params?: any): ICollectionPromise; + get(fieldName: string): ICollectionPromise; + $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; - getList(subElement: any, queryParams?: any, headers?: any): ng.IPromise; - put(queryParams?: any, headers?: any): ng.IPromise; - post(subElement: any, elementToPost: any, queryParams?: any, headers?: any): ng.IPromise; - remove(queryParams?: any, headers?: any): ng.IPromise; - head(queryParams?: any, headers?: any): ng.IPromise; - trace(queryParams?: any, headers?: any): ng.IPromise; - options(queryParams?: any, headers?: any): ng.IPromise; - patch(queryParams?: any, headers?: any): ng.IPromise; - withHttpConfig(httpConfig: RestangularRequestConfig): RestangularElement; - getRestangularUrl(): string; -} - -interface RestangularCollection extends Restangular { - getList(queryParams?: any, headers?: any): ng.IPromise; - post(elementToPost: any, queryParams?: any, headers?: any): ng.IPromise; - head(queryParams?: any, headers?: any): ng.IPromise; - trace(queryParams?: any, headers?: any): ng.IPromise; - options(queryParams?: any, headers?: any): ng.IPromise; - patch(queryParams?: any, headers?: any): ng.IPromise; - putElement(idx: any, params: any, headers: any): ng.IPromise; - withHttpConfig(httpConfig: RestangularRequestConfig): RestangularCollection; - getRestangularUrl(): string; -} - -interface RestangularCustom { - customGET(path: string, params?: any, headers?: any): ng.IPromise; - customGETLIST(path: string, params?: any, headers?: any): ng.IPromise; - customDELETE(path: string, params?: any, headers?: any): ng.IPromise; - customPOST(elem?: any, path?: string, params?: any, headers?: any): ng.IPromise; - customPUT(elem?: any, path?: string, params?: any, headers?: any): ng.IPromise; - customOperation(operation: string, path: string, params?: any, headers?: any, elem?: any): ng.IPromise; - addRestangularMethod(name: string, operation: string, path?: string, params?: any, headers?: any, elem?: any): ng.IPromise; -} - -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): void; - setResponseExtractor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: RestangularResponse, deferred: ng.IDeferred) => 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): void; + setResponseExtractor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: IResponse, deferred: ng.IDeferred) => any): void; + addResponseInterceptor(responseInterceptor: (data: any, operation: string, what: string, url: string, response: IResponse, deferred: ng.IDeferred) => 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): 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; + customGETLIST(path: string, params?: any, headers?: any): ICollectionPromise; + customDELETE(path: string, params?: any, headers?: any): IPromise; + customPOST(elem?: any, path?: string, params?: any, headers?: any): IPromise; + customPUT(elem?: any, path?: string, params?: any, headers?: any): IPromise; + customOperation(operation: string, path: string, params?: any, headers?: any, elem?: any): IPromise; + addRestangularMethod(name: string, operation: string, path?: string, params?: any, headers?: any, elem?: any): IPromise; + } + + 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; + getList(subElement?: any, queryParams?: any, headers?: any): ICollectionPromise; + put(queryParams?: any, headers?: any): IPromise; + post(subElement: any, elementToPost: any, queryParams?: any, headers?: any): IPromise; + post(elementToPost: any, queryParams?: any, headers?: any): IPromise; + remove(queryParams?: any, headers?: any): IPromise; + head(queryParams?: any, headers?: any): IPromise; + trace(queryParams?: any, headers?: any): IPromise; + options(queryParams?: any, headers?: any): IPromise; + patch(queryParams?: any, headers?: any): IPromise; + withHttpConfig(httpConfig: IRequestConfig): IElement; + getRestangularUrl(): string; + } + + interface ICollection extends IService { + getList(queryParams?: any, headers?: any): ICollectionPromise; + post(elementToPost: any, queryParams?: any, headers?: any): IPromise; + head(queryParams?: any, headers?: any): IPromise; + trace(queryParams?: any, headers?: any): IPromise; + options(queryParams?: any, headers?: any): IPromise; + patch(queryParams?: any, headers?: any): IPromise; + putElement(idx: any, params: any, headers: any): IPromise; + withHttpConfig(httpConfig: IRequestConfig): ICollection; + getRestangularUrl(): string; + } +}