Skip to content

Commit

Permalink
feat: add rum api calls to shared
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Dec 14, 2023
1 parent ee8f47a commit d51efbb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
28 changes: 28 additions & 0 deletions packages/spacecat-shared-rum-api-client/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,32 @@ export declare class RUMAPIClient {
* to view their reports and monitor real user activities.
*/
createBacklink(url: string, expiry: number): Promise<string>;

/**
* 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<Array<object>>;

/**
* 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<Array<object>>;

/**
* 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<Array<string>>;
}
21 changes: 17 additions & 4 deletions packages/spacecat-shared-rum-api-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -89,20 +92,30 @@ export default class RUMAPIClient {

async getRUMDashboard(params) {
return sendRequest(createUrl(
APIS.DOMAIN_LIST,
APIS.RUM_DASHBOARD,
{ domainkey: this.domainkey, ...params },
));
}

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}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit d51efbb

Please sign in to comment.