import {z} from 'zod' export type TaskDateRange = {startDate?: string | undefined, endDate?: string | undefined}; export type CreateApiApiTasksSearchArgs = {body: {userIds?: Array | undefined, isChecked?: boolean | undefined, isPinned?: boolean | undefined, archived?: boolean | undefined, taskPriority?: string | undefined, status?: (string | Array) | undefined, streamId?: string | undefined, channelId?: string | undefined, aptletUuid?: string | undefined, aptletInstanceId?: string | undefined, leaderboard?: boolean | undefined, onlyAssigned?: boolean | undefined, unAssigned?: boolean | undefined, useCount?: boolean | undefined, limit?: number | undefined, dueAt?: TaskDateRange | undefined, checkedAt?: TaskDateRange | undefined, updatedAt?: TaskDateRange | undefined}}; export type CreateApiApiTasksSearchOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const task = z.object({_id: z.string().optional(), companyId: z.string().optional(), title: z.string().optional(), mergedTitle: z.string().optional(), description: z.string().optional(), mergedDescription: z.string().optional(), userId: z.string().optional(), checked: z.boolean().optional(), archived: z.boolean().optional(), status: z.string().optional(), priority: z.string().optional(), dueAt: z.string().optional(), aptletInstanceId: z.string().optional(), aptletUuid: z.string().optional(), attachments: z.array(z.object({_id: z.string().optional(), name: z.string().optional(), type: z.string().optional()})).optional(), priorityLabel: z.string().optional(), statusLabel: z.string().optional(), assignee: z.object({_id: z.string().optional(), fullName: z.string().optional()}).optional()}).and(z.record(z.string(), z.unknown())); export const createApiApiTasksSearchResponse = z.object({tasks: z.array(task).optional(), count: z.number().int().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 }; /** * Search tasks * * POST /api/tasks/search * * Also accepts auth: DelegateToken */ export const createApiApiTasksSearch = async (args: CreateApiApiTasksSearchArgs, options?: CreateApiApiTasksSearchOptions) => { const {baseUrl = "https://core-api.getaptly.com", 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("x-token", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/tasks/search${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(createApiApiTasksSearchResponse, await res.json()) };