refactor(usage): shape sources as fetch functions

Keep the plugin usage API limited to stable report and presentation helpers. Source-local parsers and timeout handling preserve credential and response parity while the cases move alongside each source.
This commit is contained in:
Mohamed Boudra
2026-09-26 20:52:37 +02:00
parent 0f2f6a3b89
commit 2e53a5b9b5
40 changed files with 2628 additions and 2396 deletions
+1 -29
View File
@@ -1,4 +1,4 @@
import { z, type ZodType } from "zod";
import type { ZodType } from "zod";
import type { JsonValue } from "@getpaseo/protocol/agent-types";
export interface UsageWindow {
@@ -108,29 +108,6 @@ export function usedPctOf(
return (used / limit) * 100;
}
export const ApiNumberSchema = z.coerce.number().finite();
export const ApiNullableNumberSchema = z.preprocess(
(value) => (value == null ? null : value),
ApiNumberSchema.nullable(),
);
export const ApiOptionalStringSchema = z.preprocess(
(value) => (value == null ? undefined : value),
z.coerce.string().optional(),
);
export type UsageApiFetch = typeof fetch;
export function fetchProviderApi(
fetchApi: UsageApiFetch,
input: RequestInfo | URL,
init: RequestInit = {},
): Promise<Response> {
return fetchApi(input, {
...init,
signal: init.signal ?? AbortSignal.timeout(15_000),
});
}
export function unavailableUsage(accountKey = "default"): UsageReport {
return {
account: { key: accountKey },
@@ -140,8 +117,3 @@ export function unavailableUsage(accountKey = "default"): UsageReport {
details: [],
};
}
export function toIsoStringOrNull(timestampMs: number): string | null {
const date = new Date(timestampMs);
return Number.isFinite(date.getTime()) ? date.toISOString() : null;
}