import {z} from 'zod' export type GetApiEventsArgs = {page?: number | undefined, per_page?: number | undefined, external_subscription_id?: string | undefined, code?: string | undefined, timestamp_from_started_at?: boolean | undefined, timestamp_from?: string | undefined, timestamp_to?: string | undefined}; export type GetApiEventsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const eventObject = z.object({lago_id: z.string().nullable().optional(), transaction_id: z.string(), lago_customer_id: z.string().nullable(), code: z.string(), timestamp: z.string(), precise_total_amount_cents: z.string().nullable().optional(), properties: z.object({operation_type: z.enum(["add", "remove"]).nullable().optional()}).and(z.record(z.string(), z.union([z.string(), z.number().int(), z.number()]))).optional(), lago_subscription_id: z.string().nullable(), external_subscription_id: z.string(), created_at: z.string().nullable().optional()}); export const paginationMeta = z.object({current_page: z.number().int(), next_page: z.number().int().nullable().optional(), prev_page: z.number().int().nullable().optional(), total_pages: z.number().int(), total_count: z.number().int()}); export const eventsPaginated = z.object({events: z.array(eventObject), meta: paginationMeta}); 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 all events * * GET /events */ export const getApiEvents = async (args: GetApiEventsArgs, options?: GetApiEventsOptions) => { const {baseUrl = "https://api.getlago.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.external_subscription_id != null) query.append("external_subscription_id", String(args.external_subscription_id)) if (args.code != null) query.append("code", String(args.code)) if (args.timestamp_from_started_at != null) query.append("timestamp_from_started_at", String(args.timestamp_from_started_at)) if (args.timestamp_from != null) query.append("timestamp_from", String(args.timestamp_from)) if (args.timestamp_to != null) query.append("timestamp_to", String(args.timestamp_to)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(eventsPaginated, await res.json()) };