import {z} from 'zod' export type MerchantProfileCardDetails = {acquirer_merchant_id: string, mcc: string, merchant_name: string, country_code: string}; export type MerchantProfileCreate = {merchant_profile?: ({description?: string | undefined, sub_merchant_key?: string | undefined, visa?: MerchantProfileCardDetails | undefined, mastercard?: MerchantProfileCardDetails | undefined, amex?: MerchantProfileCardDetails | undefined, discover?: MerchantProfileCardDetails | undefined, diners?: MerchantProfileCardDetails | undefined, jcb?: MerchantProfileCardDetails | undefined, dankort?: MerchantProfileCardDetails | undefined, union_pay?: MerchantProfileCardDetails | undefined, cartes_bancaires?: MerchantProfileCardDetails | undefined}) | undefined}; export type CreateApiMerchantProfilesArgs = {body: MerchantProfileCreate}; export type CreateApiMerchantProfilesOptions = {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 merchantProfileResponse = z.object({merchant_profile: 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 }; /** * Create merchant profile * * POST /merchant_profiles */ export const createApiMerchantProfiles = async (args: CreateApiMerchantProfilesArgs, options?: CreateApiMerchantProfilesOptions) => { const {baseUrl = "https://core.spreedly.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(merchantProfileResponse, await res.json()) };