import {z} from 'zod' export type TemplateLanguage = 'fr' | 'en' | 'es' | 'it' | 'pt-br' | 'de' | 'ar' | 'nl' | 'pl' | 'cs' | 'ca' | 'sk' | 'pt' | 'lv' | 'ro' | 'bg' | 'hu'; export type GetApiReviewsTemplatesSearchArgs = {page?: number | undefined, positive?: boolean | undefined, neutral?: boolean | undefined, negative?: boolean | undefined, language__in?: Array | undefined, title_query?: string | undefined, text_query?: string | undefined}; export type GetApiReviewsTemplatesSearchOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currentPage = z.number().int(); export const maxPage = z.number().int(); export const count = z.number().int(); export const templateId = z.number().int(); export const orgId = z.number().int(); export const templateUpdateDate = z.string(); export const templateCreationDate = z.string(); export const templateLanguage = z.enum(["fr", "en", "es", "it", "pt-br", "de", "ar", "nl", "pl", "cs", "ca", "sk", "pt", "lv", "ro", "bg", "hu"]); export const templateTitle = z.string(); export const templateText = z.string(); export const templateNegative = z.boolean(); export const templateNeutral = z.boolean(); export const templatePositive = z.boolean(); export const template = z.object({id: templateId.optional(), org_id: orgId.optional(), update_date: templateUpdateDate.optional(), creation_date: templateCreationDate.optional(), language: templateLanguage.optional(), title: templateTitle.optional(), text: templateText.optional(), negative: templateNegative.optional(), neutral: templateNeutral.optional(), positive: templatePositive.optional()}); export const getApiReviewsTemplatesSearchResponse = z.object({page: currentPage.optional(), max_page: maxPage.optional(), count: count.optional(), reply_templates: z.array(template).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 }; /** * Search for review reply templates * * GET /reviews/templates/search */ export const getApiReviewsTemplatesSearch = async (args: GetApiReviewsTemplatesSearchArgs, options?: GetApiReviewsTemplatesSearchOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.positive != null) query.append("positive", String(args.positive)) if (args.neutral != null) query.append("neutral", String(args.neutral)) if (args.negative != null) query.append("negative", String(args.negative)) for (const item of args.language__in ?? []) query.append("language__in", String(item)) if (args.title_query != null) query.append("title_query", String(args.title_query)) if (args.text_query != null) query.append("text_query", String(args.text_query)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reviews/templates/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiReviewsTemplatesSearchResponse, await res.json()) };