import {z} from 'zod' export type GetApiV102ReferencesInfoArgs = {identifier?: string | undefined, reference?: string | undefined, start_date?: string | undefined}; export type GetApiV102ReferencesInfoOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV102ReferencesInfoResponse = z.union([z.object({transactionStatus: z.string().optional(), referenceList: z.array(z.object({reference: z.string().optional(), entity: z.string().optional(), amount: z.string().optional(), datetime: z.string().optional(), status: z.string().optional(), identifier: z.string().optional()})).optional()}), z.object({transactionStatus: z.string().optional(), referenceList: z.array(z.object({reference: z.string().optional(), entity: z.string().optional(), amount: z.string().optional(), datetime: z.string().optional(), status: z.string().optional(), identifier: z.string().optional(), payments: z.array(z.object({trid: z.string().optional(), amount: z.string().optional(), datePayment: z.string().optional(), dateTransfer: z.string().optional(), local: z.string().optional(), serviceCost: z.unknown()})).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 }; /** * Advanced Search * * GET /v1.02/references/info */ export const getApiV102ReferencesInfo = async (args: GetApiV102ReferencesInfoArgs, options?: GetApiV102ReferencesInfoOptions) => { const {baseUrl = "https://sandbox.eupago.pt/api/management", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.identifier != null) query.append("identifier", String(args.identifier)) if (args.reference != null) query.append("reference", String(args.reference)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (sec0 !== undefined) headers.set('authorization', `Bearer ${sec0}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1.02/references/info${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(getApiV102ReferencesInfoResponse, await res.json()) };