Skip to content

Commit

Permalink
Fix beginGetAreaLocations (#567)
Browse files Browse the repository at this point in the history
* Add null check + more clear type

* Add test

* Bump version to 12.1.1
  • Loading branch information
KonstantinTyukalov committed Nov 7, 2023
1 parent 55908ae commit fa534ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
16 changes: 10 additions & 6 deletions api/VsoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ifm.ApiResourceLocation> {
public beginGetLocation(area: string, locationId: string): Promise<ifm.ApiResourceLocation | undefined> {
return this._initializationPromise.then(() => {
return this.beginGetAreaLocations(area);
}).then((areaLocations: VssApiResourceLocationLookup) => {
Expand All @@ -162,7 +162,11 @@ export class VsoClient {
if (!areaLocationsPromise) {
let requestUrl = this.resolveUrl(VsoClient.APIS_RELATIVE_PATH + "/" + area);
areaLocationsPromise = this.restClient.options<any>(requestUrl)
.then((res:restm.IRestResponse<any>) => {
.then((res: restm.IRestResponse<any>) => {
if (!res.result) {
return {};
}

let locationsLookup: VssApiResourceLocationLookup = {};
let resourceLocations: ifm.ApiResourceLocation[] = res.result.value;
let i;
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
17 changes: 17 additions & 0 deletions test/units/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit fa534ae

Please sign in to comment.