import {z} from 'zod' export type GetApiV1UsageTermsArgs = {to?: string | undefined, 'x-checkly-account'?: string | undefined}; export type GetApiV1UsageTermsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const usageV1Account = z.object({id: z.string(), name: z.string()}); export const usageV2PricingRule = z.union([z.object({method: z.literal("per_unit"), quantity: z.number().gt(0), unit: z.string().min(1), credits: z.number().gte(0), rates: z.array(z.object({when: z.string().min(1), credits: z.number().gte(0)})).optional(), included: z.object({quantity: z.number().gte(0), period: z.string().min(1).optional()}).optional()}), z.object({method: z.literal("per_period"), period: z.literal("day"), price: z.object({annualCredits: z.number().gte(0)})}), z.object({method: z.literal("capacity_blocks_per_period"), period: z.literal("day"), unit: z.string().min(1), included: z.object({quantity: z.number().gte(0)}), block: z.object({quantity: z.number().gt(0), annualCredits: z.number().gte(0)})})]); export const usageV1TermsWithPricingResponse = z.object({id: z.string(), name: z.string(), accounts: z.array(usageV1Account), contractStartDate: z.string(), contractEndDate: z.string(), usageStartDate: z.string(), creditBudget: z.number().gt(0).nullable(), standardCreditsPerUnit: z.number().gte(0).nullable(), premiumCreditsPerUnit: z.number().gte(0).nullable(), pricingRules: z.object({apiChecks: usageV2PricingRule.optional(), multistepChecks: usageV2PricingRule.optional(), browserChecks: usageV2PricingRule.optional(), playwrightCheckSuites: usageV2PricingRule.optional(), uptimeMonitors: usageV2PricingRule.optional(), aiInvocations: usageV2PricingRule.optional(), statusPages: usageV2PricingRule.optional(), statusPageAdditionalServices: usageV2PricingRule.optional(), statusPageAdditionalSubscribers: usageV2PricingRule.optional(), additionalSms: usageV2PricingRule.optional(), additionalPhoneAlerts: usageV2PricingRule.optional()}).nullable(), packageModel: z.enum(["limits", "credits", "null"]).nullable()}); 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 }; /** * Get usage terms and accounts for the current organization. * * GET /v1/usage/terms */ export const getApiV1UsageTerms = async (args: GetApiV1UsageTermsArgs, options?: GetApiV1UsageTermsOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.to != null) query.append("to", String(args.to)) if (args['x-checkly-account'] != null) headers.set("x-checkly-account", String(args['x-checkly-account'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/usage/terms${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(usageV1TermsWithPricingResponse, await res.json()) };