import {z} from 'zod' export type TaskRequest = {name: string, description?: (string | null) | undefined, status?: (string | null) | undefined, priority?: (number | null) | undefined, due_date?: (number | null) | undefined, subject_type?: (string | null) | undefined, subject?: (string | null) | undefined, userid?: (string | null) | undefined, action?: (string | null) | undefined, action_data?: ({[key: string]: unknown} | null) | undefined, tags?: (Array | null) | undefined}; export type CreateApiCrmTasksArgs = {body: TaskRequest}; export type CreateApiCrmTasksOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const taskResponse = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), status: z.string().nullable().optional(), priority: z.number().int().nullable().optional(), due_date: z.string().nullable().optional(), is_completed: z.boolean().nullable().optional(), completed_date: z.string().nullable().optional(), subject_type: z.string().nullable().optional(), subject: z.string().nullable().optional(), userid: z.string().nullable().optional(), created_by_userid: z.string().nullable().optional(), action: z.string().nullable().optional(), action_data: z.record(z.string(), z.unknown()).nullable().optional(), tags: z.array(z.string()).nullable().optional(), created_date: z.string().nullable().optional(), updated_date: z.string().nullable().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 }; /** * Post Task * * POST /crm/tasks */ export const createApiCrmTasks = async (args: CreateApiCrmTasksArgs, options: CreateApiCrmTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/crm/tasks${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(taskResponse, await res.json()) };