import {z} from 'zod' export type GetApiAccountsAidCustomersTokensEventsArgs = {aid: unknown, limit?: number | undefined, starting_after?: string | undefined, since_datetime?: string | undefined, deleted_since?: string | undefined}; export type GetApiAccountsAidCustomersTokensEventsOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenEvent = z.object({expires_at: z.string().optional(), created_at: z.string().optional(), event: z.string(), status: z.string().optional(), metadata: z.object({}).optional()}); export const getApiAccountsAidCustomersTokensEventsResponse = z.array(z.object({id: z.string().optional(), status: z.string().optional(), events: z.array(tokenEvent).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 }; /** * aid_customers_tokens_events_get * * GET /accounts/{aid}/customers/tokens/events */ export const getApiAccountsAidCustomersTokensEvents = async (args: GetApiAccountsAidCustomersTokensEventsArgs, options?: GetApiAccountsAidCustomersTokensEventsOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.since_datetime != null) query.append("since_datetime", String(args.since_datetime)) if (args.deleted_since != null) query.append("deleted_since", String(args.deleted_since)) if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/customers/tokens/events${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(getApiAccountsAidCustomersTokensEventsResponse, await res.json()) };