Skip to content

Commit ef53c48

Browse files
authored
feat: return results on async requests (#198)
* feat: return results on async requests * fix: correct typings
1 parent bedc695 commit ef53c48

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

src/context/MoralisContext/MoralisContext.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export interface MoralisContextValue {
2525
isInitializing: boolean;
2626
initialize: (options?: { serverUrl?: string; appId?: string }) => void;
2727

28-
authenticate: (options?: AuthenticateOptions) => Promise<void>;
28+
authenticate: (
29+
options?: AuthenticateOptions,
30+
) => Promise<MoralisType.User | undefined>;
2931
logout: () => Promise<void>;
3032
signup: Signup;
3133
login: Login;
@@ -39,14 +41,16 @@ export interface MoralisContextValue {
3941
isLoggingOut: boolean;
4042
isAuthUndefined: boolean;
4143

42-
setUserData: (data: SetUserData) => Promise<void>;
44+
setUserData: (data: SetUserData) => Promise<MoralisType.User | undefined>;
4345
user: MoralisType.User | null;
4446
_setUser: (user: MoralisType.User) => void;
4547
userError: null | Error;
4648
isUserUpdating: boolean;
47-
refetchUserData: () => Promise<void>;
49+
refetchUserData: () => Promise<MoralisType.User | undefined>;
4850

49-
enableWeb3: (options?: Web3EnableOptions) => void;
51+
enableWeb3: (
52+
options?: Web3EnableOptions,
53+
) => Promise<MoralisType.Web3Provider | undefined>;
5054
deactivateWeb3: () => Promise<void>;
5155
web3: MoralisType.MoralisWeb3Provider | null;
5256
isWeb3Enabled: boolean;

src/hooks/core/useMoralis/_useMoralisAuth.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ export type Login = (
8282
username: string,
8383
password: string,
8484
options?: LoginOptions,
85-
) => Promise<void>;
85+
) => Promise<MoralisType.User<MoralisType.Attributes> | undefined>;
8686

8787
export type Signup = (
8888
username: string,
8989
password: string,
9090
email?: string,
9191
otherFields?: SetUserData,
9292
options?: SignupOptions,
93-
) => Promise<void>;
93+
) => Promise<MoralisType.User<MoralisType.Attributes> | undefined>;
9494

9595
export type OnAccountChanged = (account: string) => void;
9696

@@ -172,6 +172,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
172172
if (onSuccess) {
173173
onSuccess(user);
174174
}
175+
return user;
175176
} catch (error) {
176177
setAuth({ state: AuthenticationState.ERROR, error });
177178
setUser(null);
@@ -231,6 +232,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
231232
if (onSuccess) {
232233
onSuccess(user);
233234
}
235+
return user;
234236
} catch (error) {
235237
setAuth({ state: AuthenticationState.ERROR, error });
236238
if (throwOnError) {
@@ -274,6 +276,7 @@ export const _useMoralisAuth = (options: UseMoralisAuthOptions) => {
274276
if (onSuccess) {
275277
onSuccess(user);
276278
}
279+
return user;
277280
} catch (error) {
278281
setAuth({ state: AuthenticationState.ERROR, error });
279282
if (throwOnError) {

src/hooks/core/useMoralis/_useMoralisUser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const _useMoralisUser = (Moralis: MoralisType) => {
6363
if (onSuccess) {
6464
onSuccess(user);
6565
}
66+
return user;
6667
} catch (error) {
6768
if (userHasLocallyUpdated) {
6869
user.revert();
@@ -116,6 +117,7 @@ export const _useMoralisUser = (Moralis: MoralisType) => {
116117
if (onSuccess) {
117118
onSuccess(newUserData);
118119
}
120+
return newUserData;
119121
} catch (error) {
120122
setError(error);
121123
if (throwOnError) {

src/hooks/core/useMoralis/_useMoralisWeb3.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ export interface Web3EnableOptions {
1212
anyNetwork?: boolean;
1313
}
1414

15+
type EnableWeb3 = (
16+
options?: Web3EnableOptions | undefined,
17+
) => Promise<MoralisType.Web3Provider | undefined>;
18+
1519
/**
1620
* Handles enabling of web3 and providing it, as soon as the user is authenticated
1721
*/
1822
export const _useMoralisWeb3 = (
1923
Moralis: MoralisType,
2024
): {
21-
enableWeb3: (options?: Web3EnableOptions) => Promise<void>;
25+
enableWeb3: EnableWeb3;
2226
web3: null | MoralisType.MoralisWeb3Provider;
2327
isWeb3Enabled: boolean;
2428
web3EnableError: Error | null;
@@ -99,7 +103,7 @@ export const _useMoralisWeb3 = (
99103
/**
100104
* Enable web3 with the browsers web3Provider (only available when a user has been authenticated)
101105
*/
102-
const enableWeb3 = useCallback(
106+
const enableWeb3 = useCallback<EnableWeb3>(
103107
async ({
104108
throwOnError,
105109
onComplete,
@@ -113,13 +117,16 @@ export const _useMoralisWeb3 = (
113117
try {
114118
// TODO: fix typechecking when passing ...rest
115119
// @ts-ignore
116-
const currentWeb3 = await Moralis.enableWeb3(rest);
120+
const currentWeb3: MoralisType.Web3Provider = await Moralis.enableWeb3(
121+
rest,
122+
);
117123

118124
_setIsWeb3Enabled(true);
119125

120126
if (onSuccess) {
121127
onSuccess(currentWeb3);
122128
}
129+
return currentWeb3;
123130
} catch (error) {
124131
setEnableWeb3Error(error);
125132
if (throwOnError) {

src/hooks/internal/_useResolveAsyncCall/_useResolveAsyncCall.ts

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const _useResolveCall = <Result, Params extends ResolveCallParams>(
7575
if (onSuccess) {
7676
onSuccess(results);
7777
}
78+
return results;
7879
} catch (error) {
7980
setData(initialData);
8081
setError(error);

0 commit comments

Comments
 (0)