import {z} from 'zod' export type GetApiPoisLookupArgs = {merchantId?: string | undefined, altVfiEntityId?: string | undefined, siteReferenceId?: string | undefined, storeNumber?: string | undefined, merchantName?: string | undefined, siteName?: string | undefined, estateManagementId?: string | undefined}; export type GetApiPoisLookupOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const poiSummary = z.object({poiUid: z.string().optional(), estateManagementId: z.string().optional(), poiName: z.string().optional()}); export const poiLookupResponse = z.object({merchantName: z.string().optional(), siteName: z.string().optional(), siteReferenceId: z.string().optional(), storeNumber: z.string().optional(), merchantId: z.string().optional(), altVfiEntityId: z.string().optional(), merchantCompanyUid: z.string().optional(), entityUid: z.string().optional(), pois: z.array(poiSummary).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 }; /** * Lookup POIs by merchant identifiers * * GET /pois/lookup * * Also accepts auth: BearerAuth */ export const getApiPoisLookup = async (args: GetApiPoisLookupArgs, options?: GetApiPoisLookupOptions) => { const {baseUrl = "https://emea.gsc.verifone.cloud/oidc/checkout-service", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchantId != null) query.append("merchantId", String(args.merchantId)) if (args.altVfiEntityId != null) query.append("altVfiEntityId", String(args.altVfiEntityId)) if (args.siteReferenceId != null) query.append("siteReferenceId", String(args.siteReferenceId)) if (args.storeNumber != null) query.append("storeNumber", String(args.storeNumber)) if (args.merchantName != null) query.append("merchantName", String(args.merchantName)) if (args.siteName != null) query.append("siteName", String(args.siteName)) if (args.estateManagementId != null) query.append("estateManagementId", String(args.estateManagementId)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/pois/lookup${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(poiLookupResponse, await res.json()) };