From d51efbb66073c1694052c8e52f074749448e2cd5 Mon Sep 17 00:00:00 2001 From: alinarublea Date: Thu, 14 Dec 2023 12:09:20 +0100 Subject: [PATCH] feat: add rum api calls to shared --- .../src/index.d.ts | 28 +++++++++++++++++++ .../src/index.js | 21 +++++++++++--- .../test/rum-api-client.test.js | 25 +++++++++++++++++ 3 files changed, 70 insertions(+), 4 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 6a5b306a..b1639303 100644 --- a/packages/spacecat-shared-rum-api-client/src/index.d.ts +++ b/packages/spacecat-shared-rum-api-client/src/index.d.ts @@ -40,4 +40,32 @@ export declare class RUMAPIClient { * to view their reports and monitor real user activities. */ createBacklink(url: string, expiry: number): Promise; + + /** + * Asynchronous method to return the RUM dashboard API call response data. + * @param {object} 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>; + + /** + * Asynchronous method to return the 404 checkpoints API call response data. + * @param {object} params - An object representing the parameters to be included + * for the 404 Checkpoints API call. + * @returns A Promise resolving to the 404 checkpoints response data. + */ + get404Checkpoints(params: object): 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 + * for the domain list call. + * @param {string} url - An 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>; } diff --git a/packages/spacecat-shared-rum-api-client/src/index.js b/packages/spacecat-shared-rum-api-client/src/index.js index fda9255c..9158d064 100644 --- a/packages/spacecat-shared-rum-api-client/src/index.js +++ b/packages/spacecat-shared-rum-api-client/src/index.js @@ -17,11 +17,14 @@ import { fetch } from './utils.js'; const APIS = { ROTATE_DOMAINKEYS: 'https://helix-pages.anywhere.run/helix-services/run-query@v3/rotate-domainkeys', - RUM_DASHBOARD: 'https://main--franklin-dashboard--adobe.hlx.live/views/rum-dashboard', - DOMAIN_LIST: 'https://helix-pages.anywhere.run/helix-services/run-query@v3/rum-dashboard', + RUM_DASHBOARD_UI: 'https://main--franklin-dashboard--adobe.hlx.live/views/rum-dashboard', + RUM_DASHBOARD: 'https://helix-pages.anywhere.run/helix-services/run-query@v3/rum-dashboard', + DOMAIN_LIST: 'https://helix-pages.anywhere.run/helix-services/run-query@v3/dash/domain-list', NOT_FOUND_CHECKPOINTS: 'https://helix-pages.anywhere.run/helix-services/run-query@v3/rum-checkpoint-urls', }; +export const isAuditForAll = (url) => url.toUpperCase() === 'ALL'; + export async function sendRequest(url, opts) { let respJson; try { @@ -89,7 +92,7 @@ export default class RUMAPIClient { async getRUMDashboard(params) { return sendRequest(createUrl( - APIS.DOMAIN_LIST, + APIS.RUM_DASHBOARD, { domainkey: this.domainkey, ...params }, )); } @@ -97,12 +100,22 @@ export default class RUMAPIClient { async get404Checkpoints(params) { return sendRequest(createUrl( APIS.NOT_FOUND_CHECKPOINTS, + { domainkey: this.domainkey, checkpoint: 404, ...params }, + )); + } + + async getDomainList(params, url) { + const data = await sendRequest(createUrl( + APIS.DOMAIN_LIST, { domainkey: this.domainkey, ...params }, )); + + const urls = data.map((row) => row.hostname); + return isAuditForAll(url) ? urls : urls.filter((row) => url === row); } async createBacklink(url, expiry) { const scopedDomainKey = await generateDomainKey(this.domainkey, url, expiry); - return `${APIS.RUM_DASHBOARD}?interval=${expiry}&offset=0&limit=100&url=${url}&domainkey=${scopedDomainKey}`; + return `${APIS.RUM_DASHBOARD_UI}?interval=${expiry}&offset=0&limit=100&url=${url}&domainkey=${scopedDomainKey}`; } } 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 9418a51c..bd553856 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 @@ -77,10 +77,35 @@ describe('rum api client', () => { .get('/run-query@v3/rum-checkpoint-urls') .query({ domainkey: 'hebele', + checkpoint: 404, }) .reply(200, JSON.stringify({ results: { data: [] } })); const rumApiClient = RUMAPIClient.createFrom({ env: { RUM_API_KEY: 'hebele' } }); await expect(rumApiClient.get404Checkpoints()) .to.be.fulfilled; }); + + it('returns data when getDomainList api is successful', async () => { + nock('https://helix-pages.anywhere.run/helix-services') + .get('/run-query@v3/dash/domain-list') + .query({ + domainkey: 'hebele', + }) + .reply(200, JSON.stringify({ results: { data: [] } })); + const rumApiClient = RUMAPIClient.createFrom({ env: { RUM_API_KEY: 'hebele' } }); + await expect(rumApiClient.getDomainList({}, 'all')) + .to.be.fulfilled; + }); + + it('returns data when getDomainList api is successful', async () => { + nock('https://helix-pages.anywhere.run/helix-services') + .get('/run-query@v3/dash/domain-list') + .query({ + domainkey: 'hebele', + }) + .reply(200, JSON.stringify({ results: { data: ['spacecat.com'] } })); + const rumApiClient = RUMAPIClient.createFrom({ env: { RUM_API_KEY: 'hebele' } }); + await expect(rumApiClient.getDomainList({}, 'spaceacat.com')) + .to.be.fulfilled; + }); });