import {z} from 'zod' export type GetApiV2AggregatorCredentialsArgs = {aggregator: 'aerosync', 'Content-Type': string, authorization: string}; export type GetApiV2AggregatorCredentialsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aggregatorCredentialsResponse = z.object({fastlinkURL: z.string().optional(), token: z.string().optional(), username: z.string().optional()}); export const schema200failure = z.object({error: z.object({code: z.string().optional(), message: z.string().optional(), help: z.string().optional()}).optional(), reasonCodes: z.array(z.string()).optional()}); export const getApiV2AggregatorCredentialsResponse = z.union([aggregatorCredentialsResponse, schema200failure]); 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 }; /** * aggregatorCredentials * * GET /v2/aggregatorCredentials */ export const getApiV2AggregatorCredentials = async (args: GetApiV2AggregatorCredentialsArgs, options?: GetApiV2AggregatorCredentialsOptions) => { const {baseUrl = "https://api.sandbox-pay.aero.inc", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.aggregator != null) query.append("aggregator", String(args.aggregator)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/aggregatorCredentials${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(getApiV2AggregatorCredentialsResponse, await res.json()) };