import {z} from 'zod' export type GetApiApiV1CardsArgs = {exclude_voided?: boolean | undefined, status_code?: string | undefined}; export type GetApiApiV1CardsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1CardsResponse = z.array(z.object({id: z.string(), cardToken: z.string(), lastFourDigits: z.string(), activatedAt: z.string().nullable(), virtual: z.boolean(), statusCode: z.union([z.literal(1000), z.literal(1001), z.literal(1004), z.literal(1005), z.literal(1006), z.literal(1007), z.literal(1008), z.literal(1009), z.literal(1041), z.literal(1043), z.literal(1054), z.literal(1154), z.literal(1062), z.literal(1199)]), statusName: z.string()})); 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 all Cards * * GET /api/v1/cards */ export const getApiApiV1Cards = async (args: GetApiApiV1CardsArgs, options?: GetApiApiV1CardsOptions) => { const {baseUrl = "https://api.gnosispay.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.exclude_voided != null) query.append("exclude_voided", String(args.exclude_voided)) if (args.status_code != null) query.append("status_code", String(args.status_code)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/cards${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(getApiApiV1CardsResponse, await res.json()) };