import {z} from 'zod' export type SubscriptionEventCreate = {event_type: 'activation' | 'purchase' | 'renewal' | 'balance_topup' | 'promocode_activation' | 'referral_link_visit' | 'promo_group_change', user_id: number, subscription_id?: (number | null) | undefined, transaction_id?: (number | null) | undefined, amount_kopeks?: (number | null) | undefined, currency?: (string | null) | undefined, message?: (string | null) | undefined, occurred_at?: (string | null) | undefined, extra?: {[key: string]: unknown} | undefined}; export type CreateApiNotificationsSubscriptionsArgs = {body: SubscriptionEventCreate}; export type CreateApiNotificationsSubscriptionsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionEventResponse = z.object({id: z.number().int(), event_type: z.string(), user_id: z.number().int(), user_full_name: z.string(), user_username: z.string().nullable().optional(), user_telegram_id: z.number().int().nullable().optional(), subscription_id: z.number().int().nullable().optional(), transaction_id: z.number().int().nullable().optional(), amount_kopeks: z.number().int().nullable().optional(), currency: z.string().nullable().optional(), message: z.string().nullable().optional(), occurred_at: z.string(), created_at: z.string(), extra: z.record(z.string(), z.unknown()).optional()}); 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 }; /** * Receive Subscription Event * * POST /notifications/subscriptions */ export const createApiNotificationsSubscriptions = async (args: CreateApiNotificationsSubscriptionsArgs, options: CreateApiNotificationsSubscriptionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/notifications/subscriptions${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(subscriptionEventResponse, await res.json()) };