import {z} from 'zod' export type GetApiWorkersWorkerHandleApproversArgs = {worker_handle: string, limit?: (number | null) | undefined, pagination_token?: (string | null) | undefined, approver_type: unknown}; export type GetApiWorkersWorkerHandleApproversOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const workerApproverResponse = z.object({worker_handle: z.string(), approver_id: z.string(), approver_type: z.string(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const workerApproverListResponse = z.object({approvers: z.array(workerApproverResponse), total_count: z.number().int(), limit: z.number().int(), pagination_token: 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 }; /** * List Worker Approvers * * GET /workers/{worker_handle}/approvers * * Also accepts auth: TokenAuth */ export const getApiWorkersWorkerHandleApprovers = async (args: GetApiWorkersWorkerHandleApproversArgs, options?: GetApiWorkersWorkerHandleApproversOptions) => { const {baseUrl = "https://v3.api.hypertrack.com", fetch: fetchFn = fetch, BasicAuth} = 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.pagination_token != null) query.append("pagination_token", String(args.pagination_token)) if (args.approver_type != null) query.append("approver_type", String(args.approver_type)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/workers/${encodeURIComponent(String(args.worker_handle))}/approvers${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(workerApproverListResponse, await res.json()) };