import {z} from 'zod' export type GetApiEmploymentsIdentifierArgs = {identifier: string, page?: number | undefined, items_per_page?: number | undefined, start_date?: string | undefined, end_date?: string | undefined, updated_at_from?: string | undefined, updated_at_to?: string | undefined, institution?: ('imss' | 'issste') | undefined, data_source?: (Array<'imss_semanas_cotizadas' | 'imss_rpci' | 'imss_get_weeks' | 'imss_rpci_archive' | 'imss_get_weeks_archive' | 'imss_app_scraping' | 'issste' | 'issste_archive' | 'employment_sandbox'>) | undefined}; export type GetApiEmploymentsIdentifierOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const employmentRecord = z.object({employer: z.string().optional(), employer_registration: z.string().nullable().optional(), start_date: z.string().optional(), end_date: z.string().nullable().optional(), federal_entity: z.string().nullable().optional(), base_salary: z.number().optional(), monthly_salary: z.number().optional(), pdf_link: z.string().optional(), institution: z.enum(["imss", "issste", "employment_sandbox", "invoices_sandbox"]).optional(), data_source: z.enum(["imss_semanas_cotizadas", "imss_rpci", "imss_get_weeks", "imss_rpci_archive", "imss_get_weeks_archive", "imss_app_scraping", "issste", "issste_archive", "employment_sandbox"]).nullable().optional(), updated_at: z.string().optional()}); export const pagination = z.object({page: z.number().int().optional(), items_per_page: z.number().int().optional(), total_items: z.number().int().optional(), total_pages: z.number().int().optional()}); export const employmentHistoryResponse = z.object({identifier: z.string().optional(), updated_at: z.string().optional(), semanas_cotizadas: z.number().int().nullable().optional(), employment_history: z.array(employmentRecord).optional(), pagination: pagination.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 Employments * * GET /employments/{identifier} */ export const getApiEmploymentsIdentifier = async (args: GetApiEmploymentsIdentifierArgs, options?: GetApiEmploymentsIdentifierOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.items_per_page != null) query.append("items_per_page", String(args.items_per_page)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.updated_at_from != null) query.append("updated_at_from", String(args.updated_at_from)) if (args.updated_at_to != null) query.append("updated_at_to", String(args.updated_at_to)) if (args.institution != null) query.append("institution", String(args.institution)) for (const item of args.data_source ?? []) query.append("data_source", String(item)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/employments/${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(employmentHistoryResponse, await res.json()) };