import {z} from 'zod' export type TypeMoney = {currencyCode?: string | undefined, units?: unknown | undefined, nanos?: number | undefined}; export type GatewayAccountUsageLimitsEnforcementMode = 'ENFORCEMENT_MODE_UNSPECIFIED' | 'DISABLED' | 'WARN' | 'BLOCK'; export type PatchApiV1AccountsAccountIdUsersUserIdUsageLimitsArgs = {account_id: string, user_id: string, body: {limitOverride?: TypeMoney | undefined, effectiveLimit?: TypeMoney | undefined, used?: TypeMoney | undefined, enforcementMode?: GatewayAccountUsageLimitsEnforcementMode | undefined, exceededUntil?: string | undefined, updateTime?: string | undefined}}; export type PatchApiV1AccountsAccountIdUsersUserIdUsageLimitsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const typeMoney = z.object({currencyCode: z.string().optional(), units: z.string().optional(), nanos: z.number().int().optional()}); export const gatewayAccountUsageLimitsEnforcementMode = z.enum(["ENFORCEMENT_MODE_UNSPECIFIED", "DISABLED", "WARN", "BLOCK"]); export const gatewayUserUsageLimits = z.object({name: z.string().optional(), limitOverride: typeMoney.optional(), effectiveLimit: typeMoney.optional(), used: typeMoney.optional(), enforcementMode: gatewayAccountUsageLimitsEnforcementMode.optional(), exceededUntil: z.string().optional(), updateTime: 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 }; /** * Updates a single user's usage-limits singleton (limit_override). Clearing * the override reverts the user to the account default. * * PATCH /v1/accounts/{account_id}/users/{user_id}/usageLimits */ export const patchApiV1AccountsAccountIdUsersUserIdUsageLimits = async (args: PatchApiV1AccountsAccountIdUsersUserIdUsageLimitsArgs, options: PatchApiV1AccountsAccountIdUsersUserIdUsageLimitsOptions) => { const {baseUrl, 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(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/users/${encodeURIComponent(String(args.user_id))}/usageLimits${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(gatewayUserUsageLimits, await res.json()) };