import {z} from 'zod' export type OrchestrationAddressModeEnum = 'PER_DEPOSIT' | 'SCHEDULED' | 'THRESHOLD'; export type OrchestrationScheduleIntervalEnum = 'HOURLY' | 'DAILY' | 'WEEKLY'; export type OrchestrationAddressSchedule = {interval: OrchestrationScheduleIntervalEnum, nextRunAt?: string | undefined}; export type UpdateOrchestrationAddress = {offrampMessage?: (string | null) | undefined, mode?: OrchestrationAddressModeEnum | undefined, schedule?: (OrchestrationAddressSchedule | null) | undefined, thresholdAmount?: (string | null) | undefined, destination?: {accountId: string} | undefined}; export type CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdUpdateArgs = {userId: string, orchestrationAddressId: string, body: UpdateOrchestrationAddress}; export type CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdUpdateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationAddressModeEnum = z.enum(["PER_DEPOSIT", "SCHEDULED", "THRESHOLD"]); export const orchestrationScheduleIntervalEnum = z.enum(["HOURLY", "DAILY", "WEEKLY"]); export const orchestrationAddressStatusEnum = z.enum(["PENDING_WALLET", "ACTIVE", "DEACTIVATED"]); export const orchestrationAddressObject = z.object({id: z.string().optional(), userId: z.string().optional(), address: z.string().nullable().optional(), source: z.object({currency: z.enum(["usdc", "usdt"]).optional(), chain: z.string().optional()}).optional(), destination: z.object({currency: z.literal("usd").optional(), accountId: z.string().optional()}).optional(), offrampMessage: z.string().nullable().optional(), mode: orchestrationAddressModeEnum.optional(), schedule: z.object({interval: orchestrationScheduleIntervalEnum.optional(), nextRunAt: z.string().optional()}).nullable().optional(), thresholdAmount: z.string().nullable().optional(), status: orchestrationAddressStatusEnum.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), deactivatedAt: z.string().nullable().optional(), balance: z.object({currency: z.enum(["usdc", "usdt"]).optional(), amount: z.string().optional()}).nullable().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 }; /** * Update an orchestration address * * POST /v2/users/{userId}/orchestration-addresses/{orchestrationAddressId}/update */ export const createApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdUpdate = async (args: CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdUpdateArgs, options?: CreateApiV2UsersUserIdOrchestrationAddressesOrchestrationAddressIdUpdateOptions) => { const {baseUrl = "https://production.hifibridge.com", 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(/\/+$/, '')}/v2/users/${encodeURIComponent(String(args.userId))}/orchestration-addresses/${encodeURIComponent(String(args.orchestrationAddressId))}/update${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(orchestrationAddressObject, await res.json()) };