import {z} from 'zod' export type CommonDID = string; export type Ruleset = {order: number, actionRule: 'APPROVE' | 'REJECT' | 'NOT_READY' | 'SEND_TO_INBOX', ruleCheck: 'ALL' | 'VASP' | 'REVIEWED_VASP_LIST' | 'JURISDICTION' | 'CUSTOMER_JURISDICTION' | 'RISK_SCORE' | 'SANCTION_LIST_CHECK' | 'TRANSACTION_AMOUNT' | 'TRANSACTION_CREATION_TIME' | 'SELF_HOSTED_WALLET', ruleValue: string, source?: (string | null) | undefined}; export type TransactionRuleCreateRequestWithDirection = {vaspDID: CommonDID, ruleSet: {incoming?: Array | undefined, outgoing?: Array | undefined}}; export type CreateApiRulesSetDirectionArgs = {body: TransactionRuleCreateRequestWithDirection}; export type CreateApiRulesSetDirectionOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const commonDID = z.string(); export const vaspInfo = z.object({did: commonDID, name: z.string()}); export const transactionRuleList = z.array(z.object({id: z.string().optional(), vaspDID: commonDID.optional(), order: z.number().optional(), actionRule: z.enum(["APPROVE", "REJECT", "NOT_READY", "SEND_TO_INBOX"]).optional(), ruleCheck: z.enum(["ALL", "VASP", "REVIEWED_VASP_LIST", "JURISDICTION", "RISK_SCORE", "SANCTION_LIST_CHECK", "TRANSACTION_AMOUNT", "TRANSACTION_CREATION_TIME"]).optional(), ruleValue: z.string().optional(), source: z.string().optional(), vasps: z.array(vaspInfo).optional(), jurisdictions: z.array(z.string()).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 }; /** * Set rules * * POST /rules/set/direction */ export const createApiRulesSetDirection = async (args: CreateApiRulesSetDirectionArgs, options?: CreateApiRulesSetDirectionOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/rules/set/direction${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(transactionRuleList, await res.json()) };