import {z} from 'zod' export type ComponentsSchemasMerchantKey = string; export type ComponentsSchemasOffsetToken = string; export type GetApiFraudNotificationsArgs = {merchantKey?: ComponentsSchemasMerchantKey | undefined, limit?: number | undefined, offsetToken?: ComponentsSchemasOffsetToken | undefined, sortOrder?: ('asc' | 'desc') | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiFraudNotificationsOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const moreItems = z.boolean(); export const componentsSchemasOffsetToken = z.string().min(1).max(2048); export const totalItems = z.number(); export const fraudNotificationKey = z.string().min(1).max(120); export const schemasCardNetwork = z.enum(["mastercard", "visa", "american-express"]); export const componentsSchemasAgentKey = z.string().min(1).max(120); export const componentsSchemasMerchantKey = z.string().min(1).max(120); export const openapiComponentsSchemasAcceptorKey = z.string().min(1).max(120); export const openapiComponentsSchemasVersion2 = z.number().int().gte(1).lte(1000); export const merchantAcceptorRef = z.object({key: openapiComponentsSchemasAcceptorKey, version: openapiComponentsSchemasVersion2}); export const acquirerReferenceNumber = z.string().min(23).max(23); export const schemasLocalTransactionDateTime = z.string(); export const openapiComponentsSchemasAmountValue2 = z.number().int().gte(0).lte(900000000); export const openapiComponentsSchemasCurrencyCode3 = z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLP", "CNH", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLE", "SLL", "SOS", "SRD", "SSP", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UYW", "UZS", "VES", "VND", "VUV", "WST", "XAF", "XCD", "XCG", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL"]); export const openapiComponentsSchemasAmount = z.object({value: openapiComponentsSchemasAmountValue2, currency: openapiComponentsSchemasCurrencyCode3}); export const schemasChargeKey = z.string().min(1).max(120); export const fraudNotificationsFraudLiability = z.enum(["acquirer", "issuer"]); export const fraudNotificationsTransactionReference = z.string().min(1).max(120); export const clearingDateTime = z.string(); export const fraudNotificationsChargeInfo = z.object({arn: acquirerReferenceNumber, localTransactionDateTime: schemasLocalTransactionDateTime.optional(), amount: openapiComponentsSchemasAmount, chargeKey: schemasChargeKey.optional(), fraudLiability: fraudNotificationsFraudLiability, transactionReference: fraudNotificationsTransactionReference.optional(), clearingDateTime: clearingDateTime.optional()}); export const schemasLast4 = z.string().min(4).max(4); export const maskedNumber = z.string(); export const cardInfo = z.object({last4: schemasLast4, maskedNumber: maskedNumber.optional(), issuerName: z.string().optional()}); export const fraudType = z.object({typeCode: z.string().optional(), subTypeCode: z.string().optional()}); export const purchaseDate = z.string(); export const fraudReportingDate = z.string(); export const schemasMid = z.string().min(1).max(15); export const openapiComponentsSchemasCreated3 = z.string(); export const fraudNotificationsStatus = z.enum(["active", "archived"]); export const fraudNotification = z.object({key: fraudNotificationKey, network: schemasCardNetwork, agentKey: componentsSchemasAgentKey, merchantKey: componentsSchemasMerchantKey, merchantAcceptorRef: merchantAcceptorRef, chargeInfo: fraudNotificationsChargeInfo, cardInfo: cardInfo, fraudAmount: openapiComponentsSchemasAmount, fraudType: fraudType, purchaseDate: purchaseDate, fraudReportingDate: fraudReportingDate, mid: schemasMid, created: openapiComponentsSchemasCreated3, version: openapiComponentsSchemasVersion2, status: fraudNotificationsStatus}); export const fraudNotificationsPage = z.object({moreItems: moreItems.optional(), offsetToken: componentsSchemasOffsetToken.optional(), totalItems: totalItems.optional(), fraudNotifications: z.array(fraudNotification)}); 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 Fraud Notifications * * GET /fraudNotifications * * Also accepts auth: BearerToken */ export const getApiFraudNotifications = async (args: GetApiFraudNotificationsArgs, options?: GetApiFraudNotificationsOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchantKey != null) query.append("merchantKey", String(args.merchantKey)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offsetToken != null) query.append("offsetToken", String(args.offsetToken)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/fraudNotifications${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(fraudNotificationsPage, await res.json()) };