import {z} from 'zod' export type GetApiTinVerificationsArgs = {limit?: number | undefined, offset?: number | undefined, q?: (string | null) | undefined}; export type GetApiTinVerificationsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const taskState = z.enum(["PENDING", "EXECUTING", "COMPLETED", "FAILED", "CANCELLED"]); export const iRSTINMatchType = z.enum(["SSN", "EIN", "UNKNOWN"]); export const v1TINVerificationResponse = z.object({id: z.string(), state: taskState, name: z.string(), tin: z.string().min(9).max(9), address: z.string().max(500).nullable().optional(), tin_matched: z.boolean().nullable().optional(), warnings: z.array(z.string()).optional(), created_at: z.string(), tin_match_type: iRSTINMatchType.nullable().optional()}); export const getApiTinVerificationsResponse = z.array(v1TINVerificationResponse); 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 Tin Verifications * * GET /tin_verifications */ export const getApiTinVerifications = async (args: GetApiTinVerificationsArgs, options?: GetApiTinVerificationsOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.q != null) query.append("q", String(args.q)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tin_verifications${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(getApiTinVerificationsResponse, await res.json()) };