import {z} from 'zod' export type PostTitle = string; export type PostSummary = string; export type PostImageURL = string | Array; export type PostCTAType = 'book' | 'order' | 'shop' | 'learn_more' | 'sign_up' | 'call' | 'no_cta'; export type PostLink = {use_business_url?: boolean | undefined, custom_url?: string | undefined}; export type PostScheduleTime = string; export type PostStartAt = string; export type PostEndAt = string; export type PostCouponCode = string; export type PostOfferTerms = string; export type PostUpdateData = {title?: PostTitle | undefined, summary?: PostSummary | undefined, image_url?: PostImageURL | undefined, cta_type?: PostCTAType | undefined, link?: PostLink | undefined, schedule_time?: PostScheduleTime | undefined, start_at?: PostStartAt | undefined, end_at?: PostEndAt | undefined, coupon_code?: PostCouponCode | undefined, offer_terms?: PostOfferTerms | undefined}; export type UpdateApiPostsPostIdArgs = {post_id: number, body: PostUpdateData}; export type UpdateApiPostsPostIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 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 }; /** * Update a post * * PUT /posts/{post_id} */ export const updateApiPostsPostId = async (args: UpdateApiPostsPostIdArgs, options?: UpdateApiPostsPostIdOptions) => { 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(/\/+$/, '')}/posts/${encodeURIComponent(String(args.post_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(post, await res.json()) };