import {z} from 'zod' export type GetApiCrlvRequestCrlvIdArgs = {crlv_id: string}; export type GetApiCrlvRequestCrlvIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const cRLVDocumentStatus = z.enum(["CRLV_FAILED", "CRLV_INCORRECT_DATA", "CRLV_ISSUED", "CRLV_REQUESTED", "CRLV_UNAVAILABLE", "CRLV_SCHEDULED", "CRLV_WAITING_SETTLEMENT", "CRLV_CANCELED_PROTOCOL"]); export const crlvRequestDocumentStatusResponseDTO = z.object({protocol: z.string().nullable().optional(), license_plate: z.string(), document: z.string().nullable().optional(), renavam: z.string().nullable().optional(), state: z.string(), status: cRLVDocumentStatus, requested_year: z.string().nullable().optional(), crlv_last_year_available: z.string().nullable().optional(), crlv_issuance_year: z.string().nullable().optional(), crlv_pdf_link: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Busca o status da emissão do CRLV. * * GET /crlv/request/{crlv_id} */ export const getApiCrlvRequestCrlvId = async (args: GetApiCrlvRequestCrlvIdArgs, options?: GetApiCrlvRequestCrlvIdOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/crlv/request/${encodeURIComponent(String(args.crlv_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(crlvRequestDocumentStatusResponseDTO, await res.json()) };