Skip to content

remote ​

remote 使你的应用能够调用 PingCode 外部平台上的远程后端服务。

导入:

typescript
import { remote } from '@pc-nexus/bridge';

内置方法:

API描述
invoke允许应用能够与托管在外部的远程服务进行集成,根据配置决定是否携带 OAuth 令牌
request允许应用在前端代码中直接向远程服务发送请求,不携带 OAuth 令牌

invoke ​

允许应用能够与托管在外部的远程服务进行集成。在请求被转发到远程服务之前,会先进行验证,并添加必要的认证信息,如果配置了 OAuth 令牌,则会使用 OAuth 令牌。

函数签名 ​

typescript
function invoke(options: RemoteInvokeOptions): Promise<Response>;

type RemoteInvokeOptions = { path: string; } & Pick<RequestInit, "headers" | "body" | "method">;

参数 ​

名称说明
options调用远程函数传入的选项(见下方详细说明)

RemoteInvokeOptions 类型定义如下:

名称说明
path远程服务的路径, 完整的调用地址为 Remote 配置的 baseUrl + path
methodHTTP 请求方法
headers自定义请求头
body请求体内容

返回值 ​

参见 WHATWG 响应对象 ,通过 Promise 返回。

示例 ​

typescript
import { remote } from '@pc-nexus/bridge';

const response = await remote.invoke({
  path: '/my-api',
  method: 'POST',
  headers: {
    'content-type': 'application/json',
  },
  body: JSON.stringify({
    message: 'hello',
  }),
});

console.log(response.status);

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

request ​

允许应用在前端代码中直接向远程服务发送请求,与 invoke 方法不同,即使为远程配置了 OAuth 令牌, request 方法也不会包含这些令牌。

函数签名 ​

typescript
function request(remoteKey: string, options?: RemoteRequestOptions): Promise<Response>;

type RemoteRequestOptions = { path: string; } & Omit<RequestInit, "signal">;

参数 ​

名称说明
remoteKey远程服务配置的唯一标识字符串,对应 remotes[].key
options调用远程函数传入的选项(见下方详细说明)

RemoteRequestOptions 类型定义如下:

名称说明
path远程服务的路径, 完整的调用地址为 Remote 配置的 baseUrl + path
methodHTTP 请求方法
headers自定义请求头
body请求体内容

返回值 ​

参见 WHATWG 响应对象 ,通过 Promise 返回。

示例 ​

typescript
import { remote } from '@pc-nexus/bridge';

const response = await remote.request('my-remote', {
  path: '/my-api',
  method: 'POST',
  headers: {
    'content-type': 'application/json',
  },
  body: JSON.stringify({
    message: 'hello',
  }),
});

console.log(response.status);

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

举报邮箱:abuse@worktile.com