import {z} from 'zod' export type PromoCodeType = 'balance' | 'subscription_days' | 'trial_subscription' | 'promo_group' | 'discount'; export type AppWebapiSchemasPromocodesPromoCodeUpdateRequest = {code?: (string | null) | undefined, type?: (PromoCodeType | null) | undefined, balance_bonus_kopeks?: (number | null) | undefined, subscription_days?: (number | null) | undefined, max_uses?: (number | null) | undefined, valid_from?: (string | null) | undefined, valid_until?: (string | null) | undefined, is_active?: (boolean | null) | undefined}; export type PatchApiPromoCodesPromocodeIdArgs = {promocode_id: number, body: AppWebapiSchemasPromocodesPromoCodeUpdateRequest}; export type PatchApiPromoCodesPromocodeIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const promoCodeType = z.enum(["balance", "subscription_days", "trial_subscription", "promo_group", "discount"]); export const appWebapiSchemasPromocodesPromoCodeResponse = z.object({id: z.number().int(), code: z.string(), type: promoCodeType, balance_bonus_kopeks: z.number().int(), balance_bonus_rubles: z.number(), subscription_days: z.number().int(), max_uses: z.number().int(), current_uses: z.number().int(), uses_left: z.number().int(), is_active: z.boolean(), is_valid: z.boolean(), valid_from: z.string(), valid_until: z.string().nullable().optional(), created_by: z.number().int().nullable().optional(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Update Promocode Endpoint * * PATCH /promo-codes/{promocode_id} */ export const patchApiPromoCodesPromocodeId = async (args: PatchApiPromoCodesPromocodeIdArgs, options: PatchApiPromoCodesPromocodeIdOptions) => { 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-codes/${encodeURIComponent(String(args.promocode_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(appWebapiSchemasPromocodesPromoCodeResponse, await res.json()) };