import {z} from 'zod' export type DailyReviewPreferences = {partners?: (Array<'facebook' | 'google_my_business' | 'tripadvisor'>) | undefined, review_type?: (1 | 2 | 3) | undefined}; export type UserReports = {review_management_daily?: boolean | undefined, review_management_weekly?: boolean | undefined, review_management_monthly?: boolean | undefined, daily_review_preferences?: DailyReviewPreferences | undefined}; export type ReceiveNegativeReviewsEmailNotifications = boolean; export type CreateApiUserUserIdPreferencesArgs = {user_id: string, body: {reports?: UserReports | undefined, receive_negative_reviews_email_notifications?: ReceiveNegativeReviewsEmailNotifications | undefined}}; export type CreateApiUserUserIdPreferencesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const userId = z.string(); export const createApiUserUserIdPreferencesResponse = z.object({id: userId.optional(), status: z.literal("success").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 }; /** * Set user preferences * * POST /user/{user_id}/preferences */ export const createApiUserUserIdPreferences = async (args: CreateApiUserUserIdPreferencesArgs, options?: CreateApiUserUserIdPreferencesOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/user/${encodeURIComponent(String(args.user_id))}/preferences${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(createApiUserUserIdPreferencesResponse, await res.json()) };