Skip to content

Commit 68ad62f

Browse files
committed
[SDK] Fix: Type wallet doesn't exists in getUser (#5593)
https://linear.app/thirdweb/issue/CNCT-2523/remapping-of-siwe-wallet-type-in-typescript-sdk <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on updating the `getUser` function in the `Profiles` to align with TypeScript types and enhancing its test coverage. ### Detailed summary - Updated the `type` in `getUser` to correctly classify profile types as either "wallet" or their original type. - Added unit tests to handle various scenarios: missing secret key, missing query parameters, and handling fetch errors. - Verified correct API URL calls for different query parameters (email, phone, id, externalWalletAddress). - Included tests for fetching user profiles and handling cases where no user is found. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 1a99ea3 commit 68ad62f

File tree

3 files changed

+227
-1
lines changed

3 files changed

+227
-1
lines changed

.changeset/curly-ligers-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
update `type` in `getUser` `Profiles` to match tyepscript types
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import { createThirdwebClient } from "../../../../client/client.js";
3+
import { getClientFetch } from "../../../../utils/fetch.js";
4+
import { getUser } from "./getUser.js";
5+
6+
vi.mock("../../../../utils/fetch.js", () => ({
7+
getClientFetch: vi.fn(),
8+
}));
9+
10+
describe("getUser", () => {
11+
const mockClient = createThirdwebClient({
12+
secretKey: "secret",
13+
});
14+
15+
const mockFetch = vi.fn();
16+
beforeEach(() => {
17+
vi.clearAllMocks();
18+
vi.mocked(getClientFetch).mockReturnValue(mockFetch);
19+
});
20+
21+
it("should throw an error if no secret key is provided", async () => {
22+
await expect(
23+
getUser({
24+
client: { ...mockClient, secretKey: undefined },
25+
walletAddress: "0x123",
26+
}),
27+
).rejects.toThrow(
28+
"A secret key is required to query for users. If you're making this request from the server, please add a secret key to your client.",
29+
);
30+
});
31+
32+
it("should throw an error if no query parameter is provided", async () => {
33+
await expect(
34+
getUser({
35+
client: mockClient,
36+
}),
37+
).rejects.toThrow(
38+
"Please provide a walletAddress, email, phone, id, or externalWalletAddress to query for users.",
39+
);
40+
});
41+
42+
it("should call the correct URL with email", async () => {
43+
mockFetch.mockResolvedValueOnce({
44+
ok: true,
45+
json: async () => [
46+
{
47+
userId: "user1",
48+
walletAddress: "0x123",
49+
email: "test@test.com",
50+
createdAt: "2023-01-01T00:00:00Z",
51+
linkedAccounts: [],
52+
},
53+
],
54+
});
55+
56+
const result = await getUser({
57+
client: mockClient,
58+
email: "test@test.com",
59+
});
60+
61+
expect(mockFetch).toHaveBeenCalledWith(
62+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=email&email=test%40test.com",
63+
);
64+
expect(result).toEqual({
65+
userId: "user1",
66+
walletAddress: "0x123",
67+
email: "test@test.com",
68+
createdAt: "2023-01-01T00:00:00Z",
69+
profiles: [],
70+
});
71+
});
72+
73+
it("should call the correct URL with phone", async () => {
74+
mockFetch.mockResolvedValueOnce({
75+
ok: true,
76+
json: async () => [
77+
{
78+
userId: "user1",
79+
walletAddress: "0x123",
80+
phone: "+1234567890",
81+
createdAt: "2023-01-01T00:00:00Z",
82+
linkedAccounts: [],
83+
},
84+
],
85+
});
86+
87+
const result = await getUser({
88+
client: mockClient,
89+
phone: "+1234567890",
90+
});
91+
92+
expect(mockFetch).toHaveBeenCalledWith(
93+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=phone&phone=%2B1234567890",
94+
);
95+
expect(result).toEqual({
96+
userId: "user1",
97+
walletAddress: "0x123",
98+
phone: "+1234567890",
99+
createdAt: "2023-01-01T00:00:00Z",
100+
profiles: [],
101+
});
102+
});
103+
104+
it("should call the correct URL with id", async () => {
105+
mockFetch.mockResolvedValueOnce({
106+
ok: true,
107+
json: async () => [
108+
{
109+
userId: "user1",
110+
walletAddress: "0x123",
111+
createdAt: "2023-01-01T00:00:00Z",
112+
linkedAccounts: [
113+
{
114+
type: "id",
115+
details: {
116+
id: "0x456",
117+
},
118+
},
119+
],
120+
},
121+
],
122+
});
123+
124+
const result = await getUser({
125+
client: mockClient,
126+
id: "user1",
127+
});
128+
129+
expect(mockFetch).toHaveBeenCalledWith(
130+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=id&id=user1",
131+
);
132+
expect(result).toEqual({
133+
userId: "user1",
134+
walletAddress: "0x123",
135+
createdAt: "2023-01-01T00:00:00Z",
136+
profiles: [
137+
{
138+
type: "id",
139+
details: {
140+
id: "0x456",
141+
},
142+
},
143+
],
144+
});
145+
});
146+
147+
it("should call the correct URL with externalWalletAddress", async () => {
148+
mockFetch.mockResolvedValueOnce({
149+
ok: true,
150+
json: async () => [
151+
{
152+
userId: "user1",
153+
walletAddress: "0x123",
154+
createdAt: "2023-01-01T00:00:00Z",
155+
linkedAccounts: [
156+
{
157+
type: "siwe",
158+
details: {
159+
address: "0x456",
160+
},
161+
},
162+
],
163+
},
164+
],
165+
});
166+
167+
const result = await getUser({
168+
client: mockClient,
169+
externalWalletAddress: "0x456",
170+
});
171+
172+
expect(mockFetch).toHaveBeenCalledWith(
173+
"https://embedded-wallet.thirdweb.com/api/2023-11-30/embedded-wallet/user-details?queryBy=externalWalletAddress&externalWalletAddress=0x456",
174+
);
175+
expect(result).toEqual({
176+
userId: "user1",
177+
walletAddress: "0x123",
178+
createdAt: "2023-01-01T00:00:00Z",
179+
profiles: [
180+
{
181+
type: "wallet",
182+
details: {
183+
address: "0x456",
184+
},
185+
},
186+
],
187+
});
188+
});
189+
190+
it("should handle fetch errors", async () => {
191+
mockFetch.mockResolvedValueOnce({
192+
ok: false,
193+
});
194+
195+
await expect(
196+
getUser({
197+
client: mockClient,
198+
walletAddress: "0x123",
199+
}),
200+
).rejects.toThrow("Failed to get profiles");
201+
});
202+
203+
it("should return null if no user is found", async () => {
204+
mockFetch.mockResolvedValueOnce({
205+
ok: true,
206+
json: async () => [],
207+
});
208+
209+
const result = await getUser({
210+
client: mockClient,
211+
walletAddress: "0x123",
212+
});
213+
214+
expect(result).toBeNull();
215+
});
216+
});

packages/thirdweb/src/wallets/in-app/core/users/getUser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ export async function getUser({
113113
email: item.email,
114114
phone: item.phone,
115115
createdAt: item.createdAt,
116-
profiles: item.linkedAccounts,
116+
profiles: item.linkedAccounts.map((profile) => {
117+
return {
118+
type: (profile.type as string) === "siwe" ? "wallet" : profile.type,
119+
details: profile.details,
120+
};
121+
}),
117122
}))[0] || null
118123
);
119124
}

0 commit comments

Comments
 (0)