import {z} from 'zod' export type GetApiApiV1MeTaxFilingsFilingIdArgs = {filingId: string}; export type GetApiApiV1MeTaxFilingsFilingIdOptions = {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 getApiApiV1MeTaxFilingsFilingIdResponse = z.object({data: taxFiling}); 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 a submitted tax filing * * GET /api/v1/me/tax/filings/{filingId} * * Also accepts auth: oauth2 */ export const getApiApiV1MeTaxFilingsFilingId = async (args: GetApiApiV1MeTaxFilingsFilingIdArgs, options?: GetApiApiV1MeTaxFilingsFilingIdOptions) => { 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 (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/me/tax/filings/${encodeURIComponent(String(args.filingId))}${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(getApiApiV1MeTaxFilingsFilingIdResponse, await res.json()) };