# @cortexapps/plugin-core 中文文档

规范地址：https://npmcn.com/package/%40cortexapps/plugin-core/

版本：3\.1\.0
npm 元数据更新时间：2026-04-20T20:53:20.455Z
中文译文更新时间：2026-11-10T19:52:28.190Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cortexapps/plugin-core
原文对照：https://npmcn.com/package/%40cortexapps/plugin-core/?lang=original
项目主页：https://github.com/cortexapps/plugin-core#readme
源码仓库：https://github.com/cortexapps/plugin-core

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：48.6 MB，10156 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：未声明
- 运行依赖：65 个
- peer 依赖：react \>=18\.2\.0
- 首次发布：2023-04-21T17:13:48.335Z
- 最新版本发布：2026-04-20T20:53:20.297Z
- 近 30 天下载量：5901（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：41（近 12 个月 1 个）

## 中文简介

创建 Cortex 插件的核心助手

## 安装

```sh
npm install @cortexapps/plugin-core
```

也可以使用：`pnpm add @cortexapps/plugin-core`、`yarn add @cortexapps/plugin-core`、`bun add @cortexapps/plugin-core`

## README

[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)

# `@cortexapps/plugin-core`

此套件为Cortex插件和Cortex网络应用提供了方便使用的桥梁.

安装它

```cli
yarn add @cortexapps/plugin-core
```

或者说

```cli
npm install @cortexapps/plugin-core
```

## 克特克斯阿皮语Name

暴露的主要实体是: `CortexApi`,具有方法 `proxyFetch()`, (中文(简体) ), `getContext()` 财务报告和审定财务报表 `pluginInit()`。 。 。 .

### `pluginInit`

打电话 `pluginInit()` 尽早触发 iframe 主机将主题样式注入您的插件.

#### 参数

无

#### 回返

`void`

### `proxyFetch`

> **页:1**: 截至1.3.0,全球 `fetch` 要调用的方法是猴子补丁 `proxyFetch()` 在引擎盖下。 这意味着你可以使用 `fetch()` 您通常会在 Cortex 插件中自动通过 Cortex 代理 .

`proxyFetch` 为插件提供了一个方法,通过 Cortex 代理来提出API 请求,以避免浏览器 CORS 限制并安全地添加认证. 它的签名与本地浏览器几乎相同 `fetch()` 方法,但一些属性被平整后可以跨出iframe桥传输.

#### 参数

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| 投入 | 请求输入 | 见 [浏览器获取](https://developer.mozilla.org/en-US/docs/Web/API/fetch) |
| 资讯 | 请求信息 | 见 [浏览器获取](https://developer.mozilla.org/en-US/docs/Web/API/fetch) |

#### 回返

`Promise<ShimResponse>` - 几乎与浏览器的反应完全相同。 见 [源代码](https://npmcn.com/package/%40cortexapps/plugin-core/src/api/ShimResponse.ts) 更多细节.

#### 示例

```ts
import { CortexApi } from '@cortexapps/plugin-core';

const commentOnPullRequest = async ({
  comment: string,
  owner: string,
  pullNumber: string | number,
  repo: string,
}) => {
  const response = await CortexApi.proxyFetch(
    `https://api.github.com/${owner}/${repo}/pulls/${pullNumber}/comments`,
    {
      body: comment,
      method: 'POST',
    }
  );

  return response.json();
};
```

### 使用 `proxyFetch` 与 [缩写](https://github.com/octokit)

> **页:1**: 截至1.3.0,使用octokit插件(或调整其他库)并不必要,因为全球 `fetch` 方法默认是shimmed.

使用 `octokitPlugin` 在 Cortex 插件中方便使用 octokit.

```ts
import { CortexApi, octokitPlugin } from '@cortexapps/plugin-core';
import { Octokit, type RestEndpointMethodTypes } from "@octokit/rest";

const CortexOctokit = Octoki.plugin(octokitPlugin);

const getWorkflow = async (options: {
  id: number;
  owner: string;
  repo: string;
}): Promise<
  RestEndpointMethodTypes["actions"]["getWorkflow"]["response"]["data"]
> {
  const { owner, repo, id } = options;
  const octokit = new CortexOctokit({ CortexApi });

  const workflow = await octokit.actions.getWorkflow({
    owner,
    repo,
    workflow_id: id,
  });

  return workflow.data;
};
```

### `getContext`

> **页:1**: 自1.2.0起,上下文也可通过 `useCortexContext` 钩.

#### 参数

无

#### 回返

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| 实体 | 实体? | 关于该插件相关页面的实体的详细情况,如果适用,则正在提供,否则无效。 见 [Cortex 实体](https://npmcn.com/package/%40cortexapps/plugin-core/src/api/cortexEntityTypes.ts) 更多细节. |
| 地点 | 插件连接 | 此插件正在提供哪个位置类型 . [地点](https://npmcn.com/package/%40cortexapps/plugin-core/src/api/cortexEntityTypes.ts) 更多细节. |
| 用户 | Cortex 用户 | 见 [Cortex 用户](https://npmcn.com/package/%40cortexapps/plugin-core/src/api/cortexEntityTypes.ts) 更多细节. |

#### 示例

```ts
import { CortexApi } from '@cortexapps/plugin-core';

const getCurrentUser = async () => {
  const response = await CortexApi.getContext();
  const resJson = await response.json();

  return resJson.user;
};
```

## 构成部分库

### 2.x的折旧和迁移

组件库被折旧为QQ3.0.0版本,只支持关键修正. 请使用 https://www.npmjs.com/package/@cortexapps/react-plugin-ui 用于React组件和文档.

为了帮助你的移民, `plugin-core/components` 已转换为使用 `react-plugin-ui`但是这需要一些 充分移徙的步骤:

1.  安装两者 `@cortexapps/plugin-core@3.0.0` 财务报告和审定财务报表 `@cortexapps/react-plugin-ui@1.0.0` 作为依附关系
2.  包含 `import "@cortexapps/react-plugin-ui/index.css";` 全球样式
3.  当你的应用靴,打电话 `CortexApi.pluginInit();` 更改主题
4.  确定您的插件正在使用 `<PluginProvider>` 上下文包装。 这提供了黑暗/ 灯光模式类 `<body>`

这些步骤应该能使您的组件在暗/ 光模式之间切换, 在视觉上匹配我们的组件样式 .

新的样式将 CSS 重置器应用到基元素( 如 `ul`编号 :`li`) - 可能需要一些小的视觉调整 在您的插件中的某些区域,如果它们受到影响.

### 构成部分详情

以本地操作方式检查故事书中的组件库 `yarn storybook`。 。 。 .

从 `@cortexapps/plugin-core/components` 在应用程序中使用它们.

```tsx
import React from 'react';
import { Button } from '@cortexapps/plugin-core/components';

interface SubmitButtonProps extends React.PropsWithChildren;

const SubmitButton: React.FC<SubmitButtonProps> = ({ children }) => {
  return (
    <Button type="submit">{children}</Button>
  );
};

export default SubmitButton;
```

### 可用组件

-   锁定
-   徽标
-   方框
-   面包屑
-   按钮
-   纸牌
-   复选框
-   可点击
    -   一个方便的包装 `<a>` 或一个 `<button>` (取决于道具) 没有典型的 `<a>` 或者说 `<button>` 样式
-   下坠
-   奇幻游戏
-   窗体标签
-   图标集(恭维 [磷酸盐图标](https://phosphoricons.com/)(中文(简体) )
-   投入
-   标签
-   线性百分比
-   列表
-   装入器
-   标记
    -   这是Cortex的标志!
-   模式
-   药丸
-   门户
    -   你可能不需要担心这个-- 这是我们内部用来制造模式和相似的
-   进度栏
-   堆栈
    -   适用于子女间平分间隔的超有用部分
-   标签
-   标记
-   文本
-   标题
-   切换
-   类型头

详见"打字和故事书".

## 常见问题

### @cortexapps/plugin-core 是什么？

创建 Cortex 插件的核心助手

### 如何安装 @cortexapps/plugin-core？

在项目目录运行 npm install @cortexapps/plugin-core，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（\>=18\.2\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @cortexapps/plugin-core 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/cortexapps\_\_plugin-core 是否存在。

### @cortexapps/plugin-core 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @cortexapps/plugin-core 可以商用吗？

@cortexapps/plugin-core 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @cortexapps/plugin-core 的下载量有多少？

根据 npm 官方下载统计，@cortexapps/plugin-core 在 2026/10/11 – 2026/11/09 这 30 天内被下载 5,901 次，日均约 197 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @cortexapps/plugin-core 还在维护吗？

最新版本 3\.1\.0 发布于 2026/04/21（6 个月前）；至今共发布 41 个版本，其中过去 12 个月发布了 1 个；首次发布于 2023/04/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

