Skip to content

remote ​

remote 模块用于向远程服务发起 HTTP 请求,支持应用在 Nexus 函数内与远程后端服务进行集成。

导入:

typescript
import { remote } from "@pc-nexus/network";

内置方法:

API描述
invoke向远程服务发起 HTTP 请求,该方法会根据配置决定是否携带 OAuth 令牌

invoke ​

使用 invoke 方法发起一个 HTTP 请求到配置的远程服务端点。

函数签名 ​

javascript
function invoke(remoteKey: string, options: RemoteInvokeOptions): Promise<Response>

type RemoteInvokeOptions = { path?: string; } & RequestInit;

参数 ​

名称描述
remoteKeymanifest 中声明的 remote key
options请求选项,见下方详细说明

RemoteInvokeOptions 类型定义如下:

名称描述
path请求路由地址
methodHTTP 请求方法
headers自定义请求头
body请求体内容

返回值 ​

返回标准的 Response 对象,可通过 response.status 、 response.ok 、 response.json() 、 response.text() 等标准方法处理响应。

示例 ​

GET 请求 ​

javascript
import { remote } from "@pc-nexus/network";

const response = await remote.invoke(`my-remote-key`, {
    path: "/greeting?name=nexus",
    method: "GET"
});

const data = await response.json();
console.log(data);

POST 请求 ​

javascript
import { remote } from "@pc-nexus/network";

const response = await remote.invoke(`my-remote-key`, {
    path: "/work",
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
    body: JSON.stringify({
        team: "nexus",
    })
});

const status = response.status;
const result = await response.json();

错误处理 ​

发生错误时响应都会附带一个包含更多信息的错误代码,下面列出了所有可能的错误代码、它们的含义以及可以采取哪些措施来解决。

错误码描述
ERR_REMOTE_KEY_NOT_FOUND调用 Remote 操作参数 remoteKey 的值未在 Manifest 文件中定义。

举报邮箱:abuse@worktile.com