import {z} from 'zod' export type InvitationProduct = {sku: string, name?: string | undefined, imageUrl?: string | undefined, pageUrl?: string | undefined, gtin?: string | undefined, mpn?: string | undefined, description?: string | undefined, category?: string | undefined, lookup?: string | undefined}; export type UpdateApiProductInvitationIdArgs = {id: number, store: string, apikey: string, body: {name?: string | undefined, email?: string | undefined, order_id?: string | undefined, template_id?: string | undefined, phone?: string | undefined, date_send?: string | undefined, country_code?: string | undefined, branch?: string | undefined, products?: Array | undefined, status?: ('active' | 'inactive') | undefined}}; export type UpdateApiProductInvitationIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiProductInvitationIdResponse = z.object({success: z.string().optional(), message: 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 }; /** * Edit Product Invitations * * PUT /product/invitation/{id} */ export const updateApiProductInvitationId = async (args: UpdateApiProductInvitationIdArgs, options?: UpdateApiProductInvitationIdOptions) => { const {baseUrl = "https://api.reviews.io", 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.store != null) query.append("store", String(args.store)) if (args.apikey != null) query.append("apikey", String(args.apikey)) const url = `${baseUrl.replace(/\/+$/, '')}/product/invitation/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiProductInvitationIdResponse, await res.json()) };