import {z} from 'zod' export type TaskRequest = {order: number, task_type: 'FILE_HEADER_VALIDATOR', expected_headers: Array, match_mode: 'EXACT_SET' | 'ORDERED' | 'SUBSET', on_mismatch: 'FAIL' | 'WARN'} | {order: number, task_type: 'START_AUDIENCE_OPERATION'} | {order: number, task_type: 'START_BATCH_JOB', data_source_type: 'AUDIENCE_LIST' | 'FILE_ASSET' | 'REPORT', event_name: string}; export type UpdateApiV6JobsJobIdTasksTaskIdArgs = {jobId: string, taskId: string, body: TaskRequest}; export type UpdateApiV6JobsJobIdTasksTaskIdOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const taskResponse = z.discriminatedUnion("task_type", [z.object({id: z.string(), order: z.number().int(), task_type: z.literal("FILE_HEADER_VALIDATOR"), expected_headers: z.array(z.string()), match_mode: z.enum(["EXACT_SET", "ORDERED", "SUBSET"]), on_mismatch: z.enum(["FAIL", "WARN"])}), z.object({id: z.string(), order: z.number().int(), task_type: z.literal("START_AUDIENCE_OPERATION")}), z.object({id: z.string(), order: z.number().int(), task_type: z.literal("START_BATCH_JOB"), data_source_type: z.enum(["AUDIENCE_LIST", "FILE_ASSET", "REPORT"]), event_name: z.string()})]); 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 }; /** * Update a task * * PUT /v6/jobs/{jobId}/tasks/{taskId} * * Also accepts auth: QUERY, COOKIE */ export const updateApiV6JobsJobIdTasksTaskId = async (args: UpdateApiV6JobsJobIdTasksTaskIdArgs, options?: UpdateApiV6JobsJobIdTasksTaskIdOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/jobs/${encodeURIComponent(String(args.jobId))}/tasks/${encodeURIComponent(String(args.taskId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(taskResponse, await res.json()) };