import {z} from 'zod' export type GetApiProfileIdentifierArgs = {identifier: string, institution?: ('imss' | 'issste') | undefined}; export type GetApiProfileIdentifierOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const personalInfo = z.object({first_name: z.string().optional(), last_name: z.string().optional(), curp: z.string().optional(), nss: z.string().nullable().optional(), rfc: z.string().nullable().optional(), phone: z.string().nullable().optional(), email: z.string().nullable().optional(), tax_regime: z.string().nullable().optional()}); export const address = z.object({street: z.string().optional(), neighborhood: z.string().optional(), municipality: z.string().optional(), state: z.string().optional(), zip_code: z.string().optional()}); export const profileResponse = z.object({identifier: z.string().optional(), updated_at: z.string().optional(), personal_info: personalInfo.optional(), address: address.optional(), employment_status: z.enum(["employed", "self_employed", "unemployed", "retired", "unknown"]).optional(), estimated_monthly_income: z.string().nullable().optional(), tenure: z.enum(["less_than_3_months", "3_months_plus", "6_months_plus", "9_months_plus", "12_months_plus"]).nullable().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 }; /** * Get Profile * * GET /profile/{identifier} */ export const getApiProfileIdentifier = async (args: GetApiProfileIdentifierArgs, options?: GetApiProfileIdentifierOptions) => { const {baseUrl = "https://api.burodeingresos.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.institution != null) query.append("institution", String(args.institution)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/profile/${encodeURIComponent(String(args.identifier))}${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(profileResponse, await res.json()) };