import {z} from 'zod' export type GetApiMerchantHierarchyArgs = {'Content-Type': 'application/json' | 'application/json;charset=UTF-8', 'FSV-Interaction-Id': string, 'Institution-Number': string, Environment: string, internalMerchantId?: string | undefined, externalMerchantId?: string | undefined, parentInternalMerchantId?: string | undefined, merchantStatusIndex?: string | undefined, merchantLevelCode?: ('MEMBER' | 'GROUP' | 'SUB_GROUP') | undefined, extractDirection?: ('UP' | 'DOWN') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiMerchantHierarchyOptions = {baseUrl?: string | undefined, AppKey?: string | undefined, JWSSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const pagingMetaType = z.object({count: z.number().int().optional(), totalPages: z.number().int().optional(), currentOffset: z.number().int().optional(), currentNumberOfRetrievedRecords: z.number().int().optional(), maxLimit: z.number().int().optional(), hasMoreRecords: z.boolean().optional(), pageNumber: z.number().int().optional()}); export const sortingMetaType = z.object({sort: z.string().optional()}); export const sourceType = z.object({pointer: z.string().optional(), parameter: z.string().optional(), header: z.string().optional()}); export const errorType = z.object({id: z.string().optional(), code: z.string(), title: z.string(), detail: z.string().optional(), source: sourceType.optional()}); export const metaType = z.object({paging: pagingMetaType.optional(), sorting: sortingMetaType.optional(), messages: z.array(errorType).optional()}); export const oPYGenericDescription = z.string().max(100); export const oPYMerchantPropertyPropertyObj = z.object({propertyIndex: z.string().min(3).max(3).optional(), propertyDescription: oPYGenericDescription.optional()}); export const oPYMerchantPropertyPropertyValue = z.string().max(60); export const oPYLastUpdatedBy = z.string().max(60); export const oPYLastUpdated = z.string().min(8).max(8); export const linksType = z.object({self: z.string().optional(), first: z.string().optional(), last: z.string().optional(), prev: z.string().optional(), next: z.string().optional()}); export const inlineResponse20011 = z.object({meta: metaType.optional(), data: z.array(z.object({merchantProperty: oPYMerchantPropertyPropertyObj.optional(), propertyValue: oPYMerchantPropertyPropertyValue.optional(), lastUpdatedBy: oPYLastUpdatedBy.optional(), lastUpdatedDate: oPYLastUpdated.optional()})).optional(), errors: z.array(errorType).optional(), links: linksType.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 Merchant Hierarchy * * GET /merchant-hierarchy */ export const getApiMerchantHierarchy = async (args: GetApiMerchantHierarchyArgs, options?: GetApiMerchantHierarchyOptions) => { const {baseUrl = "https://prod.emea.api.fiservapps.com/ipp/omnipay/acquirerprofile/v1.0", fetch: fetchFn = fetch, AppKey, JWSSignature} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args['FSV-Interaction-Id'] != null) headers.set("FSV-Interaction-Id", String(args['FSV-Interaction-Id'])) if (args['Institution-Number'] != null) headers.set("Institution-Number", String(args['Institution-Number'])) if (args.Environment != null) headers.set("Environment", String(args.Environment)) if (args.internalMerchantId != null) query.append("internalMerchantId", String(args.internalMerchantId)) if (args.externalMerchantId != null) query.append("externalMerchantId", String(args.externalMerchantId)) if (args.parentInternalMerchantId != null) query.append("parentInternalMerchantId", String(args.parentInternalMerchantId)) if (args.merchantStatusIndex != null) query.append("merchantStatusIndex", String(args.merchantStatusIndex)) if (args.merchantLevelCode != null) query.append("merchantLevelCode", String(args.merchantLevelCode)) if (args.extractDirection != null) query.append("extractDirection", String(args.extractDirection)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (AppKey !== undefined) headers.set("Api-Key", AppKey) if (JWSSignature !== undefined) headers.set("JWS-Signature", JWSSignature) const url = `${baseUrl.replace(/\/+$/, '')}/merchant-hierarchy${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(inlineResponse20011, await res.json()) };