import {z} from 'zod' export type DateISO = string; export type GetApiTasksArgs = {limit?: number | undefined, cursor?: string | undefined, modifiedSince?: DateISO | undefined, modifiedUntil?: DateISO | undefined, candidateIds?: Array | undefined, clientContactIds?: Array | undefined, vacancyIds?: Array | undefined, companyIds?: Array | undefined, opportunityIds?: Array | undefined, sources?: (Array<'MANUAL' | 'AI_SUGGESTION' | 'OUTREACH_SEQUENCE_STEP' | 'AUTOMATION'>) | undefined, labelIds?: Array | undefined}; export type GetApiTasksOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dateISO = z.string(); export const userBadgeDto = z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}); export const taskMetadataDto = z.union([z.object({outreachSequenceId: z.string(), outreachSequenceName: z.string(), outreachSequenceRunId: z.string(), type: z.literal("GENERIC_TASK")}), z.object({outreachSequenceId: z.string(), outreachSequenceName: z.string(), outreachSequenceRunId: z.string(), type: z.literal("PHONE_CALL_TASK"), phoneNumber: z.string().nullable()}), z.object({outreachSequenceId: z.string(), outreachSequenceName: z.string(), outreachSequenceRunId: z.string(), type: z.literal("LINKEDIN_CONNECTION_REQUEST_TASK"), linkedInUrl: z.string().nullable()}), z.object({outreachSequenceId: z.string(), outreachSequenceName: z.string(), outreachSequenceRunId: z.string(), type: z.literal("LINKEDIN_MESSAGE_TASK"), linkedInUrl: z.string().nullable(), messageBody: z.string().nullable()}), z.object({outreachSequenceId: z.string(), outreachSequenceName: z.string(), outreachSequenceRunId: z.string(), type: z.literal("WHATSAPP_MESSAGE_TASK"), phoneNumber: z.string().nullable(), messageBody: z.string().nullable()})]); export const taskLabelDto = z.object({id: z.string(), name: z.string(), colorHex: z.string()}); export const taskLinkIdDto = z.object({id: z.string()}); export const getTaskLinksDto = z.object({candidates: z.array(taskLinkIdDto), vacancies: z.array(taskLinkIdDto), clients: z.array(taskLinkIdDto), users: z.array(taskLinkIdDto), teams: z.array(taskLinkIdDto), clientContacts: z.array(taskLinkIdDto), opportunities: z.array(taskLinkIdDto)}); export const getTaskDto = z.object({id: z.string(), content: z.string(), dueDate: dateISO.nullable(), isCompleted: z.boolean(), completedAt: dateISO.nullable(), createdAt: dateISO, createdBy: userBadgeDto.nullable(), assignedTo: userBadgeDto.nullable(), source: z.enum(["MANUAL", "AI_SUGGESTION", "OUTREACH_SEQUENCE_STEP", "AUTOMATION"]), metadata: taskMetadataDto.nullable(), labels: z.array(taskLabelDto), links: getTaskLinksDto}); export const cursorPageInfoDto = z.object({nextCursor: z.string().nullable(), hasNextPage: z.boolean()}); export const getTasksResponseDto = z.object({items: z.array(getTaskDto), pageInfo: cursorPageInfoDto}); 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 }; /** * List tasks * * GET /tasks */ export const getApiTasks = async (args: GetApiTasksArgs, options: GetApiTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.modifiedSince != null) query.append("modifiedSince", String(args.modifiedSince)) if (args.modifiedUntil != null) query.append("modifiedUntil", String(args.modifiedUntil)) for (const item of args.candidateIds ?? []) query.append("candidateIds", String(item)) for (const item of args.clientContactIds ?? []) query.append("clientContactIds", String(item)) for (const item of args.vacancyIds ?? []) query.append("vacancyIds", String(item)) for (const item of args.companyIds ?? []) query.append("companyIds", String(item)) for (const item of args.opportunityIds ?? []) query.append("opportunityIds", String(item)) for (const item of args.sources ?? []) query.append("sources", String(item)) for (const item of args.labelIds ?? []) query.append("labelIds", String(item)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/tasks${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(getTasksResponseDto, await res.json()) };