|
1 | 1 | import { createLogger } from '@sim/logger' |
2 | | -import type { ClerkGetUserParams, ClerkGetUserResponse } from '@/tools/clerk/types' |
| 2 | +import type { |
| 3 | + ClerkApiError, |
| 4 | + ClerkEmailAddress, |
| 5 | + ClerkGetUserParams, |
| 6 | + ClerkGetUserResponse, |
| 7 | + ClerkPhoneNumber, |
| 8 | + ClerkUser, |
| 9 | +} from '@/tools/clerk/types' |
3 | 10 | import type { ToolConfig } from '@/tools/types' |
4 | 11 |
|
5 | 12 | const logger = createLogger('ClerkGetUser') |
@@ -40,51 +47,55 @@ export const clerkGetUserTool: ToolConfig<ClerkGetUserParams, ClerkGetUserRespon |
40 | 47 | }, |
41 | 48 |
|
42 | 49 | transformResponse: async (response: Response) => { |
43 | | - const data = await response.json() |
| 50 | + const data: ClerkUser | ClerkApiError = await response.json() |
44 | 51 |
|
45 | 52 | if (!response.ok) { |
46 | 53 | logger.error('Clerk API request failed', { data, status: response.status }) |
47 | | - throw new Error(data.errors?.[0]?.message || 'Failed to get user from Clerk') |
| 54 | + throw new Error( |
| 55 | + (data as ClerkApiError).errors?.[0]?.message || 'Failed to get user from Clerk' |
| 56 | + ) |
48 | 57 | } |
49 | 58 |
|
| 59 | + const user = data as ClerkUser |
| 60 | + |
50 | 61 | return { |
51 | 62 | success: true, |
52 | 63 | output: { |
53 | | - id: data.id, |
54 | | - username: data.username ?? null, |
55 | | - firstName: data.first_name ?? null, |
56 | | - lastName: data.last_name ?? null, |
57 | | - imageUrl: data.image_url ?? null, |
58 | | - hasImage: data.has_image ?? false, |
59 | | - primaryEmailAddressId: data.primary_email_address_id ?? null, |
60 | | - primaryPhoneNumberId: data.primary_phone_number_id ?? null, |
61 | | - primaryWeb3WalletId: data.primary_web3_wallet_id ?? null, |
62 | | - emailAddresses: (data.email_addresses ?? []).map((email: any) => ({ |
| 64 | + id: user.id, |
| 65 | + username: user.username ?? null, |
| 66 | + firstName: user.first_name ?? null, |
| 67 | + lastName: user.last_name ?? null, |
| 68 | + imageUrl: user.image_url ?? null, |
| 69 | + hasImage: user.has_image ?? false, |
| 70 | + primaryEmailAddressId: user.primary_email_address_id ?? null, |
| 71 | + primaryPhoneNumberId: user.primary_phone_number_id ?? null, |
| 72 | + primaryWeb3WalletId: user.primary_web3_wallet_id ?? null, |
| 73 | + emailAddresses: (user.email_addresses ?? []).map((email: ClerkEmailAddress) => ({ |
63 | 74 | id: email.id, |
64 | 75 | emailAddress: email.email_address, |
65 | 76 | verified: email.verification?.status === 'verified', |
66 | 77 | })), |
67 | | - phoneNumbers: (data.phone_numbers ?? []).map((phone: any) => ({ |
| 78 | + phoneNumbers: (user.phone_numbers ?? []).map((phone: ClerkPhoneNumber) => ({ |
68 | 79 | id: phone.id, |
69 | 80 | phoneNumber: phone.phone_number, |
70 | 81 | verified: phone.verification?.status === 'verified', |
71 | 82 | })), |
72 | | - externalId: data.external_id ?? null, |
73 | | - passwordEnabled: data.password_enabled ?? false, |
74 | | - twoFactorEnabled: data.two_factor_enabled ?? false, |
75 | | - totpEnabled: data.totp_enabled ?? false, |
76 | | - backupCodeEnabled: data.backup_code_enabled ?? false, |
77 | | - banned: data.banned ?? false, |
78 | | - locked: data.locked ?? false, |
79 | | - deleteSelfEnabled: data.delete_self_enabled ?? false, |
80 | | - createOrganizationEnabled: data.create_organization_enabled ?? false, |
81 | | - lastSignInAt: data.last_sign_in_at ?? null, |
82 | | - lastActiveAt: data.last_active_at ?? null, |
83 | | - createdAt: data.created_at, |
84 | | - updatedAt: data.updated_at, |
85 | | - publicMetadata: data.public_metadata ?? {}, |
86 | | - privateMetadata: data.private_metadata ?? {}, |
87 | | - unsafeMetadata: data.unsafe_metadata ?? {}, |
| 83 | + externalId: user.external_id ?? null, |
| 84 | + passwordEnabled: user.password_enabled ?? false, |
| 85 | + twoFactorEnabled: user.two_factor_enabled ?? false, |
| 86 | + totpEnabled: user.totp_enabled ?? false, |
| 87 | + backupCodeEnabled: user.backup_code_enabled ?? false, |
| 88 | + banned: user.banned ?? false, |
| 89 | + locked: user.locked ?? false, |
| 90 | + deleteSelfEnabled: user.delete_self_enabled ?? false, |
| 91 | + createOrganizationEnabled: user.create_organization_enabled ?? false, |
| 92 | + lastSignInAt: user.last_sign_in_at ?? null, |
| 93 | + lastActiveAt: user.last_active_at ?? null, |
| 94 | + createdAt: user.created_at, |
| 95 | + updatedAt: user.updated_at, |
| 96 | + publicMetadata: user.public_metadata ?? {}, |
| 97 | + privateMetadata: user.private_metadata ?? {}, |
| 98 | + unsafeMetadata: user.unsafe_metadata ?? {}, |
88 | 99 | success: true, |
89 | 100 | }, |
90 | 101 | } |
|
0 commit comments