import {z} from 'zod' export type CreateApiPreviewCheckDataArgs = {body: {companyID: string, employeeID: string, reportingPeriodID: string, check_date: string, approval_required?: boolean | undefined, metadata?: Record | undefined, deductions?: ({employer_contribution?: ({value?: number | undefined, contribution_type?: string | undefined}) | undefined, employee_contribution?: ({override_type?: string | undefined, contribution_type?: string | undefined, value?: number | undefined}) | undefined, deductionTemplateID: string}) | undefined, shifts: Array<{time: string, hourly?: ({hours: number, wage?: number | undefined, custom_name?: string | undefined}) | undefined}>, flsa_ot_recommendation?: boolean | undefined}}; export type CreateApiPreviewCheckDataOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPreviewCheckDataResponse = z.object({success: z.boolean().optional(), data: z.object({success: z.boolean().optional(), result: z.object({checks: z.array(z.object({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()})).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()}); 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 }; /** * Preview Check Data * * POST /preview/checkData */ export const createApiPreviewCheckData = async (args: CreateApiPreviewCheckDataArgs, options?: CreateApiPreviewCheckDataOptions) => { const {baseUrl = "https://api.zeal.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/preview/checkData${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiPreviewCheckDataResponse, await res.json()) };