Skip to content

Commit

Permalink
fix: added RUMAPIOptions type
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Dec 14, 2023
1 parent f6e429e commit a46a5c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
20 changes: 12 additions & 8 deletions packages/spacecat-shared-rum-api-client/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Array<object>>;
getRUMDashboard(params?: RUMAPIOptions): Promise<Array<object>>;

/**
* 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<Array<object>>;
get404Sources(params?: RUMAPIOptions): 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
* @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<Array<string>>;
getDomainList(params?: RUMAPIOptions): Promise<Array<string>>;
}
11 changes: 4 additions & 7 deletions packages/spacecat-shared-rum-api-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
{
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});

0 comments on commit a46a5c2

Please sign in to comment.