-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #878 from MoralisWeb3/feat/next-pagination
Feat: @moralisweb3/next paginated hooks
- Loading branch information
Showing
62 changed files
with
819 additions
and
1,469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@moralisweb3/next': patch | ||
--- | ||
|
||
Added paginated hooks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/codegen/src/next/generators/hooks/templates/hook_paginated.ts.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
{{ names.operation }} as operation, | ||
{{ names.request }}, | ||
} from '{{ names.commonUtils }}'; | ||
import { FetchParams } from '../../../types'; | ||
import { useResolverPaginated } from '../../../resolvers'; | ||
|
||
export const {{ names.hook }} = ( | ||
request?: {{ names.request }}, | ||
fetchParams?: FetchParams, | ||
) => { | ||
const { data, error, fetch, isFetching } = useResolverPaginated({ | ||
endpoint: '{{ url }}', | ||
operation, | ||
request, | ||
fetchParams, | ||
}); | ||
|
||
return { | ||
data: data?.data, | ||
cursor: data?.cursor, | ||
page: data?.page, | ||
pageSize: data?.pageSize, | ||
total: data?.total, | ||
error, | ||
fetch, | ||
/** | ||
* @deprecated use `fetch()` instead | ||
*/ | ||
refetch: () => fetch(), | ||
isFetching, | ||
/** | ||
* @deprecated use `isFetching` instead | ||
*/ | ||
isValidating: isFetching, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Operation } from '@moralisweb3/common-core'; | ||
|
||
export type UnknownOperation = Operation<unknown, unknown, unknown, unknown>; | ||
export type OperationAction = Pick<UnknownOperation, 'name' | 'groupName' | 'method' | 'id'>; | ||
export type OperationAction = Pick<UnknownOperation, 'name' | 'groupName' | 'method' | 'id' | 'urlSearchParamNames'>; | ||
|
||
export type Module = 'evmApi' | 'solApi' | 'auth'; |
56 changes: 18 additions & 38 deletions
56
packages/next/src/hooks/auth/useAuthRequestChallengeEvm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,29 @@ | ||
import { | ||
requestChallengeEvmOperation as operation, | ||
RequestChallengeEvmRequest, | ||
RequestChallengeEvmResponse, | ||
} from '@moralisweb3/common-auth-utils'; | ||
import { fetcher } from '../../utils/fetcher'; | ||
import { requestChallengeEvmOperation as operation, RequestChallengeEvmRequest } from '@moralisweb3/common-auth-utils'; | ||
import { FetchParams } from '../types'; | ||
import { useCallback } from 'react'; | ||
import useSWR from 'swr'; | ||
import { useResolver } from '../resolvers'; | ||
|
||
export type RequestChallengeEvmRequestClient = Pick<RequestChallengeEvmRequest, 'chainId' | 'address'>; | ||
|
||
export const useAuthRequestChallengeEvm = (request?: RequestChallengeEvmRequestClient, fetchParams?: FetchParams) => { | ||
const endpoint = 'auth/requestChallengeEvm'; | ||
const { deserializeResponse } = operation; | ||
|
||
const { data, error, isValidating, mutate } = useSWR<RequestChallengeEvmResponse>( | ||
[ | ||
endpoint, | ||
{ | ||
deserializeResponse, | ||
request, | ||
}, | ||
], | ||
request ? fetcher : null, | ||
{ | ||
revalidateOnFocus: false, | ||
revalidateIfStale: false, | ||
...fetchParams, | ||
}, | ||
); | ||
|
||
const requestChallengeAsync = useCallback((params: RequestChallengeEvmRequestClient) => { | ||
return mutate( | ||
fetcher(endpoint, { | ||
deserializeResponse, | ||
request: params, | ||
}), | ||
); | ||
}, []); | ||
const { data, error, fetch, isFetching } = useResolver({ | ||
endpoint: 'auth/requestChallengeEvm', | ||
operation, | ||
request, | ||
fetchParams, | ||
}); | ||
|
||
return { | ||
challenge: data, | ||
error, | ||
requestChallengeAsync, | ||
refetch: async () => mutate(), | ||
isValidating, | ||
requestChallengeAsync: fetch, | ||
/** | ||
* @deprecated use `fetch()` instead | ||
*/ | ||
refetch: () => fetch(), | ||
isFetching, | ||
/** | ||
* @deprecated use `isFetching` instead | ||
*/ | ||
isValidating: isFetching, | ||
}; | ||
}; |
51 changes: 17 additions & 34 deletions
51
packages/next/src/hooks/auth/useAuthRequestChallengeSolana.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,35 @@ | ||
import { | ||
requestChallengeSolanaOperation as operation, | ||
RequestChallengeSolanaRequest, | ||
RequestChallengeSolanaResponse, | ||
} from '@moralisweb3/common-auth-utils'; | ||
import { fetcher } from '../../utils/fetcher'; | ||
import { FetchParams } from '../types'; | ||
import { useCallback } from 'react'; | ||
import useSWR from 'swr'; | ||
import { useResolver } from '../resolvers'; | ||
|
||
export type RequestChallengeSolanaRequestClient = Pick<RequestChallengeSolanaRequest, 'address' | 'network'>; | ||
|
||
export const useAuthRequestChallengeSolana = ( | ||
request?: RequestChallengeSolanaRequestClient, | ||
fetchParams?: FetchParams, | ||
) => { | ||
const endpoint = 'auth/requestChallengeSolana'; | ||
const { deserializeResponse } = operation; | ||
|
||
const { data, error, isValidating, mutate } = useSWR<RequestChallengeSolanaResponse>( | ||
[ | ||
endpoint, | ||
{ | ||
deserializeResponse, | ||
request, | ||
}, | ||
], | ||
request ? fetcher : null, | ||
{ | ||
revalidateOnFocus: false, | ||
revalidateIfStale: false, | ||
...fetchParams, | ||
}, | ||
); | ||
|
||
const requestChallengeAsync = useCallback((params: RequestChallengeSolanaRequestClient) => { | ||
return mutate( | ||
fetcher(endpoint, { | ||
deserializeResponse, | ||
request: params, | ||
}), | ||
); | ||
}, []); | ||
const { data, error, fetch, isFetching } = useResolver({ | ||
endpoint: 'auth/requestChallengeSolana', | ||
operation, | ||
request, | ||
fetchParams, | ||
}); | ||
|
||
return { | ||
challenge: data, | ||
error, | ||
requestChallengeAsync, | ||
refetch: async () => mutate(), | ||
isValidating, | ||
requestChallengeAsync: fetch, | ||
/** | ||
* @deprecated use `fetch()` instead | ||
*/ | ||
refetch: () => fetch(), | ||
isFetching, | ||
/** | ||
* @deprecated use `isFetching` instead | ||
*/ | ||
isValidating: isFetching, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3340ac5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test coverage