import {z} from 'zod' export type CreateApiAppAffiliateArgs = {body: {name: string, ref_code: string, percent_commission: number, email?: string | undefined, min_withdraw?: number | undefined, linked_discount_id?: number | undefined, package_commissions?: Array<{package_id: number, percent_commission: number}> | undefined, withdraw_settings?: ({full_name?: string | undefined, pix_key?: string | undefined, pix_key_type?: ('CPF' | 'CNPJ' | 'EMAIL' | 'PHONE' | 'RANDOM') | undefined, cpf_cnpj?: string | undefined}) | undefined}}; export type CreateApiAppAffiliateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAppAffiliateResponse = z.object({id: z.number().optional(), store_id: z.number().optional(), name: z.string().optional(), ref_code: z.string().optional(), percent_commission: z.number().optional(), balance: z.number().optional(), min_withdraw: z.number().optional(), email: z.string().optional(), self_affiliated: z.boolean().optional(), linked_discount_id: z.number().nullable().optional(), withdraw_settings: z.object({full_name: z.string().optional(), pix_key: z.string().optional(), pix_key_type: z.string().optional(), cpf_cnpj: z.string().optional()}).nullable().optional(), dash_secret: z.string().optional(), package_commissions: z.array(z.object({})).optional(), created_at: z.string().optional(), updated_at: z.string().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 Affiliate * * POST /app/affiliate */ export const createApiAppAffiliate = async (args: CreateApiAppAffiliateArgs, options?: CreateApiAppAffiliateOptions) => { const {baseUrl = "https://api.centralcart.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/app/affiliate${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(createApiAppAffiliateResponse, await res.json()) };