Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat: support for advanced kpi usages
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmichaelwallace committed Nov 26, 2020
1 parent e840cbd commit 5d7ee26
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/DPKpi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ export default class DPKpi {
this.#token = toToken(token);
}

public async getResults(kpiId: string): Promise<DPKpiResult> {
public async getResults(
kpiId: string,
advancedOptions?: Record<string, string | number>,
): Promise<DPKpiResult> {
const request: AxiosRequestConfig = {
method: 'get',
url: `/kpi/${kpiId}`,
baseURL: this.baseUrl,
params: { view: 'api' },
params: {
...(advancedOptions ?? {}),
view: 'api',
},
headers: { Authorization: this.#token },
};
const { data } = await query<DPKpiResult>(request);
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/DPKpi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,35 @@ test('get kpi from DevicePilot', async () => {
timeout: 30000,
});
});

test('get kpi from DevicePilot with advanced options', async () => {
const kpiToken = 'ghi';
const kpiId = '1234';
const url = 'http://url.com?If-None-Match=placeholder';
const headers = { location: url };
const dp = new DPKpi(kpiToken);

m(axios).mockReset();
m(axios)
// @ts-expect-error: return type isn't important
.mockResolvedValueOnce({ headers })
// @ts-expect-error: return type isn't important
.mockResolvedValueOnce({ data: 'data', headers: { etag: 'placeholder' } })
// @ts-expect-error: return type isn't important
.mockResolvedValueOnce({ data: 'data', headers: { etag: '' } });
await dp.getResults(kpiId, { scope: 'hello' });

expect(axios).toHaveBeenNthCalledWith(1, {
method: 'get',
headers: { Authorization: `TOKEN ${kpiToken}` },
baseURL: 'https://api.devicepilot.com',
params: { view: 'api', scope: 'hello' },
url: `/kpi/${kpiId}`,
timeout: 30000,
});
expect(axios).toHaveBeenCalledWith({
method: 'get',
url,
timeout: 30000,
});
});

0 comments on commit 5d7ee26

Please sign in to comment.