import {z} from 'zod' export type GetApiContractorTaxStatementsArgs = {contractor?: string | undefined, company?: string | undefined, year?: number | undefined, publication_status?: ('draft' | 'published' | 'pending_correction') | undefined, type?: ('original' | 'amendment') | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiContractorTaxStatementsOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiContractorTaxStatementsResponse = z.object({next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(z.object({id: z.string().optional(), contractor: z.string().optional(), company: z.string().optional(), year: z.number().int().optional(), document: z.string().nullable().optional(), published_at: z.string().nullable().optional(), publication_status: z.enum(["draft", "published"]).optional(), delivery_method: z.enum(["manual", "mail"]).nullable().optional(), mail_recipient: z.enum(["contractor", "company"]).nullable().optional()})).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 }; /** * List contractor tax statements * * GET /contractor_tax_statements */ export const getApiContractorTaxStatements = async (args: GetApiContractorTaxStatementsArgs, options?: GetApiContractorTaxStatementsOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.contractor != null) query.append("contractor", String(args.contractor)) if (args.company != null) query.append("company", String(args.company)) if (args.year != null) query.append("year", String(args.year)) if (args.publication_status != null) query.append("publication_status", String(args.publication_status)) if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/contractor_tax_statements${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(getApiContractorTaxStatementsResponse, await res.json()) };