import {z} from 'zod' export type GetApiCardsTokensArgs = {accountId: string, personId?: string | undefined, tokenState?: ('REQUESTED' | 'REQUEST_DECLINED' | 'ACTIVE' | 'SUSPENDED' | 'TERMINATED') | undefined, search?: string | undefined, tokenType?: ('APPLE_PAY' | 'ANDROID_PAY' | 'SAMSUNG_PAY') | undefined, deviceType?: ('MOBILE_PHONE' | 'TABLET' | 'WATCH') | undefined, deviceId?: string | undefined}; export type GetApiCardsTokensOptions = {baseUrl?: string | undefined, CommonAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCardsTokensResponse = z.object({count: z.number().int(), rows: z.array(z.object({token: z.string().optional(), deviceId: z.string().optional(), cardId: z.string().max(36).optional(), cardName: z.string().optional(), lastFour: z.string().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 }; /** * List digital wallet tokens * * GET /cards/tokens */ export const getApiCardsTokens = async (args: GetApiCardsTokensArgs, options?: GetApiCardsTokensOptions) => { const {baseUrl = "https://api.equalsmoney.com/v2", fetch: fetchFn = fetch, CommonAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.personId != null) query.append("personId", String(args.personId)) if (args.tokenState != null) query.append("tokenState", String(args.tokenState)) if (args.search != null) query.append("search", String(args.search)) if (args.tokenType != null) query.append("tokenType", String(args.tokenType)) if (args.deviceType != null) query.append("deviceType", String(args.deviceType)) if (args.deviceId != null) query.append("deviceId", String(args.deviceId)) if (CommonAuth !== undefined) headers.set("Authorization", CommonAuth) const url = `${baseUrl.replace(/\/+$/, '')}/cards/tokens${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(getApiCardsTokensResponse, await res.json()) };