import {z} from 'zod' export type TMAttachmentInfo = {id?: string | undefined, type?: ('File' | 'Note' | 'Event' | 'Card') | undefined}; export type TMCreatePostRequest = {text?: string | undefined, attachments?: Array | undefined}; export type CreateApiTeamMessagingV1ChatsChatIdPostsArgs = {chatId: string, body: TMCreatePostRequest}; export type CreateApiTeamMessagingV1ChatsChatIdPostsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const tMAttachmentAuthorInfo = z.object({name: z.string().optional(), uri: z.string().optional(), iconUri: z.string().optional()}); export const tMAttachmentFieldsInfo = z.object({title: z.string().optional(), value: z.string().optional(), style: z.enum(["Short", "Long"]).optional()}); export const tMAttachmentFootnoteInfo = z.object({text: z.string().optional(), iconUri: z.string().optional(), time: z.string().optional()}); export const eventRecurrenceInfo = z.object({schedule: z.enum(["None", "Day", "Weekday", "Week", "Month", "Year"]).optional(), endingCondition: z.enum(["None", "Count", "Date"]).optional(), endingAfter: z.number().int().optional(), endingOn: z.string().optional()}); export const tMMessageAttachmentInfo = z.object({id: z.string().optional(), type: z.enum(["Card", "Event", "File", "Note", "Task"]).optional(), fallback: z.string().optional(), intro: z.string().optional(), author: tMAttachmentAuthorInfo.optional(), title: z.string().optional(), text: z.string().optional(), imageUri: z.string().optional(), thumbnailUri: z.string().optional(), fields: z.array(tMAttachmentFieldsInfo).optional(), footnote: tMAttachmentFootnoteInfo.optional(), creatorId: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), allDay: z.boolean().optional(), recurrence: eventRecurrenceInfo.optional(), color: z.enum(["Black", "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Magenta"]).optional(), location: z.string().optional(), description: z.string().optional()}); export const tMMentionsInfo = z.object({id: z.string().optional(), type: z.enum(["Person", "Team", "File", "Link", "Event", "Task", "Note", "Card"]).optional(), name: z.string().optional()}); export const tMPostInfo = z.object({id: z.string().optional(), groupId: z.string().optional(), type: z.enum(["TextMessage", "PersonJoined", "PersonsAdded"]).optional(), text: z.string().optional(), creatorId: z.string().optional(), addedPersonIds: z.array(z.string()).optional(), creationTime: z.string().optional(), lastModifiedTime: z.string().optional(), attachments: z.array(tMMessageAttachmentInfo).optional(), mentions: z.array(tMMentionsInfo).optional(), activity: z.string().optional(), title: z.string().optional(), iconUri: z.string().optional(), iconEmoji: z.string().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 Post * * POST /team-messaging/v1/chats/{chatId}/posts */ export const createApiTeamMessagingV1ChatsChatIdPosts = async (args: CreateApiTeamMessagingV1ChatsChatIdPostsArgs, options?: CreateApiTeamMessagingV1ChatsChatIdPostsOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/team-messaging/v1/chats/${encodeURIComponent(String(args.chatId))}/posts${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(tMPostInfo, await res.json()) };