import {z} from 'zod' export type AddUserAdExtra = {ad_id: number, ad_extra_id: number, ad_extra_day_id: number, amount: number, payment_gateway_id: number, gateway_id: number, buyer_name: string, buyer_email: string, buyer_address: string, buyer_city: string, buyer_state: string, buyer_country_iso2: string, buyer_phone: number, buyer_zipcode: number, credit_card_code: number, credit_card_expire: string, credit_card_name_on_card: string, credit_card_number: number}; export type CreateApiUserAdExtrasArgs = {token: unknown, body: AddUserAdExtra}; export type CreateApiUserAdExtrasOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userAdExtra = z.object({id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional(), ad_id: z.number().int().optional(), ad_extra_id: z.number().int().optional(), ad_extra_day_id: z.number().int().optional(), amount: z.number().optional(), payment_gateway_id: z.number().int().optional(), gateway_id: z.number().int().optional(), buyer_name: z.string().optional(), buyer_email: z.string().optional(), buyer_address: z.string().optional(), buyer_city: z.string().optional(), buyer_state: z.string().optional(), buyer_country_iso2: z.string().optional(), buyer_phone: z.number().int().optional(), buyer_zipcode: z.number().int().optional(), credit_card_code: z.number().int().optional(), credit_card_expire: z.string().optional(), credit_card_name_on_card: z.string().optional(), credit_card_number: z.number().int().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 }; /** * Creates a new user ad extra * * POST /user_ad_extras */ export const createApiUserAdExtras = async (args: CreateApiUserAdExtrasArgs, options: CreateApiUserAdExtrasOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) const url = `${baseUrl.replace(/\/+$/, '')}/user_ad_extras${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(userAdExtra, await res.json()) };