forked from DefinitelyTyped/DefinitelyTyped
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-resource-tests.ts
234 lines (201 loc) · 9.52 KB
/
angular-resource-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Examples of working with resource, previously from the angular.d.ts. readme
// Working with $resource
// We have the option to define arguments for a custom resource
interface IArticleParameters {
id: number;
}
interface IArticleResource extends ng.resource.IResource<IArticleResource> {
title: string;
text: string;
date: Date;
author: number;
// Although all actions defined on IArticleResourceClass are available with
// the '$' prefix, we have the choice to expose only what we will use
$publish(): IArticleResource;
$unpublish(): IArticleResource;
}
// Let's define a custom resource
interface IArticleResourceClass extends ng.resource.IResourceClass<IArticleResource> {
// Overload get to accept our custom parameters
get(): IArticleResource;
get(params: IArticleParameters, onSuccess: Function): IArticleResource;
// Add our custom resource actions
publish(params?: IArticleParameters): IArticleResource;
unpublish(params: IArticleParameters): IArticleResource;
}
function MainController($resource: ng.resource.IResourceService): void {
// IntelliSense will provide IActionDescriptor interface and will validate
// your assignment against it
const publishDescriptor: ng.resource.IActionDescriptor = {
method: "GET",
isArray: false,
};
// A call to the $resource service returns a IResourceClass. Since
// our own IArticleResourceClass defines 2 more actions, we cast the return
// value to make the compiler aware of that
const articleResource: IArticleResourceClass = $resource<IArticleResource, IArticleResourceClass>(
"/articles/:id",
null,
{
publish: publishDescriptor,
unpublish: {
method: "POST",
},
},
);
// Now we can do this
articleResource.unpublish({ id: 1 });
// IResourceClass.get() will be automatically available here
const article: IArticleResource = articleResource.get({ id: 1 }, function success(): void {
// Again, default + custom action here...
article.title = "New Title";
article.$save();
article.$publish();
});
}
import IHttpResponse = angular.IHttpResponse;
interface IMyData {}
interface IMyResource extends angular.resource.IResource<IMyResource> {}
interface IMyResourceClass extends angular.resource.IResourceClass<IMyResource> {}
///////////////////////////////////////
// IActionDescriptor
///////////////////////////////////////
let actionDescriptor: angular.resource.IActionDescriptor;
angular.injector(["ng"]).invoke(function($cacheFactory: angular.ICacheFactoryService) {
actionDescriptor.method = "method action";
actionDescriptor.params = { key: "value" };
actionDescriptor.url = "/api/test-url/";
actionDescriptor.isArray = true;
actionDescriptor.transformRequest = function() {};
actionDescriptor.transformRequest = [function() {}];
actionDescriptor.transformResponse = function() {};
actionDescriptor.transformResponse = [function() {}];
actionDescriptor.headers = { header: "value" };
actionDescriptor.cache = true;
actionDescriptor.cache = $cacheFactory("cacheId");
actionDescriptor.timeout = 1000;
actionDescriptor.withCredentials = true;
actionDescriptor.responseType = "response type";
actionDescriptor.interceptor = {
response() {
return {} as IHttpResponse<IMyData>;
},
responseError() {},
};
actionDescriptor.cancellable = true;
actionDescriptor.hasBody = true;
});
///////////////////////////////////////
// IResourceClass
///////////////////////////////////////
let resourceClass: IMyResourceClass;
let resource: IMyResource;
let resourceArray: angular.resource.IResourceArray<IMyResource>;
resource = resourceClass.delete();
resource = resourceClass.delete({ key: "value" });
resource = resourceClass.delete({ key: "value" }, function() {});
resource = resourceClass.delete(function() {});
resource = resourceClass.delete(function() {}, function() {});
resource = resourceClass.delete({ key: "value" }, { key: "value" });
resource = resourceClass.delete({ key: "value" }, { key: "value" }, function() {});
resource = resourceClass.delete({ key: "value" }, { key: "value" }, function() {}, function() {});
resource.$promise.then(function(data: IMyResource) {});
resource.$cancelRequest();
resource = resourceClass.get();
resource = resourceClass.get({ key: "value" });
resource = resourceClass.get({ key: "value" }, function() {});
resource = resourceClass.get(function() {});
resource = resourceClass.get(function() {}, function() {});
resource = resourceClass.get({ key: "value" }, { key: "value" });
resource = resourceClass.get({ key: "value" }, { key: "value" }, function() {});
resource = resourceClass.get({ key: "value" }, { key: "value" }, function() {}, function() {});
resourceArray = resourceClass.query();
resourceArray = resourceClass.query({ key: "value" });
resourceArray = resourceClass.query({ key: "value" }, function() {});
resourceArray = resourceClass.query(function() {});
resourceArray = resourceClass.query(function() {}, function() {});
resourceArray = resourceClass.query({ key: "value" }, { key: "value" });
resourceArray = resourceClass.query({ key: "value" }, { key: "value" }, function() {});
resourceArray = resourceClass.query({ key: "value" }, { key: "value" }, function() {}, function() {});
resourceArray.push(resource);
resourceArray.$promise.then(function(data: angular.resource.IResourceArray<IMyResource>) {});
resource = resourceClass.remove();
resource = resourceClass.remove({ key: "value" });
resource = resourceClass.remove({ key: "value" }, function() {});
resource = resourceClass.remove(function() {});
resource = resourceClass.remove(function() {}, function() {});
resource = resourceClass.remove({ key: "value" }, { key: "value" });
resource = resourceClass.remove({ key: "value" }, { key: "value" }, function() {});
resource = resourceClass.remove({ key: "value" }, { key: "value" }, function() {}, function() {});
resource = resourceClass.save();
resource = resourceClass.save({ key: "value" });
resource = resourceClass.save({ key: "value" }, function() {});
resource = resourceClass.save(function() {});
resource = resourceClass.save(function() {}, function() {});
resource = resourceClass.save({ key: "value" }, { key: "value" });
resource = resourceClass.save({ key: "value" }, { key: "value" }, function() {});
resource = resourceClass.save({ key: "value" }, { key: "value" }, function() {}, function() {});
///////////////////////////////////////
// IResource
///////////////////////////////////////
let promise: angular.IPromise<IMyResource>;
let arrayPromise: angular.IPromise<IMyResource[]>;
let json: IMyResource;
promise = resource.$delete();
promise = resource.$delete({ key: "value" });
promise = resource.$delete({ key: "value" }, function() {});
promise = resource.$delete(function() {});
promise = resource.$delete(function() {}, function() {});
promise = resource.$delete({ key: "value" }, function() {}, function() {});
promise.then(function(data: IMyResource) {});
promise = resource.$get();
promise = resource.$get({ key: "value" });
promise = resource.$get({ key: "value" }, function() {});
promise = resource.$get(function() {});
promise = resource.$get(function() {}, function() {});
promise = resource.$get({ key: "value" }, function() {}, function() {});
arrayPromise = resourceArray[0].$query();
arrayPromise = resourceArray[0].$query({ key: "value" });
arrayPromise = resourceArray[0].$query({ key: "value" }, function() {});
arrayPromise = resourceArray[0].$query(function() {});
arrayPromise = resourceArray[0].$query(function() {}, function() {});
arrayPromise = resourceArray[0].$query({ key: "value" }, function() {}, function() {});
arrayPromise.then(function(data: angular.resource.IResourceArray<IMyResource>) {});
promise = resource.$remove();
promise = resource.$remove({ key: "value" });
promise = resource.$remove({ key: "value" }, function() {});
promise = resource.$remove(function() {});
promise = resource.$remove(function() {}, function() {});
promise = resource.$remove({ key: "value" }, function() {}, function() {});
promise = resource.$save();
promise = resource.$save({ key: "value" });
promise = resource.$save({ key: "value" }, function() {});
promise = resource.$save(function() {});
promise = resource.$save(function() {}, function() {});
promise = resource.$save({ key: "value" }, function() {}, function() {});
json = resource.toJSON();
///////////////////////////////////////
// IResourceService
///////////////////////////////////////
let resourceService: angular.resource.IResourceService;
resourceClass = resourceService<IMyResource, IMyResourceClass>("test");
resourceClass = resourceService<IMyResource>("test");
resourceClass = resourceService("test");
///////////////////////////////////////
// IModule
///////////////////////////////////////
let mod: ng.IModule;
let resourceServiceFactoryFunction: angular.resource.IResourceServiceFactoryFunction<IMyResource>;
resourceClass = resourceServiceFactoryFunction<IMyResourceClass>(resourceService);
resourceServiceFactoryFunction = function(resourceService: angular.resource.IResourceService) {
return resourceClass as any;
};
mod = mod.factory("factory name", resourceServiceFactoryFunction);
///////////////////////////////////////
// IResource
///////////////////////////////////////
///////////////////////////////////////
// IResourceServiceProvider
///////////////////////////////////////
let resourceServiceProvider: angular.resource.IResourceServiceProvider;
resourceServiceProvider.defaults.stripTrailingSlashes = false;