From 2e030fa8c428767e335df204015fed5d8e2a775f Mon Sep 17 00:00:00 2001 From: John Reilly Date: Mon, 2 Jun 2014 14:13:54 +0100 Subject: [PATCH] Added typing to IHttpService --- angularjs/angular-tests.ts | 8 +- angularjs/angular.d.ts | 157 +++++++++++++++++++++++++++++++------ 2 files changed, 139 insertions(+), 26 deletions(-) diff --git a/angularjs/angular-tests.ts b/angularjs/angular-tests.ts index 4acd18eeb1..419bed0d6f 100644 --- a/angularjs/angular-tests.ts +++ b/angularjs/angular-tests.ts @@ -98,12 +98,12 @@ module HttpAndRegularPromiseTests { } var someController: Function = ($scope: SomeControllerScope, $http: ng.IHttpService, $q: ng.IQService) => { - $http.get("http://somewhere/some/resource") + $http.get("http://somewhere/some/resource") .success((data: ExpectedResponse) => { $scope.person = data; }); - $http.get("http://somewhere/some/resource") + $http.get("http://somewhere/some/resource") .then((response: ng.IHttpPromiseCallbackArg) => { // typing lost, so something like // var i: number = response.data @@ -111,7 +111,7 @@ module HttpAndRegularPromiseTests { $scope.person = response.data; }); - $http.get("http://somewhere/some/resource") + $http.get("http://somewhere/some/resource") .then((response: ng.IHttpPromiseCallbackArg) => { // typing lost, so something like // var i: number = response.data @@ -148,7 +148,7 @@ module HttpAndRegularPromiseTests { var buildFooData: Function = () => 42; var doFoo: Function = (callback: ng.IHttpPromiseCallback) => { - $http.get('/foo', buildFooData()) + $http.get('/foo', buildFooData()) .success(callback); } diff --git a/angularjs/angular.d.ts b/angularjs/angular.d.ts index 1ee9079d3c..f1637fe400 100755 --- a/angularjs/angular.d.ts +++ b/angularjs/angular.d.ts @@ -589,44 +589,157 @@ declare module ng { register(name: string, dependencyAnnotatedConstructor: any[]): void; } - /////////////////////////////////////////////////////////////////////////// - // HttpService - // see http://docs.angularjs.org/api/ng.$http - /////////////////////////////////////////////////////////////////////////// + /** + * HttpService + * see http://docs.angularjs.org/api/ng/service/$http + */ interface IHttpService { - // At least moethod and url must be provided... - (config: IRequestConfig): IHttpPromise; - get (url: string, RequestConfig?: any): IHttpPromise; - delete (url: string, RequestConfig?: any): IHttpPromise; - head(url: string, RequestConfig?: any): IHttpPromise; - jsonp(url: string, RequestConfig?: any): IHttpPromise; - post(url: string, data: any, RequestConfig?: any): IHttpPromise; - put(url: string, data: any, RequestConfig?: any): IHttpPromise; + /** + * Object describing the request to be made and how it should be processed. + */ + (config: IRequestConfig): IHttpPromise; + + /** + * Shortcut method to perform GET request. + * + * @param url Relative or absolute URL specifying the destination of the request + * @param config Optional configuration object + */ + get(url: string, config?: any): IHttpPromise; + + /** + * Shortcut method to perform DELETE request. + * + * @param url Relative or absolute URL specifying the destination of the request + * @param config Optional configuration object + */ + delete(url: string, config?: any): IHttpPromise; + + /** + * Shortcut method to perform HEAD request. + * + * @param url Relative or absolute URL specifying the destination of the request + * @param config Optional configuration object + */ + head(url: string, config?: any): IHttpPromise; + + /** + * Shortcut method to perform JSONP request. + * + * @param url Relative or absolute URL specifying the destination of the request + * @param config Optional configuration object + */ + jsonp(url: string, config?: any): IHttpPromise; + + /** + * Shortcut method to perform POST request. + * + * @param url Relative or absolute URL specifying the destination of the request + * @param data Request content + * @param config Optional configuration object + */ + post(url: string, data: any, config?: any): IHttpPromise; + + /** + * Shortcut method to perform PUT request. + * + * @param url Relative or absolute URL specifying the destination of the request + * @param data Request content + * @param config Optional configuration object + */ + put(url: string, data: any, config?: any): IHttpPromise; + + /** + * Runtime equivalent of the $httpProvider.defaults property. Allows configuration of default headers, withCredentials as well as request and response transformations. + */ defaults: IRequestConfig; - // For debugging, BUT it is documented as public, so... + /** + * Array of config objects for currently pending requests. This is primarily meant to be used for debugging purposes. + */ pendingRequests: any[]; } - // This is just for hinting. - // Some opetions might not be available depending on the request. - // see http://docs.angularjs.org/api/ng.$http#Usage for options explanations - interface IRequestConfig { - method: string; - url: string; + /** + * Object describing the request to be made and how it should be processed. + * see http://docs.angularjs.org/api/ng/service/$http#usage + */ + interface IRequestShortcutConfig { + /** + * {Object.} + * Map of strings or objects which will be turned to ?key1=value1&key2=value2 after the url. If the value is not a string, it will be JSONified. + */ params?: any; - // XXX it has it's own structure... perhaps we should define it in the future + /** + * Map of strings or functions which return strings representing HTTP headers to send to the server. If the return value of a function is null, the header will not be sent. + */ headers?: any; + /** + * Name of HTTP header to populate with the XSRF token. + */ + xsrfHeaderName?: string; + + /** + * Name of cookie containing the XSRF token. + */ + xsrfCookieName?: string; + + /** + * {boolean|Cache} + * If true, a default $http cache will be used to cache the GET request, otherwise if a cache instance built with $cacheFactory, this cache will be used for caching. + */ cache?: any; + + /** + * whether to to set the withCredentials flag on the XHR object. See [requests with credentials]https://developer.mozilla.org/en/http_access_control#section_5 for more information. + */ withCredentials?: boolean; - // These accept multiple types, so let's define them as any + /** + * {string|Object} + * Data to be sent as the request message data. + */ data?: any; + + /** + * {function(data, headersGetter)|Array.} + * Transform function or an array of such functions. The transform function takes the http request body and headers and returns its transformed (typically serialized) version. + */ transformRequest?: any; + + /** + * {function(data, headersGetter)|Array.} + * Transform function or an array of such functions. The transform function takes the http response body and headers and returns its transformed (typically deserialized) version. + */ transformResponse?: any; - timeout?: any; // number | promise + + /** + * {number|Promise} + * Timeout in milliseconds, or promise that should abort the request when resolved. + */ + timeout?: any; + + /** + * See requestType. + */ + responseType?: string; + } + + /** + * Object describing the request to be made and how it should be processed. + * see http://docs.angularjs.org/api/ng/service/$http#usage + */ + interface IRequestConfig extends IRequestShortcutConfig { + /** + * HTTP method (e.g. 'GET', 'POST', etc) + */ + method: string; + /** + * Absolute or relative URL of the resource that is being requested. + */ + url: string; } interface IHttpPromiseCallback {