import {z} from 'zod' export type GetApiPreviewArgs = {job_id: string}; export type GetApiPreviewOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPreviewResponse = z.object({status: z.number().int().optional(), success: z.boolean().optional(), data: z.object({job_id: z.string().optional(), status: z.string().optional(), code: z.number().int().optional(), created_at: z.string().optional(), request_body: z.object({checks: z.array(z.string()).optional(), companyID: z.string().optional(), test_mode: z.boolean().optional(), job_type: z.string().optional(), has_live_key: z.unknown()}).optional(), payload: z.object({success: z.boolean().optional(), result: z.object({checks: z.array(z.object({employeeCheckID: z.string().optional(), gross_pay: z.string().optional(), net_pay: z.string().optional(), employer_taxes: z.string().optional(), employee_taxes: z.string().optional(), employee_deductions: z.string().optional(), employer_deductions: z.string().optional(), taxes: z.array(z.object({name: z.string().optional(), codename: z.string().optional(), amount: z.string().optional(), paidBy: z.string().optional()})).optional(), deductions: z.array(z.object({deduction_type: z.string().optional(), employee_calculated_contribution: z.string().optional(), employer_calculated_contribution: z.string().optional()})).optional(), flsa_ot_transformed: z.boolean().optional()})).optional(), total_flsa_transformed: z.number().int().optional(), total_employer_taxes: z.string().optional(), total_employee_taxes: z.string().optional(), total_employee_deductions: z.string().optional(), total_employer_deductions: z.string().optional()}).optional()}).optional()}).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 }; /** * Get Preview * * GET /preview */ export const getApiPreview = async (args: GetApiPreviewArgs, options?: GetApiPreviewOptions) => { const {baseUrl = "https://api.zeal.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.job_id != null) query.append("job_id", String(args.job_id)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/preview${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(getApiPreviewResponse, await res.json()) };