import {z} from 'zod' export type PromoOfferCreateRequest = {user_id?: (number | null) | undefined, telegram_id?: (number | null) | undefined, notification_type: string, valid_hours: number, discount_percent?: number | undefined, bonus_amount_kopeks?: number | undefined, subscription_id?: (number | null) | undefined, effect_type?: string | undefined, extra_data?: {[key: string]: unknown} | undefined}; export type CreateApiPromoOffersArgs = {body: PromoOfferCreateRequest}; export type CreateApiPromoOffersOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const appWebapiSchemasPromoOffersPromoOfferUserInfo = z.object({id: z.number().int(), telegram_id: z.number().int().nullable().optional(), username: z.string().nullable().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), full_name: z.string().nullable().optional()}); export const promoOfferSubscriptionInfo = z.object({id: z.number().int(), status: z.string(), is_trial: z.boolean(), start_date: z.string(), end_date: z.string(), autopay_enabled: z.boolean()}); export const appWebapiSchemasPromoOffersPromoOfferResponse = z.object({id: z.number().int(), user_id: z.number().int(), subscription_id: z.number().int().nullable().optional(), notification_type: z.string(), discount_percent: z.number().int(), bonus_amount_kopeks: z.number().int(), expires_at: z.string(), claimed_at: z.string().nullable().optional(), is_active: z.boolean(), effect_type: z.string(), extra_data: z.record(z.string(), z.unknown()).optional(), created_at: z.string(), updated_at: z.string(), user: appWebapiSchemasPromoOffersPromoOfferUserInfo.nullable().optional(), subscription: promoOfferSubscriptionInfo.nullable().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 }; /** * Create Promo Offer * * POST /promo-offers */ export const createApiPromoOffers = async (args: CreateApiPromoOffersArgs, options: CreateApiPromoOffersOptions) => { 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(/\/+$/, '')}/promo-offers${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(appWebapiSchemasPromoOffersPromoOfferResponse, await res.json()) };