import {z} from 'zod' export type UsageMeterAggregation = 'COUNT' | 'SUM'; export type Computation = {id?: string | undefined, matcher?: string | undefined, computation: string, order: number}; export type UsageMeterFilterEntry = {field: string, value: string}; export type UpdateUsageMeterRequest = {name?: string | undefined, billableName?: string | undefined, description?: string | undefined, eventSchemaName?: string | undefined, type?: 'COUNTER' | undefined, aggregation?: UsageMeterAggregation | undefined, computations?: Array | undefined, filters?: Array | undefined}; export type PatchApiUsageMetersUsageMeterIdArgs = {usage_meter_id: string, body: UpdateUsageMeterRequest}; export type PatchApiUsageMetersUsageMeterIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageMeterFilterEntry = z.object({field: z.string(), value: z.string()}); export const usageMeterAggregation = z.enum(["COUNT", "SUM"]); export const computation = z.object({id: z.string().max(50).optional(), matcher: z.string().max(1500).optional(), computation: z.string().max(500), order: z.number().int()}); export const usageMeter = z.object({id: z.string().max(20), name: z.string().max(50), billableName: z.string().max(255).optional(), displayName: z.string().max(255), description: z.string().max(255).optional(), filters: z.array(usageMeterFilterEntry).optional(), type: z.literal("COUNTER"), status: z.enum(["DRAFT", "ACTIVE", "INACTIVE", "ARCHIVED"]).optional(), aggregation: usageMeterAggregation, computations: z.array(computation).optional(), createdAt: z.string().optional(), lastActivatedAt: z.string().optional(), updatedAt: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Update an usage meter * * PATCH /usage_meters/{usage_meter_id} */ export const patchApiUsageMetersUsageMeterId = async (args: PatchApiUsageMetersUsageMeterIdArgs, options?: PatchApiUsageMetersUsageMeterIdOptions) => { const {baseUrl = "https://api.togai.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/usage_meters/${encodeURIComponent(String(args.usage_meter_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(usageMeter, await res.json()) };