import {z} from 'zod' export type MerchantCreationRequest = {defaultMerchantCategoryCode: string, merchantAccountCreation?: ({ip?: (string | null) | undefined, timestamp?: (number | null) | undefined} | null) | undefined, merchantId: string, name: string}; export type CreateApiMerchantsArgs = {'api-version': string, 'x-forter-siteid': string, Authorization: string, body: MerchantCreationRequest}; export type CreateApiMerchantsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const merchantCreationResponse = z.object({defaultMerchantCategoryCode: z.string().max(4), id: z.string(), merchantAccountCreation: z.object({ip: z.string().max(45).nullable().optional(), timestamp: z.number().nullable().optional()}).nullable().optional(), merchantId: z.string().max(50), name: z.string()}); 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 }; /** * Merchant * * POST /merchants */ export const createApiMerchants = async (args: CreateApiMerchantsArgs, options?: CreateApiMerchantsOptions) => { const {baseUrl = "https://api.forter-secure.com", 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 (args['api-version'] != null) headers.set("api-version", String(args['api-version'])) if (args['x-forter-siteid'] != null) headers.set("x-forter-siteid", String(args['x-forter-siteid'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchants${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(merchantCreationResponse, await res.json()) };