import {z} from 'zod' export type CreateApiCommentsArgs = {body: {review_id: number, parent_id?: number | undefined, content: unknown, reply_template_id?: number | undefined, reply_suggestion?: ({reply_suggestion_id?: number | undefined, reply_suggestion_modified?: boolean | undefined}) | undefined}}; export type CreateApiCommentsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commentId = z.number().int(); export const reviewPublisherId = z.string(); export const createdDate = z.string(); export const reviewAuthorName = z.string(); export const commentBody = z.string(); export const reviewId = z.number().int(); export const parentId = z.number().int(); export const comment: z.ZodType = z.object({id: commentId.optional(), partner_id: reviewPublisherId.optional(), created: createdDate.optional(), author_name: reviewAuthorName.optional(), content: commentBody.optional(), date: z.string().optional(), update_date: z.string().optional(), can_edit: z.boolean().optional(), review_id: reviewId.optional(), parent_id: parentId.optional(), user_id: z.string().optional(), is_reply_suggestion: z.boolean().optional(), replies: z.array(z.lazy(() => comment)).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 }; /** * Post a reply to a review * * POST /comments */ export const createApiComments = async (args: CreateApiCommentsArgs, options?: CreateApiCommentsOptions) => { 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(/\/+$/, '')}/comments${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(comment, await res.json()) };