import {z} from 'zod' export type GetApiOrchestrationRulesArgs = {profile_id?: string | undefined, status?: (Array<'ACTIVE' | 'DELETED' | 'PENDING' | 'REJECTED'>) | undefined, source_asset?: string | undefined, destination_asset?: string | undefined, ids?: Array | undefined, limit?: number | undefined, order?: ('DESC' | 'ASC') | undefined, page_cursor?: string | undefined}; export type GetApiOrchestrationRulesOptions = {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 listOrchestrationRulesResponse = z.object({rules: z.array(orchestrationRule).optional(), next_page_cursor: z.string().optional(), items: z.array(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 }; /** * List Orchestration Rules * * GET /orchestration/rules */ export const getApiOrchestrationRules = async (args: GetApiOrchestrationRulesArgs, options?: GetApiOrchestrationRulesOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.profile_id != null) query.append("profile_id", String(args.profile_id)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.source_asset != null) query.append("source_asset", String(args.source_asset)) if (args.destination_asset != null) query.append("destination_asset", String(args.destination_asset)) for (const item of args.ids ?? []) query.append("ids", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/orchestration/rules${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(listOrchestrationRulesResponse, await res.json()) };