import {z} from 'zod' export type GetApiMerchantProfilesArgs = {order?: string | undefined, since_token?: string | undefined, count?: string | undefined}; export type GetApiMerchantProfilesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const merchantProfileCardDetails = z.object({acquirer_merchant_id: z.string(), mcc: z.string(), merchant_name: z.string(), country_code: z.string()}); export const merchantProfileProperties = z.object({token: z.string().optional(), description: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), sub_merchant_key: z.string().optional(), card_networks: z.object({visa: merchantProfileCardDetails.optional(), mastercard: merchantProfileCardDetails.optional(), amex: merchantProfileCardDetails.optional(), discover: merchantProfileCardDetails.optional(), diners: merchantProfileCardDetails.optional(), jcb: merchantProfileCardDetails.optional(), dankort: merchantProfileCardDetails.optional(), union_pay: merchantProfileCardDetails.optional(), cartes_bancaires: merchantProfileCardDetails.optional()}).optional()}); export const listMerchantProfilesResponse = z.object({merchant_profiles: z.array(merchantProfileProperties).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 merchant profiles * * GET /merchant_profiles */ export const getApiMerchantProfiles = async (args: GetApiMerchantProfilesArgs, options?: GetApiMerchantProfilesOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order != null) query.append("order", String(args.order)) if (args.since_token != null) query.append("since_token", String(args.since_token)) if (args.count != null) query.append("count", String(args.count)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchant_profiles${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(listMerchantProfilesResponse, await res.json()) };