@42ailab/42plugin 0.1.12 → 0.1.13

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/api.ts +25 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@42ailab/42plugin",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "活水插件",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/api.ts CHANGED
@@ -288,20 +288,37 @@ class ApiClient {
288
288
  }),
289
289
  });
290
290
 
291
- const data = await response.json();
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