import {z} from 'zod' export type BusinessId = string; export type GetApiPostsArgs = {business__in?: Array | undefined, start_datetime?: string | undefined, end_datetime?: string | undefined, summary?: string | undefined, state__in?: ('live' | 'to_configure' | 'pending' | 'scheduled' | 'error' | 'expired' | 'to_validate') | undefined, topic_type?: ('standard' | 'event' | 'offer' | 'alert') | undefined, topic_type__in?: ('standard' | 'event' | 'offer' | 'alert') | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiPostsOptions = {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 postId = z.number().int(); export const businessId = z.string(); export const postType = z.enum(["news", "event", "offer", "covid"]); export const postSummary = z.string(); export const postImageURL = z.union([z.string(), z.array(z.string())]); export const postMedias = z.array(z.object({media_url: postImageURL.optional()})); export const createdDate = z.string(); export const updatedDate = z.string(); export const postCreatedOnPartoo = z.boolean(); export const postUpdatedOnPartoo = z.boolean(); export const postBusinessInfo = z.string(); export const postPublisherPost = z.enum(["google_my_business__posts", "facebook__posts"]); export const postInsight = z.array(z.object({name: postPublisherPost.optional(), click_count: z.number().int().optional(), view_count: z.number().int().optional()})); export const postState = z.enum(["live", "pending", "to_configure", "scheduled", "error", "expired"]); export const postCTALink = z.string(); export const postStatus = z.array(z.object({name: postPublisherPost.optional(), state: postState.optional(), link: postCTALink.optional()})); export const postOfferTerms = z.string(); export const postCouponCode = z.string(); export const postCTAType = z.enum(["book", "order", "shop", "learn_more", "sign_up", "call", "no_cta"]); export const post = z.object({id: postId.optional(), business_id: businessId.optional(), post_type: postType.optional(), summary: postSummary.optional(), schedule_time: z.string().optional(), expiration_time: z.string().optional(), validation_time: z.string().optional(), post_medias: postMedias.optional(), created_at: createdDate.optional(), updated_at: updatedDate.optional(), created_on_partoo: postCreatedOnPartoo.optional(), updated_on_partoo: postUpdatedOnPartoo.optional(), business_info: postBusinessInfo.optional(), post_insight: postInsight.optional(), post_status: postStatus.optional(), offer_terms: postOfferTerms.optional(), offer_code: postCouponCode.optional(), cta_link: postCTALink.optional(), cta_type: postCTAType.optional()}); export const getApiPostsResponse = z.object({page: currentPage.optional(), max_page: maxPage.optional(), count: count.optional(), posts: z.array(post).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 post * * GET /posts */ export const getApiPosts = async (args: GetApiPostsArgs, options?: GetApiPostsOptions) => { 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)) if (args.start_datetime != null) query.append("start_datetime", String(args.start_datetime)) if (args.end_datetime != null) query.append("end_datetime", String(args.end_datetime)) if (args.summary != null) query.append("summary", String(args.summary)) if (args.state__in != null) query.append("state__in", String(args.state__in)) if (args.topic_type != null) query.append("topic_type", String(args.topic_type)) if (args.topic_type__in != null) query.append("topic_type__in", String(args.topic_type__in)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/posts${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(getApiPostsResponse, await res.json()) };