-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…7845) * feat: ⬆️ update `@deriv/api-types` to `1.0.85` * feat(api): ✨ update api types with all the requests from `@deriv/api-types` * feat(api): ✨ add `useAPI` hook * feat(api): ✨ add `useAPISubscription` hook * feat(api): ✨ add `useAPISubscription` hook * feat(api): ✨ improve types * feat(api): ✨ rename hooks * feat(api): ✨ rename hooks * refactor(api): 🔥 remove `QueryClient` config * refactor(api): 🔥 remove unnecessary utils file * refactor(api): 🔥 remove unnecessary export types * test(api): ✅ add test for `useFetch` hook * refactor(api): 🔥 improve `useSubscription` hook * feat(api): ✨ use `useMutation` hook inside `useRequest` hook * test(cashier): ✅ fix the failing test * refactor(api): ♻️ move request props to `payload` object * refactor(api): 📝 resolve PR comments * feat(api): ⬆️ migrating to React Query 4 * fix(cashier): 🐛 fix type errors * feat(api): ✨ add `useInvalidateQuery` hook * refactor(api): 📝 resolve PR comments * ci: 💚 trigger build * ci: 💚 trigger build --------- Co-authored-by: Farzin Mirzaie <farzin@deriv.com>
- Loading branch information
1 parent
64183a8
commit b727dc2
Showing
27 changed files
with
290 additions
and
218 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
const APIProvider = ({ children }: PropsWithChildren<unknown>) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
<ReactQueryDevtools /> | ||
</QueryClientProvider> | ||
); | ||
|
||
export default APIProvider; |
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,29 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useFetch from '../useFetch'; | ||
import APIProvider from '../APIProvider'; | ||
import { TSocketResponse } from '../../types'; | ||
|
||
jest.mock('@deriv/shared', () => ({ | ||
WS: { | ||
send: jest.fn(() => | ||
Promise.resolve<TSocketResponse<'ping'>>({ | ||
msg_type: 'ping', | ||
ping: 'pong', | ||
echo_req: {}, | ||
}) | ||
), | ||
}, | ||
})); | ||
|
||
describe('useFetch', () => { | ||
test('should call ping and get pong in response', async () => { | ||
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>; | ||
|
||
const { result, waitFor } = renderHook(() => useFetch('ping'), { wrapper }); | ||
|
||
await waitFor(() => result.current.isSuccess, { timeout: 10000 }); | ||
|
||
expect(result.current.data).toEqual('pong'); | ||
}); | ||
}); |
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,31 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useRequest from '../useRequest'; | ||
import APIProvider from '../APIProvider'; | ||
import { TSocketResponse } from '../../types'; | ||
|
||
jest.mock('@deriv/shared', () => ({ | ||
WS: { | ||
send: jest.fn(() => | ||
Promise.resolve<TSocketResponse<'verify_email'>>({ | ||
msg_type: 'verify_email', | ||
verify_email: 1, | ||
echo_req: {}, | ||
}) | ||
), | ||
}, | ||
})); | ||
|
||
describe('useRequest', () => { | ||
test('should call verify_email and get 1 in response', async () => { | ||
const wrapper = ({ children }: { children: JSX.Element }) => <APIProvider>{children}</APIProvider>; | ||
|
||
const { result, waitFor } = renderHook(() => useRequest('verify_email'), { wrapper }); | ||
|
||
result.current.mutate([{ payload: { verify_email: 'john@example.com', type: 'request_email' } }]); | ||
|
||
await waitFor(() => result.current.isSuccess, { timeout: 10000 }); | ||
|
||
expect(result.current.data).toEqual(1); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
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,2 +1,5 @@ | ||
export { default as useWS } from './useWS'; | ||
export { default as APIProvider } from './APIProvider'; | ||
export { default as useFetch } from './useFetch'; | ||
export { default as useInvalidateQuery } from './useInvalidateQuery'; | ||
export { default as useRequest } from './useRequest'; | ||
export { default as useSubscription } from './useSubscription'; |
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,19 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { getQueryKeys, send } from './utils'; | ||
import type { | ||
TSocketEndpointNames, | ||
TSocketAcceptableProps, | ||
TSocketResponseData, | ||
TSocketRequestCleaned, | ||
TSocketRequestQueryOptions, | ||
} from '../types'; | ||
|
||
const useFetch = <T extends TSocketEndpointNames>(name: T, ...props: TSocketAcceptableProps<T, true>) => { | ||
const prop = props?.[0]; | ||
const payload = prop && 'payload' in prop ? (prop.payload as TSocketRequestCleaned<T>) : undefined; | ||
const options = prop && 'options' in prop ? (prop.options as TSocketRequestQueryOptions<T>) : undefined; | ||
|
||
return useQuery<TSocketResponseData<T>, unknown>(getQueryKeys(name, payload), () => send(name, payload), options); | ||
}; | ||
|
||
export default useFetch; |
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,10 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { TSocketEndpointNames } from '../types'; | ||
|
||
const useInvalidateQuery = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return <T extends TSocketEndpointNames>(name: T) => queryClient.invalidateQueries([name]); | ||
}; | ||
|
||
export default useInvalidateQuery; |
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,20 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { send } from './utils'; | ||
import type { | ||
TSocketEndpointNames, | ||
TSocketAcceptableProps, | ||
TSocketResponseData, | ||
TSocketRequestCleaned, | ||
TSocketRequestMutationOptions, | ||
} from '../types'; | ||
|
||
// Todo: Get rid of redundant array wrapper for the props argument. | ||
const useRequest = <T extends TSocketEndpointNames>(name: T, options?: TSocketRequestMutationOptions<T>) => | ||
useMutation<TSocketResponseData<T>, unknown, TSocketAcceptableProps<T>>(props => { | ||
const prop = props?.[0]; | ||
const payload = prop && 'payload' in prop ? (prop.payload as TSocketRequestCleaned<T>) : undefined; | ||
|
||
return send(name, payload); | ||
}, options); | ||
|
||
export default useRequest; |
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.
b727dc2
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.
Successfully deployed to the following URLs:
deriv-app – ./
binary.sx
deriv-app.vercel.app
deriv-app-git-master.binary.sx
deriv-app.binary.sx