[SDK] Feature: adds findUsers function (#4541)

This replaces https://portal.thirdweb.com/connect/in-app-wallet/how-to/get-in-app-wallet-details-on-server

<!-- start pr-codex -->

---

## PR-Codex overview
This PR adds a `getUser` function to query users on the server and updates related files with necessary exports and type definitions.

### Detailed summary
- Added `getUser` function to query users on the server
- Updated exports in `thirdweb.ts` and `wallets.ts`
- Added type definitions for `GetUserResult`
- Implemented `getUser` function in `getUser.ts`

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
This commit is contained in:
gregfromstl
2024-09-12 00:51:32 +00:00
parent eda37f3031
commit 0596fa24c2
5 changed files with 140 additions and 1 deletions
+14
View File
@@ -0,0 +1,14 @@
---
"thirdweb": minor
---
Adds getUsers function to query users on the server
```ts
import { getUser } from "thirdweb/wallets";
const user = await getUser({
client,
walletAddress: "0x123...",
});
```
@@ -68,6 +68,14 @@ export {
type ThirdwebContract,
} from "../contract/contract.js";
/**
* WALLETS
*/
export {
getUser,
type GetUserResult,
} from "../wallets/in-app/core/users/getUser.js";
/**
* TRANSACTIONS
*/
+5
View File
@@ -102,6 +102,11 @@ export {
linkProfile,
} from "../wallets/in-app/core/wallet/profiles.js";
export {
getUser,
type GetUserResult,
} from "../wallets/in-app/core/users/getUser.js";
export type {
CoinbaseWalletCreationOptions,
CoinbaseSDKWalletConnectionOptions,
@@ -18,7 +18,7 @@ import { useActiveWallet } from "../wallets/useActiveWallet.js";
* const { data: profiles } = useProfiles();
*
* console.log("Type:", profiles[0].type); // "discord"
* console.log("Email:", profiles[0].email); // "[email protected]"
* console.log("Email:", profiles[0].details.email); // "[email protected]"
* ```
*
* @wallet
@@ -0,0 +1,112 @@
import type { ThirdwebClient } from "../../../../client/client.js";
import { getThirdwebBaseUrl } from "../../../../utils/domains.js";
import { getClientFetch } from "../../../../utils/fetch.js";
import type { OneOf, Prettify } from "../../../../utils/type-utils.js";
import type { Profile } from "../authentication/types.js";
export type GetUserResult = {
userId: string;
walletAddress: string;
email?: string;
phone?: string;
createdAt: string;
profiles: Profile[];
};
/**
* Gets user based on the provided query parameters.
* @note This function is only available on the server (a secret key is required in the client).
*
* @param options - The options for the find users function.
* @param options.client - The Thirdweb client with a secret key included.
* @param [options.walletAddress] - The wallet address to query by.
* @param [options.email] - The email to query by.
* @param [options.phone] - The phone number to query by.
* @param [options.id] - The user ID to query by.
*
* @returns An array of user objects.
*
* @example
* ```ts
* import { getUser } from "thirdweb/wallets";
*
* const user = await getUser({
* client,
* walletAddress: "0x123...",
* });
* ```
*
* @wallet
*/
export async function getUser({
client,
walletAddress,
email,
phone,
id,
}: Prettify<
{
client: ThirdwebClient;
} & OneOf<{
walletAddress?: string;
email?: string;
phone?: string;
id?: string;
}>
>): Promise<GetUserResult | null> {
if (!client.secretKey) {
throw new Error(
"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.",
);
}
const url = new URL(
`${getThirdwebBaseUrl("inAppWallet")}/api/2023-11-30/embedded-wallet/user-details`,
);
if (walletAddress) {
url.searchParams.set("queryBy", "walletAddress");
url.searchParams.set("walletAddress", walletAddress);
} else if (email) {
url.searchParams.set("queryBy", "email");
url.searchParams.set("email", email);
} else if (phone) {
url.searchParams.set("queryBy", "phone");
url.searchParams.set("phone", phone);
} else if (id) {
url.searchParams.set("queryBy", "id");
url.searchParams.set("id", id);
} else {
throw new Error(
"Please provide a walletAddress, email, phone, or id to query for users.",
);
}
const clientFetch = getClientFetch(client);
const res = await clientFetch(url.toString());
if (!res.ok) {
throw new Error("Failed to get profiles");
}
const data = (await res.json()) as {
userId: string;
walletAddress: string;
email?: string;
phone?: string;
createdAt: string;
linkedAccounts: Profile[];
}[];
return (
data.map((item) => ({
userId: item.userId,
walletAddress: item.walletAddress,
email: item.email,
phone: item.phone,
createdAt: item.createdAt,
profiles: item.linkedAccounts,
}))[0] || null
);
}