import {z} from 'zod' export type ActionKey = string; export type GetApiCardManagementActionsActionKeyArgs = {actionKey: ActionKey}; export type GetApiCardManagementActionsActionKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const actionKey = z.string().min(5).max(120); export const maskedCard = z.object({maskedNumber: z.string()}); export const merchantAcceptorKey = z.string().min(5).max(120); export const merchantAcceptorRef = z.object({key: merchantAcceptorKey, version: z.number().int()}); export const terminalEntryMode = z.enum(["chip", "contactless", "magstripe", "magstripe_fallback", "contactless_magstripe"]); export const schemasTerminalCapability = z.enum(["contact_chip", "contactless_chip", "magstripe"]); export const schemasTerminal = z.object({id: z.string().min(1).max(8), capabilities: z.array(schemasTerminalCapability)}); export const transactionReference = z.string().min(1).max(120); export const visaNetworkFields = z.object({network: z.literal("visa"), transactionIdentifier: z.string()}); export const mastercardNetworkFields = z.object({network: z.literal("mastercard"), banknetReference: z.string(), traceId: z.string()}); export const networkSpecificFields = z.discriminatedUnion("network", [visaNetworkFields, mastercardNetworkFields]); export const authorizationIsoFields = z.object({responseCode: z.string(), responseCodeDescription: z.string(), authorizationCode: z.string().optional(), networkCode: z.string(), systemTraceAuditNumber: z.string(), retrievalReferenceNumber: z.string(), networkSpecificFields: networkSpecificFields}); export const localTransactionDateTime = z.string(); export const schemasCreated = z.string(); export const openapiComponentsSchemasLastModified = z.string(); export const componentsSchemasVersion = z.number().int(); export const getPinChangeResponse = z.object({key: actionKey, type: z.literal("pin_change"), result: z.enum(["approved", "declined", "reversed"]), card: maskedCard, merchantAcceptorRef: merchantAcceptorRef, terminalEntryMode: terminalEntryMode, terminal: schemasTerminal, transactionReference: transactionReference.optional(), authorizationIsoFields: authorizationIsoFields, localTransactionDateTime: localTransactionDateTime.optional(), created: schemasCreated, lastModified: openapiComponentsSchemasLastModified.optional(), version: componentsSchemasVersion}); export const actionResult = z.enum(["approved", "declined"]); export const getPinUnblockResponse = z.object({key: actionKey, type: z.literal("pin_unblock"), result: actionResult, card: maskedCard, merchantAcceptorRef: merchantAcceptorRef, terminalEntryMode: terminalEntryMode, terminal: schemasTerminal, transactionReference: transactionReference.optional(), authorizationIsoFields: authorizationIsoFields, localTransactionDateTime: localTransactionDateTime.optional(), created: schemasCreated, version: componentsSchemasVersion}); export const getBalanceInquiryResponse = z.object({key: actionKey, type: z.literal("balance_inquiry"), result: actionResult, card: maskedCard, merchantAcceptorRef: merchantAcceptorRef, terminalEntryMode: terminalEntryMode, terminal: schemasTerminal, transactionReference: transactionReference.optional(), authorizationIsoFields: authorizationIsoFields, localTransactionDateTime: localTransactionDateTime.optional(), created: schemasCreated, version: componentsSchemasVersion}); export const reversePinChangeResponse = z.object({key: actionKey, reversedActionKey: z.string().min(5).max(120), type: z.literal("pin_change_reversal"), result: actionResult, authorizationIsoFields: authorizationIsoFields, created: schemasCreated, version: componentsSchemasVersion}); export const getCardManagementActionResponse = z.discriminatedUnion("type", [getPinChangeResponse, getPinUnblockResponse, getBalanceInquiryResponse, reversePinChangeResponse]); 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 }; /** * Get Card Management Action [EXPERIMENTAL] * * GET /cardManagement/actions/{actionKey} * * Also accepts auth: BearerToken */ export const getApiCardManagementActionsActionKey = async (args: GetApiCardManagementActionsActionKeyArgs, options?: GetApiCardManagementActionsActionKeyOptions) => { 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 (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/cardManagement/actions/${encodeURIComponent(String(args.actionKey))}${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(getCardManagementActionResponse, await res.json()) };