UNPKG

@nktkas/hyperliquid

Version:

Hyperliquid API SDK for all major JS runtimes, written in TypeScript.

88 lines (87 loc) 2.57 kB
/** * Error types and utilities for Exchange API responses. * @module */ /// <amd-module name="file:///home/runner/work/hyperliquid/hyperliquid/src/api/exchange/_methods/_base/errors.ts" /> import { ApiRequestError } from "../../../_errors.js"; export { ApiRequestError }; /** Top-level error shape. */ type TopLevelError = { status: "err"; response: string; }; /** Bulk error shape with array of statuses. */ type BulkError = { response: { type: string; data: { statuses: unknown[]; }; }; }; /** Single error shape with one status. */ type SingleError = { response: { data: { status: { error: string; }; }; }; }; /** True if `r` matches any of the three Hyperliquid error response shapes. */ export declare function isErrorResponse(r: unknown): r is TopLevelError | BulkError | SingleError; /** * Throws {@linkcode ApiRequestError} if the response is an error; otherwise returns void. * * @param response Raw API response to validate. * * @throws {ApiRequestError} If the response contains an error. */ export declare function assertSuccessResponse(response: unknown): void; /** Flatten an intersection type for cleaner IDE display. */ type Prettify<T> = { [K in keyof T]: T[K]; } & {}; /** Filter out `{ status: "err" }` top-level error shape. */ type ExcludeTopLevelError<T> = T extends { status: "err"; } ? never : T; /** Filter out error variants from `response.data.statuses[]` array. */ type ExcludeBulkError<T> = T extends { response: { data: { statuses: ReadonlyArray<infer S>; }; }; } ? Exclude<S, { error: unknown; }> extends never ? never : Prettify<Omit<T, "response"> & { response: Prettify<Omit<T["response"], "data"> & { data: { statuses: Array<Exclude<S, { error: unknown; }>>; }; }>; }> : T; /** Filter out error variant from `response.data.status` single status. */ type ExcludeSingleError<T> = T extends { response: { data: { status: infer S; }; }; } ? S extends { error: unknown; } ? never : Prettify<Omit<T, "response"> & { response: Prettify<Omit<T["response"], "data"> & { data: { status: Exclude<S, { error: unknown; }>; }; }>; }> : T; /** Exclude all three Hyperliquid error response shapes from `T`. */ export type ExcludeErrorResponse<T> = ExcludeTopLevelError<ExcludeBulkError<ExcludeSingleError<T>>>;