import {z} from 'zod' export type GetApiApiV1MeTaxFilingsArgs = {subject?: ('personal' | string) | undefined, type?: ('income' | 'vat') | undefined, year?: number | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiApiV1MeTaxFilingsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const taxSubject = z.object({type: z.enum(["natural_person", "legal_entity"]), id: z.string(), name: z.string(), residentPermitNumber: z.string().nullable()}); export const taxFiling = z.object({id: z.string(), subject: taxSubject, type: z.enum(["income", "vat"]), period: z.object({year: z.number().int(), quarter: z.number().int().gte(1).lte(4).nullable()}), submittedAt: z.string(), amended: z.boolean(), currency: z.enum(["usd", "btc"]), accountingMethod: z.string().nullable(), reported: z.object({grossIncome: z.string().nullable(), taxCredit: z.string().nullable(), taxWithheld: z.string().nullable()}), usdEquivalent: z.object({grossIncome: z.string().nullable(), taxCredit: z.string().nullable(), taxWithheld: z.string().nullable()}).nullable(), taxLiability: z.string().nullable(), exchangeRate: z.object({btcUsdRate: z.string().nullable(), capturedAt: z.string().nullable(), source: z.string().nullable()}).nullable(), invoice: z.object({id: z.string(), status: z.string()}).nullable(), availableDocuments: z.array(z.enum(["assessment", "return"]))}); export const getApiApiV1MeTaxFilingsResponse = z.object({data: z.array(taxFiling), meta: z.object({nextCursor: z.string().nullable()})}); 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 }; /** * List submitted tax filings * * GET /api/v1/me/tax/filings * * Also accepts auth: oauth2 */ export const getApiApiV1MeTaxFilings = async (args: GetApiApiV1MeTaxFilingsArgs, options?: GetApiApiV1MeTaxFilingsOptions) => { const {baseUrl = "https://portal.eprospera.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.subject != null) query.append("subject", String(args.subject)) if (args.type != null) query.append("type", String(args.type)) if (args.year != null) query.append("year", String(args.year)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/me/tax/filings${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(getApiApiV1MeTaxFilingsResponse, await res.json()) };