import {z} from 'zod' export type StateFiling = {state_code?: string | undefined, state_name?: string | undefined, state_id_number?: string | undefined, state_tax_withheld?: number | undefined, state_income?: number | undefined}; export type Form1099MISCRequest = {tax_year: string, rents?: number | undefined, royalties?: number | undefined, other_income?: number | undefined, federal_tax_withheld?: number | undefined, fishing_boat_proceeds?: number | undefined, medical_healthcare_payments?: number | undefined, is_direct_sale?: boolean | undefined, substitute_payments?: number | undefined, crop_insurance?: number | undefined, gross_proceeds_attorney?: number | undefined, fish_purchased_for_resale?: number | undefined, section_409a_deferrals?: number | undefined, is_fatca?: boolean | undefined, excess_golden_parachute?: number | undefined, nonqualified_deferred_compensation?: number | undefined, account_number?: string | undefined, is_second_tin_notice?: boolean | undefined, is_postal?: boolean | undefined, email_address?: string | undefined, state_filing?: Array | undefined}; export type CreateApiPayoutUserTokenTaxForm1099miscArgs = {body: Form1099MISCRequest}; export type CreateApiPayoutUserTokenTaxForm1099miscOptions = {baseUrl?: string | undefined, AUTHORIZERNAME?: string | undefined, fetch?: typeof fetch | undefined}; export const form1099NECReturnStatus = z.object({status: z.string().optional(), status_timestamp: z.string().optional(), info: z.string().optional()}); export const form1099NECStateReturnStatus = z.object({state_code: z.string().optional(), status: z.string().optional(), status_timestamp: z.string().optional(), info: z.string().optional()}); export const form1099MISCSubmissionResponse = z.object({submission_id: z.string().optional(), form_type: z.literal("1099-MISC").optional(), tax_year: z.string().optional(), status: z.enum(["Created", "Pending", "Validated", "Error"]).optional(), federal_return: form1099NECReturnStatus.optional(), state_returns: z.array(form1099NECStateReturnStatus).optional(), submitted_at: z.string().optional(), pdf_content: z.string().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 }; /** * Submit Form 1099-MISC * * POST /payout/{user_token}/tax/form1099misc * * Also accepts auth: api_key */ export const createApiPayoutUserTokenTaxForm1099misc = async (args: CreateApiPayoutUserTokenTaxForm1099miscArgs, options?: CreateApiPayoutUserTokenTaxForm1099miscOptions) => { const {baseUrl = "https://api.masspay.io/v1.0.0", fetch: fetchFn = fetch, AUTHORIZERNAME} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AUTHORIZERNAME !== undefined) headers.set("Authorization", AUTHORIZERNAME) const url = `${baseUrl.replace(/\/+$/, '')}/payout/${encodeURIComponent(String(args.user_token))}/tax/form1099misc${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(form1099MISCSubmissionResponse, await res.json()) };