import {z} from 'zod' export type GetApiWorkspacesWorkspaceIdTasksArgs = {workspace_id: number, since?: number | undefined, page?: number | undefined, per_page?: number | undefined, sort_order?: string | undefined, sort_field?: string | undefined, active?: boolean | undefined, pid?: number | undefined, start_date?: string | undefined, end_date?: string | undefined}; export type GetApiWorkspacesWorkspaceIdTasksOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const taskWithTotal = z.object({active: z.boolean().optional(), at: z.string().optional(), estimated_seconds: z.number().int().nullable().optional(), id: z.number().int().optional(), name: z.string().optional(), permissions: z.string().optional(), project_id: z.number().int().optional(), recurring: z.boolean().optional(), server_deleted_at: z.string().nullable().optional(), toggl_accounts_id: z.string().nullable().optional(), tracked_seconds: z.number().int().optional(), user_id: z.number().int().nullable().optional(), workspace_id: z.number().int().optional()}); export const taskResponse = z.object({data: z.array(taskWithTotal).optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), sort_field: z.string().optional(), sort_order: z.string().optional(), total_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 }; /** * Tasks * * GET /workspaces/{workspace_id}/tasks */ export const getApiWorkspacesWorkspaceIdTasks = async (args: GetApiWorkspacesWorkspaceIdTasksArgs, options: GetApiWorkspacesWorkspaceIdTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.since != null) query.append("since", String(args.since)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.active != null) query.append("active", String(args.active)) if (args.pid != null) query.append("pid", String(args.pid)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/workspaces/${encodeURIComponent(String(args.workspace_id))}/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(taskResponse, await res.json()) };