diff --git a/src/client.ts b/src/client.ts index 29e9b56..34db15b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -36,8 +36,8 @@ export type ScubaClientParameters = Omit< }; export type ScubaMetrics = { - objectsTotal: string; - bytesTotal: string; + objectsTotal: bigint; + bytesTotal: bigint; metricsClass: string; resourceName: string; id?: number; @@ -60,8 +60,8 @@ export type ScubaHttpError = { export type GetMetricsBatchResponseDateMetrics = { date: string; - bytesTotal: string; - objectsTotal: string; + bytesTotal: bigint; + objectsTotal: bigint; }; export type GetMetricsBatchResponseDateError = { @@ -186,7 +186,9 @@ export default class ScubaClient { const resp = (await this._api.getLatestMetrics(metricsClass, resourceName, body, { ...this._defaultReqOptions, ...options, - })) as any; + })) as unknown as { data: ScubaMetrics }; + resp.data.bytesTotal = BigInt(resp.data.bytesTotal); + resp.data.objectsTotal = BigInt(resp.data.objectsTotal); return resp.data; } @@ -204,7 +206,9 @@ export default class ScubaClient { const resp = (await this._api.getMetrics(metricsClass, resourceName, dateString, body, { ...this._defaultReqOptions, ...options, - })) as any; + })) as unknown as { data: ScubaMetrics }; + resp.data.bytesTotal = BigInt(resp.data.bytesTotal); + resp.data.objectsTotal = BigInt(resp.data.objectsTotal); return resp.data; } @@ -216,7 +220,19 @@ export default class ScubaClient { const resp = (await this._api.getMetricsBatch(metricsClass, body, { ...this._defaultReqOptions, ...options, - })) as any; + })) as unknown as { data: GetMetricsBatchResponse }; + resp.data.metrics.forEach(resource => { + if ('error' in resource) { + return; + } + resource.metrics?.forEach(metric => { + if ('error' in metric) { + return; + } + metric.bytesTotal = BigInt(metric.bytesTotal); + metric.objectsTotal = BigInt(metric.objectsTotal); + }); + }); return resp.data; } @@ -242,7 +258,9 @@ export default class ScubaClient { const resp = (await this._api.internalGetLatestAccountMetrics(canonicalId, { ...this._defaultReqOptions, ...options, - })) as any; + })) as unknown as { data: ScubaMetrics }; + resp.data.bytesTotal = BigInt(resp.data.bytesTotal); + resp.data.objectsTotal = BigInt(resp.data.objectsTotal); return resp.data; } } diff --git a/tests/client.spec.ts b/tests/client.spec.ts index 44f95c4..ff5cdd4 100644 --- a/tests/client.spec.ts +++ b/tests/client.spec.ts @@ -2,10 +2,17 @@ import { AddressInfo } from 'net'; import { Server, createServer, IncomingMessage, ServerResponse } from 'http'; import { AxiosError } from 'axios'; -import ScubaClient, { GetMetricsBatchResponse, ScubaMetrics, AdminResponseCseq } from '../src/client'; +import ScubaClient, { GetMetricsBatchResponse, ScubaMetrics, AdminResponseCseq, GetMetricsBatchResponseResourceMetrics } from '../src/client'; import { AdminActions, GetMetricsBatchBody } from '../src/api'; import { RequiredError } from '../src/base'; +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json +// @ts-ignore we extend the prototype for testing purposes +// eslint-disable-next-line no-extend-native +BigInt.prototype.toJSON = function toJSON() { + return this.toString(); +}; + let response; class MockScubaServer { server: Server; @@ -71,8 +78,8 @@ class MockScubaServer { response = { metricsClass: urlSections[2], resourceName: urlSections[3], - objectsTotal: '100', - bytesTotal: '1000', + objectsTotal: 100n, + bytesTotal: 1000n, date: '2024-12-06', }; @@ -89,8 +96,8 @@ class MockScubaServer { resourceName: resourceNames[0], metrics: [ { - objectsTotal: '100', - bytesTotal: '1000', + objectsTotal: 100n, + bytesTotal: 1000n, date: '2024-12-06', }, ], @@ -150,11 +157,11 @@ describe('Test client', () => { describe('Test getLatestMetrics', () => { it('should return a successful response received by scuba', async () => { - const expectedResponse = { + const expectedResponse = { metricsClass, resourceName, - objectsTotal: '100', - bytesTotal: '1000', + objectsTotal: 100n, + bytesTotal: 1000n, date: '2024-12-06', }; @@ -164,11 +171,11 @@ describe('Test client', () => { }); it('should handle responses with large numbers', async () => { - const largeNumberResponse = { + const largeNumberResponse = { metricsClass, resourceName, - objectsTotal: '9007199254740992', - bytesTotal: '90071992547409920', + objectsTotal: 9007199254740992n, + bytesTotal: 90071992547409920n, date: '2024-12-06', }; @@ -195,11 +202,11 @@ describe('Test client', () => { describe('Test getMetrics', () => { it('should return a successful response received by scuba', async () => { - const expectedResponse = { + const expectedResponse = { metricsClass, resourceName, - objectsTotal: '100', - bytesTotal: '1000', + objectsTotal: 100n, + bytesTotal: 1000n, date: '2024-12-06', }; @@ -209,11 +216,11 @@ describe('Test client', () => { }); it('should handle responses with large numbers', async () => { - const largeNumberResponse = { + const largeNumberResponse = { metricsClass, resourceName, - objectsTotal: '9007199254740992', - bytesTotal: '90071992547409920', + objectsTotal: 9007199254740992n, + bytesTotal: 90071992547409920n, date: '2024-12-06', }; @@ -240,15 +247,15 @@ describe('Test client', () => { describe('Test getMetricsBatch', () => { it('should return a successful response received by scuba', async () => { - const expectedResponse = { + const expectedResponse = { metrics: [ { metricsClass, resourceName, metrics: [ { - objectsTotal: '100', - bytesTotal: '1000', + objectsTotal: 100n, + bytesTotal: 1000n, date: '2024-12-06', }, ], @@ -262,15 +269,15 @@ describe('Test client', () => { }); it('should handle responses with large numbers', async () => { - const largeNumberResponse = { + const largeNumberResponse = { metrics: [ { metricsClass, resourceName, metrics: [ { - objectsTotal: '9007199254740992', // Number.MAX_SAFE_INTEGER + 1 - bytesTotal: '90071992547409920', + objectsTotal: 9007199254740992n, // Number.MAX_SAFE_INTEGER + 1 + bytesTotal: 90071992547409920n, date: '2024-12-06', }, ],