import {z} from 'zod' export type GetApiConnectivityV2betaConnectionsArgs = {limit?: number | undefined, token?: string | undefined}; export type GetApiConnectivityV2betaConnectionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const resourceID = z.string().min(32).max(64); export const organizationID = z.string(); export const thirdPartyConfiguration = z.object({environment: z.string()}); export const reportsTransportConfiguration = z.object({mode: z.enum(["ATLAR_INITIATED", "THIRDPARTY_INITIATED", "DISABLED"])}); export const reportsConfiguration = z.object({transport: reportsTransportConfiguration}); export const transportConfiguration = z.object({requiredSignatures: z.number().int(), disabled: z.boolean(), signatureScope: z.enum(["ALL", "CUSTOMER_UPLOADED_ONLY"]).optional()}); export const connectionConfiguration = z.object({thirdParty: thirdPartyConfiguration, reports: reportsConfiguration, transport: transportConfiguration}); export const date = z.string(); export const exclusion = z.object({dayOfWeek: z.number().int().gte(0).lte(7).optional(), date: date.optional(), from: z.string(), to: z.string()}); export const schedulingSchedule = z.object({frequency: z.enum(["DEFAULT", "EVERY_15M", "HOURLY", "DISABLED"]), schedule: z.array(z.number().int()).optional(), location: z.string().optional(), exclusions: z.array(exclusion).optional()}); export const thirdPartyVendor = z.enum(["astropay", "checkout", "directa24", "inpay", "paypal", "skrill", "nuvei", "truelayer", "trustly"]); export const matches = z.object({bic: z.array(z.string()).optional(), vendor: z.array(thirdPartyVendor).optional()}); export const forward = z.object({matches: matches, affiliationId: z.string().optional(), canInitiatePayments: z.boolean().optional()}); export const connectionPool = z.object({forwards: z.array(forward).optional()}); export const deprecatedConnectionConfiguration = z.object({disabledTransport: z.boolean(), maxBatchSize: z.number().int(), pushSchedule: schedulingSchedule, pushPriority: z.number().int(), bypassTimeToSchedulePaymentCheck: z.boolean().optional(), fetchFrequency: z.enum(["DEFAULT", "EVERY_15M", "HOURLY", "DISABLED"]).optional(), fetchSchedules: z.record(z.string(), schedulingSchedule), fetchDataTypes: z.array(z.enum(["PAYMENT_STATUS_REPORTS", "TRANSACTIONS", "ACCOUNTS", "PAYMENTS"])), ebicsFetchConfig: z.object({eodyFetchTypesOverride: z.array(z.string()).optional(), interimFetchTypesOverride: z.array(z.string()).optional()}), connectionPool: connectionPool, thirdParty: z.object({env: z.string().optional()}), ignoredData: z.array(z.unknown()), transport: z.object({requiredSignatures: z.number().int().optional(), signatureScope: z.enum(["ALL", "CUSTOMER_UPLOADED_ONLY"]).optional()}).optional()}); export const capabilities = z.object({}); export const resourceVersion = z.number().int().gte(1); export const eTag = z.string(); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const connection = z.object({id: resourceID, organizationId: organizationID, connectionTypeId: z.string(), thirdPartyId: z.string(), affiliationId: z.string(), alias: z.string().optional(), configuration: connectionConfiguration, deprecatedConfiguration: deprecatedConnectionConfiguration, capabilities: capabilities, lifecycleState: z.enum(["UNDER_DEVELOPMENT", "LIVE", "CANCELLED"]), inactive: z.boolean(), version: resourceVersion, etag: eTag, created: createdTimestamp, updated: updatedTimestamp}); export const getApiConnectivityV2betaConnectionsResponse = z.object({items: z.array(connection), nextToken: z.string(), token: z.string(), limit: z.number().int()}); 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 }; /** * List connections * * GET /connectivity/v2beta/connections */ export const getApiConnectivityV2betaConnections = async (args: GetApiConnectivityV2betaConnectionsArgs, options?: GetApiConnectivityV2betaConnectionsOptions) => { const {baseUrl = "https://api.atlar.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.token != null) query.append("token", String(args.token)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/connectivity/v2beta/connections${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(getApiConnectivityV2betaConnectionsResponse, await res.json()) };