import {z} from 'zod' export type EntityType = 'CARD' | 'CARD_HIERARCHY_GROUP'; export type GetApiV1RuleSetsArgs = {auditUser: string, 'entity-id'?: string | undefined, 'entity-type'?: EntityType | undefined}; export type GetApiV1RuleSetsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const id = z.string(); export const ruleSetOperatorType = z.enum(["ANY", "ALL"]); export const ruleSetType = z.enum(["STANDARD", "STIP"]); export const ruleType = z.enum(["DECLINE", "ALLOWED_MCC", "BLOCKED_MCC", "ALLOWED_MERCHANT_ID", "BLOCKED_MERCHANT_ID", "AMOUNT", "ALLOWED_MERCHANT_COUNTRY", "BLOCKED_MERCHANT_COUNTRY", "ALLOWED_CARD_ENTRY_MODES", "BLOCKED_CARD_ENTRY_MODES", "ALLOWED_CVM", "BLOCKED_CVM", "CARDHOLDER_PRESENT", "EXTERNAL_BALANCE_CHECK", "CUMULATIVE_AMOUNT_INDIVIDUAL_LIMITS", "CUMULATIVE_COUNT_INDIVIDUAL_LIMITS", "ENFUCE_INTERNAL_PSD2_PIN_COUNT_RULE", "ENFUCE_INTERNAL_PSD2_PIN_AMOUNT_RULE"]); export const filterOperatorType = z.enum(["ANY", "ALL"]); export const filterType = z.enum(["MCC", "AMOUNT", "MERCHANT_COUNTRY", "CARDHOLDER_VERIFICATION", "CARD_ENTRY_MODE", "CARDHOLDER_PRESENT", "ACQUIRER_ID", "TRANSACTION_TYPE"]); export const filterResponseBody = z.object({id: id.optional(), type: filterType.optional(), reversed: z.boolean().optional()}); export const ruleResponseBody = z.object({id: id.optional(), ruleType: ruleType.optional(), name: z.string(), filterOperatorType: filterOperatorType, filters: z.array(filterResponseBody).optional()}); export const ruleSet = z.object({id: id.optional(), name: z.string().optional(), operatorType: ruleSetOperatorType.optional(), ruleSetType: ruleSetType.optional(), rules: z.array(ruleResponseBody).optional()}); export const getApiV1RuleSetsResponse = z.array(ruleSet); 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 Rule Sets * * GET /v1/rule-sets */ export const getApiV1RuleSets = async (args: GetApiV1RuleSetsArgs, options: GetApiV1RuleSetsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) if (args['entity-id'] != null) query.append("entity-id", String(args['entity-id'])) if (args['entity-type'] != null) query.append("entity-type", String(args['entity-type'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/rule-sets${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(getApiV1RuleSetsResponse, await res.json()) };