UNPKG

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