Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | // Generated by typings |
| 2 | // Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/19854af46f2bcd37cf416341ce74ef03ab1717b9/angularjs/angular-resource.d.ts |
| 3 | declare module 'angular-resource' { |
| 4 | var _: string; |
| 5 | export = _; |
| 6 | } |
| 7 | |
| 8 | /////////////////////////////////////////////////////////////////////////////// |
| 9 | // ngResource module (angular-resource.js) |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | declare namespace angular.resource { |
| 12 | |
| 13 | /** |
| 14 | * Currently supported options for the $resource factory options argument. |
| 15 | */ |
| 16 | interface IResourceOptions { |
| 17 | /** |
| 18 | * If true then the trailing slashes from any calculated URL will be stripped (defaults to true) |
| 19 | */ |
| 20 | stripTrailingSlashes?: boolean; |
| 21 | /** |
| 22 | * If true, the request made by a "non-instance" call will be cancelled (if not already completed) by calling |
| 23 | * $cancelRequest() on the call's return value. This can be overwritten per action. (Defaults to false.) |
| 24 | */ |
| 25 | cancellable?: boolean; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /////////////////////////////////////////////////////////////////////////// |
| 30 | // ResourceService |
| 31 | // see http://docs.angularjs.org/api/ngResource.$resource |
| 32 | // Most part of the following definitions were achieved by analyzing the |
| 33 | // actual implementation, since the documentation doesn't seem to cover |
| 34 | // that deeply. |
| 35 | /////////////////////////////////////////////////////////////////////////// |
| 36 | interface IResourceService { |
| 37 | (url: string, paramDefaults?: any, |
| 38 | /** example: {update: { method: 'PUT' }, delete: deleteDescriptor } |
| 39 | where deleteDescriptor : IActionDescriptor */ |
| 40 | actions?: IActionHash, options?: IResourceOptions): IResourceClass<IResource<any>>; |
| 41 | <T, U>(url: string, paramDefaults?: any, |
| 42 | /** example: {update: { method: 'PUT' }, delete: deleteDescriptor } |
| 43 | where deleteDescriptor : IActionDescriptor */ |
| 44 | actions?: IActionHash, options?: IResourceOptions): U; |
| 45 | <T>(url: string, paramDefaults?: any, |
| 46 | /** example: {update: { method: 'PUT' }, delete: deleteDescriptor } |
| 47 | where deleteDescriptor : IActionDescriptor */ |
| 48 | actions?: IActionHash, options?: IResourceOptions): IResourceClass<T>; |
| 49 | } |
| 50 | |
| 51 | // Hash of action descriptors allows custom action names |
| 52 | interface IActionHash { |
| 53 | [action: string]: IActionDescriptor |
| 54 | } |
| 55 | |
| 56 | // Just a reference to facilitate describing new actions |
| 57 | interface IActionDescriptor { |
| 58 | method: string; |
| 59 | params?: any; |
| 60 | url?: string; |
| 61 | isArray?: boolean; |
| 62 | transformRequest?: angular.IHttpRequestTransformer | angular.IHttpRequestTransformer[]; |
| 63 | transformResponse?: angular.IHttpResponseTransformer | angular.IHttpResponseTransformer[]; |
| 64 | headers?: any; |
| 65 | cache?: boolean | angular.ICacheObject; |
| 66 | /** |
| 67 | * Note: In contrast to $http.config, promises are not supported in $resource, because the same value |
| 68 | * would be used for multiple requests. If you are looking for a way to cancel requests, you should |
| 69 | * use the cancellable option. |
| 70 | */ |
| 71 | timeout?: number |
| 72 | cancellable?: boolean; |
| 73 | withCredentials?: boolean; |
| 74 | responseType?: string; |
| 75 | interceptor?: IHttpInterceptor; |
| 76 | } |
| 77 | |
| 78 | // Allow specify more resource methods |
| 79 | // No need to add duplicates for all four overloads. |
| 80 | interface IResourceMethod<T> { |
| 81 | (): T; |
| 82 | (params: Object): T; |
| 83 | (success: Function, error?: Function): T; |
| 84 | (params: Object, success: Function, error?: Function): T; |
| 85 | (params: Object, data: Object, success?: Function, error?: Function): T; |
| 86 | } |
| 87 | |
| 88 | // Allow specify resource moethod which returns the array |
| 89 | // No need to add duplicates for all four overloads. |
| 90 | interface IResourceArrayMethod<T> { |
| 91 | (): IResourceArray<T>; |
| 92 | (params: Object): IResourceArray<T>; |
| 93 | (success: Function, error?: Function): IResourceArray<T>; |
| 94 | (params: Object, success: Function, error?: Function): IResourceArray<T>; |
| 95 | (params: Object, data: Object, success?: Function, error?: Function): IResourceArray<T>; |
| 96 | } |
| 97 | |
| 98 | // Baseclass for every resource with default actions. |
| 99 | // If you define your new actions for the resource, you will need |
| 100 | // to extend this interface and typecast the ResourceClass to it. |
| 101 | // |
| 102 | // In case of passing the first argument as anything but a function, |
| 103 | // it's gonna be considered data if the action method is POST, PUT or |
| 104 | // PATCH (in other words, methods with body). Otherwise, it's going |
| 105 | // to be considered as parameters to the request. |
| 106 | // https://github.com/angular/angular.js/blob/v1.2.0/src/ngResource/resource.js#L461-L465 |
| 107 | // |
| 108 | // Only those methods with an HTTP body do have 'data' as first parameter: |
| 109 | // https://github.com/angular/angular.js/blob/v1.2.0/src/ngResource/resource.js#L463 |
| 110 | // More specifically, those methods are POST, PUT and PATCH: |
| 111 | // https://github.com/angular/angular.js/blob/v1.2.0/src/ngResource/resource.js#L432 |
| 112 | // |
| 113 | // Also, static calls always return the IResource (or IResourceArray) retrieved |
| 114 | // https://github.com/angular/angular.js/blob/v1.2.0/src/ngResource/resource.js#L538-L549 |
| 115 | interface IResourceClass<T> { |
| 116 | new(dataOrParams? : any) : T & IResource<T>; |
| 117 | get: IResourceMethod<T>; |
| 118 | |
| 119 | query: IResourceArrayMethod<T>; |
| 120 | |
| 121 | save: IResourceMethod<T>; |
| 122 | |
| 123 | remove: IResourceMethod<T>; |
| 124 | |
| 125 | delete: IResourceMethod<T>; |
| 126 | } |
| 127 | |
| 128 | // Instance calls always return the the promise of the request which retrieved the object |
| 129 | // https://github.com/angular/angular.js/blob/v1.2.0/src/ngResource/resource.js#L538-L546 |
| 130 | interface IResource<T> { |
| 131 | $get(): angular.IPromise<T>; |
| 132 | $get(params?: Object, success?: Function, error?: Function): angular.IPromise<T>; |
| 133 | $get(success: Function, error?: Function): angular.IPromise<T>; |
| 134 | |
| 135 | $query(): angular.IPromise<IResourceArray<T>>; |
| 136 | $query(params?: Object, success?: Function, error?: Function): angular.IPromise<IResourceArray<T>>; |
| 137 | $query(success: Function, error?: Function): angular.IPromise<IResourceArray<T>>; |
| 138 | |
| 139 | $save(): angular.IPromise<T>; |
| 140 | $save(params?: Object, success?: Function, error?: Function): angular.IPromise<T>; |
| 141 | $save(success: Function, error?: Function): angular.IPromise<T>; |
| 142 | |
| 143 | $remove(): angular.IPromise<T>; |
| 144 | $remove(params?: Object, success?: Function, error?: Function): angular.IPromise<T>; |
| 145 | $remove(success: Function, error?: Function): angular.IPromise<T>; |
| 146 | |
| 147 | $delete(): angular.IPromise<T>; |
| 148 | $delete(params?: Object, success?: Function, error?: Function): angular.IPromise<T>; |
| 149 | $delete(success: Function, error?: Function): angular.IPromise<T>; |
| 150 | |
| 151 | $cancelRequest(): void; |
| 152 | |
| 153 | /** the promise of the original server interaction that created this instance. **/ |
| 154 | $promise : angular.IPromise<T>; |
| 155 | $resolved : boolean; |
| 156 | toJSON(): T; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Really just a regular Array object with $promise and $resolve attached to it |
| 161 | */ |
| 162 | interface IResourceArray<T> extends Array<T & IResource<T>> { |
| 163 | $cancelRequest(): void; |
| 164 | |
| 165 | /** the promise of the original server interaction that created this collection. **/ |
| 166 | $promise : angular.IPromise<IResourceArray<T>>; |
| 167 | $resolved : boolean; |
| 168 | } |
| 169 | |
| 170 | /** when creating a resource factory via IModule.factory */ |
| 171 | interface IResourceServiceFactoryFunction<T> { |
| 172 | ($resource: angular.resource.IResourceService): IResourceClass<T>; |
| 173 | <U extends IResourceClass<T>>($resource: angular.resource.IResourceService): U; |
| 174 | } |
| 175 | |
| 176 | // IResourceServiceProvider used to configure global settings |
| 177 | interface IResourceServiceProvider extends angular.IServiceProvider { |
| 178 | |
| 179 | defaults: IResourceOptions; |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
| 184 | /** extensions to base ng based on using angular-resource */ |
| 185 | declare namespace angular { |
| 186 | |
| 187 | interface IModule { |
| 188 | /** creating a resource service factory */ |
| 189 | factory(name: string, resourceServiceFactoryFunction: angular.resource.IResourceServiceFactoryFunction<any>): IModule; |
| 190 | } |
| 191 | |
| 192 | namespace auto { |
| 193 | interface IInjectorService { |
| 194 | get(name: '$resource'): ng.resource.IResourceService; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | interface Array<T> |
| 200 | { |
| 201 | /** the promise of the original server interaction that created this collection. **/ |
| 202 | $promise : angular.IPromise<Array<T>>; |
| 203 | $resolved : boolean; |
| 204 | } |