import {z} from 'zod' export type GetApiUsageSummariesArgs = {company?: string | undefined, category?: ('company' | 'employee' | 'contractor' | 'company_funding_failure' | 'payee_failed_payment' | 'wire' | 'one_day_payroll_processing_period') | undefined, period_start?: string | undefined, period_end?: string | undefined}; export type GetApiUsageSummariesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiUsageSummariesResponse = z.object({previous: z.unknown(), next: z.unknown(), results: z.array(z.object({company: z.string().optional(), category: z.string().optional(), count: z.number().int().optional(), period_start: z.string().optional(), period_end: z.string().optional()})).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 }; /** * List usage summaries * * GET /usage/summaries */ export const getApiUsageSummaries = async (args: GetApiUsageSummariesArgs, options?: GetApiUsageSummariesOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company != null) query.append("company", String(args.company)) if (args.category != null) query.append("category", String(args.category)) if (args.period_start != null) query.append("period_start", String(args.period_start)) if (args.period_end != null) query.append("period_end", String(args.period_end)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/usage/summaries${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(getApiUsageSummariesResponse, await res.json()) };