import {z} from 'zod' export type TemplateQueryPaperworkType = 'W4' | 'I9' | 'W9' | 'CustomPaperwork'; export type WorkerType = 'Employee' | 'Contractor' | 'All'; export type JurisdictionFilter = {type?: ('all' | 'exclude' | 'include') | undefined, jurisdictions?: Array | undefined}; export type JurisdictionType = 'WorkLocation' | 'Residency' | 'All'; export type GetPaperworkTemplatesRequest = {paperwork_type?: TemplateQueryPaperworkType | undefined, worker_type?: WorkerType | undefined, jurisdictions?: JurisdictionFilter | undefined, jurisdiction_type?: JurisdictionType | undefined, effective_date?: string | undefined, archive_date?: string | undefined, companyID: string}; export type CreateApiPaperworkTemplatesArgs = {body: GetPaperworkTemplatesRequest}; export type CreateApiPaperworkTemplatesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paperworkType = z.enum(["W4", "I9", "W9", "CustomPaperwork"]); export const workerType = z.enum(["Employee", "Contractor", "All"]); export const jurisdictionFilter = z.object({type: z.enum(["all", "exclude", "include"]).optional(), jurisdictions: z.array(z.string()).optional()}); export const jurisdictionType = z.enum(["WorkLocation", "Residency", "All"]); export const formField = z.object({field_name: z.string().optional(), label: z.string().optional(), error_text: z.string().optional(), type: z.string().optional(), zeal_autofill: z.boolean().optional(), required: z.boolean().optional(), options: z.array(z.object({label: z.string().optional(), value: z.string().optional()})).optional(), constraint: z.object({min: z.number().optional(), max: z.number().optional()}).optional(), description: z.string().optional(), x: z.number().int().optional(), y: z.number().int().optional(), width: z.number().int().optional(), height: z.number().int().optional()}); export const templateStatus = z.enum(["Draft", "Live", "Archived"]); export const template = z.object({templateID: z.string().optional(), companyID: z.string().optional(), form_name: z.string().optional(), description: z.string().optional(), paperwork_type: paperworkType.optional(), worker_type: workerType.optional(), jurisdictions_filter: jurisdictionFilter.optional(), jurisdiction_type: jurisdictionType.optional(), effective_date: z.string().optional(), archive_date: z.string().optional(), form_fields: z.array(formField).optional(), urls: z.array(z.string()).optional(), status: templateStatus.optional()}); export const getPaperworkTemplatesResponse = z.object({templates: z.array(template).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 Paperwork Templates * * POST /paperwork/templates */ export const createApiPaperworkTemplates = async (args: CreateApiPaperworkTemplatesArgs, options?: CreateApiPaperworkTemplatesOptions) => { const {baseUrl = "https://api.zeal.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/paperwork/templates${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(getPaperworkTemplatesResponse, await res.json()) };