import {z} from 'zod' export type CreateApiCustomerEngagementReviewTemplateArgs = {body: {name: string, locationIds: Array, bodyText: string, senderName?: (string | null) | undefined, email?: ({senderEmailId: string, footerText: string, subject: string, actionButtonColor: string, privacyLink: string, mediaIds?: (Array | null) | undefined}) | undefined, reviewLinks: Array<{url: string, label?: (string | null) | undefined}>}}; export type CreateApiCustomerEngagementReviewTemplateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCustomerEngagementReviewTemplateResponse = z.object({id: z.number().int().nullable().optional(), type: z.string().nullable().optional(), name: z.string().nullable().optional(), locationIds: z.array(z.number().int()).nullable().optional(), bodyText: z.string().nullable().optional(), senderName: z.string().nullable().optional(), email: z.object({senderEmailId: z.string(), footerText: z.string(), subject: z.string(), actionButtonColor: z.string(), privacyLink: z.string().nullable().optional(), media: z.array(z.object({mediaId: z.number().int().nullable().optional(), message: z.string().nullable().optional(), uploadUrl: z.string().nullable().optional()})).nullable().optional()}).optional(), reviewLinks: z.array(z.object({url: z.string(), label: z.string().nullable().optional()})).nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), errors: z.array(z.string()).nullable().optional(), message: z.string().nullable().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 new review template for given location Ids * * POST /customer-engagement/review-template */ export const createApiCustomerEngagementReviewTemplate = async (args: CreateApiCustomerEngagementReviewTemplateArgs, options?: CreateApiCustomerEngagementReviewTemplateOptions) => { const {baseUrl = "https://uberall.com/api", 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(/\/+$/, '')}/customer-engagement/review-template${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(createApiCustomerEngagementReviewTemplateResponse, await res.json()) };