@42ailab/42plugin 0.1.12 → 0.1.14
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/package.json +1 -1
- package/src/api.ts +25 -8
- package/src/commands/auth.ts +7 -2
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -288,20 +288,37 @@ class ApiClient {
|
|
|
288
288
|
}),
|
|
289
289
|
});
|
|
290
290
|
|
|
291
|
-
|
|
291
|
+
// 处理 JSON 解析失败
|
|
292
|
+
let data: Record<string, unknown>;
|
|
293
|
+
try {
|
|
294
|
+
data = await response.json();
|
|
295
|
+
} catch {
|
|
296
|
+
return { error: 'invalid_response', errorDescription: 'Invalid response from server' };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// 处理空响应
|
|
300
|
+
if (!data) {
|
|
301
|
+
return { error: 'empty_response', errorDescription: 'Empty response from server' };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// 如果有错误,直接返回错误信息
|
|
305
|
+
if (data.error) {
|
|
306
|
+
return {
|
|
307
|
+
error: data.error as string,
|
|
308
|
+
errorDescription: data.error_description as string,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
292
311
|
|
|
293
312
|
return {
|
|
294
|
-
accessToken: data.access_token,
|
|
313
|
+
accessToken: data.access_token as string,
|
|
295
314
|
user: data.user
|
|
296
315
|
? {
|
|
297
|
-
id: data.user.id,
|
|
298
|
-
name: data.user.name,
|
|
299
|
-
username: data.user.username,
|
|
300
|
-
email: data.user.email,
|
|
316
|
+
id: (data.user as Record<string, unknown>).id as string,
|
|
317
|
+
name: (data.user as Record<string, unknown>).name as string,
|
|
318
|
+
username: (data.user as Record<string, unknown>).username as string,
|
|
319
|
+
email: (data.user as Record<string, unknown>).email as string,
|
|
301
320
|
}
|
|
302
321
|
: undefined,
|
|
303
|
-
error: data.error,
|
|
304
|
-
errorDescription: data.error_description,
|
|
305
322
|
};
|
|
306
323
|
}
|
|
307
324
|
|
package/src/commands/auth.ts
CHANGED
|
@@ -66,7 +66,7 @@ async function login(): Promise<void> {
|
|
|
66
66
|
|
|
67
67
|
const tokenResponse = await api.pollDeviceToken(deviceCode.deviceCode);
|
|
68
68
|
|
|
69
|
-
if (tokenResponse.accessToken
|
|
69
|
+
if (tokenResponse.accessToken) {
|
|
70
70
|
pollSpinner.succeed('授权成功!');
|
|
71
71
|
|
|
72
72
|
// 保存 token
|
|
@@ -74,7 +74,12 @@ async function login(): Promise<void> {
|
|
|
74
74
|
api.setSessionToken(tokenResponse.accessToken);
|
|
75
75
|
|
|
76
76
|
console.log();
|
|
77
|
-
|
|
77
|
+
try {
|
|
78
|
+
const session = await api.getSession();
|
|
79
|
+
console.log(chalk.green(`欢迎,${session.user.name || session.user.username || session.user.email}!`));
|
|
80
|
+
} catch {
|
|
81
|
+
console.log(chalk.green('授权成功!'));
|
|
82
|
+
}
|
|
78
83
|
return;
|
|
79
84
|
}
|
|
80
85
|
|