-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcredential.ts
More file actions
146 lines (121 loc) · 3.71 KB
/
credential.ts
File metadata and controls
146 lines (121 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Credential Example / 凭证示例
*
* 此示例展示如何使用 AgentRun SDK 创建和管理 Credential。
* This example demonstrates how to create and manage Credential using AgentRun SDK.
*
* 运行前请确保设置了环境变量 / Ensure environment variables are set:
* - AGENTRUN_ACCESS_KEY_ID
* - AGENTRUN_ACCESS_KEY_SECRET
* - AGENTRUN_ACCOUNT_ID
*
* 运行方式 / Run with:
* npm run example:credential
*/
import {
Credential,
CredentialClient,
CredentialConfig,
ResourceAlreadyExistError,
ResourceNotExistError,
} from '../src/index';
import { logger } from '../src/utils/log';
// Logger helper
function log(message: string, ...args: unknown[]) {
logger.info(`[${new Date().toISOString()}] ${message}`, ...args);
}
const client = new CredentialClient();
const credentialName = 'sdk-test-credential-nodejs';
/**
* 创建或获取 Credential / Create or get Credential
*/
async function createOrGetCredential(): Promise<Credential> {
log('创建或获取已有的资源 / Creating or getting existing resource');
let cred: Credential;
try {
cred = await Credential.create({
input: {
credentialName,
description: '这是通过 Node.js SDK 创建的测试凭证 / Test credential created by Node.js SDK',
credentialConfig: CredentialConfig.inboundApiKey({
apiKey: `sk-test-${Date.now()}`,
}),
},
});
log(`创建成功 / Created successfully: ${cred.credentialId}`);
} catch (error) {
if (error instanceof ResourceAlreadyExistError) {
log('已存在,获取已有资源 / Already exists, getting existing resource');
cred = await Credential.get({ name: credentialName });
} else {
throw error;
}
}
return cred;
}
/**
* 更新 Credential / Update Credential
*/
async function updateCredential(cred: Credential): Promise<void> {
log('更新描述为当前时间 / Updating description to current time');
await cred.update({
input: { description: `当前时间戳 / Current timestamp: ${Date.now()}` },
});
log('更新成功 / Update successful');
log(` - Description: ${cred.description}`);
}
/**
* 列出所有 Credential / List all Credentials
*/
async function listCredentials(): Promise<void> {
log('枚举资源列表 / Listing resources');
const credentials = await Credential.listAll();
log(
`共有 ${credentials.length} 个资源 / Total ${credentials.length} resources:`,
credentials.map(c => c.credentialName)
);
}
/**
* 删除 Credential / Delete Credential
*/
async function deleteCredential(cred: Credential): Promise<void> {
log('开始清理资源 / Starting cleanup');
await cred.delete();
log('删除请求已发送 / Delete request sent');
log('再次尝试获取 / Trying to get again');
try {
await cred.refresh();
log('资源仍然存在 / Resource still exists');
} catch (error) {
if (error instanceof ResourceNotExistError) {
log('得到资源不存在报错,删除成功 / Resource not found, deletion successful');
} else {
throw error;
}
}
}
/**
* 主函数 / Main function
*/
async function main() {
log('==== 凭证模块基本功能示例 / Credential Module Example ====');
try {
// List existing credentials
await listCredentials();
// Create or get credential
const cred = await createOrGetCredential();
// List again
await listCredentials();
// Update credential
await updateCredential(cred);
// Delete credential
await deleteCredential(cred);
// List again to confirm deletion
await listCredentials();
log('==== 示例完成 / Example Complete ====');
} catch (error) {
logger.error('Error:', error);
process.exit(1);
}
}
main();