import {z} from 'zod' export type GetApiCreditorAgentsIcaCreditorsArgs = {ica: string, legal_business_name?: string | undefined, dba?: string | undefined, creditor_profile_id?: string | undefined, is_active?: boolean | undefined, offset?: number | undefined, limit?: number | undefined, sort?: ('ORGANISATION_NAME' | 'ADDRESS_LINE' | 'TOWN_NAME' | 'COUNTRY_SUB_DIVISION' | 'POST_CODE' | 'COUNTRY_CODE' | 'ORGANISATION_IDENTIFICATION' | 'PROFILE_COUNT') | undefined}; export type GetApiCreditorAgentsIcaCreditorsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const creditorLegalAddress = z.object({addressLine: z.string().max(70), country: z.string().min(2).max(2), countrySubDivision: z.string().max(35).optional(), postCode: z.string().optional(), townName: z.string().max(50).optional()}); export const organisationIdentification = z.object({identification: z.string().min(1).max(15), type: z.string()}); export const creditorSearchResultItem = z.object({dba: z.string().optional(), id: z.string().min(36).max(36).optional(), inactiveReason: z.string().optional(), isActive: z.boolean().optional(), legalBusinessAddress: creditorLegalAddress.optional(), legalBusinessName: z.string().optional(), organisationIdentifications: z.array(organisationIdentification).optional(), profileCount: z.number().int().optional()}); export const creditorSearchResults = z.object({data: z.object({count: z.number().int().optional(), items: z.array(creditorSearchResultItem).optional(), limit: z.number().int().optional(), offset: z.number().int().optional(), total: z.number().int().optional()}).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 }; /** * Retrieve All Creditor Registrations * * GET /creditor-agents/{ica}/creditors */ export const getApiCreditorAgentsIcaCreditors = async (args: GetApiCreditorAgentsIcaCreditorsArgs, options?: GetApiCreditorAgentsIcaCreditorsOptions) => { const {baseUrl = "https://api.mastercard.com/commercial/payments", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.legal_business_name != null) query.append("legal_business_name", String(args.legal_business_name)) if (args.dba != null) query.append("dba", String(args.dba)) if (args.creditor_profile_id != null) query.append("creditor_profile_id", String(args.creditor_profile_id)) if (args.is_active != null) query.append("is_active", String(args.is_active)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/creditor-agents/${encodeURIComponent(String(args.ica))}/creditors${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(creditorSearchResults, await res.json()) };