import {z} from 'zod' export type TalkManagerPinTargetType = 'chat' | 'message'; export type GetApiServiceAgentsTalkPinsArgs = {target_type?: TalkManagerPinTargetType | undefined, chat_id?: string | undefined, page_size?: number | undefined, page_token?: string | undefined}; export type GetApiServiceAgentsTalkPinsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const talkManagerPinTargetType = z.enum(["chat", "message"]); export const talkManagerMessageType = z.enum(["normal", "system"]); export const talkManagerMediaType = z.enum(["address", "agent", "file", "link"]); export const commonAddress = z.object({type: z.enum(["", "agent", "conference", "email", "extension", "line", "sip", "tel", "web_session"]).optional(), target: z.string().optional(), target_name: z.string().optional(), name: z.string().optional(), detail: z.string().optional()}); export const talkManagerMedia = z.object({type: talkManagerMediaType.optional(), address: commonAddress.optional(), agent_id: z.string().optional(), file_id: z.string().optional(), link_url: z.string().optional()}); export const talkManagerReaction = z.object({emoji: z.string().optional(), owner_type: z.string().optional(), owner_id: z.string().optional(), tm_create: z.string().optional()}); export const talkManagerMetadata = z.object({reactions: z.array(talkManagerReaction).optional()}); export const talkManagerMessage = z.object({id: z.string().optional(), customer_id: z.string().optional(), owner_type: z.string().optional(), owner_id: z.string().optional(), chat_id: z.string().optional(), parent_id: z.string().optional(), type: talkManagerMessageType.optional(), text: z.string().optional(), medias: z.array(talkManagerMedia).optional(), metadata: talkManagerMetadata.optional(), tm_create: z.string().optional(), tm_update: z.string().optional(), tm_delete: z.string().optional()}); export const talkManagerPin = z.object({id: z.string().optional(), target_type: talkManagerPinTargetType.optional(), target_id: z.string().optional(), chat_id: z.string().optional(), message: talkManagerMessage.optional(), tm_create: z.string().optional()}); export const getApiServiceAgentsTalkPinsResponse = z.object({next_page_token: z.string().optional(), result: z.array(talkManagerPin).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 }; /** * Get a list of the pins of the calling agent * * GET /service_agents/talk_pins */ export const getApiServiceAgentsTalkPins = async (args: GetApiServiceAgentsTalkPinsArgs, options?: GetApiServiceAgentsTalkPinsOptions) => { const {baseUrl = "https://api.voipbin.net/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.target_type != null) query.append("target_type", String(args.target_type)) if (args.chat_id != null) query.append("chat_id", String(args.chat_id)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) const url = `${baseUrl.replace(/\/+$/, '')}/service_agents/talk_pins${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(getApiServiceAgentsTalkPinsResponse, await res.json()) };