import {z} from 'zod' export type FilingStatusSetup = {federal?: ('SINGLE' | 'MARRIED' | 'MARRIED_SEPARATELY' | 'HEAD' | 'WIDOWER') | undefined, state?: ('SINGLE' | 'MARRIED' | 'MARRIED_SEPARATELY' | 'HEAD' | 'WIDOWER') | undefined}; export type Withholding = {useWithholding?: boolean | undefined, federalWithholding?: number | undefined, stateWithholding?: number | undefined}; export type HomeOffice = {useHomeOfficeDeduction?: boolean | undefined, homeOfficeArea?: number | undefined}; export type Mileage = {useEstimate?: boolean | undefined, estimate?: number | undefined, useMileageDeduction?: boolean | undefined, vehicleBusinessPercent?: string | undefined}; export type Expenses = {useEstimate?: boolean | undefined, estimate?: number | undefined}; export type Income = {useEstimate?: boolean | undefined, estimate?: number | undefined}; export type TaxSetupPostDto = {countryCode?: string | undefined, state?: string | undefined, filingStatus?: FilingStatusSetup | undefined, annualW2Income?: number | undefined, withholding?: Withholding | undefined, numExemptions?: number | undefined, tipIncome?: number | undefined, overtimeIncome?: number | undefined, homeOffice?: HomeOffice | undefined, mileage?: Mileage | undefined, expenses?: Expenses | undefined, income?: Income | undefined, isSetupComplete?: boolean | undefined} | null; export type CreateApiV6TaxesSetupArgs = {body: TaxSetupPostDto}; export type CreateApiV6TaxesSetupOptions = {baseUrl?: string | undefined, EmbedToken?: string | undefined, fetch?: typeof fetch | undefined}; export const filingStatusSetup = z.object({federal: z.enum(["SINGLE", "MARRIED", "MARRIED_SEPARATELY", "HEAD", "WIDOWER"]).optional(), state: z.enum(["SINGLE", "MARRIED", "MARRIED_SEPARATELY", "HEAD", "WIDOWER"]).optional()}); export const withholding = z.object({useWithholding: z.boolean().optional(), federalWithholding: z.number().int().optional(), stateWithholding: z.number().int().optional()}); export const homeOffice = z.object({useHomeOfficeDeduction: z.boolean().optional(), homeOfficeArea: z.number().optional()}); export const mileage = z.object({useEstimate: z.boolean().optional(), estimate: z.number().int().optional(), useMileageDeduction: z.boolean().optional(), vehicleBusinessPercent: z.string().optional()}); export const expenses = z.object({useEstimate: z.boolean().optional(), estimate: z.number().int().optional()}); export const income = z.object({useEstimate: z.boolean().optional(), estimate: z.number().int().optional()}); export const taxSetupGetDto = z.object({countryCode: z.string().nullable(), state: z.string().nullable(), filingStatus: filingStatusSetup, annualW2Income: z.number().int().nullable(), withholding: withholding, numExemptions: z.number().int().nullable(), tipIncome: z.number().int().nullable(), overtimeIncome: z.number().int().nullable(), homeOffice: homeOffice, mileage: mileage, expenses: expenses, income: income, isSetupComplete: z.boolean().nullable()}); export const successResponseTaxSetupGetDto = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: taxSetupGetDto.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 }; /** * Save tax profile * * POST /v6/taxes/setup */ export const createApiV6TaxesSetup = async (args: CreateApiV6TaxesSetupArgs, options?: CreateApiV6TaxesSetupOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, EmbedToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (EmbedToken !== undefined) headers.set('authorization', `Bearer ${EmbedToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/taxes/setup${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(successResponseTaxSetupGetDto, await res.json()) };