import {z} from 'zod' export type GetApiV3JobsIdArgs = {id: string}; export type GetApiV3JobsIdOptions = {baseUrl?: string | undefined, customHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const integratorsApiError = z.object({code: z.number(), message: z.string(), details: z.object({}).optional()}); export const getApiV3JobsIdResponse = z.object({payload: z.object({id: z.string(), status: z.enum(["FAILURE", "SUCCESS", "IN_PROGRESS", "NEW"]), type: z.enum(["SYNC", "SYNC_DELETE", "LLM_RENEWALS", "INVOICE_PDF_EXPORT", "INTEGRATORS_CUSTOMER_IMPORT", "SEND_CUSTOMER_ADDRESS_TO_ERP", "SEND_PAYMENT_TO_ERP", "CREATE_CUSTOMER", "PROCESS_INBOUND_CUSTOMER_EMAIL", "GENERATE_REVENUE", "INTEGRATORS_ITEM_IMPORT", "QBO_AST_TAX_ESTIMATE_INVOICE_DELETE", "CREATE_SALES_ORDER", "UPDATE_SALES_ORDER", "DELETE_SALES_ORDER", "DUPLICATE_SANDBOX_DATA", "PROCESS_CSV", "DELETE_EVENT_CSV", "ERP_CANCEL_PAYMENT", "PROCESS_USAGE_AI", "PROCESS_TRANSACTION_CSV", "STRIPE_BULK_PAYOUT_APPLICATION", "TRANSACTION_RECOMMENDATIONS", "INVOICE_VECTOR_DB", "SEND_RECONCILIATION_TO_ERP", "REMOVE_RECONCILIATION_TO_ERP", "SEND_INVOICE_PAID_COMMUNICATIONS", "GENERATE_INVOICES", "SEND_CUSTOMER_ADDRESS_TO_ERP_WITH_ROLLBACK", "SEND_INVOICE_ACTION", "RECALCULATE_INVOICES_FOR_EVENTS", "AUTOMATIC_CONTRACT_PROCESSING", "CREATE_CONTRACT"]), processStartedAt: z.string(), startedAt: z.string(), finishedAt: z.string(), details: z.string().optional(), results: z.object({}).optional()}), success: z.boolean(), message: z.string(), error: integratorsApiError}); 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 }; /** * Get job by ID * * GET /v3/jobs/{id} */ export const getApiV3JobsId = async (args: GetApiV3JobsIdArgs, options?: GetApiV3JobsIdOptions) => { const {baseUrl = "https://integrators.prod.api.tabsplatform.com", fetch: fetchFn = fetch, customHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (customHeader !== undefined) headers.set("Authorization", customHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v3/jobs/${encodeURIComponent(String(args.id))}${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(getApiV3JobsIdResponse, await res.json()) };