import {z} from 'zod' export type GetApiLaasV2MinutesArgs = {category?: ('termo-de-cessao' | 'termo-de-adesao' | 'ccb' | 'termo-constitutivo-da-nota-comercial') | undefined}; export type GetApiLaasV2MinutesOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, sec1?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiLaasV2MinutesResponse = z.object({ccb: z.array(z.object({id: z.string().optional(), partner_id: z.string().optional(), name: z.string().optional(), category: z.string().optional(), status: z.string().optional(), approved_at: z.string().optional()})).optional(), commercial_note: z.array(z.object({id: z.string().optional(), partner_id: z.string().optional(), name: z.string().optional(), category: z.string().optional(), status: z.string().optional(), approved_at: z.string().optional()})).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 }; /** * Listar minutas * * GET /laas/v2/minutes */ export const getApiLaasV2Minutes = async (args: GetApiLaasV2MinutesArgs, options?: GetApiLaasV2MinutesOptions) => { const {baseUrl = "https://pagamentos.grafeno.be/api/v2", fetch: fetchFn = fetch, sec0, sec1} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.category != null) query.append("category", String(args.category)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) if (sec1 !== undefined) headers.set("Authorization", sec1) const url = `${baseUrl.replace(/\/+$/, '')}/laas/v2/minutes${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(getApiLaasV2MinutesResponse, await res.json()) };