import {z} from 'zod' export type BusinessId = string; export type ReviewPublisherLabel = string; export type ReviewState = 'treated' | 'not_treated' | 'deleted'; export type TagLabel = string; export type GetApiReviewsArgs = {business__in?: Array | undefined, business__notin?: Array | undefined, content__isnull?: ('true' | 'false') | undefined, partner__in?: Array | undefined, gmb_rating__in?: Array | undefined, tripadvisor_rating__in?: Array | undefined, recommended__in?: Array | undefined, update_date__gte?: unknown | undefined, update_date__lte?: unknown | undefined, state__in?: Array | undefined, state__notin?: Array | undefined, tag_label__in?: Array | undefined, without_tag?: boolean | undefined, keywords?: Array | undefined, with_media_links?: boolean | undefined, page?: number | undefined, per_page?: number | undefined, order_by?: ('update_date' | '-update_date') | undefined}; export type GetApiReviewsOptions = {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 orgId = z.number().int(); export const reviewId = z.number().int(); export const businessId = z.string(); export const reviewPublisherLabel = z.string(); export const reviewPublisherId = z.string(); export const reviewAuthorName = z.string(); export const reviewCreateDate = z.string(); export const reviewUpdateDate = z.string(); export const reviewDeleteDate = z.string(); export const reviewRating = z.number().int().gte(0).lte(5); export const reviewRecommended = z.boolean(); export const reviewTitle = z.string(); export const reviewBody = z.string(); export const reviewState = z.enum(["treated", "not_treated", "deleted"]); export const reviewLink = z.string(); export const commentId = z.number().int(); export const createdDate = z.string(); export const commentBody = z.string(); 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 tagId = z.number().int(); export const tagLabel = z.string(); export const tagColor = z.enum(["#808080", "#9B7CDB", "#F47FBE", "#4D4D4D", "#9E6957", "#2F8DE4", "#37CED0", "#53C944", "#B1DA34", "#F78234", "#F4BD38", "#992842"]); export const tag = z.object({id: tagId.optional(), label: tagLabel.optional(), color: tagColor.optional()}); export const review = z.object({key: z.string().optional(), org_id: orgId.optional(), id: reviewId.optional(), business_id: businessId.optional(), partner: reviewPublisherLabel.optional(), partner_id: reviewPublisherId.optional(), author_name: reviewAuthorName.optional(), date: reviewCreateDate.optional(), update_date: reviewUpdateDate.optional(), delete_date: reviewDeleteDate.optional(), rating: reviewRating.optional(), recommended: reviewRecommended.optional(), title: reviewTitle.optional(), content: reviewBody.optional(), state: reviewState.optional(), link: reviewLink.optional(), comments: z.array(comment).optional(), tags: z.array(tag).optional(), media_links: z.array(z.string()).optional()}); export const getApiReviewsResponse = z.object({page: currentPage.optional(), max_page: maxPage.optional(), count: count.optional(), reviews: z.array(review).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 reviews * * GET /reviews */ export const getApiReviews = async (args: GetApiReviewsArgs, options?: GetApiReviewsOptions) => { 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") for (const item of args.business__in ?? []) query.append("business__in", String(item)) for (const item of args.business__notin ?? []) query.append("business__notin", String(item)) if (args.content__isnull != null) query.append("content__isnull", String(args.content__isnull)) for (const item of args.partner__in ?? []) query.append("partner__in", String(item)) for (const item of args.gmb_rating__in ?? []) query.append("gmb_rating__in", String(item)) for (const item of args.tripadvisor_rating__in ?? []) query.append("tripadvisor_rating__in", String(item)) for (const item of args.recommended__in ?? []) query.append("recommended__in", String(item)) if (args.update_date__gte != null) query.append("update_date__gte", String(args.update_date__gte)) if (args.update_date__lte != null) query.append("update_date__lte", String(args.update_date__lte)) for (const item of args.state__in ?? []) query.append("state__in", String(item)) for (const item of args.state__notin ?? []) query.append("state__notin", String(item)) for (const item of args.tag_label__in ?? []) query.append("tag_label__in", String(item)) if (args.without_tag != null) query.append("without_tag", String(args.without_tag)) for (const item of args.keywords ?? []) query.append("keywords", String(item)) if (args.with_media_links != null) query.append("with_media_links", String(args.with_media_links)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reviews${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(getApiReviewsResponse, await res.json()) };