app
app 模块内置的方法可以获取应用的上下文详情,包括函数运行时所处的应用环境及版本
导入:
typescript
import { app } from "@pc-nexus/core";内置方法:
| API | 描述 |
|---|---|
getContext | 获取 Nexus 函数的上下文详情 |
getContext
getContext 函数可获取 Nexus 函数的上下文详情,包括该函数运行时所处的应用环境及版本。
函数签名
typescript
export declare function getContext(): NexusAppContext;
export interface NexusAppContext {
app: {
id: string;
version: string;
licensing: { enabled: boolean };
};
environment: {
id: string;
type: EnvironmentType;
};
team: Team;
installation: {
id: string;
};
invocation: {
id: string;
};
user?: User;
extension?: Extension;
event?: {
trigger: {
key: string;
type: string
};
};
async?: {
consumer: AsyncConsumerInfo;
};
exposer?: {
route: { key: string };
};
}
export enum EnvironmentType {
Development = "development",
Staging = "staging",
Production = "production"
}
export interface Team {
id: string;
url: string;
locale: string;
timezone: string;
}
export interface User {
id: string;
locale: string;
timezone: string;
}
export interface Extension {
key: string;
local_id: string;
target: string;
location: string;
data?: Record<string, unknown>;
}
export interface AsyncConsumerInfo {
key: string;
concurrency: number;
}参数
空
返回值
返回值类型为 NexusAppContext ,包含应用当前运行环境上下文信息的对象,可用的数据取决于应用所使用的模块。
app
定义当前应用数据
| 属性 | 描述 |
|---|---|
id | 当前应用 ID |
version | 当前应用版本 |
licensing | 当前应用许可信息 |
licensing.enabled | 是否启用许可状态 |
team
定义当前应用安装的企业数据
| 属性 | 描述 |
|---|---|
id | 当前应用安装的企业 ID |
url | 当前应用安装的企业访问地址 |
locale | 当前应用安装的企业设置的语言 |
timezone | 当前应用安装的企业设置的时区 |
installation
定义当前应用安装数据
| 属性 | 描述 |
|---|---|
id | 当前应用安装时的唯一标识 |
invocation
定义当前调用的数据
| 属性 | 描述 |
|---|---|
id | 当前调用的唯一标识 |
environment
定义当前应用所在环境数据
| 属性 | 描述 |
|---|---|
id | 当前应用所在的环境 ID |
type | 当前应用所在的环境类型, development 、 staging 还是 production |
user
定义当前用户数据
| 属性 | 描述 |
|---|---|
id | 当前帐户所对应的用户 ID |
locale | 当前用户设置的语言 |
timezone | 当前用户设置的时区 |
extension
定义扩展模块数据
| 属性 | 描述 |
|---|---|
key | 扩展模块唯一标识,在 manifest.yaml 文件定义 |
local_id | 应用在当前页面的唯一 ID |
target | 扩展模块对应扩展点目标,即扩展模块在产品中出现的位置 |
location | 当前页面的位置 |
data | 扩展模块能够访问的上下文数据,与前端获取的上下文数据一致,详情参考 上下文数据 |
event
定义事件订阅数据
| 属性 | 描述 |
|---|---|
trigger.key | 触发器在 manifest.yaml 中定义的 key |
trigger.type | 触发器类型 |
async
定义 异步任务数据。
| 属性 | 描述 |
|---|---|
consumer.key | 异步任务消费者在 manifest.yaml 中定义的 key |
consumer.concurrency | 异步任务消费者在 manifest.yaml 中定义的并发度 |
exposer
定义应用自定义 REST APIs 数据。
| 属性 | 描述 |
|---|---|
route.key | 自定义路由在 manifest.yaml 中定义的 key |
示例
以下是一段使用 getContext 方法获取的数据示例:
typescript
{
"app": {
"id": "fe85a6b3-4073-4fd2-89e0-481a38ca5958",
"version": "1.3.0",
"licensing": {
"enabled": false
}
},
"environment": {
"id": "375cc666-98a7-4bf0-a85e-28085fe1772f",
"type": "development"
},
"team": {
"id": "5db7a0ed77c86b2d749605ad",
"url": "https://your-domain.pingcode.com",
"locale": "en-US",
"timezone": "Asia/Shanghai"
},
"installation": {
"id": "63dd9483-7652-4a2a-9fd7-15783380467b"
},
"invocation": {
"id": "1a538371-7aea-4943-969b-d9c759a80baf"
},
"user": {
"id": "52b9af20da8a4969aab88092d1fa64ce",
"locale": "zh-CN",
"timezone": "Asia/Shanghai"
},
"extension": {
"key": "my-ship-page",
"local_id": "fe85a6b3-4073-4fd2-89e0-481a38ca5958/375cc666-98a7-4bf0-a85e-28085fe1772f/my-ship-page",
"target": "pcm:ship:product:page",
"location": "https://your-domain.pingcode.com/ship/products/CPCWU/apps/fe85a6b3-4073-4fd2-89e0-481a38ca5958/375cc666-98a7-4bf0-a85e-28085fe1772f",
"data": {
"product": {
"id": "68d4983fcd51c2d6113d79c6",
"identifier": "CPCWU",
"name": "产品测试"
}
}
}
}