Skip to content

Commit

Permalink
feat(UA-9179): add properties endpoints to client (#839)
Browse files Browse the repository at this point in the history
* feat(UA-9179): add properties endpoints to client

* feat(UA-9179): add filter to list endpoint + apply review

* feat(UA-9179): fix path to BaseInterfaces

* feat(UA-9179): fix import path (again)
  • Loading branch information
aperron-coveo authored Aug 22, 2024
1 parent bd581dc commit b744a5b
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/resources/AnalyticsAdmin/Properties/Properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Resource from '../../Resource.js';
import API from '../../../APICore.js';
import {PageModel} from '../../BaseInterfaces.js';
import {ListPropertiesParams, PropertiesResponseMessage, PropertyModel} from './PropertiesInterfaces.js';

export default class Properties extends Resource {
static baseUrl = `/rest/organizations/${API.orgPlaceholder}/analyticsadmin/v1`;
/**
* Build the request path, handling the optional `org` query parameter.
*
* @param route The path part of the request.
* @param queryParams Optional query parameters object.
* If this object contains an `org` property, it will override the value from the configuration.
* @returns The request path including formatted query parameters.
*/
protected buildPathWithOrg(route: string, queryParams?: any): string {
return super.buildPath(route, {org: this.api.organizationId, ...queryParams});
}

/**
* List all properties
*
* @returns Promise<PageModel<PropertyModel>>
*/
list(params?: ListPropertiesParams): Promise<PageModel<PropertyModel>> {
return this.api.get<PageModel<PropertyModel>>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/list`, params),
);
}

/**
* Get a property
*
* @returns Promise<PropertyModel>
*/
get(trackingId: string): Promise<PropertyModel> {
return this.api.get<PropertyModel>(this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`));
}

/**
* Create a property
*
* @returns Promise<PropertiesResponseMessage>
*/
create(trackingId: string, displayName: string): Promise<PropertiesResponseMessage> {
return this.api.post<PropertiesResponseMessage>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`, {displayName}),
);
}

/**
* Edit a property
*
* @returns Promise<PropertiesResponseMessage>
*/
update(trackingId: string, displayName: string): Promise<PropertiesResponseMessage> {
return this.api.put<PropertiesResponseMessage>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`, {displayName}),
);
}

/**
* Delete a property
*
* @returns Promise<PropertiesResponseMessage>
*/
delete(trackingId: string): Promise<PropertiesResponseMessage> {
return this.api.delete<PropertiesResponseMessage>(
this.buildPathWithOrg(`${Properties.baseUrl}/properties/${trackingId}`),
);
}
}
14 changes: 14 additions & 0 deletions src/resources/AnalyticsAdmin/Properties/PropertiesInterfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Paginated} from '../../BaseInterfaces.js';

export interface PropertyModel {
trackingId: string;
displayName: string;
}

export interface PropertiesResponseMessage {
message: string;
}

export interface ListPropertiesParams extends Paginated {
filter?: string;
}
2 changes: 2 additions & 0 deletions src/resources/AnalyticsAdmin/Properties/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Properties.js';
export * from './PropertiesInterfaces.js';
1 change: 1 addition & 0 deletions src/resources/AnalyticsAdmin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Properties/index.js';
78 changes: 78 additions & 0 deletions src/resources/AnalyticsAdmin/tests/Properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import API from '../../../APICore.js';
import Properties from '../Properties/Properties.js';

jest.mock('../../../APICore.js');

const APIMock: jest.Mock<API> = API as any;

describe('Properties', () => {
let properties: Properties;
const api = new APIMock() as jest.Mocked<API>;
const serverlessApi = new APIMock() as jest.Mocked<API>;

beforeEach(() => {
jest.clearAllMocks();
properties = new Properties(api, serverlessApi);
});

describe('listProperties', () => {
it('should make a GET call to the list path', () => {
properties.list();

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/list`);
});

it('should make a GET call to the list path with pagination', () => {
properties.list({page: 1, perPage: 2});

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/list?page=1&perPage=2`);
});

it('should make a GET call to the list path with filter', () => {
properties.list({filter: 'test'});

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/list?filter=test`);
});
});

describe('getProperty', () => {
it('should make a GET call to the properties path', () => {
properties.get('trackingId');

expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/trackingId`);
});
});

describe('createProperty', () => {
it('should make a POST call to the properties path', () => {
properties.create('trackingId', 'displayName');

expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(
`${Properties.baseUrl}/properties/trackingId?displayName=displayName`,
);
});
});

describe('updateProperty', () => {
it('should make a PUT call to the properties path', () => {
properties.update('trackingId', 'displayName');

expect(api.put).toHaveBeenCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/trackingId?displayName=displayName`);
});
});

describe('deleteProperty', () => {
it('should make a DELETE call to the properties path', () => {
properties.delete('trackingId');

expect(api.delete).toHaveBeenCalledTimes(1);
expect(api.delete).toHaveBeenCalledWith(`${Properties.baseUrl}/properties/trackingId`);
});
});
});
3 changes: 3 additions & 0 deletions src/resources/PlatformResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import SearchAnalysis from './SearchAnalysis/SearchAnalysis.js';
import Project from './Projects/Project.js';
import Resources from './Resources/Resources.js';
import CatalogContent from './Catalogs/CatalogContent.js';
import Properties from './AnalyticsAdmin/Properties/Properties.js';

const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'activity', resource: Activity},
Expand Down Expand Up @@ -91,6 +92,7 @@ const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'productListing', resource: ProductListing},
{key: 'productListingConfiguration', resource: ProductListingConfiguration},
{key: 'products', resource: Products},
{key: 'properties', resource: Properties},
{key: 'pushApi', resource: PushApi},
{key: 'resourceSnapshot', resource: ResourceSnapshots},
{key: 'saml', resource: Saml},
Expand Down Expand Up @@ -151,6 +153,7 @@ class PlatformResources {
productListing: ProductListing;
productListingConfiguration: ProductListingConfiguration;
products: Products;
properties: Properties;
pushApi: PushApi;
resourceSnapshot: ResourceSnapshots;
saml: Saml;
Expand Down
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ export * from './Sources/index.js';
export * from './UsageAnalytics/index.js';
export * from './Users/index.js';
export * from './Vaults/index.js';
export * from './AnalyticsAdmin/index.js';

0 comments on commit b744a5b

Please sign in to comment.