import {z} from 'zod' export type GetApiMonitoringSubscriptionsArgs = {page?: number | undefined, size?: number | undefined, is_active?: boolean | undefined, is_canceled?: boolean | undefined, external_id?: (string | null) | undefined, driver_document?: (string | null) | undefined, license_plate?: (string | null) | undefined}; export type GetApiMonitoringSubscriptionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const restContactDTO = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional(), phone: z.string().nullable().optional()}); export const state = z.enum(["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO"]); export const restVehicleResponseDTO = z.object({id: z.string(), contract_id: z.string(), license_plate: z.string(), state: state.nullable().optional(), owner_document: z.string().nullable().optional(), renavam: z.string().nullable().optional(), chassis: z.string().nullable().optional(), is_active: z.boolean().nullable().optional()}); export const restDriverResponseDTO = z.object({name: z.string(), document: z.string(), actual_points: z.number().int().nullable().optional()}); export const restVehicleHistoryDTO = z.object({license_plate: z.string()}); export const restCompleteSubscriptionResponseDTO = z.object({id: z.string(), contact: restContactDTO, external_id: z.string().nullable().optional(), created_at: z.string(), next_search_at: z.string().nullable().optional(), plan_id: z.string(), is_active: z.boolean(), is_canceled: z.boolean(), vehicles: z.array(restVehicleResponseDTO).optional(), drivers: z.array(restDriverResponseDTO).optional(), vehicle_histories: z.array(restVehicleHistoryDTO).optional()}); export const pageRestCompleteSubscriptionResponseDTO = z.object({items: z.array(restCompleteSubscriptionResponseDTO), total: z.number().int().gte(0).nullable(), page: z.number().int().gte(1).nullable(), size: z.number().int().gte(1).nullable(), pages: z.number().int().gte(0).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 }; /** * Lista as assinaturas de monitoramento * * GET /monitoring/subscriptions */ export const getApiMonitoringSubscriptions = async (args: GetApiMonitoringSubscriptionsArgs, options?: GetApiMonitoringSubscriptionsOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", fetch: fetchFn = fetch, basicAuth} = 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.size != null) query.append("size", String(args.size)) if (args.is_active != null) query.append("is_active", String(args.is_active)) if (args.is_canceled != null) query.append("is_canceled", String(args.is_canceled)) if (args.external_id != null) query.append("external_id", String(args.external_id)) if (args.driver_document != null) query.append("driver_document", String(args.driver_document)) if (args.license_plate != null) query.append("license_plate", String(args.license_plate)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/monitoring/subscriptions${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(pageRestCompleteSubscriptionResponseDTO, await res.json()) };