Skip to content

Commit fa534ae

Browse files
authored
Fix beginGetAreaLocations (#567)
* Add null check + more clear type * Add test * Bump version to 12.1.1
1 parent 55908ae commit fa534ae

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

api/VsoClient.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class VsoClient {
149149
* @param area resource area name
150150
* @param locationId Guid of the location to get
151151
*/
152-
public beginGetLocation(area: string, locationId: string): Promise<ifm.ApiResourceLocation> {
152+
public beginGetLocation(area: string, locationId: string): Promise<ifm.ApiResourceLocation | undefined> {
153153
return this._initializationPromise.then(() => {
154154
return this.beginGetAreaLocations(area);
155155
}).then((areaLocations: VssApiResourceLocationLookup) => {
@@ -162,7 +162,11 @@ export class VsoClient {
162162
if (!areaLocationsPromise) {
163163
let requestUrl = this.resolveUrl(VsoClient.APIS_RELATIVE_PATH + "/" + area);
164164
areaLocationsPromise = this.restClient.options<any>(requestUrl)
165-
.then((res:restm.IRestResponse<any>) => {
165+
.then((res: restm.IRestResponse<any>) => {
166+
if (!res.result) {
167+
return {};
168+
}
169+
166170
let locationsLookup: VssApiResourceLocationLookup = {};
167171
let resourceLocations: ifm.ApiResourceLocation[] = res.result.value;
168172
let i;
@@ -190,7 +194,7 @@ export class VsoClient {
190194
}
191195
let queryString: string = '';
192196

193-
if (typeof(queryParams) !== 'string') {
197+
if (typeof (queryParams) !== 'string') {
194198
for (let property in queryParams) {
195199
if (queryParams.hasOwnProperty(property)) {
196200
const prop = queryParams[property];
@@ -200,14 +204,14 @@ export class VsoClient {
200204
}
201205
}
202206

203-
if (queryString === '' && prefix.length > 0){
207+
if (queryString === '' && prefix.length > 0) {
204208
// Date.prototype.toString() returns a string that is not valid for the REST API.
205209
// Need to specially call `toUTCString()` instead for such cases
206210
const queryValue = typeof queryParams === 'object' && 'toUTCString' in queryParams ? (queryParams as Date).toUTCString() : queryParams.toString();
207211

208212

209213
// Will always need to chop period off of end of prefix
210-
queryString = prefix.slice(0,-1) + '=' + encodeURIComponent(queryValue) + '&';
214+
queryString = prefix.slice(0, -1) + '=' + encodeURIComponent(queryValue) + '&';
211215
}
212216
return queryString;
213217
}
@@ -216,7 +220,7 @@ export class VsoClient {
216220
const queryString: string = '?' + this.queryParamsToStringHelper(queryParams, '');
217221

218222
// Will always need to slice either a ? or & off of the end
219-
return queryString.slice(0,-1);
223+
return queryString.slice(0, -1);
220224
}
221225

222226
protected getRequestUrl(routeTemplate: string, area: string, resource: string, routeValues: any, queryParams?: any): string {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "azure-devops-node-api",
33
"description": "Node client for Azure DevOps and TFS REST APIs",
4-
"version": "12.1.0",
4+
"version": "12.1.1",
55
"main": "./WebApi.js",
66
"types": "./WebApi.d.ts",
77
"scripts": {

test/units/tests.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ describe('VSOClient Units', function () {
228228
//Assert
229229
assert(res.id === "testLocation");
230230
});
231+
232+
it('Returns \'undefined\' when the location is not found', async () => {
233+
nock('https://dev.azure.com/_apis/testArea8', {
234+
//Arrange
235+
reqheaders: {
236+
'accept': 'application/json',
237+
'user-agent': 'testAgent'
238+
}})
239+
.options('')
240+
.reply(404, 'Not Found"');
241+
242+
//Act
243+
const res = await vsoClient.beginGetLocation('testArea8', 'testLocation');
244+
245+
//Assert
246+
assert(res === undefined);
247+
})
231248
});
232249

233250
describe('WebApi Units', function () {

0 commit comments

Comments
 (0)