import {z} from 'zod' export type GetApiBatchProcessesArgs = {'Account-ID': string, 'filters[status]'?: ('pending' | 'in_progress' | 'completed' | 'failed' | 'stopped' | Array<'pending' | 'in_progress' | 'completed' | 'failed' | 'stopped'>) | undefined}; export type GetApiBatchProcessesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const batchProcess = z.object({id: z.number().int().optional(), status: z.enum(["pending", "in_progress", "completed", "failed", "stopped"]).optional(), total: z.number().int().optional(), selected_total: z.number().int().optional(), processed_count: z.number().int().optional(), failed_count: z.number().int().optional(), action_label: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), meta: z.object({}).nullable().optional(), progress: z.number().int().optional(), processing_errors: z.array(z.string()).optional()}); export const getApiBatchProcessesResponse = z.array(batchProcess); 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 in progress batch processes. * * GET /batch_processes */ export const getApiBatchProcesses = async (args: GetApiBatchProcessesArgs, options?: GetApiBatchProcessesOptions) => { const {baseUrl = "https://api.firstpromoter.com/api/v2/company", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Account-ID'] != null) headers.set("Account-ID", String(args['Account-ID'])) if (args['filters[status]'] != null) query.append("filters[status]", String(args['filters[status]'])) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/batch_processes${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(getApiBatchProcessesResponse, await res.json()) };