import {z} from 'zod' export type GetApiMerchantsArgs = {merchant_descriptor: string, match_type?: string | undefined}; export type GetApiMerchantsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const country = z.object({code: z.string().optional(), name: z.string().optional()}); export const countrySubDivision = z.object({code: z.string().optional(), name: z.string().optional()}); export const address = z.object({city: z.string().optional(), country: country.optional(), countrySubdivision: countrySubDivision.optional(), line1: z.string().optional(), line2: z.string().optional(), postalCode: z.string().optional()}); export const merchant = z.object({address: address.optional(), brandName: z.string().optional(), merchantDescriptor: z.string().optional(), legalCorporateName: z.string().optional(), locationId: z.number().int().optional(), matchConfidenceScore: z.number().int().optional(), merchantCategory: z.string().optional(), merchantDbaName: z.string().optional(), phoneNumber: z.string().optional(), isBrickAndMortar: z.boolean().optional(), isEcommerce: z.boolean().optional()}); export const merchantList = z.object({merchants: z.array(merchant).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 matched merchants for a given merchant descriptor * * GET /merchants */ export const getApiMerchants = async (args: GetApiMerchantsArgs, options?: GetApiMerchantsOptions) => { const {baseUrl = "https://api.mastercard.com/merchant-identifier", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchant_descriptor != null) query.append("merchant_descriptor", String(args.merchant_descriptor)) if (args.match_type != null) query.append("match_type", String(args.match_type)) const url = `${baseUrl.replace(/\/+$/, '')}/merchants${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(merchantList, await res.json()) };