import {z} from 'zod' export type Id = unknown; export type GetApiV1RuleSetsRuleSetIdArgs = {'rule-set-id': Id, auditUser: string}; export type GetApiV1RuleSetsRuleSetIdOptions = {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 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 Set * * GET /v1/rule-sets/{rule-set-id}/ */ export const getApiV1RuleSetsRuleSetId = async (args: GetApiV1RuleSetsRuleSetIdArgs, options: GetApiV1RuleSetsRuleSetIdOptions) => { 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)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/rule-sets/${encodeURIComponent(String(args['rule-set-id']))}/${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(ruleSet, await res.json()) };