import {z} from 'zod' export type WebhookUrl = string; export type WebhookEventType = string; export type CustomHeaders = {[key: string]: string}; export type CreateWebhookPayloadItem = {url: WebhookUrl, event_type: WebhookEventType, custom_headers?: CustomHeaders | undefined}; export type CreateWebhooksPayload = {items: Array}; export type CreateApiWebhooksArgs = {org_id?: number | undefined, body: CreateWebhooksPayload}; export type CreateApiWebhooksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const url = z.string(); export const eventType = z.enum(["business_created", "business_updated", "business_deleted", "message_updated", "message_created", "lead_received", "review_created", "review_updated", "review_reply_created", "review_reply_updated", "review_reply_deleted", "email_opt_out_created", "post_created", "post_updated", "post_validated", "post_deleted", "scheduled_post_pushed"]); export const customHeaders = z.record(z.string(), z.string().max(512)); export const webhookItem = z.object({id: z.number().int(), url: url, event_type: eventType, created_at: z.string(), updated_at: z.string(), org_id: z.number().int(), custom_headers: customHeaders.optional()}); export const createApiWebhooksResponse = z.object({items: z.array(webhookItem).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 }; /** * Create webhook configurations * * POST /webhooks */ export const createApiWebhooks = async (args: CreateApiWebhooksArgs, options?: CreateApiWebhooksOptions) => { 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 (args.org_id != null) query.append("org_id", String(args.org_id)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/webhooks${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(createApiWebhooksResponse, await res.json()) };