import {z} from 'zod' export type GetApiTaxesArgs = {id?: Array | undefined, jurisdiction?: Array | undefined, supported?: boolean | undefined, remittable?: boolean | undefined, effective?: boolean | undefined, label_contains?: string | undefined, limit?: number | undefined}; export type GetApiTaxesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTaxesResponse = z.object({next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(z.object({id: z.string().optional(), label: z.string().optional(), jurisdiction: z.string().optional(), payer: z.enum(["employee", "company"]).optional(), supported: z.boolean().nullable().optional(), remittable: z.boolean().optional(), effective_from: z.string().nullable().optional(), effective_to: z.string().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 taxes * * GET /taxes */ export const getApiTaxes = async (args: GetApiTaxesArgs, options?: GetApiTaxesOptions) => { const {baseUrl = "https://sandbox.checkhq.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.id ?? []) query.append("id", String(item)) for (const item of args.jurisdiction ?? []) query.append("jurisdiction", String(item)) if (args.supported != null) query.append("supported", String(args.supported)) if (args.remittable != null) query.append("remittable", String(args.remittable)) if (args.effective != null) query.append("effective", String(args.effective)) if (args.label_contains != null) query.append("label_contains", String(args.label_contains)) if (args.limit != null) query.append("limit", String(args.limit)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/taxes${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(getApiTaxesResponse, await res.json()) };