Skip to content

Commit d91f2e2

Browse files
Copilothotlong
andcommitted
refactor: use @objectstack/client analytics.query() instead of raw fetch()
Replace manual URL construction, auth header handling, and raw fetch() in ObjectStackAdapter.aggregate() with this.client.analytics.query() from the @objectstack/client SDK. This leverages the SDK's built-in auth, headers, custom fetch config, and base URL resolution — consistent with all other adapter methods. Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent f25e6c2 commit d91f2e2

1 file changed

Lines changed: 9 additions & 19 deletions

File tree

packages/data-objectstack/src/index.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -692,35 +692,25 @@ export class ObjectStackAdapter<T = unknown> implements DataSource<T> {
692692

693693
/**
694694
* Perform server-side aggregation via the ObjectStack analytics API.
695-
* Calls GET /api/v1/analytics/{resource} with category, metric, and agg params.
695+
* Uses `this.client.analytics.query()` from @objectstack/client to leverage
696+
* the SDK's built-in auth, headers, and fetch configuration.
696697
* Falls back to client-side aggregation via find() if the analytics endpoint
697698
* is not available.
698699
*/
699700
async aggregate(resource: string, params: { field: string; function: string; groupBy: string; filter?: any }): Promise<any[]> {
700701
await this.connect();
701702

702703
try {
703-
const url = `${this.baseUrl}/api/v1/analytics/${encodeURIComponent(resource)}`;
704-
const searchParams = new URLSearchParams({
705-
category: params.groupBy,
706-
metric: params.field,
707-
agg: params.function,
708-
});
704+
const payload: Record<string, unknown> = {
705+
object: resource,
706+
measures: [{ field: params.field, function: params.function }],
707+
dimensions: [params.groupBy],
708+
};
709709
if (params.filter) {
710-
searchParams.set('filter', typeof params.filter === 'string' ? params.filter : JSON.stringify(params.filter));
711-
}
712-
713-
const headers: Record<string, string> = { 'Accept': 'application/json' };
714-
if (this.token) {
715-
headers['Authorization'] = `Bearer ${this.token}`;
716-
}
717-
718-
const response = await fetch(`${url}?${searchParams.toString()}`, { headers });
719-
if (!response.ok) {
720-
throw new Error(`Analytics API returned ${response.status}`);
710+
payload.filters = params.filter;
721711
}
722712

723-
const data = await response.json();
713+
const data = await this.client.analytics.query(payload);
724714
if (Array.isArray(data)) return data;
725715
if (data?.data && Array.isArray(data.data)) return data.data;
726716
if (data?.results && Array.isArray(data.results)) return data.results;

0 commit comments

Comments
 (0)