import {z} from 'zod' export type GetApiConcessionsArgs = {'q[createdAtGt]'?: string | undefined, 'q[createdAtLt]'?: string | undefined, 'q[statusEq]'?: ('pending' | 'approved' | 'rejected' | 'canceled' | 'expired') | undefined, 'q[typeEq]'?: ('offering' | 'catching' | 'returning') | undefined, 'p[page]'?: number | undefined, 'p[per_page]'?: number | undefined}; export type GetApiConcessionsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, sec1?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiConcessionsResponse = z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.array(z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), status: z.string().optional(), accessId: z.string().optional(), type: z.string().optional(), count: z.number().int().optional(), value: z.string().optional()})).optional()}).optional(), meta: z.object({search: z.object({filteredFieldEq: z.string().optional()}).optional(), currentPage: z.number().int().optional(), previousPage: z.number().int().optional(), nextPage: z.number().int().optional(), totalPages: z.number().int().optional(), perPage: z.number().int().optional(), totalCount: z.number().int().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 capturas * * GET /concessions */ export const getApiConcessions = async (args: GetApiConcessionsArgs, options?: GetApiConcessionsOptions) => { 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['q[createdAtGt]'] != null) query.append("q[createdAtGt]", String(args['q[createdAtGt]'])) if (args['q[createdAtLt]'] != null) query.append("q[createdAtLt]", String(args['q[createdAtLt]'])) if (args['q[statusEq]'] != null) query.append("q[statusEq]", String(args['q[statusEq]'])) if (args['q[typeEq]'] != null) query.append("q[typeEq]", String(args['q[typeEq]'])) if (args['p[page]'] != null) query.append("p[page]", String(args['p[page]'])) if (args['p[per_page]'] != null) query.append("p[per_page]", String(args['p[per_page]'])) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) if (sec1 !== undefined) headers.set("Authorization", sec1) const url = `${baseUrl.replace(/\/+$/, '')}/concessions${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(getApiConcessionsResponse, await res.json()) };