For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/sdk/sapi/runtime/classes/HttpDB.md.
  • 简体中文
  • Class: HttpDB

    Defined in: sapi/runtime/httpdb.ts:78

    SAPI 端 db-server HTTP 客户端门面(底层基于 @minecraft/server-net)。 默认连接本地 3001 端口;各业务模块的 db/config/service 客户端按请求注入各自的私有 token。

    Constructors

    Constructor

    new HttpDB(): HttpDB;

    Returns

    HttpDB

    Methods

    configure()

    static configure(opts: {
      baseUrl?: string;
    }): void;

    Defined in: sapi/runtime/httpdb.ts:89

    注入 db-server 服务基准地址(例如 http://127.0.0.1:4000),会自动去除末尾斜杠。

    Parameters

    ParameterTypeDescription
    opts{ baseUrl?: string; }配置选项。
    opts.baseUrl?string-

    Returns

    void


    getBaseUrl()

    static getBaseUrl(): string;

    Defined in: sapi/runtime/httpdb.ts:96

    返回当前使用的 db-server 服务基准地址。

    Returns

    string


    setAuthToken()

    static setAuthToken(token: string): void;

    Defined in: sapi/runtime/httpdb.ts:105

    设置进程级默认 Bearer token(供平台 ConfigManager / DataAdapter 内部使用)。

    Parameters

    ParameterTypeDescription
    tokenstring平台级鉴权 token。

    Returns

    void


    resolveAuthToken()

    static resolveAuthToken(opts?: HttpRequestAuthOpts): string;

    Defined in: sapi/runtime/httpdb.ts:116

    解析本次请求生效的 Bearer 鉴权 token。 优先使用请求级传入的非空 token;若为空串或未指定则回退使用进程级默认 token。

    Parameters

    ParameterTypeDescription
    opts?HttpRequestAuthOpts请求级鉴权选项。

    Returns

    string

    有效的 Bearer token 字符串。


    withModuleId()

    static withModuleId(path: string, moduleId: string): string;

    Defined in: sapi/runtime/httpdb.ts:129

    为目标 URL 路径附加 ?moduleId= 或 &moduleId= 查询参数(db/config/service 客户端共享)。 服务端鉴权中间件通过 URL 查询参数校验模块身份。

    Parameters

    ParameterTypeDescription
    pathstring原始请求相对路径。
    moduleIdstring模块唯一标识符。

    Returns

    string

    附加了模块参数后的完整请求路径。


    isAvailable()

    static isAvailable(): boolean;

    Defined in: sapi/runtime/httpdb.ts:137

    最近一次健康检查是否成功。

    Returns

    boolean


    checkHealth()

    static checkHealth(): Promise<boolean>;

    Defined in: sapi/runtime/httpdb.ts:151

    探测 /api/health,最多重试 5 次(间隔 2s)。

    Returns

    Promise<boolean>


    fetchJSON()

    static fetchJSON<T>(
       basePath: string, 
       id: string, 
       key: string): Promise<T | null>;

    Defined in: sapi/runtime/httpdb.ts:175

    GET 并解析 JSON 中指定 key 的字段。

    Type Parameters

    Type Parameter
    T

    Parameters

    ParameterType
    basePathstring
    idstring
    keystring

    Returns

    Promise<T | null>


    requestJSON()

    static requestJSON(
       method: HttpRequestMethod, 
       path: string, 
       bodyData?: Record<string, unknown>, 
       opts?: HttpRequestAuthOpts): Promise<{
      status: number;
      body: string;
    }>;

    Defined in: sapi/runtime/httpdb.ts:220

    发起 HTTP 请求并返回原始 status/body。

    Parameters

    ParameterType
    methodHttpRequestMethod
    pathstring
    bodyData?Record<string, unknown>
    opts?HttpRequestAuthOpts

    Returns

    Promise<{ status: number; body: string; }>


    typedRequest()

    static typedRequest<T>(
       method: HttpRequestMethod, 
       path: string, 
       bodyData?: Record<string, unknown>, 
       opts?: HttpRequestAuthOpts): Promise<{
      ok: boolean;
      data?: T;
      error?: string;
      status: number;
    }>;

    Defined in: sapi/runtime/httpdb.ts:230

    发起 HTTP 请求并解析 { ok, data, error } 信封。

    Type Parameters

    Type ParameterDefault type
    Tany

    Parameters

    ParameterType
    methodHttpRequestMethod
    pathstring
    bodyData?Record<string, unknown>
    opts?HttpRequestAuthOpts

    Returns

    Promise<{ ok: boolean; data?: T; error?: string; status: number; }>


    get()

    static get(path: string, opts?: HttpRequestAuthOpts): Promise<string | null>;

    Defined in: sapi/runtime/httpdb.ts:251

    GET 请求;非 200 返回 null。

    Parameters

    ParameterType
    pathstring
    opts?HttpRequestAuthOpts

    Returns

    Promise<string | null>


    post()

    static post(
       path: string, 
       bodyData: Record<string, unknown>, 
       opts?: HttpRequestAuthOpts): Promise<boolean>;

    Defined in: sapi/runtime/httpdb.ts:258

    POST 请求;返回是否 HTTP 200。

    Parameters

    ParameterType
    pathstring
    bodyDataRecord<string, unknown>
    opts?HttpRequestAuthOpts

    Returns

    Promise<boolean>


    put()

    static put(
       path: string, 
       bodyData: Record<string, unknown>, 
       opts?: HttpRequestAuthOpts): Promise<boolean>;

    Defined in: sapi/runtime/httpdb.ts:269

    PUT 请求;返回是否 HTTP 200。

    Parameters

    ParameterType
    pathstring
    bodyDataRecord<string, unknown>
    opts?HttpRequestAuthOpts

    Returns

    Promise<boolean>


    del()

    static del(path: string, opts?: HttpRequestAuthOpts): Promise<boolean>;

    Defined in: sapi/runtime/httpdb.ts:280

    DELETE 请求;返回是否 HTTP 200。

    Parameters

    ParameterType
    pathstring
    opts?HttpRequestAuthOpts

    Returns

    Promise<boolean>