UNPKG

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