import {z} from 'zod' export type GetApiActivitiesArgs = {type?: string | undefined, from_time?: string | undefined, until_time?: string | undefined}; export type GetApiActivitiesOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const activity = z.object({id: z.string().optional(), type: z.string().optional(), created_at: z.string().optional(), amount_in_cents: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), status: z.string().nullable().optional()}); export const getApiActivitiesResponse = z.array(activity); 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 }; /** * Listar actividad de la cuenta * * GET /activities */ export const getApiActivities = async (args: GetApiActivitiesArgs, options?: GetApiActivitiesOptions) => { const {baseUrl = "https://app.recurrente.com/api", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.from_time != null) query.append("from_time", String(args.from_time)) if (args.until_time != null) query.append("until_time", String(args.until_time)) if (secretKey !== undefined) headers.set("X-SECRET-KEY", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/activities${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(getApiActivitiesResponse, await res.json()) };