Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to override fetch function in farcaster identity hooks #528

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-phones-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@frames.js/render": patch
---

feat(@frames.js/render): allow to override fetch function in farcaster identity hooks
42 changes: 28 additions & 14 deletions packages/render/src/identity/farcaster/use-farcaster-identity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ type UseFarcasterIdentityOptions = {
* @defaultValue 'farcasterIdentity'
*/
storageKey?: string;
/**
* Function used to generate a unique user id for signer._id property value
*/
generateUserId?: () => string | number;
/**
* Custom fetch function used to do requests
*/
fetchFn?: typeof fetch;
/**
* Used to detect if the current context is visible, this affects the polling of the signer approval status.
*/
Expand All @@ -157,6 +164,8 @@ const defaultGenerateUserId = (): number => {
return Date.now();
};

const defaultFetchFn: typeof fetch = (...args) => fetch(...args);

export function useFarcasterIdentity({
onMissingIdentity,
enableIdentityPolling = true,
Expand All @@ -169,6 +178,7 @@ export function useFarcasterIdentity({
onLogInStart,
onLogOut,
generateUserId = defaultGenerateUserId,
fetchFn = defaultFetchFn,
}: UseFarcasterIdentityOptions): FarcasterSignerInstance {
const storageRef = useRef(storage);
const identityPoller = useRef(new IdentityPoller()).current;
Expand All @@ -194,13 +204,14 @@ export function useFarcasterIdentity({
const onLogOutRef = useFreshRef(onLogOut);
const generateUserIdRef = useFreshRef(generateUserId);
const onMissingIdentityRef = useFreshRef(onMissingIdentity);
const fetchFnRef = useFreshRef(fetchFn);

const createFarcasterSigner =
useCallback(async (): Promise<FarcasterCreateSignerResult> => {
try {
const keypair = await createKeypairEDDSA();
const keypairString = convertKeypairToHex(keypair);
const authorizationResponse = await fetch(
const authorizationResponse = await fetchFnRef.current(
// real signer or local one are handled by local route so we don't need to expose anything to client side bundle
signerUrl,
{
Expand Down Expand Up @@ -231,18 +242,21 @@ export function useFarcasterIdentity({
const {
result: { signedKeyRequest },
} = (await (
await fetch(`https://api.warpcast.com/v2/signed-key-requests`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
key: keypairString.publicKey,
signature,
requestFid,
deadline,
}),
})
await fetchFnRef.current(
"https://api.warpcast.com/v2/signed-key-requests",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
key: keypairString.publicKey,
signature,
requestFid,
deadline,
}),
}
)
).json()) as {
result: {
signedKeyRequest: { token: string; deeplinkUrl: string };
Expand Down Expand Up @@ -297,7 +311,7 @@ export function useFarcasterIdentity({
console.error("@frames.js/render: API Call failed", error);
throw error;
}
}, [generateUserIdRef, onLogInStartRef, setState, signerUrl]);
}, [fetchFnRef, generateUserIdRef, onLogInStartRef, setState, signerUrl]);

const impersonateUser = useCallback(
async (fid: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ type UseFarcasterMultiIdentityOptions = {
onLogOut?: (identity: FarcasterSigner) => void;
onIdentityRemove?: (identity: FarcasterSigner) => void;
onIdentitySelect?: (identity: FarcasterSigner) => void;
/**
* Function used to generate a unique user id for signer._id property value
*/
generateUserId?: () => string | number;
/**
* Custom fetch function used to do requests
*/
fetchFn?: typeof fetch;
};

export type FarcasterMultiSignerInstance =
Expand All @@ -210,6 +217,7 @@ export type FarcasterMultiSignerInstance =

const defaultStorage = new WebStorage();
const defaultGenerateUserId = (): number => Date.now();
const defaultFetchFn: typeof fetch = (...args) => fetch(...args);

type SignedKeyRequestSponsorship = {
sponsorFid: number;
Expand All @@ -229,6 +237,7 @@ export function useFarcasterMultiIdentity({
onIdentityRemove,
onIdentitySelect,
generateUserId = defaultGenerateUserId,
fetchFn = defaultFetchFn,
}: UseFarcasterMultiIdentityOptions): FarcasterMultiSignerInstance {
const storageRef = useRef(storage);
const identityPoller = useRef(new IdentityPoller()).current;
Expand Down Expand Up @@ -268,13 +277,14 @@ export function useFarcasterMultiIdentity({
const onIdentitySelectRef = useFreshRef(onIdentitySelect);
const generateUserIdRef = useFreshRef(generateUserId);
const onMissingIdentityRef = useFreshRef(onMissingIdentity);
const fetchFnRef = useFreshRef(fetchFn);

const createFarcasterSigner =
useCallback(async (): Promise<FarcasterCreateSignerResult> => {
try {
const keypair = await createKeypairEDDSA();
const keypairString = convertKeypairToHex(keypair);
const authorizationResponse = await fetch(
const authorizationResponse = await fetchFnRef.current(
// real signer or local one are handled by local route so we don't need to expose anything to client side bundle
signerUrl,
{
Expand Down Expand Up @@ -312,19 +322,22 @@ export function useFarcasterMultiIdentity({
const {
result: { signedKeyRequest },
} = (await (
await fetch(`https://api.warpcast.com/v2/signed-key-requests`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
key: keypairString.publicKey,
signature,
requestFid,
deadline,
sponsorship,
}),
})
await fetchFnRef.current(
"https://api.warpcast.com/v2/signed-key-requests",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
key: keypairString.publicKey,
signature,
requestFid,
deadline,
sponsorship,
}),
}
)
).json()) as {
result: {
signedKeyRequest: {
Expand Down Expand Up @@ -383,7 +396,7 @@ export function useFarcasterMultiIdentity({
console.error("@frames.js/render: API Call failed", error);
throw error;
}
}, [generateUserIdRef, onLogInStartRef, setState, signerUrl]);
}, [fetchFnRef, generateUserIdRef, onLogInStartRef, setState, signerUrl]);

const impersonateUser = useCallback(
async (fid: number) => {
Expand Down
Loading