From a46a5c258547306dadb510c1660884bd50ec391a Mon Sep 17 00:00:00 2001 From: ekremney Date: Thu, 14 Dec 2023 15:14:32 +0100 Subject: [PATCH] fix: added RUMAPIOptions type --- .../src/index.d.ts | 20 +++++++++++-------- .../src/index.js | 11 ++++------ .../test/rum-api-client.test.js | 11 +++++++--- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/spacecat-shared-rum-api-client/src/index.d.ts b/packages/spacecat-shared-rum-api-client/src/index.d.ts index 74bd158c..2fd0892f 100644 --- a/packages/spacecat-shared-rum-api-client/src/index.d.ts +++ b/packages/spacecat-shared-rum-api-client/src/index.d.ts @@ -12,6 +12,12 @@ import { UniversalContext } from '@adobe/helix-universal'; +export interface RUMAPIOptions { + interval: number; + offset: number; + limit: number; +} + export declare class RUMAPIClient { /** * Static factory method to create an instance of RUMAPIClient. @@ -43,29 +49,27 @@ export declare class RUMAPIClient { /** * Asynchronous method to return the RUM dashboard API call response data. - * @param {object} params - An object representing the parameters to be included + * @param {RUMAPIOptions} params - An object representing the parameters to be included * for the RUM Dashboard API call. * @returns A Promise resolving to the RUM dashboard response data. */ - getRUMDashboard(params: object): Promise>; + getRUMDashboard(params?: RUMAPIOptions): Promise>; /** * Asynchronous method to return the 404 sources API call response data. - * @param {object} params - An object representing the parameters to be included + * @param {RUMAPIOptions} params - An object representing the parameters to be included * for the 404 sources API call. * @returns A Promise resolving to the 404 sources response data. */ - get404Sources(params: object): Promise>; + get404Sources(params?: RUMAPIOptions): Promise>; /** * Asynchronous method to return an array with the domain for a specific url * or an array of all domain urls - * @param {object} params - An object representing the parameters to be included + * @param {RUMAPIOptions} params - An object representing the parameters to be included * for the domain list call. - * @param {string} url - A string representing the url to be filtered - * from the domain list call or all(representing all domains). * @returns A Promise resolving to an array of the domain for a specific url * or an array of all domain urls . */ - getDomainList(params:object, url:string): Promise>; + getDomainList(params?: RUMAPIOptions): Promise>; } diff --git a/packages/spacecat-shared-rum-api-client/src/index.js b/packages/spacecat-shared-rum-api-client/src/index.js index 72703689..f143d558 100644 --- a/packages/spacecat-shared-rum-api-client/src/index.js +++ b/packages/spacecat-shared-rum-api-client/src/index.js @@ -35,8 +35,6 @@ export const RUM_DEFAULT_PARAMS = { limit: 101, }; -const isAllDomains = (url) => url.toUpperCase() === 'ALL'; - export async function sendRequest(url, opts) { let respJson; try { @@ -102,14 +100,14 @@ export default class RUMAPIClient { this.domainkey = domainkey; } - async getRUMDashboard(params) { + async getRUMDashboard(params = {}) { return sendRequest(createUrl( APIS.RUM_DASHBOARD, { domainkey: this.domainkey, ...RUM_DEFAULT_PARAMS, ...params }, )); } - async get404Sources(params) { + async get404Sources(params = {}) { return sendRequest(createUrl( APIS.RUM_SOURCES, { @@ -118,14 +116,13 @@ export default class RUMAPIClient { )); } - async getDomainList(params, url) { + async getDomainList(params = {}) { const data = await sendRequest(createUrl( APIS.DOMAIN_LIST, { domainkey: this.domainkey, ...DOMAIN_LIST_DEFAULT_PARAMS, ...params }, )); - const urls = data.map((row) => row.hostname); - return isAllDomains(url) ? urls : urls.filter((row) => url === row); + return data.map((row) => row.hostname); } async createBacklink(url, expiry) { diff --git a/packages/spacecat-shared-rum-api-client/test/rum-api-client.test.js b/packages/spacecat-shared-rum-api-client/test/rum-api-client.test.js index 60b62695..87623cd0 100644 --- a/packages/spacecat-shared-rum-api-client/test/rum-api-client.test.js +++ b/packages/spacecat-shared-rum-api-client/test/rum-api-client.test.js @@ -131,9 +131,14 @@ describe('rum api client', () => { offset: 0, limit: 100000, }) - .reply(200, JSON.stringify({ results: { data: [{ hostname: 'spacecat.com' }] } })); + .reply(200, JSON.stringify({ + results: { + data: [ + { hostname: 'spacecat.com' }, + { hostname: 'spacekatze.com' }], + }, + })); const rumApiClient = RUMAPIClient.createFrom({ env: { RUM_API_KEY: 'hebele' } }); - await expect(rumApiClient.getDomainList({}, 'spacecat.com')) - .to.eventually.eql(['spacecat.com']); + await expect(rumApiClient.getDomainList()).to.eventually.eql(['spacecat.com', 'spacekatze.com']); }); });