connect-api-cli 1.1.3 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -19
- package/dist/cli.js +523 -331
- package/dist/index.d.mts +13 -7
- package/dist/index.d.ts +13 -7
- package/dist/index.js +234 -30
- package/dist/index.mjs +1379 -10
- package/package.json +11 -7
- package/dist/chunk-3WGV37EN.mjs +0 -1187
- package/dist/chunk-3WGV37EN.mjs.map +0 -1
- package/dist/cli.d.mts +0 -1
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/cli.mjs +0 -1711
- package/dist/cli.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/chunk-3WGV37EN.mjs
DELETED
|
@@ -1,1187 +0,0 @@
|
|
|
1
|
-
// src/config/index.ts
|
|
2
|
-
import * as dotenv from "dotenv";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import fs from "fs";
|
|
5
|
-
var envPath = path.resolve(process.cwd(), ".env");
|
|
6
|
-
if (fs.existsSync(envPath)) {
|
|
7
|
-
dotenv.config({ path: envPath, quiet: true });
|
|
8
|
-
} else {
|
|
9
|
-
dotenv.config({ quiet: true });
|
|
10
|
-
}
|
|
11
|
-
var DEFAULT_OAUTH = {
|
|
12
|
-
clientId: "117427577",
|
|
13
|
-
authUrl: "https://oauth-login.cloud.huawei.com/oauth2/v3/authorize",
|
|
14
|
-
tokenUrl: "https://connect-api.cloud.huawei.com/api/cag/v1/access-token/generate",
|
|
15
|
-
refreshUrl: "https://connect-api.cloud.huawei.com/api/cag/v1/access-token/refresh",
|
|
16
|
-
redirectUri: "http://localhost:12306",
|
|
17
|
-
scope: "openid profile https://www.huawei.com/auth/agc/publish https://www.huawei.com/auth/agc/develop"
|
|
18
|
-
};
|
|
19
|
-
function loadServiceAccountCredentials(filePath) {
|
|
20
|
-
const abs = path.isAbsolute(filePath) ? filePath : path.resolve(process.cwd(), filePath);
|
|
21
|
-
if (!fs.existsSync(abs)) {
|
|
22
|
-
throw new Error(`Service Account credentials file not found: ${abs}`);
|
|
23
|
-
}
|
|
24
|
-
let raw;
|
|
25
|
-
try {
|
|
26
|
-
raw = JSON.parse(fs.readFileSync(abs, "utf8"));
|
|
27
|
-
} catch {
|
|
28
|
-
throw new Error(`Invalid Service Account credentials JSON: ${abs}`);
|
|
29
|
-
}
|
|
30
|
-
const creds = raw;
|
|
31
|
-
if (!creds.key_id || !creds.private_key || !creds.sub_account) {
|
|
32
|
-
throw new Error(
|
|
33
|
-
`Service Account credentials missing required fields (key_id, private_key, sub_account): ${abs}`
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
key_id: creds.key_id,
|
|
38
|
-
private_key: creds.private_key,
|
|
39
|
-
sub_account: creds.sub_account,
|
|
40
|
-
project_id: creds.project_id,
|
|
41
|
-
auth_uri: creds.auth_uri,
|
|
42
|
-
token_uri: creds.token_uri,
|
|
43
|
-
auth_provider_cert_uri: creds.auth_provider_cert_uri,
|
|
44
|
-
client_cert_uri: creds.client_cert_uri
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function getConfig() {
|
|
48
|
-
const clientId = process.env.AGC_CLIENT_ID;
|
|
49
|
-
const clientSecret = process.env.AGC_CLIENT_SECRET;
|
|
50
|
-
const authMode = process.env.AGC_AUTH_MODE ?? "client_credentials";
|
|
51
|
-
if (authMode === "client_credentials" && (!clientId || !clientSecret)) {
|
|
52
|
-
throw new Error("Missing AGC credentials. Please set AGC_CLIENT_ID and AGC_CLIENT_SECRET environment variables.");
|
|
53
|
-
}
|
|
54
|
-
if (authMode === "service_account" && !process.env.AGC_SA_CREDENTIALS) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
"Missing Service Account credentials. Please set AGC_SA_CREDENTIALS to the path of your *_private.json file."
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
const oauth = {
|
|
60
|
-
clientId: process.env.AGC_OAUTH_CLIENT_ID || DEFAULT_OAUTH.clientId,
|
|
61
|
-
authUrl: process.env.AGC_OAUTH_AUTH_URL || DEFAULT_OAUTH.authUrl,
|
|
62
|
-
tokenUrl: process.env.AGC_OAUTH_TOKEN_URL || DEFAULT_OAUTH.tokenUrl,
|
|
63
|
-
refreshUrl: process.env.AGC_OAUTH_REFRESH_URL || DEFAULT_OAUTH.refreshUrl,
|
|
64
|
-
redirectUri: process.env.AGC_OAUTH_REDIRECT_URI || DEFAULT_OAUTH.redirectUri,
|
|
65
|
-
scope: process.env.AGC_OAUTH_SCOPE || DEFAULT_OAUTH.scope
|
|
66
|
-
};
|
|
67
|
-
let serviceAccount;
|
|
68
|
-
if (authMode === "service_account") {
|
|
69
|
-
const credentialsPath = path.isAbsolute(process.env.AGC_SA_CREDENTIALS) ? process.env.AGC_SA_CREDENTIALS : path.resolve(process.cwd(), process.env.AGC_SA_CREDENTIALS);
|
|
70
|
-
serviceAccount = {
|
|
71
|
-
credentialsPath,
|
|
72
|
-
credentials: loadServiceAccountCredentials(credentialsPath)
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
const resolvedClientId = authMode === "oauth" ? oauth.clientId : authMode === "service_account" ? serviceAccount.credentials.key_id : clientId || "";
|
|
76
|
-
return {
|
|
77
|
-
clientId: resolvedClientId,
|
|
78
|
-
clientSecret: clientSecret || "",
|
|
79
|
-
domain: process.env.AGC_DOMAIN || "connect-api.cloud.huawei.com",
|
|
80
|
-
authMode,
|
|
81
|
-
oauth,
|
|
82
|
-
teamId: process.env.AGC_TEAM_ID || void 0,
|
|
83
|
-
serviceAccount
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/services/auth.ts
|
|
88
|
-
import axios from "axios";
|
|
89
|
-
import * as crypto from "crypto";
|
|
90
|
-
var SA_AUD = "https://oauth-login.cloud.huawei.com/oauth2/v3/token";
|
|
91
|
-
var SA_JWT_TTL_SEC = 3600;
|
|
92
|
-
var AuthService = class {
|
|
93
|
-
/**
|
|
94
|
-
* 获取访问API的Token(service to service,client_credentials 模式)
|
|
95
|
-
*/
|
|
96
|
-
static async getToken() {
|
|
97
|
-
const config2 = getConfig();
|
|
98
|
-
const url = `https://${config2.domain}/api/oauth2/v1/token`;
|
|
99
|
-
const payload = {
|
|
100
|
-
grant_type: "client_credentials",
|
|
101
|
-
client_id: config2.clientId,
|
|
102
|
-
client_secret: config2.clientSecret
|
|
103
|
-
};
|
|
104
|
-
const response = await axios.post(url, payload, {
|
|
105
|
-
headers: {
|
|
106
|
-
"Content-Type": "application/json"
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
return response.data;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 使用 Service Account 凭据本地签发 PS256 JWT(可直接作为 Authorization Bearer)。
|
|
113
|
-
* 参见:获取服务端授权 → Service Account 方式。
|
|
114
|
-
*/
|
|
115
|
-
static createServiceAccountJwt(credentials, nowSec = Math.floor(Date.now() / 1e3)) {
|
|
116
|
-
const iat = nowSec;
|
|
117
|
-
const exp = iat + SA_JWT_TTL_SEC;
|
|
118
|
-
const aud = credentials.token_uri || SA_AUD;
|
|
119
|
-
const header = {
|
|
120
|
-
kid: credentials.key_id,
|
|
121
|
-
typ: "JWT",
|
|
122
|
-
alg: "PS256"
|
|
123
|
-
};
|
|
124
|
-
const payload = {
|
|
125
|
-
aud,
|
|
126
|
-
iss: credentials.sub_account,
|
|
127
|
-
exp,
|
|
128
|
-
iat
|
|
129
|
-
};
|
|
130
|
-
const encodedHeader = base64UrlEncode(Buffer.from(JSON.stringify(header), "utf8"));
|
|
131
|
-
const encodedPayload = base64UrlEncode(Buffer.from(JSON.stringify(payload), "utf8"));
|
|
132
|
-
const signingInput = `${encodedHeader}.${encodedPayload}`;
|
|
133
|
-
const privateKey = crypto.createPrivateKey(normalizePem(credentials.private_key));
|
|
134
|
-
const signature = crypto.sign("sha256", Buffer.from(signingInput, "utf8"), {
|
|
135
|
-
key: privateKey,
|
|
136
|
-
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
137
|
-
// PS256:saltLength = digest length(与 Java SHA256withRSA/PSS 常见默认一致)
|
|
138
|
-
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST
|
|
139
|
-
});
|
|
140
|
-
const jwt = `${signingInput}.${base64UrlEncode(signature)}`;
|
|
141
|
-
return {
|
|
142
|
-
jwt,
|
|
143
|
-
expiresIn: SA_JWT_TTL_SEC,
|
|
144
|
-
expiresAt: exp * 1e3
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* 拼装 OAuth 授权 URL(含随机 state 防 CSRF)
|
|
149
|
-
*/
|
|
150
|
-
static buildAuthUrl(state) {
|
|
151
|
-
const cfg = getConfig();
|
|
152
|
-
const url = new URL(cfg.oauth.authUrl);
|
|
153
|
-
url.searchParams.set("client_id", cfg.oauth.clientId);
|
|
154
|
-
url.searchParams.set("redirect_uri", cfg.oauth.redirectUri);
|
|
155
|
-
url.searchParams.set("scope", cfg.oauth.scope);
|
|
156
|
-
url.searchParams.set("response_type", "code");
|
|
157
|
-
url.searchParams.set("state", state);
|
|
158
|
-
url.searchParams.set("access_type", "offline");
|
|
159
|
-
return url.toString();
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* 用授权码换取 OAuth token(与插件 /access-token/generate 对齐)
|
|
163
|
-
*/
|
|
164
|
-
static async getOAuthToken(code, redirectUri) {
|
|
165
|
-
const cfg = getConfig();
|
|
166
|
-
const controller = new AbortController();
|
|
167
|
-
const timeoutId = setTimeout(() => controller.abort(), 3e4);
|
|
168
|
-
try {
|
|
169
|
-
const res = await axios.post(cfg.oauth.tokenUrl, { code, redirectUri }, {
|
|
170
|
-
headers: { "Content-Type": "application/json" },
|
|
171
|
-
signal: controller.signal
|
|
172
|
-
});
|
|
173
|
-
if (!res.data.ret || res.data.ret.code !== 0) {
|
|
174
|
-
throw new Error(res.data.ret?.msg || "Authentication failed");
|
|
175
|
-
}
|
|
176
|
-
return res.data;
|
|
177
|
-
} catch (err) {
|
|
178
|
-
if (err instanceof Error && err.name === "AbortError") {
|
|
179
|
-
throw new Error("\u8BF7\u6C42\u8D85\u65F6\uFF0C\u8BF7\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5");
|
|
180
|
-
}
|
|
181
|
-
throw err;
|
|
182
|
-
} finally {
|
|
183
|
-
clearTimeout(timeoutId);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* 用 refreshToken 刷新 OAuth token(与插件 /access-token/refresh 对齐)。
|
|
188
|
-
* 刷新接口只返回新的 accessToken,refreshToken 沿用旧值。
|
|
189
|
-
*/
|
|
190
|
-
static async refreshOAuthToken(refreshToken) {
|
|
191
|
-
const cfg = getConfig();
|
|
192
|
-
const controller = new AbortController();
|
|
193
|
-
const timeoutId = setTimeout(() => controller.abort(), 3e4);
|
|
194
|
-
try {
|
|
195
|
-
const res = await axios.post(cfg.oauth.refreshUrl, { refreshToken }, {
|
|
196
|
-
headers: { "Content-Type": "application/json" },
|
|
197
|
-
signal: controller.signal
|
|
198
|
-
});
|
|
199
|
-
if (!res.data.ret || res.data.ret.code !== 0) {
|
|
200
|
-
throw new Error(res.data.ret?.msg || "Refresh failed");
|
|
201
|
-
}
|
|
202
|
-
return res.data;
|
|
203
|
-
} catch (err) {
|
|
204
|
-
if (err instanceof Error && err.name === "AbortError") {
|
|
205
|
-
throw new Error("\u8BF7\u6C42\u8D85\u65F6\uFF0C\u8BF7\u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5");
|
|
206
|
-
}
|
|
207
|
-
throw err;
|
|
208
|
-
} finally {
|
|
209
|
-
clearTimeout(timeoutId);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
function base64UrlEncode(buf) {
|
|
214
|
-
return buf.toString("base64").replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
215
|
-
}
|
|
216
|
-
function normalizePem(privateKey) {
|
|
217
|
-
const trimmed = privateKey.trim();
|
|
218
|
-
if (trimmed.includes("\\n")) {
|
|
219
|
-
return trimmed.replace(/\\n/g, "\n");
|
|
220
|
-
}
|
|
221
|
-
return trimmed;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// src/core/oauth-team.ts
|
|
225
|
-
import axios2 from "axios";
|
|
226
|
-
import fs2 from "fs";
|
|
227
|
-
import path2 from "path";
|
|
228
|
-
import os from "os";
|
|
229
|
-
var TOKEN_FILE = path2.join(os.homedir(), ".connect-api-cli-token.json");
|
|
230
|
-
var cachedTeamId;
|
|
231
|
-
function readPersistedTeamId() {
|
|
232
|
-
try {
|
|
233
|
-
if (!fs2.existsSync(TOKEN_FILE)) return void 0;
|
|
234
|
-
const data = JSON.parse(fs2.readFileSync(TOKEN_FILE, "utf8"));
|
|
235
|
-
return data.teamId || void 0;
|
|
236
|
-
} catch {
|
|
237
|
-
return void 0;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
function writePersistedTeamId(teamId) {
|
|
241
|
-
try {
|
|
242
|
-
if (!fs2.existsSync(TOKEN_FILE)) return;
|
|
243
|
-
const data = JSON.parse(fs2.readFileSync(TOKEN_FILE, "utf8"));
|
|
244
|
-
data.teamId = teamId;
|
|
245
|
-
fs2.writeFileSync(TOKEN_FILE, JSON.stringify(data, null, 2), "utf8");
|
|
246
|
-
} catch {
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
async function resolveOAuthTeamId(cfg, accessToken) {
|
|
250
|
-
if (cfg.teamId) {
|
|
251
|
-
return cfg.teamId;
|
|
252
|
-
}
|
|
253
|
-
if (cachedTeamId !== void 0) {
|
|
254
|
-
return cachedTeamId || void 0;
|
|
255
|
-
}
|
|
256
|
-
const persisted = readPersistedTeamId();
|
|
257
|
-
if (persisted) {
|
|
258
|
-
cachedTeamId = persisted;
|
|
259
|
-
return persisted;
|
|
260
|
-
}
|
|
261
|
-
const res = await axios2.get(
|
|
262
|
-
`https://${cfg.domain}/api/ups/user-permission-service/v1/user-team-list`,
|
|
263
|
-
{
|
|
264
|
-
headers: {
|
|
265
|
-
oauth2Token: accessToken,
|
|
266
|
-
"Content-Type": "application/json"
|
|
267
|
-
},
|
|
268
|
-
timeout: 3e4
|
|
269
|
-
}
|
|
270
|
-
);
|
|
271
|
-
const teams = res.data.teams || [];
|
|
272
|
-
if (res.data.ret && res.data.ret.code !== 0) {
|
|
273
|
-
throw new Error(
|
|
274
|
-
`Failed to list teams for OAuth teamId: ${res.data.ret.msg || JSON.stringify(res.data.ret)}`
|
|
275
|
-
);
|
|
276
|
-
}
|
|
277
|
-
if (teams.length === 0) {
|
|
278
|
-
cachedTeamId = null;
|
|
279
|
-
throw new Error("OAuth account has no teams. Cannot set teamId for API calls.");
|
|
280
|
-
}
|
|
281
|
-
if (teams.length === 1) {
|
|
282
|
-
cachedTeamId = teams[0].id;
|
|
283
|
-
writePersistedTeamId(cachedTeamId);
|
|
284
|
-
return cachedTeamId;
|
|
285
|
-
}
|
|
286
|
-
cachedTeamId = null;
|
|
287
|
-
const preview = teams.slice(0, 8).map((t) => `${t.id}${t.name ? `(${t.name})` : ""}`).join(", ");
|
|
288
|
-
throw new Error(
|
|
289
|
-
`Multiple teams found (${teams.length}). Set AGC_TEAM_ID to one of: ${preview}`
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
function getCachedOAuthTeamId() {
|
|
293
|
-
if (cachedTeamId) return cachedTeamId;
|
|
294
|
-
return readPersistedTeamId();
|
|
295
|
-
}
|
|
296
|
-
function clearOAuthTeamIdCache() {
|
|
297
|
-
cachedTeamId = void 0;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// src/core/auth-mgr.ts
|
|
301
|
-
import * as crypto2 from "crypto";
|
|
302
|
-
import * as http from "http";
|
|
303
|
-
import { exec } from "child_process";
|
|
304
|
-
import fs3 from "fs";
|
|
305
|
-
import path3 from "path";
|
|
306
|
-
import os2 from "os";
|
|
307
|
-
var AuthManager = class {
|
|
308
|
-
static cacheFile = path3.join(os2.homedir(), ".connect-api-cli-token.json");
|
|
309
|
-
/**
|
|
310
|
-
* 获取当前生效的 access token,并具备自动刷新能力:
|
|
311
|
-
* - OAuth 模式:token 过期时优先用 refreshToken 自动刷新,无需重新登录
|
|
312
|
-
* - client_credentials / service_account:过期后重新换取 / 重新签 JWT
|
|
313
|
-
*/
|
|
314
|
-
static async getAccessToken() {
|
|
315
|
-
const cfg = getConfig();
|
|
316
|
-
const cache = this.readCache();
|
|
317
|
-
const sameCredential = cache && cache.clientId === cfg.clientId && cache.domain === cfg.domain && cache.authMode === cfg.authMode;
|
|
318
|
-
if (sameCredential && cache.expiresAt > Date.now() + 5 * 60 * 1e3) {
|
|
319
|
-
return cache.accessToken;
|
|
320
|
-
}
|
|
321
|
-
if (cfg.authMode === "oauth" && cache?.refreshToken) {
|
|
322
|
-
try {
|
|
323
|
-
const refreshed = await AuthService.refreshOAuthToken(cache.refreshToken);
|
|
324
|
-
const expiresAt = Date.now() + refreshed.expireTime * 1e3;
|
|
325
|
-
this.writeCache({
|
|
326
|
-
accessToken: refreshed.accessToken,
|
|
327
|
-
expiresAt,
|
|
328
|
-
clientId: cfg.clientId,
|
|
329
|
-
domain: cfg.domain,
|
|
330
|
-
authMode: cfg.authMode,
|
|
331
|
-
refreshToken: cache.refreshToken
|
|
332
|
-
// 刷新接口不返回新 refreshToken,沿用旧值
|
|
333
|
-
});
|
|
334
|
-
return refreshed.accessToken;
|
|
335
|
-
} catch (e) {
|
|
336
|
-
console.error("OAuth token refresh failed, need to re-login:", e instanceof Error ? e.message : String(e));
|
|
337
|
-
this.clearCache();
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
return this.acquire();
|
|
341
|
-
}
|
|
342
|
-
/** 登录入口:按当前认证模式获取并缓存 token */
|
|
343
|
-
static async login() {
|
|
344
|
-
clearOAuthTeamIdCache();
|
|
345
|
-
return this.acquire();
|
|
346
|
-
}
|
|
347
|
-
/** 按当前认证模式获取 token(client_credentials / service_account / oauth) */
|
|
348
|
-
static async acquire() {
|
|
349
|
-
const cfg = getConfig();
|
|
350
|
-
if (cfg.authMode === "oauth") {
|
|
351
|
-
return this.loginOAuth();
|
|
352
|
-
}
|
|
353
|
-
if (cfg.authMode === "service_account") {
|
|
354
|
-
if (!cfg.serviceAccount) {
|
|
355
|
-
throw new Error("Service Account credentials not loaded. Set AGC_SA_CREDENTIALS.");
|
|
356
|
-
}
|
|
357
|
-
const { jwt, expiresAt } = AuthService.createServiceAccountJwt(cfg.serviceAccount.credentials);
|
|
358
|
-
this.writeCache({
|
|
359
|
-
accessToken: jwt,
|
|
360
|
-
expiresAt,
|
|
361
|
-
clientId: cfg.clientId,
|
|
362
|
-
domain: cfg.domain,
|
|
363
|
-
authMode: cfg.authMode
|
|
364
|
-
});
|
|
365
|
-
return jwt;
|
|
366
|
-
}
|
|
367
|
-
const res = await AuthService.getToken();
|
|
368
|
-
if (res.access_token && res.expires_in) {
|
|
369
|
-
const expiresAt = Date.now() + res.expires_in * 1e3;
|
|
370
|
-
this.writeCache({
|
|
371
|
-
accessToken: res.access_token,
|
|
372
|
-
expiresAt,
|
|
373
|
-
clientId: cfg.clientId,
|
|
374
|
-
domain: cfg.domain,
|
|
375
|
-
authMode: cfg.authMode
|
|
376
|
-
});
|
|
377
|
-
return res.access_token;
|
|
378
|
-
}
|
|
379
|
-
throw new Error(`Failed to get token: ${JSON.stringify(res.ret)}`);
|
|
380
|
-
}
|
|
381
|
-
/** OAuth 授权码登录:启动本地回调服务器 + 打开浏览器 */
|
|
382
|
-
static loginOAuth() {
|
|
383
|
-
return new Promise((resolve, reject) => {
|
|
384
|
-
const cfg = getConfig();
|
|
385
|
-
const redirectUri = cfg.oauth.redirectUri;
|
|
386
|
-
const port = Number(new URL(redirectUri).port || 80);
|
|
387
|
-
const state = crypto2.randomBytes(32).toString("hex");
|
|
388
|
-
const server = http.createServer(async (req, res) => {
|
|
389
|
-
try {
|
|
390
|
-
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
391
|
-
const code = url.searchParams.get("code");
|
|
392
|
-
const returnedState = url.searchParams.get("state");
|
|
393
|
-
if (!code || returnedState !== state) {
|
|
394
|
-
res.writeHead(400, { "Content-Type": "text/html; charset=utf-8" });
|
|
395
|
-
res.end("<h3>\u65E0\u6548\u7684\u56DE\u8C03\u53C2\u6570</h3>");
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
const token = await AuthService.getOAuthToken(code, redirectUri);
|
|
399
|
-
const expiresAt = Date.now() + token.expireTime * 1e3;
|
|
400
|
-
this.writeCache({
|
|
401
|
-
accessToken: token.accessToken,
|
|
402
|
-
expiresAt,
|
|
403
|
-
clientId: cfg.clientId,
|
|
404
|
-
domain: cfg.domain,
|
|
405
|
-
authMode: cfg.authMode,
|
|
406
|
-
refreshToken: token.refreshToken
|
|
407
|
-
});
|
|
408
|
-
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
409
|
-
res.end('<!DOCTYPE html><html><body style="font-family:sans-serif;padding:40px;text-align:center"><h2>\u767B\u5F55\u6210\u529F\uFF0C\u53EF\u5173\u95ED\u6B64\u7A97\u53E3</h2></body></html>');
|
|
410
|
-
server.close();
|
|
411
|
-
resolve(token.accessToken);
|
|
412
|
-
} catch (err) {
|
|
413
|
-
server.close();
|
|
414
|
-
reject(err instanceof Error ? err : new Error(String(err)));
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
server.on("error", (err) => {
|
|
418
|
-
server.close();
|
|
419
|
-
reject(err);
|
|
420
|
-
});
|
|
421
|
-
server.listen(port, () => {
|
|
422
|
-
const authUrl = AuthService.buildAuthUrl(state);
|
|
423
|
-
console.log("\u8BF7\u5728\u6D4F\u89C8\u5668\u4E2D\u5B8C\u6210\u534E\u4E3A\u8D26\u53F7\u6388\u6743...");
|
|
424
|
-
console.log(`\u82E5\u6D4F\u89C8\u5668\u672A\u81EA\u52A8\u6253\u5F00\uFF0C\u8BF7\u8BBF\u95EE: ${authUrl}`);
|
|
425
|
-
openBrowser(authUrl);
|
|
426
|
-
});
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
/** 查询缓存状态(供 status 命令显示) */
|
|
430
|
-
static getStatus() {
|
|
431
|
-
return this.readCache();
|
|
432
|
-
}
|
|
433
|
-
static readCache() {
|
|
434
|
-
try {
|
|
435
|
-
if (fs3.existsSync(this.cacheFile)) {
|
|
436
|
-
const data = fs3.readFileSync(this.cacheFile, "utf8");
|
|
437
|
-
return JSON.parse(data);
|
|
438
|
-
}
|
|
439
|
-
} catch (e) {
|
|
440
|
-
}
|
|
441
|
-
return null;
|
|
442
|
-
}
|
|
443
|
-
static writeCache(cache) {
|
|
444
|
-
try {
|
|
445
|
-
fs3.writeFileSync(this.cacheFile, JSON.stringify(cache, null, 2), "utf8");
|
|
446
|
-
} catch (e) {
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* 清除缓存的 Token(登出)
|
|
451
|
-
*/
|
|
452
|
-
static clearCache() {
|
|
453
|
-
clearOAuthTeamIdCache();
|
|
454
|
-
try {
|
|
455
|
-
if (fs3.existsSync(this.cacheFile)) {
|
|
456
|
-
fs3.unlinkSync(this.cacheFile);
|
|
457
|
-
return true;
|
|
458
|
-
}
|
|
459
|
-
return false;
|
|
460
|
-
} catch (e) {
|
|
461
|
-
return false;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
function openBrowser(url) {
|
|
466
|
-
const platform = process.platform;
|
|
467
|
-
const cmd = platform === "darwin" ? "open" : platform === "win32" ? 'start ""' : "xdg-open";
|
|
468
|
-
exec(`${cmd} "${url}"`, (err) => {
|
|
469
|
-
if (err) {
|
|
470
|
-
console.error("\u65E0\u6CD5\u81EA\u52A8\u6253\u5F00\u6D4F\u89C8\u5668\uFF0C\u8BF7\u624B\u52A8\u590D\u5236\u4E0A\u9762\u7684\u94FE\u63A5\u8BBF\u95EE\u3002");
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// src/core/http.ts
|
|
476
|
-
import axios3 from "axios";
|
|
477
|
-
var agcClient = axios3.create();
|
|
478
|
-
function setHeader(headers, name, value) {
|
|
479
|
-
if (typeof headers?.set === "function") {
|
|
480
|
-
headers.set(name, value);
|
|
481
|
-
} else {
|
|
482
|
-
headers[name] = value;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
function deleteHeader(headers, name) {
|
|
486
|
-
if (typeof headers?.delete === "function") {
|
|
487
|
-
headers.delete(name);
|
|
488
|
-
} else {
|
|
489
|
-
delete headers[name];
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
function getHeader(headers, name) {
|
|
493
|
-
if (typeof headers?.get === "function") {
|
|
494
|
-
return headers.get(name);
|
|
495
|
-
}
|
|
496
|
-
return headers?.[name] ?? headers?.[name.toLowerCase()];
|
|
497
|
-
}
|
|
498
|
-
function applyAuthHeaders(headers, token, cfg, force = false) {
|
|
499
|
-
if (cfg.authMode === "oauth") {
|
|
500
|
-
deleteHeader(headers, "Authorization");
|
|
501
|
-
deleteHeader(headers, "client_id");
|
|
502
|
-
setHeader(headers, "oauth2Token", token);
|
|
503
|
-
return;
|
|
504
|
-
}
|
|
505
|
-
deleteHeader(headers, "oauth2Token");
|
|
506
|
-
if (force || !getHeader(headers, "Authorization")) {
|
|
507
|
-
setHeader(headers, "Authorization", `Bearer ${token}`);
|
|
508
|
-
}
|
|
509
|
-
if (cfg.authMode !== "service_account" && !getHeader(headers, "client_id")) {
|
|
510
|
-
setHeader(headers, "client_id", cfg.clientId);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
agcClient.interceptors.request.use(async (config2) => {
|
|
514
|
-
const cfg = getConfig();
|
|
515
|
-
if (!config2.baseURL) {
|
|
516
|
-
config2.baseURL = `https://${cfg.domain}`;
|
|
517
|
-
}
|
|
518
|
-
const token = await AuthManager.getAccessToken();
|
|
519
|
-
if (!config2.headers) {
|
|
520
|
-
config2.headers = {};
|
|
521
|
-
}
|
|
522
|
-
applyAuthHeaders(config2.headers, token, cfg);
|
|
523
|
-
if (cfg.authMode === "oauth" && !getHeader(config2.headers, "teamId")) {
|
|
524
|
-
const teamId = await resolveOAuthTeamId(cfg, token);
|
|
525
|
-
if (teamId) {
|
|
526
|
-
setHeader(config2.headers, "teamId", teamId);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
if (config2.data !== void 0 && !getHeader(config2.headers, "Content-Type")) {
|
|
530
|
-
setHeader(config2.headers, "Content-Type", "application/json");
|
|
531
|
-
}
|
|
532
|
-
return config2;
|
|
533
|
-
}, (error) => {
|
|
534
|
-
return Promise.reject(error);
|
|
535
|
-
});
|
|
536
|
-
agcClient.interceptors.response.use(
|
|
537
|
-
(res) => res,
|
|
538
|
-
async (err) => {
|
|
539
|
-
const original = err.config;
|
|
540
|
-
const status = err.response?.status;
|
|
541
|
-
if (status === 401 && original && !original._retry) {
|
|
542
|
-
original._retry = true;
|
|
543
|
-
try {
|
|
544
|
-
const cfg = getConfig();
|
|
545
|
-
const newToken = await AuthManager.getAccessToken();
|
|
546
|
-
if (!original.headers) {
|
|
547
|
-
original.headers = {};
|
|
548
|
-
}
|
|
549
|
-
applyAuthHeaders(original.headers, newToken, cfg, true);
|
|
550
|
-
return agcClient(original);
|
|
551
|
-
} catch (e) {
|
|
552
|
-
return Promise.reject(err);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
if (status === 401 || status === 403) {
|
|
556
|
-
const body = err.response?.data;
|
|
557
|
-
const msg = typeof body === "object" ? JSON.stringify(body, null, 2) : body;
|
|
558
|
-
err.message = err.message + (msg ? `
|
|
559
|
-
API \u54CD\u5E94: ${msg}` : "");
|
|
560
|
-
}
|
|
561
|
-
return Promise.reject(err);
|
|
562
|
-
}
|
|
563
|
-
);
|
|
564
|
-
|
|
565
|
-
// src/services/publish.ts
|
|
566
|
-
function appIdHeaders(appId) {
|
|
567
|
-
return { appId };
|
|
568
|
-
}
|
|
569
|
-
function withOptionalParams(base) {
|
|
570
|
-
const out = {};
|
|
571
|
-
for (const [k, v] of Object.entries(base)) {
|
|
572
|
-
if (v !== void 0) out[k] = v;
|
|
573
|
-
}
|
|
574
|
-
return out;
|
|
575
|
-
}
|
|
576
|
-
var PublishService = class {
|
|
577
|
-
/** 查询应用包名对应的 appid */
|
|
578
|
-
static async getAppIdList(packageName, packageTypes) {
|
|
579
|
-
const response = await agcClient.get("/api/publish/v2/appid-list", {
|
|
580
|
-
params: withOptionalParams({ packageName, packageTypes })
|
|
581
|
-
});
|
|
582
|
-
return response.data;
|
|
583
|
-
}
|
|
584
|
-
/** 创建应用或元服务(projectId 为 query 参数) */
|
|
585
|
-
static async createApp(req) {
|
|
586
|
-
const { projectId, packageName, ...rest } = req;
|
|
587
|
-
const body = { ...rest };
|
|
588
|
-
if (req.installationFree === 0 && packageName) {
|
|
589
|
-
body.packageName = packageName;
|
|
590
|
-
}
|
|
591
|
-
const response = await agcClient.post(
|
|
592
|
-
`/api/publish/v3/app?projectId=${encodeURIComponent(projectId)}`,
|
|
593
|
-
body
|
|
594
|
-
);
|
|
595
|
-
return response.data;
|
|
596
|
-
}
|
|
597
|
-
/** 查询团队账号下的成员角色列表 */
|
|
598
|
-
static async getMembersRoleList(req) {
|
|
599
|
-
const response = await agcClient.get(
|
|
600
|
-
"/api/publish/v3/members-role-list",
|
|
601
|
-
{
|
|
602
|
-
params: {
|
|
603
|
-
fromPage: req.fromPage,
|
|
604
|
-
pageSize: req.pageSize,
|
|
605
|
-
languageCode: req.languageCode
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
);
|
|
609
|
-
return response.data;
|
|
610
|
-
}
|
|
611
|
-
/** 查询应用信息 */
|
|
612
|
-
static async getAppInfo(appIdOrReq, lang) {
|
|
613
|
-
const req = typeof appIdOrReq === "string" ? { appId: appIdOrReq, lang } : appIdOrReq;
|
|
614
|
-
const response = await agcClient.get("/api/publish/v3/app-info", {
|
|
615
|
-
params: withOptionalParams({
|
|
616
|
-
appId: req.appId,
|
|
617
|
-
lang: req.lang,
|
|
618
|
-
releaseType: req.releaseType,
|
|
619
|
-
versionId: req.versionId
|
|
620
|
-
})
|
|
621
|
-
});
|
|
622
|
-
return response.data;
|
|
623
|
-
}
|
|
624
|
-
/** 更新应用基本信息 */
|
|
625
|
-
static async updateAppInfo(req) {
|
|
626
|
-
const { appId, releaseType, releasePhase, ...body } = req;
|
|
627
|
-
const response = await agcClient.put(
|
|
628
|
-
"/api/publish/v3/app-info",
|
|
629
|
-
body,
|
|
630
|
-
{
|
|
631
|
-
params: withOptionalParams({ appId, releaseType, releasePhase })
|
|
632
|
-
}
|
|
633
|
-
);
|
|
634
|
-
return response.data;
|
|
635
|
-
}
|
|
636
|
-
/** 更新应用语言信息 */
|
|
637
|
-
static async updateLanguageInfo(req) {
|
|
638
|
-
const { appId, releaseType, releasePhase, ...body } = req;
|
|
639
|
-
const response = await agcClient.put(
|
|
640
|
-
"/api/publish/v3/app-language-info",
|
|
641
|
-
body,
|
|
642
|
-
{
|
|
643
|
-
params: withOptionalParams({ appId, releaseType, releasePhase })
|
|
644
|
-
}
|
|
645
|
-
);
|
|
646
|
-
return response.data;
|
|
647
|
-
}
|
|
648
|
-
/** 删除应用语言信息 */
|
|
649
|
-
static async deleteLanguageInfo(req) {
|
|
650
|
-
const response = await agcClient.delete(
|
|
651
|
-
"/api/publish/v2/app-language-info",
|
|
652
|
-
{
|
|
653
|
-
params: withOptionalParams({
|
|
654
|
-
appId: req.appId,
|
|
655
|
-
lang: req.lang,
|
|
656
|
-
releaseType: req.releaseType
|
|
657
|
-
})
|
|
658
|
-
}
|
|
659
|
-
);
|
|
660
|
-
return response.data;
|
|
661
|
-
}
|
|
662
|
-
/** 更新应用文件信息(图标/截图/视频等) */
|
|
663
|
-
static async updateAppFileInfo(req) {
|
|
664
|
-
const { appId, releaseType, releasePhase, ...body } = req;
|
|
665
|
-
const response = await agcClient.put(
|
|
666
|
-
"/api/publish/v3/app-file-info",
|
|
667
|
-
body,
|
|
668
|
-
{
|
|
669
|
-
params: withOptionalParams({ appId, releaseType, releasePhase })
|
|
670
|
-
}
|
|
671
|
-
);
|
|
672
|
-
return response.data;
|
|
673
|
-
}
|
|
674
|
-
/** 更新应用软件包信息(OBS 上传后上报,AGC 控制台才会出现记录) */
|
|
675
|
-
static async updateAppPackageInfo(req) {
|
|
676
|
-
const { appId, releaseType, releasePhase, fileName, objectId } = req;
|
|
677
|
-
const response = await agcClient.put(
|
|
678
|
-
"/api/publish/v3/app-package-info",
|
|
679
|
-
{ fileName, objectId },
|
|
680
|
-
{
|
|
681
|
-
params: withOptionalParams({ appId, releaseType, releasePhase })
|
|
682
|
-
}
|
|
683
|
-
);
|
|
684
|
-
return response.data;
|
|
685
|
-
}
|
|
686
|
-
/** 更新版本上架时间 */
|
|
687
|
-
static async updateReleaseTime(req) {
|
|
688
|
-
const { appId, ...body } = req;
|
|
689
|
-
const response = await agcClient.put(
|
|
690
|
-
"/api/publish/v2/on-shelf-time",
|
|
691
|
-
body,
|
|
692
|
-
{ params: { appId } }
|
|
693
|
-
);
|
|
694
|
-
return response.data;
|
|
695
|
-
}
|
|
696
|
-
/** 更新分阶段发布 */
|
|
697
|
-
static async updatePhasedRelease(req) {
|
|
698
|
-
const { appId, ...body } = req;
|
|
699
|
-
const response = await agcClient.put(
|
|
700
|
-
"/api/publish/v2/version/phased-release",
|
|
701
|
-
body,
|
|
702
|
-
{ headers: appIdHeaders(appId) }
|
|
703
|
-
);
|
|
704
|
-
return response.data;
|
|
705
|
-
}
|
|
706
|
-
/** 查询应用软件包信息 */
|
|
707
|
-
static async getPackageInfo(req) {
|
|
708
|
-
const response = await agcClient.get(
|
|
709
|
-
"/api/publish/v2/app-package-info",
|
|
710
|
-
{
|
|
711
|
-
params: { packageId: req.packageId },
|
|
712
|
-
headers: appIdHeaders(req.appId)
|
|
713
|
-
}
|
|
714
|
-
);
|
|
715
|
-
return response.data;
|
|
716
|
-
}
|
|
717
|
-
/** 查询软件包编译状态 */
|
|
718
|
-
static async getCompileStatus(req) {
|
|
719
|
-
const response = await agcClient.get(
|
|
720
|
-
"/api/publish/v3/package/compile/status",
|
|
721
|
-
{ params: { appId: req.appId, pkgIds: req.pkgIds } }
|
|
722
|
-
);
|
|
723
|
-
return response.data;
|
|
724
|
-
}
|
|
725
|
-
/** 查询应用所有版本类型的版本列表 */
|
|
726
|
-
static async queryBriefInfoList(req) {
|
|
727
|
-
const { appId, ...body } = req;
|
|
728
|
-
const response = await agcClient.post(
|
|
729
|
-
"/api/publish/v3/version/brief-info/list",
|
|
730
|
-
body,
|
|
731
|
-
{ headers: appIdHeaders(appId) }
|
|
732
|
-
);
|
|
733
|
-
return response.data;
|
|
734
|
-
}
|
|
735
|
-
/** 查询应用未处理的问题工单 */
|
|
736
|
-
static async queryVersionVoc(req) {
|
|
737
|
-
const { appId, ...body } = req;
|
|
738
|
-
const response = await agcClient.post(
|
|
739
|
-
"/api/publish/v1/app-voc/version-voc",
|
|
740
|
-
body,
|
|
741
|
-
{ headers: appIdHeaders(appId) }
|
|
742
|
-
);
|
|
743
|
-
return response.data;
|
|
744
|
-
}
|
|
745
|
-
/** 查询需要上传的资质文件信息 */
|
|
746
|
-
static async queryTagQualDependent() {
|
|
747
|
-
const response = await agcClient.get(
|
|
748
|
-
"/api/publish/v2/tag-qual-dependent"
|
|
749
|
-
);
|
|
750
|
-
return response.data;
|
|
751
|
-
}
|
|
752
|
-
/** 提交资质审核任务 */
|
|
753
|
-
static async submitQualificationReview(req) {
|
|
754
|
-
const { appId, ...body } = req;
|
|
755
|
-
const response = await agcClient.post(
|
|
756
|
-
"/api/publish/v2/qualification-review-task",
|
|
757
|
-
body,
|
|
758
|
-
{ headers: appIdHeaders(appId) }
|
|
759
|
-
);
|
|
760
|
-
return response.data;
|
|
761
|
-
}
|
|
762
|
-
/** 查询资质审核任务详情 */
|
|
763
|
-
static async getQualificationReview(req) {
|
|
764
|
-
const response = await agcClient.get(
|
|
765
|
-
"/api/publish/v2/qualification-review-task",
|
|
766
|
-
{
|
|
767
|
-
params: { tagIds: req.tagIds },
|
|
768
|
-
headers: appIdHeaders(req.appId)
|
|
769
|
-
}
|
|
770
|
-
);
|
|
771
|
-
return response.data;
|
|
772
|
-
}
|
|
773
|
-
/** 修改资质审核任务 */
|
|
774
|
-
static async updateQualificationReview(req) {
|
|
775
|
-
const { appId, ...body } = req;
|
|
776
|
-
const response = await agcClient.put(
|
|
777
|
-
"/api/publish/v2/qualification-review-task",
|
|
778
|
-
body,
|
|
779
|
-
{ headers: appIdHeaders(appId) }
|
|
780
|
-
);
|
|
781
|
-
return response.data;
|
|
782
|
-
}
|
|
783
|
-
/** 撤销资质审核任务 */
|
|
784
|
-
static async cancelQualificationReview(req) {
|
|
785
|
-
const response = await agcClient.put(
|
|
786
|
-
"/api/publish/v2/qualification-review-task/cancel",
|
|
787
|
-
{ taskId: req.taskId },
|
|
788
|
-
{ headers: appIdHeaders(req.appId) }
|
|
789
|
-
);
|
|
790
|
-
return response.data;
|
|
791
|
-
}
|
|
792
|
-
/** 删除资质审核任务 */
|
|
793
|
-
static async deleteQualificationReview(req) {
|
|
794
|
-
const response = await agcClient.delete(
|
|
795
|
-
"/api/publish/v2/qualification-review-task",
|
|
796
|
-
{
|
|
797
|
-
data: { tagIds: req.tagIds },
|
|
798
|
-
headers: appIdHeaders(req.appId)
|
|
799
|
-
}
|
|
800
|
-
);
|
|
801
|
-
return response.data;
|
|
802
|
-
}
|
|
803
|
-
/** 新建用户协议 / 隐私政策协议(body.type 区分) */
|
|
804
|
-
static async createAgreement(req) {
|
|
805
|
-
const { appId, ...body } = req;
|
|
806
|
-
const response = await agcClient.post(
|
|
807
|
-
"/api/publish/v2/agreement",
|
|
808
|
-
body,
|
|
809
|
-
{ headers: appIdHeaders(appId) }
|
|
810
|
-
);
|
|
811
|
-
return response.data;
|
|
812
|
-
}
|
|
813
|
-
/** 查询用户协议 / 隐私政策协议列表(body.type 区分) */
|
|
814
|
-
static async listAgreements(req) {
|
|
815
|
-
const { appId, ...body } = req;
|
|
816
|
-
const response = await agcClient.post(
|
|
817
|
-
"/api/publish/v2/agreement/list",
|
|
818
|
-
body,
|
|
819
|
-
{ headers: appIdHeaders(appId) }
|
|
820
|
-
);
|
|
821
|
-
return response.data;
|
|
822
|
-
}
|
|
823
|
-
/** 更新用户协议 / 隐私政策协议 */
|
|
824
|
-
static async updateAgreement(req) {
|
|
825
|
-
const { appId, ...body } = req;
|
|
826
|
-
const response = await agcClient.put(
|
|
827
|
-
"/api/publish/v2/agreement",
|
|
828
|
-
body,
|
|
829
|
-
{ headers: appIdHeaders(appId) }
|
|
830
|
-
);
|
|
831
|
-
return response.data;
|
|
832
|
-
}
|
|
833
|
-
/** 提交隐私政策协议 */
|
|
834
|
-
static async submitPrivacyAgreement(req) {
|
|
835
|
-
const { appId, ...body } = req;
|
|
836
|
-
const response = await agcClient.put(
|
|
837
|
-
"/api/publish/v2/agreement/submit",
|
|
838
|
-
body,
|
|
839
|
-
{ headers: appIdHeaders(appId) }
|
|
840
|
-
);
|
|
841
|
-
return response.data;
|
|
842
|
-
}
|
|
843
|
-
/** 申请年龄分级 */
|
|
844
|
-
static async applyAgeRating(req) {
|
|
845
|
-
const { appId, releaseType, releasePhase, ...body } = req;
|
|
846
|
-
const response = await agcClient.post(
|
|
847
|
-
"/api/publish/v2/app-age-rating",
|
|
848
|
-
body,
|
|
849
|
-
{
|
|
850
|
-
params: withOptionalParams({ releaseType, releasePhase }),
|
|
851
|
-
headers: appIdHeaders(appId)
|
|
852
|
-
}
|
|
853
|
-
);
|
|
854
|
-
return response.data;
|
|
855
|
-
}
|
|
856
|
-
/** 提交发布 */
|
|
857
|
-
static async submitApp(req) {
|
|
858
|
-
const { appId, ...body } = req;
|
|
859
|
-
const response = await agcClient.post(
|
|
860
|
-
"/api/publish/v3/app-submit",
|
|
861
|
-
body,
|
|
862
|
-
{ headers: appIdHeaders(appId) }
|
|
863
|
-
);
|
|
864
|
-
return response.data;
|
|
865
|
-
}
|
|
866
|
-
/** 撤销审核 */
|
|
867
|
-
static async cancelReview(req) {
|
|
868
|
-
const response = await agcClient.put(
|
|
869
|
-
"/api/publish/v3/version/on-shelf/cancel",
|
|
870
|
-
{ versionId: req.versionId },
|
|
871
|
-
{ headers: appIdHeaders(req.appId) }
|
|
872
|
-
);
|
|
873
|
-
return response.data;
|
|
874
|
-
}
|
|
875
|
-
/** 下架应用 */
|
|
876
|
-
static async shelfOff(req) {
|
|
877
|
-
const response = await agcClient.post(
|
|
878
|
-
"/api/publish/v2/app-shelf-off",
|
|
879
|
-
null,
|
|
880
|
-
{
|
|
881
|
-
params: withOptionalParams({
|
|
882
|
-
appId: req.appId,
|
|
883
|
-
reason: req.reason,
|
|
884
|
-
releaseType: req.releaseType
|
|
885
|
-
})
|
|
886
|
-
}
|
|
887
|
-
);
|
|
888
|
-
return response.data;
|
|
889
|
-
}
|
|
890
|
-
};
|
|
891
|
-
|
|
892
|
-
// src/services/project.ts
|
|
893
|
-
var ProjectService = class {
|
|
894
|
-
/** 查询团队下的项目列表(Project Management API) */
|
|
895
|
-
static async listProjects() {
|
|
896
|
-
const response = await agcClient.get("/api/project-service/v1/projects");
|
|
897
|
-
return response.data;
|
|
898
|
-
}
|
|
899
|
-
};
|
|
900
|
-
|
|
901
|
-
// src/services/upload.ts
|
|
902
|
-
import axios4 from "axios";
|
|
903
|
-
import fs4 from "fs";
|
|
904
|
-
import path4 from "path";
|
|
905
|
-
function pickObjectId(res) {
|
|
906
|
-
return res.urlInfo?.objectId || res.objectId;
|
|
907
|
-
}
|
|
908
|
-
var UploadService = class {
|
|
909
|
-
// ================= Raw APIs =================
|
|
910
|
-
static async getUploadUrl(req) {
|
|
911
|
-
const response = await agcClient.get("/api/publish/v2/upload-url/for-obs", {
|
|
912
|
-
params: req
|
|
913
|
-
});
|
|
914
|
-
return response.data;
|
|
915
|
-
}
|
|
916
|
-
static async initMultipart(req) {
|
|
917
|
-
const response = await agcClient.post("/api/publish/v2/upload/multipart/init", null, {
|
|
918
|
-
params: req
|
|
919
|
-
});
|
|
920
|
-
return response.data;
|
|
921
|
-
}
|
|
922
|
-
static async getMultipartParts(req) {
|
|
923
|
-
const { objectId, nspUploadId, parts } = req;
|
|
924
|
-
const response = await agcClient.post("/api/publish/v2/upload/multipart/parts", parts, {
|
|
925
|
-
params: { objectId, nspUploadId }
|
|
926
|
-
});
|
|
927
|
-
return response.data;
|
|
928
|
-
}
|
|
929
|
-
static async composeMultipart(req) {
|
|
930
|
-
const { objectId, nspUploadId, parts } = req;
|
|
931
|
-
const response = await agcClient.post("/api/publish/v2/upload/multipart/compose", parts, {
|
|
932
|
-
params: { objectId, nspUploadId }
|
|
933
|
-
});
|
|
934
|
-
return response.data;
|
|
935
|
-
}
|
|
936
|
-
// ================= High-level Operations =================
|
|
937
|
-
/**
|
|
938
|
-
* 封装好的上传文件核心流程(支持根据文件大小自动单文件或分片上传)
|
|
939
|
-
* 文件大小 < 5MB 时使用单文件上传,否则使用分片上传。
|
|
940
|
-
* 仅完成 OBS 落盘;HarmonyOS `.app` 还须调用更新应用软件包信息,AGC 控制台才会有记录。
|
|
941
|
-
*/
|
|
942
|
-
static async uploadFile(options) {
|
|
943
|
-
const stat = fs4.statSync(options.filePath);
|
|
944
|
-
const fileName = path4.basename(options.filePath) || "unknown";
|
|
945
|
-
if (stat.size < 5 * 1024 * 1024) {
|
|
946
|
-
return this.uploadSingleFile(options, fileName, stat.size);
|
|
947
|
-
}
|
|
948
|
-
return this.uploadMultipartFile(options, fileName, stat.size);
|
|
949
|
-
}
|
|
950
|
-
/**
|
|
951
|
-
* OBS 预签名 PUT 头:SignedHeaders 含 content-length / host。
|
|
952
|
-
* 必须显式带 Content-Length;Host 交给客户端按 URL 设置,勿原样回传。
|
|
953
|
-
*/
|
|
954
|
-
static buildObsPutHeaders(headers, contentLength) {
|
|
955
|
-
const out = { ...headers };
|
|
956
|
-
delete out.Host;
|
|
957
|
-
delete out.host;
|
|
958
|
-
out["Content-Length"] = String(contentLength);
|
|
959
|
-
if (!out["Content-Type"] && !out["content-type"]) {
|
|
960
|
-
out["Content-Type"] = "application/octet-stream";
|
|
961
|
-
}
|
|
962
|
-
return out;
|
|
963
|
-
}
|
|
964
|
-
/**
|
|
965
|
-
* 单文件上传流程
|
|
966
|
-
*/
|
|
967
|
-
static async uploadSingleFile(options, fileName, contentLength) {
|
|
968
|
-
const urlRes = await this.getUploadUrl({
|
|
969
|
-
appId: options.appId,
|
|
970
|
-
fileName,
|
|
971
|
-
contentLength,
|
|
972
|
-
releaseType: options.releaseType,
|
|
973
|
-
chineseMainlandFlag: options.chineseMainlandFlag
|
|
974
|
-
});
|
|
975
|
-
if (!urlRes.urlInfo) {
|
|
976
|
-
throw new Error(`Failed to get upload URL: ${JSON.stringify(urlRes.ret)}`);
|
|
977
|
-
}
|
|
978
|
-
const objectId = pickObjectId(urlRes);
|
|
979
|
-
if (!objectId) {
|
|
980
|
-
throw new Error(
|
|
981
|
-
`Failed to get objectId from upload URL: ${JSON.stringify(urlRes.ret)}`
|
|
982
|
-
);
|
|
983
|
-
}
|
|
984
|
-
const { url, headers } = urlRes.urlInfo;
|
|
985
|
-
const fileStream = fs4.createReadStream(options.filePath);
|
|
986
|
-
await axios4.put(url, fileStream, {
|
|
987
|
-
headers: this.buildObsPutHeaders(headers, contentLength),
|
|
988
|
-
maxBodyLength: Infinity,
|
|
989
|
-
maxContentLength: Infinity
|
|
990
|
-
});
|
|
991
|
-
return { fileName, objectId };
|
|
992
|
-
}
|
|
993
|
-
/**
|
|
994
|
-
* 分片上传流程
|
|
995
|
-
*/
|
|
996
|
-
static async uploadMultipartFile(options, fileName, fileSize) {
|
|
997
|
-
const initRes = await this.initMultipart({
|
|
998
|
-
appId: options.appId,
|
|
999
|
-
fileName,
|
|
1000
|
-
releaseType: options.releaseType,
|
|
1001
|
-
chineseMainlandFlag: options.chineseMainlandFlag
|
|
1002
|
-
});
|
|
1003
|
-
const { objectId, nspUploadId, nspPartMinSize = 5242880 } = initRes;
|
|
1004
|
-
if (!objectId || !nspUploadId) {
|
|
1005
|
-
throw new Error(`Failed to init multipart upload: ${JSON.stringify(initRes.ret)}`);
|
|
1006
|
-
}
|
|
1007
|
-
const partSize = Number(nspPartMinSize);
|
|
1008
|
-
const partsCount = Math.ceil(fileSize / partSize);
|
|
1009
|
-
const partsReqBody = {};
|
|
1010
|
-
for (let i = 1; i <= partsCount; i++) {
|
|
1011
|
-
const start = (i - 1) * partSize;
|
|
1012
|
-
const end = Math.min(start + partSize, fileSize);
|
|
1013
|
-
partsReqBody[`additionalProp${i}`] = {
|
|
1014
|
-
length: end - start
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
const partsRes = await this.getMultipartParts({
|
|
1018
|
-
objectId,
|
|
1019
|
-
nspUploadId,
|
|
1020
|
-
parts: partsReqBody
|
|
1021
|
-
});
|
|
1022
|
-
const uploadInfoMap = partsRes.uploadInfoMap;
|
|
1023
|
-
if (!uploadInfoMap) {
|
|
1024
|
-
throw new Error(`Failed to get parts upload URL: ${JSON.stringify(partsRes.ret)}`);
|
|
1025
|
-
}
|
|
1026
|
-
const fd = fs4.openSync(options.filePath, "r");
|
|
1027
|
-
const composeReqBody = {};
|
|
1028
|
-
for (let i = 1; i <= partsCount; i++) {
|
|
1029
|
-
const propKey = `additionalProp${i}`;
|
|
1030
|
-
const uploadInfo = uploadInfoMap[propKey];
|
|
1031
|
-
const start = (i - 1) * partSize;
|
|
1032
|
-
const end = Math.min(start + partSize, fileSize);
|
|
1033
|
-
const length = end - start;
|
|
1034
|
-
const buffer = Buffer.alloc(length);
|
|
1035
|
-
fs4.readSync(fd, buffer, 0, length, start);
|
|
1036
|
-
const res = await axios4.put(uploadInfo.url, buffer, {
|
|
1037
|
-
headers: this.buildObsPutHeaders(uploadInfo.headers, length),
|
|
1038
|
-
maxBodyLength: Infinity,
|
|
1039
|
-
maxContentLength: Infinity
|
|
1040
|
-
});
|
|
1041
|
-
const etag = res.headers["etag"];
|
|
1042
|
-
composeReqBody[propKey] = {
|
|
1043
|
-
partObjectId: uploadInfo.partObjectId || "",
|
|
1044
|
-
etag
|
|
1045
|
-
};
|
|
1046
|
-
}
|
|
1047
|
-
fs4.closeSync(fd);
|
|
1048
|
-
await this.composeMultipart({
|
|
1049
|
-
objectId,
|
|
1050
|
-
nspUploadId,
|
|
1051
|
-
parts: composeReqBody
|
|
1052
|
-
});
|
|
1053
|
-
return { fileName, objectId };
|
|
1054
|
-
}
|
|
1055
|
-
};
|
|
1056
|
-
|
|
1057
|
-
// src/services/provision.ts
|
|
1058
|
-
var ProvisionService = class {
|
|
1059
|
-
// ================= Certificates =================
|
|
1060
|
-
static async createCert(req) {
|
|
1061
|
-
const response = await agcClient.post("/api/publish/v3/cert", req);
|
|
1062
|
-
return response.data;
|
|
1063
|
-
}
|
|
1064
|
-
static async getCertList(req) {
|
|
1065
|
-
const response = await agcClient.post("/api/publish/v3/cert/list", req);
|
|
1066
|
-
return response.data;
|
|
1067
|
-
}
|
|
1068
|
-
static async deleteCert(req) {
|
|
1069
|
-
const response = await agcClient.post("/api/publish/v2/cert/delete", req);
|
|
1070
|
-
return response.data;
|
|
1071
|
-
}
|
|
1072
|
-
// ================= Devices =================
|
|
1073
|
-
static async addDevice(req) {
|
|
1074
|
-
const response = await agcClient.post("/api/publish/v2/device", req);
|
|
1075
|
-
return response.data;
|
|
1076
|
-
}
|
|
1077
|
-
static async getDeviceList(req) {
|
|
1078
|
-
const params = {};
|
|
1079
|
-
if (req.deviceName) params.deviceName = req.deviceName;
|
|
1080
|
-
if (req.fromRecCount !== void 0) params.fromRecCount = req.fromRecCount;
|
|
1081
|
-
if (req.maxReqCount !== void 0) params.maxReqCount = req.maxReqCount;
|
|
1082
|
-
if (req.order !== void 0) params.order = req.order;
|
|
1083
|
-
const response = await agcClient.get("/api/publish/v2/device/list", { params });
|
|
1084
|
-
return response.data;
|
|
1085
|
-
}
|
|
1086
|
-
static async deleteDevice(req) {
|
|
1087
|
-
const response = await agcClient.post("/api/publish/v2/device/delete", req);
|
|
1088
|
-
return response.data;
|
|
1089
|
-
}
|
|
1090
|
-
// ================= Profile / Provision =================
|
|
1091
|
-
static async createProvision(req) {
|
|
1092
|
-
const response = await agcClient.post("/api/publish/v3/provision", req);
|
|
1093
|
-
return response.data;
|
|
1094
|
-
}
|
|
1095
|
-
static async getProvisionList(req) {
|
|
1096
|
-
const params = {};
|
|
1097
|
-
if (req.fromRecCount !== void 0) params.fromRecCount = req.fromRecCount;
|
|
1098
|
-
if (req.maxReqCount !== void 0) params.maxReqCount = req.maxReqCount;
|
|
1099
|
-
const headers = { appId: req.appId };
|
|
1100
|
-
if (req.provisionId) headers.provisionId = req.provisionId;
|
|
1101
|
-
const response = await agcClient.get("/api/publish/v3/provision/list", {
|
|
1102
|
-
params,
|
|
1103
|
-
headers
|
|
1104
|
-
});
|
|
1105
|
-
return response.data;
|
|
1106
|
-
}
|
|
1107
|
-
static async updateProvision(req) {
|
|
1108
|
-
const response = await agcClient.put("/api/publish/v3/provision", req);
|
|
1109
|
-
return response.data;
|
|
1110
|
-
}
|
|
1111
|
-
static async deleteProvision(req) {
|
|
1112
|
-
const response = await agcClient.delete("/api/publish/v2/provision", {
|
|
1113
|
-
params: { id: req.id }
|
|
1114
|
-
});
|
|
1115
|
-
return response.data;
|
|
1116
|
-
}
|
|
1117
|
-
// ================= Fingerprint =================
|
|
1118
|
-
static async addFingerprint(req) {
|
|
1119
|
-
const { appId, ...body } = req;
|
|
1120
|
-
const response = await agcClient.post(
|
|
1121
|
-
"/api/provision/v1/fingerprints",
|
|
1122
|
-
body,
|
|
1123
|
-
{ headers: { appId } }
|
|
1124
|
-
);
|
|
1125
|
-
return response.data;
|
|
1126
|
-
}
|
|
1127
|
-
static async getFingerprintList(req) {
|
|
1128
|
-
const response = await agcClient.get("/api/provision/v1/fingerprints", {
|
|
1129
|
-
headers: { appId: req.appId }
|
|
1130
|
-
});
|
|
1131
|
-
return response.data;
|
|
1132
|
-
}
|
|
1133
|
-
static async deleteFingerprint(req) {
|
|
1134
|
-
const { appId, ...body } = req;
|
|
1135
|
-
const response = await agcClient.delete(
|
|
1136
|
-
"/api/provision/v1/fingerprints",
|
|
1137
|
-
{ headers: { appId }, data: body }
|
|
1138
|
-
);
|
|
1139
|
-
return response.data;
|
|
1140
|
-
}
|
|
1141
|
-
// ================= ACL Permission =================
|
|
1142
|
-
/** 查询可申请的 ACL 权限列表 */
|
|
1143
|
-
static async getEligibleACL(req) {
|
|
1144
|
-
const { appId, ...body } = req;
|
|
1145
|
-
const response = await agcClient.post(
|
|
1146
|
-
"/api/provision/v1/user/permission/apply/list",
|
|
1147
|
-
body,
|
|
1148
|
-
{ headers: { appId } }
|
|
1149
|
-
);
|
|
1150
|
-
return response.data;
|
|
1151
|
-
}
|
|
1152
|
-
static async applyACL(req) {
|
|
1153
|
-
const { appId, ...body } = req;
|
|
1154
|
-
const response = await agcClient.post(
|
|
1155
|
-
"/api/provision/v1/user/permission/apply",
|
|
1156
|
-
body,
|
|
1157
|
-
{ headers: { appId } }
|
|
1158
|
-
);
|
|
1159
|
-
return response.data;
|
|
1160
|
-
}
|
|
1161
|
-
static async getACLStatus(req) {
|
|
1162
|
-
const response = await agcClient.get(
|
|
1163
|
-
"/api/provision/v1/user/permission/apply/status",
|
|
1164
|
-
{ headers: { appId: req.appId } }
|
|
1165
|
-
);
|
|
1166
|
-
return response.data;
|
|
1167
|
-
}
|
|
1168
|
-
static async getACLQuery(req) {
|
|
1169
|
-
const response = await agcClient.get("/api/provision/v1/user/permission", {
|
|
1170
|
-
headers: { appId: req.appId }
|
|
1171
|
-
});
|
|
1172
|
-
return response.data;
|
|
1173
|
-
}
|
|
1174
|
-
};
|
|
1175
|
-
|
|
1176
|
-
export {
|
|
1177
|
-
getConfig,
|
|
1178
|
-
AuthService,
|
|
1179
|
-
getCachedOAuthTeamId,
|
|
1180
|
-
AuthManager,
|
|
1181
|
-
agcClient,
|
|
1182
|
-
PublishService,
|
|
1183
|
-
ProjectService,
|
|
1184
|
-
UploadService,
|
|
1185
|
-
ProvisionService
|
|
1186
|
-
};
|
|
1187
|
-
//# sourceMappingURL=chunk-3WGV37EN.mjs.map
|