import {z} from 'zod' export type GetApiPaymentsNetworkTokensNetworkTokenIdArgs = {network_token_id: string}; export type GetApiPaymentsNetworkTokensNetworkTokenIdOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const networkTokenExpiry = z.object({month: z.string(), year: z.string()}); export const networkToken = z.object({id: z.string(), number: z.string(), expiry: networkTokenExpiry, card: z.object({lastFour: z.string(), expiry: z.object({month: z.string().optional(), year: z.string().optional()}), brand: z.enum(["visa", "mastercard", "american-express"])}), tokenRequestorIdentifier: z.string(), tokenServiceProvider: z.enum(["mdes", "vts", "aets"]), paymentAccountReference: z.string().optional(), status: z.enum(["active", "inactive", "blocked", "provisioning", "suspended", "deleted", "expired"]), merchant: z.string(), createdAt: z.number().int(), updatedAt: z.number().int().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 }; /** * Retrieve a Network Token * * GET /payments/network-tokens/{network_token_id} */ export const getApiPaymentsNetworkTokensNetworkTokenId = async (args: GetApiPaymentsNetworkTokensNetworkTokenIdArgs, options?: GetApiPaymentsNetworkTokensNetworkTokenIdOptions) => { const {baseUrl = "https://api.evervault.com", 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(/\/+$/, '')}/payments/network-tokens/${encodeURIComponent(String(args.network_token_id))}${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(networkToken, await res.json()) };