import {z} from 'zod' export type GetApiFinalizeV2TasksArgs = {page?: number | undefined, per_page?: number | undefined, filter?: Array> | undefined}; export type GetApiFinalizeV2TasksOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiFinalizeV2TasksResponse = z.object({data: z.array(z.object({id: z.string(), active: z.boolean(), agreement_id: z.string().nullable(), approval_type: z.enum(["latest_version", "text", "attachment"]).nullable(), assignee_ids: z.array(z.string()), attachment_id: z.string().nullable(), author_id: z.string().nullable(), created_at: z.string(), due_date: z.string().nullable(), name: z.string().nullable(), position: z.number().int().nullable(), resolver_id: z.string().nullable(), status: z.enum(["unreviewed", "approved", "rejected(deprecated)"]), text: z.string(), type: z.enum(["ApprovalTask", "GenericTask", "StandaloneTask"])})), total_count: z.number().int(), _links: z.object({next: z.object({href: z.string().optional()}).optional(), prev: z.object({href: z.string().optional()}).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 }; /** * List Tasks * * GET /finalize/v2/tasks */ export const getApiFinalizeV2Tasks = async (args: GetApiFinalizeV2TasksArgs, options?: GetApiFinalizeV2TasksOptions) => { const {baseUrl = "https://api.linksquares.com/api", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) for (const item of args.filter ?? []) query.append("filter", String(item)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/finalize/v2/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(getApiFinalizeV2TasksResponse, await res.json()) };