mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-06 08:24:46 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user