import {z} from 'zod' export type GetApiStatusArgs = {id: string, 'X-API-Key-ID': string, 'X-API-Timestamp': string, 'X-API-Nonce': string, Digest: string, Authorization: string}; export type GetApiStatusOptions = {baseUrl?: string | undefined, Ed25519?: string | undefined, fetch?: typeof fetch | undefined}; export const getCardStatusResponse = z.object({id: z.string().optional(), mask: z.string().optional(), holder_name: z.string().optional(), card_alias: z.string().optional(), status: z.string().optional(), currency: z.string().optional(), available_balance: z.string().optional(), user_id: z.string().optional(), created_at: z.number().int().optional(), updated_at: z.number().int().optional()}); export const getCardStatusEnvelope = z.object({code: z.number().int(), message: z.string(), data: getCardStatusResponse.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 }; /** * Get organization card status by id * * GET /status */ export const getApiStatus = async (args: GetApiStatusArgs, options?: GetApiStatusOptions) => { const {baseUrl = "https://openapi.infini.money/v2/cards", fetch: fetchFn = fetch, Ed25519} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args['X-API-Key-ID'] != null) headers.set("X-API-Key-ID", String(args['X-API-Key-ID'])) if (args['X-API-Timestamp'] != null) headers.set("X-API-Timestamp", String(args['X-API-Timestamp'])) if (args['X-API-Nonce'] != null) headers.set("X-API-Nonce", String(args['X-API-Nonce'])) if (args.Digest != null) headers.set("Digest", String(args.Digest)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (Ed25519 !== undefined) headers.set("Authorization", Ed25519) const url = `${baseUrl.replace(/\/+$/, '')}/status${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(getCardStatusEnvelope, await res.json()) };