-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathVsoClient.ts
318 lines (276 loc) · 12.8 KB
/
VsoClient.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//*******************************************************************************************************
// significant portions of this file copied from: VSO\src\Vssf\WebPlatform\Platform\Scripts\VSS\WebApi\RestClient.ts
//*******************************************************************************************************
/// Imports of 3rd Party ///
import url = require("url");
import path = require("path");
/// Import base rest class ///
import * as restm from 'typed-rest-client/RestClient';
import ifm = require("./interfaces/common/VsoBaseInterfaces");
interface VssApiResourceLocationLookup {
[locationId: string]: ifm.ApiResourceLocation;
}
export interface ClientVersioningData {
/**
* The api version string to send in the request (e.g. "1.0" or "2.0-preview.2")
*/
apiVersion?: string;
/**
* The request path string to send the request to. Looked up via an options request with the location id.
*/
requestUrl?: string;
}
export class InvalidApiResourceVersionError implements Error {
public name: string = "Invalid resource version";
public message: string;
constructor(message?: string) {
this.message = message;
}
}
/**
* Base class that should be used (derived from) to make requests to VSS REST apis
*/
export class VsoClient {
private static APIS_RELATIVE_PATH = "_apis";
private static PREVIEW_INDICATOR = "-preview.";
private _locationsByAreaPromises: { [areaName: string]: Promise<VssApiResourceLocationLookup>; };
private _initializationPromise: Promise<any>;
restClient: restm.RestClient;
baseUrl: string;
basePath: string;
constructor(baseUrl: string, restClient: restm.RestClient) {
this.baseUrl = baseUrl;
this.basePath = url.parse(baseUrl).pathname;
this.restClient = restClient;
this._locationsByAreaPromises = {};
this._initializationPromise = Promise.resolve(true);
}
protected autoNegotiateApiVersion(location: ifm.ApiResourceLocation, requestedVersion: string): string {
let negotiatedVersion: string;
let apiVersion: number;
let apiVersionString: string;
if (requestedVersion) {
let apiVersionRegEx = new RegExp('(\\d+(\\.\\d+)?)(-preview(\\.(\\d+))?)?');
// Need to handle 3 types of api versions + invalid apiversion
// '2.1-preview.1' = ["2.1-preview.1", "2.1", ".1", -preview.1", ".1", "1"]
// '2.1-preview' = ["2.1-preview", "2.1", ".1", "-preview", undefined, undefined]
// '2.1' = ["2.1", "2.1", ".1", undefined, undefined, undefined]
let isPreview = false;
let resourceVersion: number;
let regExExecArray = apiVersionRegEx.exec(requestedVersion);
if (regExExecArray) {
if (regExExecArray[1]) {
// we have an api version
apiVersion = +regExExecArray[1];
apiVersionString = regExExecArray[1];
if (regExExecArray[3]) {
// requesting preview
isPreview = true;
if (regExExecArray[5]) {
// we have a resource version
resourceVersion = +regExExecArray[5];
}
}
// compare the location version and requestedversion
if (apiVersion <= +location.releasedVersion
|| (!resourceVersion && apiVersion <= +location.maxVersion && isPreview)
|| (resourceVersion && apiVersion <= +location.maxVersion && resourceVersion <= +location.resourceVersion)) {
negotiatedVersion = requestedVersion;
}
// else fall back to latest version of the resource from location
}
}
}
if (!negotiatedVersion) {
// Use the latest version of the resource if the api version was not specified in the request or if the requested version is higher then the location's supported version
if (apiVersion < +location.maxVersion) {
negotiatedVersion = apiVersionString + "-preview";
}
else if (location.maxVersion === location.releasedVersion) {
negotiatedVersion = location.maxVersion;
}
else {
negotiatedVersion = location.maxVersion + "-preview." + location.resourceVersion;
}
}
return negotiatedVersion;
}
/**
* Gets the route template for a resource based on its location ID and negotiates the api version
*/
public getVersioningData(apiVersion: string, area: string, locationId: string, routeValues: any, queryParams?: any): Promise<ClientVersioningData> {
let requestUrl;
return this.beginGetLocation(area, locationId)
.then((location: ifm.ApiResourceLocation): ClientVersioningData => {
if (!location) {
throw new Error("Failed to find api location for area: " + area + " id: " + locationId);
}
apiVersion = this.autoNegotiateApiVersion(location, apiVersion);
requestUrl = this.getRequestUrl(location.routeTemplate, location.area, location.resourceName, routeValues, queryParams);
return {
apiVersion: apiVersion,
requestUrl: requestUrl
};
});
}
/**
* Sets a promise that is waited on before any requests are issued. Can be used to asynchronously
* set the request url and auth token manager.
*/
public _setInitializationPromise(promise: Promise<any>) {
if (promise) {
this._initializationPromise = promise;
}
}
/**
* Gets information about an API resource location (route template, supported versions, etc.)
*
* @param area resource area name
* @param locationId Guid of the location to get
*/
public beginGetLocation(area: string, locationId: string): Promise<ifm.ApiResourceLocation> {
return this._initializationPromise.then(() => {
return this.beginGetAreaLocations(area);
}).then((areaLocations: VssApiResourceLocationLookup) => {
return areaLocations[(locationId || "").toLowerCase()];
});
}
private beginGetAreaLocations(area: string): Promise<VssApiResourceLocationLookup> {
let areaLocationsPromise = this._locationsByAreaPromises[area];
if (!areaLocationsPromise) {
let requestUrl = this.resolveUrl(VsoClient.APIS_RELATIVE_PATH + "/" + area);
areaLocationsPromise = this.restClient.options<any>(requestUrl)
.then((res:restm.IRestResponse<any>) => {
let locationsLookup: VssApiResourceLocationLookup = {};
let resourceLocations: ifm.ApiResourceLocation[] = res.result.value;
let i;
for (i = 0; i < resourceLocations.length; i++) {
let resourceLocation = resourceLocations[i];
locationsLookup[resourceLocation.id.toLowerCase()] = resourceLocation;
}
// If we have completed successfully, cache the response.
this._locationsByAreaPromises[area] = areaLocationsPromise;
return locationsLookup;
});
}
return areaLocationsPromise;
}
public resolveUrl(relativeUrl: string): string {
return url.resolve(this.baseUrl, path.join(this.basePath, relativeUrl));
}
private queryParamsToStringHelper(queryParams: any, prefix: string): string {
if (queryParams == null || queryParams.length === 0) {
return '';
}
let queryString: string = '';
if (typeof(queryParams) !== 'string' && !Array.isArray(queryParams)) {
for (let property in queryParams) {
if (queryParams.hasOwnProperty(property)) {
const prop = queryParams[property];
const newPrefix = prefix + encodeURIComponent(property.toString()) + '.';
queryString += this.queryParamsToStringHelper(prop, newPrefix);
}
}
}
if (queryString === '' && prefix.length > 0){
// Date.prototype.toString() returns a string that is not valid for the REST API.
// Need to specially call `toUTCString()` instead for such cases
const queryValue = typeof queryParams === 'object' && 'toUTCString' in queryParams ? (queryParams as Date).toUTCString() : queryParams.toString();
// Will always need to chop period off of end of prefix
queryString = prefix.slice(0,-1) + '=' + encodeURIComponent(queryValue) + '&';
}
return queryString;
}
private queryParamsToString(queryParams: any): string {
const queryString: string = '?' + this.queryParamsToStringHelper(queryParams, '');
// Will always need to slice either a ? or & off of the end
return queryString.slice(0,-1);
}
protected getRequestUrl(routeTemplate: string, area: string, resource: string, routeValues: any, queryParams?: any): string {
// Add area/resource route values (based on the location)
routeValues = routeValues || {};
if (!routeValues.area) {
routeValues.area = area;
}
if (!routeValues.resource) {
routeValues.resource = resource;
}
// Replace templated route values
let relativeUrl = this.replaceRouteValues(routeTemplate, routeValues);
// Append query parameters to the end
if (queryParams) {
relativeUrl += this.queryParamsToString(queryParams);
}
// Resolve the relative url with the base
return url.resolve(this.baseUrl, path.join(this.basePath, relativeUrl));
}
// helper method copied directly from VSS\WebAPI\restclient.ts
private replaceRouteValues(routeTemplate: string, routeValues: any): string {
let result = "",
currentPathPart = "",
paramName = "",
insideParam = false,
charIndex: number,
routeTemplateLength = routeTemplate.length,
c: string;
for (charIndex = 0; charIndex < routeTemplateLength; charIndex++) {
c = routeTemplate[charIndex];
if (insideParam) {
if (c == "}") {
insideParam = false;
if (routeValues[paramName]) {
currentPathPart += encodeURIComponent(routeValues[paramName]);
} else {
// Normalize param name in order to capture wild-card routes
let strippedParamName = paramName.replace(/[^a-z0-9]/ig, '');
if (routeValues[strippedParamName]) {
currentPathPart += encodeURIComponent(routeValues[strippedParamName]);
}
}
paramName = "";
}
else {
paramName += c;
}
}
else {
if (c == "/") {
if (currentPathPart) {
if (result) {
result += "/";
}
result += currentPathPart;
currentPathPart = "";
}
}
else if (c == "{") {
if ((charIndex + 1) < routeTemplateLength && routeTemplate[charIndex + 1] == "{") {
// Escaped '{'
currentPathPart += c;
charIndex++;
}
else {
insideParam = true;
}
}
else if (c == '}') {
currentPathPart += c;
if ((charIndex + 1) < routeTemplateLength && routeTemplate[charIndex + 1] == "}") {
// Escaped '}'
charIndex++;
}
}
else {
currentPathPart += c;
}
}
}
if (currentPathPart) {
if (result) {
result += "/";
}
result += currentPathPart;
}
return result;
}
}