import {z} from 'zod' export type OutboundMessageType = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; export type OutboundMessageSendType = 1 | 2; export type OutboundMessageData = {id?: number | undefined, departmentId?: number | undefined, type: OutboundMessageType, name: string, message?: (string | null) | undefined, runCount?: number | undefined, workingHourType?: number | undefined, sendType?: OutboundMessageSendType | undefined, status?: number | undefined, settings: unknown, startTime?: (string | null) | undefined, endTime?: (string | null) | undefined, createdAt?: (string | null) | undefined, updatedAt?: (string | null) | undefined, lastRunAt?: (string | null) | undefined}; export type OutboundMessageFilterType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; export type OutboundMessageFilter = {id?: number | undefined, messageId?: number | undefined, filterType?: OutboundMessageFilterType | undefined, filterColumn?: (string | null) | undefined, filterValue?: (string | null) | undefined, filterOperation?: (string | null) | undefined, filterPrefix?: (string | null) | undefined}; export type OutboundMessage = {data: OutboundMessageData, filters: Array}; export type CreateApiV1OutboundMessagesArgs = {body: OutboundMessage}; export type CreateApiV1OutboundMessagesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const outboundMessageType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9), z.literal(10)]); export const outboundMessageSendType = z.union([z.literal(1), z.literal(2)]); export const outboundMessageData = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), type: outboundMessageType, name: z.string().min(1).max(128), message: z.string().nullable().optional(), runCount: z.number().int().optional(), workingHourType: z.number().int().optional(), sendType: outboundMessageSendType.optional(), status: z.number().int().optional(), settings: z.unknown(), startTime: z.string().nullable().optional(), endTime: z.string().nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), lastRunAt: z.string().nullable().optional()}); export const outboundMessageFilterType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8), z.literal(9)]); export const outboundMessageFilter = z.object({id: z.number().int().optional(), messageId: z.number().int().optional(), filterType: outboundMessageFilterType.optional(), filterColumn: z.string().nullable().optional(), filterValue: z.string().nullable().optional(), filterOperation: z.string().nullable().optional(), filterPrefix: z.string().nullable().optional()}); export const outboundMessage = z.object({data: outboundMessageData, filters: z.array(outboundMessageFilter)}); 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 }; /** * Saves an outbound message. * * POST /v1/outbound/messages * * Also accepts auth: API Key */ export const createApiV1OutboundMessages = async (args: CreateApiV1OutboundMessagesArgs, options: CreateApiV1OutboundMessagesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/outbound/messages${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(outboundMessage, await res.json()) };