From fa534aef7d79ab4a30ae2b8823654795b6eed1aa Mon Sep 17 00:00:00 2001 From: Konstantin Tyukalov <52399739+KonstantinTyukalov@users.noreply.github.com> Date: Tue, 7 Nov 2023 20:20:09 +0400 Subject: [PATCH] Fix `beginGetAreaLocations` (#567) * Add null check + more clear type * Add test * Bump version to 12.1.1 --- api/VsoClient.ts | 16 ++++++++++------ package-lock.json | 2 +- package.json | 2 +- test/units/tests.ts | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/api/VsoClient.ts b/api/VsoClient.ts index eefca52..4370024 100644 --- a/api/VsoClient.ts +++ b/api/VsoClient.ts @@ -149,7 +149,7 @@ export class VsoClient { * @param area resource area name * @param locationId Guid of the location to get */ - public beginGetLocation(area: string, locationId: string): Promise { + public beginGetLocation(area: string, locationId: string): Promise { return this._initializationPromise.then(() => { return this.beginGetAreaLocations(area); }).then((areaLocations: VssApiResourceLocationLookup) => { @@ -162,7 +162,11 @@ export class VsoClient { if (!areaLocationsPromise) { let requestUrl = this.resolveUrl(VsoClient.APIS_RELATIVE_PATH + "/" + area); areaLocationsPromise = this.restClient.options(requestUrl) - .then((res:restm.IRestResponse) => { + .then((res: restm.IRestResponse) => { + if (!res.result) { + return {}; + } + let locationsLookup: VssApiResourceLocationLookup = {}; let resourceLocations: ifm.ApiResourceLocation[] = res.result.value; let i; @@ -190,7 +194,7 @@ export class VsoClient { } let queryString: string = ''; - if (typeof(queryParams) !== 'string') { + if (typeof (queryParams) !== 'string') { for (let property in queryParams) { if (queryParams.hasOwnProperty(property)) { const prop = queryParams[property]; @@ -200,14 +204,14 @@ export class VsoClient { } } - if (queryString === '' && prefix.length > 0){ + 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) + '&'; + queryString = prefix.slice(0, -1) + '=' + encodeURIComponent(queryValue) + '&'; } return queryString; } @@ -216,7 +220,7 @@ export class VsoClient { const queryString: string = '?' + this.queryParamsToStringHelper(queryParams, ''); // Will always need to slice either a ? or & off of the end - return queryString.slice(0,-1); + return queryString.slice(0, -1); } protected getRequestUrl(routeTemplate: string, area: string, resource: string, routeValues: any, queryParams?: any): string { diff --git a/package-lock.json b/package-lock.json index 68992bc..4aaee44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "azure-devops-node-api", - "version": "12.0.0", + "version": "12.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 136798f..667c3c5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "azure-devops-node-api", "description": "Node client for Azure DevOps and TFS REST APIs", - "version": "12.1.0", + "version": "12.1.1", "main": "./WebApi.js", "types": "./WebApi.d.ts", "scripts": { diff --git a/test/units/tests.ts b/test/units/tests.ts index a4062a5..923293f 100644 --- a/test/units/tests.ts +++ b/test/units/tests.ts @@ -228,6 +228,23 @@ describe('VSOClient Units', function () { //Assert assert(res.id === "testLocation"); }); + + it('Returns \'undefined\' when the location is not found', async () => { + nock('https://dev.azure.com/_apis/testArea8', { + //Arrange + reqheaders: { + 'accept': 'application/json', + 'user-agent': 'testAgent' + }}) + .options('') + .reply(404, 'Not Found"'); + + //Act + const res = await vsoClient.beginGetLocation('testArea8', 'testLocation'); + + //Assert + assert(res === undefined); + }) }); describe('WebApi Units', function () {