import {z} from 'zod' export type GetApiApiBrEmploymentsIdArgs = {id: string, omit?: string | undefined, fields?: string | undefined}; export type GetApiApiBrEmploymentsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const link = z.string().nullable(); export const createdAt = z.string(); export const collectedAt = z.string(); export const employerDataBrazil = z.object({name: z.string().min(5), code: z.string().min(8).max(14), economic_activity: z.string().min(4)}); export const occupationBrazil = z.object({start_date: z.string().min(10).max(10), end_date: z.string().min(10).max(10).nullable(), description: z.string().min(5), name: z.string().min(5), locale: z.string()}); export const enumEmploymentSalaryType = z.enum(["REGULAR", "THIRTEENTH", "VOLUNTARY", "RETIREMENT", "null"]).nullable(); export const salaryBrazil = z.object({base_amount: z.number(), retained_amount: z.number(), type: enumEmploymentSalaryType.optional(), month: z.string().min(7).max(7), currency: z.string().min(3).max(3)}); export const employmentBrazil = z.object({id: id, link: link, created_at: createdAt, collected_at: collectedAt, start_date: z.string().min(10).max(10), end_date: z.string().min(10).max(10).nullable(), employer_data: employerDataBrazil, occupations: z.array(occupationBrazil), salaries: z.array(salaryBrazil)}); 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 an employment's details * * GET /api/br/employments/{id}/ */ export const getApiApiBrEmploymentsId = async (args: GetApiApiBrEmploymentsIdArgs, options?: GetApiApiBrEmploymentsIdOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/br/employments/${encodeURIComponent(String(args.id))}/${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(employmentBrazil, await res.json()) };