import {z} from 'zod' export type Invite = {questionnaireTemplate: {id: string}, template: {id: string}, customer: {firstName?: string | undefined, lastName?: string | undefined, email: string}, locale?: string | undefined, transaction: {reference: string, date?: string | undefined}, products?: (Array<{gtin?: string | undefined, imageUrl?: string | undefined, name: string, mpn?: string | undefined, sku: string, brand?: string | undefined, url: string}>) | undefined, preferredSendTime?: string | undefined, metadata?: Record | undefined}; export type InvitesRequest = {channel: {id: string, type?: ('user_defined' | 'etrusted') | undefined}, system: string, systemVersion: string, invites: Array}; export type CreateApiInvitesArgs = {body: InvitesRequest}; export type CreateApiInvitesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const invite = z.object({questionnaireTemplate: z.object({id: z.string()}), template: z.object({id: z.string()}), customer: z.object({firstName: z.string().optional(), lastName: z.string().optional(), email: z.string()}), locale: z.string().optional(), transaction: z.object({reference: z.string(), date: z.string().optional()}), products: z.array(z.object({gtin: z.string().optional(), imageUrl: z.string().optional(), name: z.string(), mpn: z.string().optional(), sku: z.string(), brand: z.string().optional(), url: z.string()})).optional(), preferredSendTime: z.string().optional(), metadata: z.object({}).optional()}); export const failedInvite = z.object({initialInvite: invite, status: z.number().int(), reasons: z.array(z.string())}); export const invitesResponse = z.object({batchId: z.string().optional(), successfulInvites: z.number().int(), failedInvites: z.array(failedInvite).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 }; /** * Schedule new invites * * POST /invites */ export const createApiInvites = async (args: CreateApiInvitesArgs, options: CreateApiInvitesOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/invites${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(invitesResponse, await res.json()) };