Product/components used and version/fix level:
@c8y/client@1020.18.3
Detailed explanation of the problem:
In Cumulocity API, when requesting for a list of e.g. alarms you can use the withTotalElements parameter that will respond including totalElements in the statistics. However, using the @c8y/client for Javascript, there seems to be no way of getting that information from the same request. (pageSize and totalPages is not enough information).
Hi @MagnusAkermanAspire,
this is currently not available as part of the Paging object, but will be available in a future release.
You can however just implement this on your own:
import { Client, BasicAuth } from '@c8y/client';
const user = '<your-user>';
const password = '<your-password>';
const baseUrl = 'https://<your-domain>';
const client = new Client(new BasicAuth({ user, password }), baseUrl);
async function withTotalElements() {
const response = await client.core.fetch('/inventory/managedObjects', {
params: { withTotalElements: true }
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const responseBody = await response.json();
const { totalElements } = responseBody.statistics;
console.log(totalElements);
}
Within an Web SDK based Angular application you could even just use dependency injection:
import { FetchClient } from '@c8y/client';
[...]
constructor(private fetchClient: FetchClient) {}
async withTotalElements() {
const response = await this.fetchClient.fetch('/inventory/managedObjects', {
params: { withTotalElements: true }
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const responseBody = await response.json();
const { totalElements } = responseBody.statistics;
console.log(totalElements);
}
[...]
Regards,
Tristan
FYI: Version 1021.14.0
of the @c8y/client
package will include the totalElements
attribute on the paging object.