import {z} from 'zod' export type GetApiV1AssetsArgs = {page_size?: number | undefined, cursor?: string | undefined, order_by?: string | undefined, direction?: string | undefined, user?: string | undefined, status?: string | undefined, name?: string | undefined, metadata?: string | undefined, sell_orders?: boolean | undefined, buy_orders?: boolean | undefined, include_fees?: boolean | undefined, collection?: string | undefined, updated_min_timestamp?: string | undefined, updated_max_timestamp?: string | undefined}; export type GetApiV1AssetsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const application = z.object({id: z.string().min(1), name: z.string().min(1), created_at: z.string().min(1)}); export const assetList = z.object({result: z.array(application), cursor: z.string().min(1), remaining: z.number()}); 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 a list of assets * * GET /v1/assets */ export const getApiV1Assets = async (args: GetApiV1AssetsArgs, options?: GetApiV1AssetsOptions) => { 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.page_size != null) query.append("page_size", String(args.page_size)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.user != null) query.append("user", String(args.user)) if (args.status != null) query.append("status", String(args.status)) if (args.name != null) query.append("name", String(args.name)) if (args.metadata != null) query.append("metadata", String(args.metadata)) if (args.sell_orders != null) query.append("sell_orders", String(args.sell_orders)) if (args.buy_orders != null) query.append("buy_orders", String(args.buy_orders)) if (args.include_fees != null) query.append("include_fees", String(args.include_fees)) if (args.collection != null) query.append("collection", String(args.collection)) if (args.updated_min_timestamp != null) query.append("updated_min_timestamp", String(args.updated_min_timestamp)) if (args.updated_max_timestamp != null) query.append("updated_max_timestamp", String(args.updated_max_timestamp)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets${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(assetList, await res.json()) };