import {z} from 'zod' export type PatchApiIntentsIdStatusArgs = {id: string, body: {status: 'PENDING' | 'FUNDED' | 'INITIATED' | 'COMPLETED' | 'EXPIRED'}}; export type PatchApiIntentsIdStatusOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiIntentsIdStatusResponse = z.object({id: z.number().optional(), client_id: z.string().optional(), sender: z.string().optional(), initialAmount: z.string().optional(), fees_in_usd: z.string().optional(), app_fee_in_usd: z.string().optional(), total_amount_in_usd: z.string().optional(), total_amount_in_asset_token: z.string().optional(), fees_in_asset_token: z.string().optional(), app_fee_in_asset_token: z.string().optional(), asset_token_symbol: z.string().optional(), asset_token_decimals: z.number().optional(), slippage: z.string().optional(), tokenIn: z.string().optional(), tokenOut: z.string().optional(), intent_address: z.string().optional(), deposit_token_account: z.string().optional(), destination_intent_address: z.string().optional(), source_chain: z.string().optional(), destination_chain: z.string().optional(), recipient: z.string().optional(), refund_address: z.string().optional(), relayer: z.string().optional(), coordinator: z.string().optional(), bridger: z.string().optional(), bridgeExtraData: z.string().optional(), intent_nonce: z.number().optional(), intent_status: z.enum(["PENDING", "FUNDED", "INITIATED", "COMPLETED", "EXPIRED"]).optional(), tx_hash: z.string().optional(), needs_relay: z.boolean().optional(), relayer_claimed: z.boolean().optional(), paymaster_used: z.boolean().optional(), mode: z.enum(["test", "live"]).optional(), expires_at: z.string().optional(), metadata: z.object({}).optional(), created_at: z.string().optional(), updated_at: 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 }; /** * Update intent status * * PATCH /intents/{id}/status */ export const patchApiIntentsIdStatus = async (args: PatchApiIntentsIdStatusArgs, options?: PatchApiIntentsIdStatusOptions) => { const {baseUrl = "https://api.chainrails.io/api/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/intents/${encodeURIComponent(String(args.id))}/status${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiIntentsIdStatusResponse, await res.json()) };