import {z} from 'zod' export type GetApiTaxArgs = {startDate: string, endDate: string, projectId?: string | undefined, accountId?: string | undefined}; export type GetApiTaxOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const tax = z.object({dateFrom: z.string(), dateTo: z.string(), taxReturn: z.object({sa105: z.object({income: z.object({total: z.object({value: z.number().int(), majorUnits: z.number().int().optional(), minorUnits: z.number().int().optional(), currency: z.string()}), details: z.array(z.object({category: z.string(), amount: z.object({value: z.number().int(), currency: z.string()})}))}), expenditure: z.object({total: z.object({value: z.number().int(), majorUnits: z.number().int().optional(), minorUnits: z.number().int().optional(), currency: z.string()}), details: z.array(z.object({category: z.string(), amount: z.object({value: z.number().int(), currency: z.string()})}))}), taxableIncome: z.object({total: z.object({value: z.number().int(), majorUnits: z.number().int().optional(), minorUnits: z.number().int().optional(), currency: z.string()})})}).optional()})}); export const links = z.object({next: z.string().optional(), prev: z.string().optional(), self: z.string()}); export const getApiTaxResponse = z.object({data: tax.optional(), links: links.optional(), meta: z.object({}).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 }; /** * Retrieve transactions to help answer SA105 - the Self Assessment questions for properties for HMRC * * GET /tax */ export const getApiTax = async (args: GetApiTaxArgs, options?: GetApiTaxOptions) => { const {baseUrl = "https://api.moneyhub.co.uk/v2.0", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/tax${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(getApiTaxResponse, await res.json()) };