import {z} from 'zod' export type EntityType = 'CARD' | 'CARD_HIERARCHY_GROUP'; export type RuleSetType = 'STANDARD' | 'STIP'; export type Id = unknown; export type GetApiV1RulesRuleIdParametersArgs = {'entity-id': string, 'entity-type': EntityType, 'ruleset-type'?: RuleSetType | undefined, 'rule-id': Id, auditUser: string}; export type GetApiV1RulesRuleIdParametersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; 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 entityType = z.enum(["CARD", "CARD_HIERARCHY_GROUP"]); export const externalEntityReference = z.object({id: z.string(), type: entityType}); export const parametersResponseBody = z.object({ruleType: ruleType, entity: externalEntityReference.optional()}); export const getApiV1RulesRuleIdParametersResponse = z.array(parametersResponseBody); 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 Parameters * * GET /v1/rules/{rule-id}/parameters */ export const getApiV1RulesRuleIdParameters = async (args: GetApiV1RulesRuleIdParametersArgs, options: GetApiV1RulesRuleIdParametersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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'])) if (args['ruleset-type'] != null) query.append("ruleset-type", String(args['ruleset-type'])) if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/rules/${encodeURIComponent(String(args['rule-id']))}/parameters${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(getApiV1RulesRuleIdParametersResponse, await res.json()) };