import {z} from 'zod' export type SegmentTypes = 'BroadcastMessage'; export type MediaPath = {fileName?: string | undefined, extension?: string | undefined, fileSaveId?: string | undefined, mimetype?: string | undefined, url?: string | undefined}; export type MediaDetails = {mediaPath?: (Array | null) | undefined}; export type FallbackMessage = {from?: (string | null) | undefined, message?: string | undefined, channelType?: string | undefined}; export type SegmentMessageRequest = {from?: string | undefined, message: string, templateId?: (string | null) | undefined, type?: SegmentTypes | undefined, mediaDetails?: (MediaDetails | null) | undefined, referenceId?: (string | null) | undefined, subscriberNumbers?: (Array | null) | undefined, segmentSubscriberFileUrl?: (string | null) | undefined, listIds?: (Array<{ListId: string}> | null) | undefined, channelType?: ('SMS' | 'MMS' | 'WHATSAPP') | undefined, fallbackMessage?: (FallbackMessage | null) | undefined}; export type CreateApiOrgunitOrgunitIdGroupsGroupIdMessagesSegmentArgs = {orgunitId: string, groupId: string, body: SegmentMessageRequest}; export type CreateApiOrgunitOrgunitIdGroupsGroupIdMessagesSegmentOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const segmentMessageResponse = z.object({referenceId: z.string().nullable().optional()}); export const segmentMessageResponseExternalApiResponseNew = z.object({data: segmentMessageResponse.optional(), appCode: z.string().nullable().optional(), message: z.string().nullable().optional(), isError: z.boolean().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 }; /** * Send SMS, MMS or WhatsApp to a Segment * * POST /orgunit/{orgunitId}/groups/{groupId}/messages/segment */ export const createApiOrgunitOrgunitIdGroupsGroupIdMessagesSegment = async (args: CreateApiOrgunitOrgunitIdGroupsGroupIdMessagesSegmentArgs, options: CreateApiOrgunitOrgunitIdGroupsGroupIdMessagesSegmentOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/orgunit/${encodeURIComponent(String(args.orgunitId))}/groups/${encodeURIComponent(String(args.groupId))}/messages/segment${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(segmentMessageResponseExternalApiResponseNew, await res.json()) };