import {z} from 'zod' export type CreateOrchestrationRequestProfileSource = {profile_id?: string | undefined}; export type CreateOrchestrationRequestSource = {profile?: CreateOrchestrationRequestProfileSource | undefined}; export type CreateOrchestrationRequestCryptoDestination = {address?: string | undefined, network?: string | undefined, address_id?: string | undefined}; export type CreateOrchestrationRequestFiatDestination = {bank_account_id?: string | undefined, memo?: string | undefined}; export type CreateOrchestrationRequestProfileDestination = {profile_id?: string | undefined}; export type CreateOrchestrationRequestDestination = {crypto?: CreateOrchestrationRequestCryptoDestination | undefined, fiat?: CreateOrchestrationRequestFiatDestination | undefined, profile?: CreateOrchestrationRequestProfileDestination | undefined}; export type CreateOrchestrationRequest = {identity_id?: string | undefined, account_id?: string | undefined, ref_id: string, source_asset: string, destination_asset: string, source_amount: unknown, source?: CreateOrchestrationRequestSource | undefined, destination?: CreateOrchestrationRequestDestination | undefined, metadata?: {[key: string]: string} | undefined}; export type CreateApiOrchestrationOrchestrationsArgs = {body: CreateOrchestrationRequest}; export type CreateApiOrchestrationOrchestrationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const transferDetailsTransferStatus = z.enum(["PENDING", "PROCESSING", "COMPLETED", "FAILED"]); export const transferDetails = z.object({transfer_id: z.string().optional(), status: transferDetailsTransferStatus.optional()}); export const orchestrationStatus = z.enum(["PROCESSING", "COMPLETED", "FAILED", "PENDING", "REJECTED"]); export const failReason = z.object({detail: z.string().optional()}); export const orchestrationCryptoSource = z.object({address_id: z.string().optional(), address: z.string().optional(), network: z.string().optional()}); export const orchestrationFiatSource = z.object({deposit_instructions_id: z.string().optional(), memo: z.string().optional()}); export const orchestrationProfileSource = z.object({profile_id: z.string().optional()}); export const orchestrationSource = z.object({crypto: orchestrationCryptoSource.optional(), fiat: orchestrationFiatSource.optional(), profile: orchestrationProfileSource.optional()}); export const orchestrationCryptoDestination = z.object({address_id: z.string().optional(), address: z.string().optional(), network: z.string().optional()}); export const orchestrationFiatDestination = z.object({bank_account_id: z.string().optional(), memo: z.string().optional()}); export const orchestrationProfileDestination = z.object({profile_id: z.string().optional()}); export const orchestrationDestination = z.object({crypto: orchestrationCryptoDestination.optional(), fiat: orchestrationFiatDestination.optional(), profile: orchestrationProfileDestination.optional()}); export const orchestration = z.object({id: z.string().optional(), profile_id: z.string().optional(), identity_id: z.string().optional(), account_id: z.string().optional(), customer_id: z.string().optional(), ref_id: z.string().optional(), source_asset: z.string().optional(), destination_asset: z.string().optional(), source_amount: z.string().optional(), source_transfer: transferDetails.optional(), destination_transfer: transferDetails.optional(), status: orchestrationStatus.optional(), fail_reason: failReason.optional(), source: orchestrationSource.optional(), destination: orchestrationDestination.optional(), orchestration_rule_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), metadata: z.record(z.string(), 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 }; /** * Create Orchestration * * POST /orchestration/orchestrations */ export const createApiOrchestrationOrchestrations = async (args: CreateApiOrchestrationOrchestrationsArgs, options?: CreateApiOrchestrationOrchestrationsOptions) => { 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/orchestrations${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(orchestration, await res.json()) };