import {z} from 'zod' export type GetApiV6JobsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); 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 triggerResponse = z.discriminatedUnion("trigger_type", [z.object({id: z.string(), trigger_type: z.literal("FILE_NAME_MATCHES"), match_type: z.enum(["EXACT", "REGEX"]), pattern: z.string()}), z.object({id: z.string(), trigger_type: z.literal("LANDS_IN_DIRECTORY"), directory_path: z.string()})]); export const jobResponse = z.object({client_id: z.string(), created_date: zonedDateTime, enabled: z.boolean(), id: z.string(), name: z.string(), tasks: z.array(taskResponse), triggers: z.array(triggerResponse), updated_date: zonedDateTime}); export const getApiV6JobsResponse = z.array(jobResponse); 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 jobs * * GET /v6/jobs * * Also accepts auth: QUERY, COOKIE */ export const getApiV6Jobs = async (options?: GetApiV6JobsOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/jobs${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(getApiV6JobsResponse, await res.json()) };