import * as z from "zod" import { defineAction } from "../../automation/actions" import { getResendApi, RESEND_FULL_ACCESS_ACCOUNT_OPTIONS } from "./lib" const RESEND_ACCOUNT = "resend" const LOG_METHOD_SCHEMA = z.enum([ "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", ]) const LOG_SUMMARY_WIRE_SCHEMA = z.object({ created_at: z.coerce.date(), endpoint: z.string().min(1), id: z.uuid(), method: LOG_METHOD_SCHEMA, response_status: z.number().int().min(100).max(599), user_agent: z.string().nullable(), }) const LOG_SUMMARY_SCHEMA = LOG_SUMMARY_WIRE_SCHEMA.transform( ({ created_at: createdAt, response_status: responseStatus, user_agent: userAgent, ...log }) => ({ ...log, createdAt, responseStatus, userAgent }), ) const LOG_WIRE_SCHEMA = LOG_SUMMARY_WIRE_SCHEMA.extend({ object: z.literal("log"), request_body: z.record(z.string(), z.json()).nullable(), response_body: z.record(z.string(), z.json()).nullable(), }) const LOG_SCHEMA = LOG_WIRE_SCHEMA.transform( ({ created_at: createdAt, request_body: requestBody, response_body: responseBody, response_status: responseStatus, user_agent: userAgent, ...log }) => ({ ...log, createdAt, requestBody, responseBody, responseStatus, userAgent, }), ) const LOG_LIST_WIRE_SCHEMA = z.object({ data: LOG_SUMMARY_WIRE_SCHEMA.array(), has_more: z.boolean(), object: z.literal("list"), }) const LOG_LIST_SCHEMA = LOG_LIST_WIRE_SCHEMA.transform( ({ data, has_more: hasMore, ...result }) => ({ ...result, data: data.map((log) => LOG_SUMMARY_SCHEMA.parse(log)), hasMore, }), ) const PAGINATION_SCHEMA = z .object({ /** Return logs after this cursor. */ after: z.string().min(1).optional(), /** Return logs before this cursor. */ before: z.string().min(1).optional(), /** Maximum logs to return. */ limit: z.number().int().min(1).max(100).optional(), }) .refine(({ after, before }) => !(after && before), { message: "after and before cannot be used together.", }) /** Lists Resend API request logs with cursor pagination. */ export const listResendLogs = defineAction("List Resend logs") .describe("Lists API request logs for a connected Resend account.") .account(RESEND_ACCOUNT, RESEND_FULL_ACCESS_ACCOUNT_OPTIONS) .input(PAGINATION_SCHEMA) .output(LOG_LIST_SCHEMA) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getResendApi(account).call("logs", { query: input, responseSchema: LOG_LIST_WIRE_SCHEMA, }), ) /** Retrieves one Resend API request log. */ export const getResendLog = defineAction("Get Resend log") .describe("Retrieves request and response details for one Resend API log.") .account(RESEND_ACCOUNT, RESEND_FULL_ACCESS_ACCOUNT_OPTIONS) .input(z.object({ logId: z.uuid() })) .output(LOG_SCHEMA) .retry({ replaySafety: "safe" }) .handler( async ({ account, input }) => await getResendApi(account).call( `logs/${encodeURIComponent(input.logId)}`, { responseSchema: LOG_WIRE_SCHEMA }, ), )