import {z} from 'zod' export type CreateOrchestrationRuleRequestCryptoSource = {network?: string | undefined}; export type CreateOrchestrationRuleRequestFiatSource = {network?: string | undefined, account_type?: string | undefined}; export type CreateOrchestrationRuleRequestSource = {crypto?: CreateOrchestrationRuleRequestCryptoSource | undefined, fiat?: CreateOrchestrationRuleRequestFiatSource | undefined}; export type CreateOrchestrationRuleRequestCryptoDestination = {address?: string | undefined, network?: string | undefined, crypto_address_id?: string | undefined}; export type CreateOrchestrationRuleRequestFiatDestination = {fiat_account_id?: string | undefined, memo?: string | undefined}; export type CreateOrchestrationRuleRequestProfileDestination = {profile_id?: string | undefined}; export type CreateOrchestrationRuleRequestDestination = {crypto?: CreateOrchestrationRuleRequestCryptoDestination | undefined, fiat?: CreateOrchestrationRuleRequestFiatDestination | undefined, profile?: CreateOrchestrationRuleRequestProfileDestination | undefined}; export type CreateOrchestrationRuleRequest = {ref_id: string, nickname?: string | undefined, profile_id: string, identity_id?: string | undefined, account_id?: string | undefined, source_asset: string, destination_asset: string, source: CreateOrchestrationRuleRequestSource, destination?: CreateOrchestrationRuleRequestDestination | undefined}; export type CreateApiOrchestrationRulesArgs = {body: CreateOrchestrationRuleRequest}; export type CreateApiOrchestrationRulesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationRuleStatus = z.enum(["ACTIVE", "DELETED", "PENDING", "REJECTED"]); export const orchestrationRuleCryptoSource = z.object({address_id: z.string().optional(), address: z.string().optional(), network: z.string().optional()}); export const orchestrationRuleFiatSource = z.object({deposit_instructions_id: z.string().optional(), network: z.string().optional(), account_type: z.string().optional(), memo_id: z.string().optional()}); export const orchestrationRuleProfileSource = z.object({profile_id: z.string().optional()}); export const orchestrationRuleSource = z.object({crypto: orchestrationRuleCryptoSource.optional(), fiat: orchestrationRuleFiatSource.optional(), profile: orchestrationRuleProfileSource.optional()}); export const orchestrationRuleCryptoDestination = z.object({address: z.string().optional(), network: z.string().optional()}); export const orchestrationRuleFiatDestination = z.object({fiat_account_id: z.string().optional(), memo: z.string().optional()}); export const orchestrationRuleProfileDestination = z.object({profile_id: z.string().optional()}); export const orchestrationRuleDestination = z.object({crypto: orchestrationRuleCryptoDestination.optional(), fiat: orchestrationRuleFiatDestination.optional(), profile: orchestrationRuleProfileDestination.optional()}); export const orchestrationRule = z.object({id: z.string().optional(), ref_id: z.string().optional(), nickname: z.string().optional(), profile_id: z.string().optional(), identity_id: z.string().optional(), account_id: z.string().optional(), status: orchestrationRuleStatus.optional(), source_asset: z.string().optional(), destination_asset: z.string().optional(), source: orchestrationRuleSource.optional(), destination: orchestrationRuleDestination.optional(), created_at: z.string().optional()}); export const createOrchestrationRuleResponse = z.object({rule: orchestrationRule.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 }; /** * Create Orchestration Rule * * POST /orchestration/rules */ export const createApiOrchestrationRules = async (args: CreateApiOrchestrationRulesArgs, options?: CreateApiOrchestrationRulesOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/orchestration/rules${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(createOrchestrationRuleResponse, await res.json()) };