Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
refactor!: rename useRequest to useManualQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed May 2, 2020
1 parent 95493aa commit d5ba65c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Here how someone would make a request using the [useQuery](#usequery) hook provi
const { data, error, hasMore, fetchMore, ...rest } = useQuery(/* query */);
```

> **Note:** [useRequest](#userequest) hook works the same way as `useQuery` expect it doesn't fetch the request automatically on mount/updates but rather exposes a helper to manually fetch when needed.
> **Note:** [useManualQuery](#useManualQuery) hook works the same way as `useQuery` expect it doesn't fetch the request automatically on mount/updates but rather exposes a helper to manually fetch when needed.
When resolving a request, Oktane checks the value returned by `clientOptions.fetch(request, ctx)` call and does one of the following based to the type:

Expand Down Expand Up @@ -139,10 +139,10 @@ function useQuery(query: QueryFunc | any): {
}
```

#### useRequest
#### useManualQuery

```javascript
function useRequest(query: any): {
function useManualQuery(query: any): {
fetch: () => void;
cancel: () => void;
refetch: () => void;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { Client, createClient } from './client';
export { Request, createRequest } from './request';

// React
export { useQuery, useRequest } from './react/fetchers';
export { useQuery, useManualQuery } from './react/query';
export { useClient, ClientProvider } from './react/useClient';

// Typings
Expand Down
8 changes: 4 additions & 4 deletions src/react/fetchers.test.tsx → src/react/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createClient } from '../client';
import { createRequest } from '../request';
import { ClientProvider } from './useClient';
import { wrap, spyOnFetch } from './test/utils';
import { useQuery, useRequest } from './fetchers';
import { useQuery, useManualQuery } from './query';

// @ts-ignore
global.__DEV__ = true;
Expand Down Expand Up @@ -296,12 +296,12 @@ describe('useQuery', () => {
});
});

describe('useRequest', () => {
describe('useManualQuery', () => {
test('should only fetch on .fetch() call', async () => {
const client = createClient({ fetch });

const Example = wrap(() => {
const { status, data, fetch } = useRequest('/api');
const { status, data, fetch } = useManualQuery('/api');

return (
<>
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('useRequest', () => {
test('should throw if query is a function', () => {
const client = createClient({ fetch });

const { result } = renderHook(() => useRequest(() => '/api'), {
const { result } = renderHook(() => useManualQuery(() => '/api'), {
wrapper: ({ children }) => (
<ClientProvider value={client}>{children}</ClientProvider>
),
Expand Down
24 changes: 12 additions & 12 deletions src/react/fetchers.ts → src/react/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import is from '../utils/is';
import { Client } from '../client';
import { Result } from '../utils/cache';
import { useClient } from './useClient';
import { useStableRequest } from './useStableRequest';
import { useRequest } from './useRequest';

interface FetchOperations {
interface QueryOperations {
fetch?: () => void;
cancel: () => void;
refetch: () => void;
hasMore: () => boolean;
fetchMore: () => void;
}

function createFetcher(
function createQueryHook(
manual: false
): (query: any) => Result & Omit<FetchOperations, 'fetch'>;
function createFetcher(
): (query: any) => Result & Omit<QueryOperations, 'fetch'>;
function createQueryHook(
manual: true
): (query: any) => Result & Required<FetchOperations>;
function createFetcher(manual: boolean) {
): (query: any) => Result & Required<QueryOperations>;
function createQueryHook(manual: boolean) {
return (query: any) => {
if (manual && is.func(query)) {
throw new Error('useRequest() does not accept a function');
throw new Error('useManualQuery() does not accept a function');
}

// Fetch response
Expand All @@ -38,7 +38,7 @@ function createFetcher(manual: boolean) {
const fns = React.useRef<ReturnType<Client['fetch']>>(null);

const client = useClient();
const request = useStableRequest(query);
const request = useRequest(query);

const fetch = React.useCallback(() => {
// bail out if request is not ready
Expand Down Expand Up @@ -95,7 +95,7 @@ function createFetcher(manual: boolean) {
}, [fetch]);

// Exposed interface
const exposed: Result & FetchOperations = {
const exposed: Result & QueryOperations = {
...response,
cancel,
refetch,
Expand All @@ -111,5 +111,5 @@ function createFetcher(manual: boolean) {
};
}

export const useQuery = createFetcher(false);
export const useRequest = createFetcher(true);
export const useQuery = createQueryHook(false);
export const useManualQuery = createQueryHook(true);
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import { renderHook } from '@testing-library/react-hooks';

// Ours
import { useStableRequest } from './useStableRequest';
import { useRequest } from './useRequest';

test('should return undefined if query is falsy', () => {
const { result, rerender } = renderHook((query) =>
useStableRequest(query)
);
const { result, rerender } = renderHook((query) => useRequest(query));

expect(result.current).toEqual(undefined);

Expand All @@ -24,7 +22,7 @@ test('should preserve object if id has not changed', () => {
};

const { result, rerender } = renderHook(
({ query }) => useStableRequest(query),
({ query }) => useRequest(query),
{ initialProps: { query } }
);

Expand Down Expand Up @@ -56,9 +54,7 @@ test('should preserve object if id has not changed', () => {

test('should not fail if error is thrown when resolving query', () => {
const request: any = null;
const { result } = renderHook(() =>
useStableRequest(() => request.query)
);
const { result } = renderHook(() => useRequest(() => request.query));

expect(result.current).toEqual(undefined);
expect(result.error).toEqual(undefined);
Expand Down
2 changes: 1 addition & 1 deletion src/react/useStableRequest.ts → src/react/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRef, useMemo } from 'react';
import is from '../utils/is';
import { createRequest, Request } from '../request';

export function useStableRequest(query: any): Request {
export function useRequest(query: any): Request {
const prev = useRef<Request>(undefined);

return useMemo(() => {
Expand Down

0 comments on commit d5ba65c

Please sign in to comment.