import {z} from 'zod' export type CreateApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationIdArgs = {environmentId: string, integrationId: string, 'idempotency-key'?: string | undefined, body: {[key: string]: unknown}}; export type CreateApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationIdOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const eventBody = z.object({status: z.enum(["opened", "rejected", "sent", "deferred", "delivered", "bounced", "dropped", "clicked", "blocked", "spam", "unsubscribed", "delayed", "complaint", "created", "delivered", "accepted", "queued", "sending", "sent", "failed", "undelivered", "rejected", "delivered", "opened", "dismissed", "clicked", "failed"]), date: z.string(), externalId: z.string().optional(), attempts: z.number().optional(), response: z.string().optional(), row: z.string().optional()}); export const webhookResultDto = z.object({id: z.string(), event: eventBody}); export const createApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationIdResponse = z.array(webhookResultDto); 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 }; /** * Track provider activity and engagement events * * POST /v2/inbound-webhooks/delivery-providers/{environmentId}/{integrationId} */ export const createApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationId = async (args: CreateApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationIdArgs, options?: CreateApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationIdOptions) => { const {baseUrl = "https://api.novu.co", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (secretKey !== undefined) headers.set("Authorization", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/inbound-webhooks/delivery-providers/${encodeURIComponent(String(args.environmentId))}/${encodeURIComponent(String(args.integrationId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2InboundWebhooksDeliveryProvidersEnvironmentIdIntegrationIdResponse, await res.json()) };