import {z} from 'zod' export type GetApiV1OrdersIdArgs = {include_fees?: boolean | undefined}; export type GetApiV1OrdersIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const order = z.object({order_id: z.number(), status: z.string().min(1), user: z.string().min(1), sell: z.object({type: z.string().min(1), data: z.object({token_id: z.string().min(1), id: z.string().min(1), token_address: z.string().min(1), quantity: z.string().min(1), properties: z.object({name: z.string().min(1), image_url: z.string().min(1), collection: z.object({name: z.string().min(1), icon_url: z.string().min(1)})})})}), buy: z.object({type: z.string().min(1), data: z.object({token_address: z.string(), decimals: z.number(), quantity: z.string().min(1)})}), amount_sold: z.string().optional(), expiration_timestamp: z.string().min(1), timestamp: z.string().min(1), updated_timestamp: z.string().min(1)}); 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 }; /** * Get details of an order with the given ID * * GET /v1/orders/{id} */ export const getApiV1OrdersId = async (args: GetApiV1OrdersIdArgs, options?: GetApiV1OrdersIdOptions) => { const {baseUrl = "https://api.x.immutable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_fees != null) query.append("include_fees", String(args.include_fees)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orders/${encodeURIComponent(String(args.id))}${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(order, await res.json()) };