import {z} from 'zod' export type GetApiV1MessageLogsArgs = {messageID?: string | undefined, changeType?: ('CampaignEvaluationTreeChanged' | 'CampaignNotification' | 'CouponCreated' | 'CouponUpdated' | 'CouponDeleted' | 'AsyncCouponsCreated' | 'CouponsDeleted' | 'CouponsUpdated' | 'CouponCodeExpiring' | 'StrikethroughPrice' | 'LoyaltyPointsAdded' | 'LoyaltyPointsDeducted' | 'LoyaltyPointsExpiring' | 'LoyaltyPointsPendingToActive' | 'LoyaltyAddedDeductedPointsBalances' | 'LoyaltyCardAddedDeductedPointsBalances' | 'TierWillDowngrade' | 'TierUpgrade' | 'TierDowngrade' | 'LoyaltyCardPointsAdded' | 'LoyaltyCardPointsDeducted' | 'LoyaltyCardPointsExpiring') | undefined, notificationIDs?: string | undefined, createdBefore?: string | undefined, createdAfter?: string | undefined, cursor?: string | undefined, pageSize?: number | undefined, period?: ('15m' | '30m' | '1h' | '4h' | '1d' | '2d') | undefined, isSuccessful?: boolean | undefined, entityType: 'application' | 'loyalty_program' | 'webhook', applicationId?: number | undefined, campaignId?: number | undefined, loyaltyProgramId?: number | undefined, responseCode?: number | undefined, webhookIDs?: string | undefined}; export type GetApiV1MessageLogsOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const messageLogRequest = z.object({createdAt: z.string(), request: z.string()}); export const messageLogResponse = z.object({createdAt: z.string().optional(), response: z.string().optional(), status: z.number().int().optional()}); export const messageLogEntry = z.object({id: z.string(), service: z.string(), changeType: z.string().optional(), notificationId: z.number().int().optional(), notificationName: z.string().optional(), webhookId: z.number().int().optional(), webhookName: z.string().optional(), request: messageLogRequest.optional(), response: messageLogResponse.optional(), createdAt: z.string(), entityType: z.enum(["application", "loyalty_program", "webhook"]), url: z.string().optional(), applicationId: z.number().int().gte(1).optional(), loyaltyProgramId: z.number().int().gte(1).optional(), campaignId: z.number().int().gte(1).optional()}); export const messageLogEntries = z.object({nextCursor: z.string().optional(), data: z.array(messageLogEntry)}); 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 message log entries * * GET /v1/message_logs * * Also accepts auth: management_key */ export const getApiV1MessageLogs = async (args: GetApiV1MessageLogsArgs, options?: GetApiV1MessageLogsOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.messageID != null) query.append("messageID", String(args.messageID)) if (args.changeType != null) query.append("changeType", String(args.changeType)) if (args.notificationIDs != null) query.append("notificationIDs", String(args.notificationIDs)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.period != null) query.append("period", String(args.period)) if (args.isSuccessful != null) query.append("isSuccessful", String(args.isSuccessful)) if (args.entityType != null) query.append("entityType", String(args.entityType)) if (args.applicationId != null) query.append("applicationId", String(args.applicationId)) if (args.campaignId != null) query.append("campaignId", String(args.campaignId)) if (args.loyaltyProgramId != null) query.append("loyaltyProgramId", String(args.loyaltyProgramId)) if (args.responseCode != null) query.append("responseCode", String(args.responseCode)) if (args.webhookIDs != null) query.append("webhookIDs", String(args.webhookIDs)) if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/message_logs${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(messageLogEntries, await res.json()) };