Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
registry-url: https://registry.npmjs.org/
- name: Install dependencies
uses: bahmutov/npm-install@v1
- run: yarn build
- run: yarn build && yarn build:types
- run: npx semantic-release@17
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ size-plugin.json
stats.html
.vscode/settings.json

!/types/index.d.ts
types/**/*.ts
types
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
"scripts": {
"test": "is-ci \"test:ci\" \"test:dev\"",
"test:dev": "npm run test:types && npm run test:eslint && jest --watch",
"test:ci": "npm run test:types && npm run test:eslint && jest && yarn dtslint",
"test:ci": "npm run test:types && npm run test:eslint && jest",
"test:coverage": "yarn test:ci; open coverage/lcov-report/index.html",
"test:types": "tsc",
"test:eslint": "eslint --ext .ts,.tsx ./src",
"gen:types": "tsc --project ./tsconfig.types.json",
"build": "NODE_ENV=production rollup -c",
"build:types": "tsc --project ./tsconfig.types.json && replace 'import type' 'import' ./types -r && replace 'export type' 'export' ./types -r",
"now-build": "yarn && cd www && yarn && yarn build",
"start": "rollup -c -w",
"format": "prettier {.,src,src/**,example/src,example/src/**,types}/*.{md,js,jsx,tsx,json} --write",
"stats": "open ./stats.html",
"dtslint": "dtslint types"
"stats": "open ./stats.html"
},
"release": {
"branches": [
Expand All @@ -44,9 +43,7 @@
"types",
"scripts"
],
"dependencies": {
"ts-toolbelt": "^6.9.4"
},
"dependencies": {},
"peerDependencies": {
"react": "^16.8.0"
},
Expand All @@ -66,7 +63,6 @@
"babel-jest": "^26.0.1",
"babel-plugin-transform-async-to-promises": "^0.8.15",
"cross-env": "^7.0.2",
"dtslint": "^3.6.12",
"eslint": "7.x",
"eslint-config-prettier": "^6.11.0",
"eslint-config-react-app": "^5.2.1",
Expand All @@ -87,6 +83,7 @@
"react": "^16.13.0",
"react-dom": "^16.13.1",
"react-error-boundary": "^2.2.2",
"replace": "^1.2.0",
"rollup": "^2.16.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
Expand Down
67 changes: 21 additions & 46 deletions src/core/queryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import { getDefaultedQueryConfig } from './config'
import { Query } from './query'
import {
QueryConfig,
QueryFunction,
QueryKey,
QueryKeyWithoutObject,
ReactQueryConfig,
QueryKeyWithoutArray,
QueryKeyWithoutObjectAndArray,
TupleQueryFunction,
TupleQueryKey,
TypedQueryFunction,
TypedQueryFunctionArgs,
} from './types'

// TYPES
Expand Down Expand Up @@ -49,13 +47,9 @@ type QueryPredicate = QueryKey | QueryPredicateFn | true

type QueryPredicateFn = (query: Query<unknown, unknown>) => boolean

export interface PrefetchQueryObjectConfig<
TResult,
TError,
TKey extends TupleQueryKey
> {
export interface PrefetchQueryObjectConfig<TResult, TError> {
queryKey: QueryKey
queryFn?: TupleQueryFunction<TResult, TKey>
queryFn?: QueryFunction<TResult>
config?: QueryConfig<TResult, TError>
options?: PrefetchQueryOptions
}
Expand Down Expand Up @@ -255,61 +249,42 @@ export class QueryCache {
}

// Parameter syntax with optional prefetch options
async prefetchQuery<TResult, TError, TKey extends QueryKeyWithoutObject>(
queryKey: TKey,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>

// Parameter syntax with config and optional prefetch options
async prefetchQuery<TResult, TError, TKey extends QueryKeyWithoutObject>(
queryKey: TKey,
config: QueryConfig<TResult, TError>,
async prefetchQuery<TResult = unknown, TError = unknown>(
queryKey: QueryKey,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>

// Parameter syntax with query function and optional prefetch options
async prefetchQuery<
TResult,
TError,
TKey extends QueryKeyWithoutObjectAndArray
>(
queryKey: TKey,
queryFn: TupleQueryFunction<TResult, [TKey]>,
async prefetchQuery<TResult, TError, TArgs extends TypedQueryFunctionArgs>(
queryKey: QueryKey,
queryFn: TypedQueryFunction<TResult, TArgs>,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>

async prefetchQuery<TResult, TError, TKey extends TupleQueryKey>(
queryKey: TKey,
queryFn: TupleQueryFunction<TResult, TKey>,
async prefetchQuery<TResult = unknown, TError = unknown>(
queryKey: QueryKey,
queryFn: QueryFunction<TResult>,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>

// Parameter syntax with query function, config and optional prefetch options
async prefetchQuery<
TResult,
TError,
TKey extends QueryKeyWithoutObjectAndArray
>(
queryKey: TKey,
queryFn: TupleQueryFunction<TResult, [TKey]>,
async prefetchQuery<TResult, TError, TArgs extends TypedQueryFunctionArgs>(
queryKey: QueryKey,
queryFn: TypedQueryFunction<TResult, TArgs>,
queryConfig: QueryConfig<TResult, TError>,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>

async prefetchQuery<TResult, TError, TKey extends TupleQueryKey>(
queryKey: TKey,
queryFn: TupleQueryFunction<TResult, TKey>,
async prefetchQuery<TResult = unknown, TError = unknown>(
queryKey: QueryKey,
queryFn: QueryFunction<TResult>,
queryConfig: QueryConfig<TResult, TError>,
options?: PrefetchQueryOptions
): Promise<TResult | undefined>

// Object syntax
async prefetchQuery<TResult, TError, TKey extends QueryKeyWithoutArray>(
config: PrefetchQueryObjectConfig<TResult, TError, [TKey]>
): Promise<TResult | undefined>

async prefetchQuery<TResult, TError, TKey extends TupleQueryKey>(
config: PrefetchQueryObjectConfig<TResult, TError, TKey>
async prefetchQuery<TResult = unknown, TError = unknown>(
config: PrefetchQueryObjectConfig<TResult, TError>
): Promise<TResult | undefined>

// Implementation
Expand Down
3 changes: 1 addition & 2 deletions src/core/tests/queryCache.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sleep, queryKey } from '../../react/tests/utils'
import { queryCache as defaultQueryCache } from '../'
import { makeQueryCache } from '../queryCache'
import { makeQueryCache, queryCache as defaultQueryCache } from '..'

describe('queryCache', () => {
test('setQueryData does not crash if query could not be found', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/tests/utils.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setConsole, queryCache } from '../'
import { deepEqual, replaceEqualDeep } from '../utils'
import { setConsole, queryCache } from '..'
import { queryKey } from '../../react/tests/utils'

describe('core/utils', () => {
Expand Down
32 changes: 12 additions & 20 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
import type { Query, FetchMoreOptions } from './query'
import type { QueryCache } from './queryCache'

export type QueryKeyObject =
export type QueryKey =
| boolean
| null
| number
| object
| { [key: string]: QueryKey }
| string
| undefined
| { [key: number]: QueryKey }

export type QueryKeyPrimitive = string | boolean | number | null | undefined

export type QueryKeyWithoutObjectAndArray = QueryKeyPrimitive

export type QueryKeyWithoutObject =
| QueryKeyWithoutObjectAndArray
| { [key: string]: QueryKey }
| readonly QueryKey[]

export type QueryKeyWithoutArray =
| QueryKeyWithoutObjectAndArray
| QueryKeyObject

export type QueryKey = QueryKeyWithoutObject | QueryKeyObject

export type ArrayQueryKey = QueryKey[]

export type QueryFunction<TResult> = (
...args: any[]
) => TResult | Promise<TResult>

// The tuple variants are only to infer types in the public API
export type TupleQueryKey = readonly [QueryKey, ...QueryKey[]]
export type TypedQueryFunction<
TResult,
TArgs extends TypedQueryFunctionArgs = TypedQueryFunctionArgs
> = (...args: TArgs) => TResult | Promise<TResult>

export type TupleQueryFunction<TResult, TKey extends TupleQueryKey> = (
...args: TKey
) => TResult | Promise<TResult>
export type TypedQueryFunctionArgs = readonly [unknown, ...unknown[]]

export type InitialDataFunction<TResult> = () => TResult | undefined

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './core/index'
export * from './react/index'
3 changes: 0 additions & 3 deletions src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export * from '../core/index'

// React
export {
ReactQueryCacheProvider,
useQueryCache,
Expand Down
11 changes: 3 additions & 8 deletions src/react/tests/ReactQueryCacheProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import React, { useEffect } from 'react'
import { render, waitFor } from '@testing-library/react'
import {
ReactQueryCacheProvider,
makeQueryCache,
queryCache,
useQuery,
useQueryCache,
} from '../index'

import { sleep, queryKey } from './utils'
import { QueryCache } from '../../core/queryCache'
import { ReactQueryCacheProvider, useQuery, useQueryCache } from '..'
import { makeQueryCache, queryCache, QueryCache } from '../../core'

describe('ReactQueryCacheProvider', () => {
test('when not used, falls back to global cache', async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/react/tests/ReactQueryConfigProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from 'react'
import { act, fireEvent, render, waitFor } from '@testing-library/react'
import { ReactQueryConfigProvider, useQuery, queryCache } from '../index'

import { sleep, queryKey } from './utils'
import { ReactQueryConfigProvider, useQuery } from '..'
import { queryCache } from '../../core'

describe('ReactQueryConfigProvider', () => {
// // See https://github.com/tannerlinsley/react-query/issues/105
Expand Down
10 changes: 3 additions & 7 deletions src/react/tests/ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
import React from 'react'
// @ts-ignore
import { renderToString } from 'react-dom/server'
import {
usePaginatedQuery,
ReactQueryCacheProvider,
queryCache,
makeQueryCache,
useQuery,
} from '../index'

import { sleep, queryKey } from './utils'
import { usePaginatedQuery, ReactQueryCacheProvider, useQuery } from '..'
import { queryCache, makeQueryCache } from '../../core'

describe('Server Side Rendering', () => {
// A frozen cache does not cache any data. This is the default
Expand Down
3 changes: 2 additions & 1 deletion src/react/tests/suspense.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { render, waitFor, fireEvent } from '@testing-library/react'
import { ErrorBoundary } from 'react-error-boundary'
import * as React from 'react'

import { useQuery, queryCache } from '../index'
import { sleep, queryKey } from './utils'
import { useQuery } from '..'
import { queryCache } from '../../core'

describe("useQuery's in Suspense mode", () => {
it('should not call the queryFn twice when used in Suspense mode', async () => {
Expand Down
16 changes: 8 additions & 8 deletions src/react/tests/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { render, waitFor, fireEvent } from '@testing-library/react'
import * as React from 'react'

import { useInfiniteQuery, useQueryCache } from '../index'
import { sleep, queryKey } from './utils'
import { InfiniteQueryResult } from '../../core/types'
import { useInfiniteQuery, useQueryCache } from '..'
import { InfiniteQueryResult } from '../../core'

interface Result {
items: number[]
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('useInfiniteQuery', () => {
function Page() {
const state = useInfiniteQuery(
key,
(_key: string, nextId = 0) => fetchItems(nextId, count++),
(_key: string, nextId?: number) => fetchItems(nextId || 0, count++),
{
getFetchMore: (lastGroup, _allGroups) => Boolean(lastGroup.nextId),
}
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('useInfiniteQuery', () => {
fetchMore,
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
} = useInfiniteQuery<Result, Error, [string, number]>(
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
Expand Down Expand Up @@ -308,7 +308,7 @@ describe('useInfiniteQuery', () => {
fetchMore,
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
} = useInfiniteQuery<Result, Error, [string, number]>(
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('useInfiniteQuery', () => {
fetchMore,
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
} = useInfiniteQuery<Result, Error, [string, number]>(
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('useInfiniteQuery', () => {
fetchMore,
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
} = useInfiniteQuery<Result, Error, [string, number]>(
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
Expand Down Expand Up @@ -572,7 +572,7 @@ describe('useInfiniteQuery', () => {
fetchMore,
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
} = useInfiniteQuery<Result, Error, [string, number]>(
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
Expand Down
2 changes: 1 addition & 1 deletion src/react/tests/useIsFetching.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render, fireEvent, waitFor } from '@testing-library/react'
import * as React from 'react'

import { useQuery, useIsFetching } from '../index'
import { sleep, queryKey } from './utils'
import { useQuery, useIsFetching } from '..'

describe('useIsFetching', () => {
// See https://github.com/tannerlinsley/react-query/issues/105
Expand Down
2 changes: 1 addition & 1 deletion src/react/tests/useMutation.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, fireEvent, waitFor } from '@testing-library/react'
import * as React from 'react'

import { useMutation } from '../index'
import { useMutation } from '..'

describe('useMutation', () => {
it('should be able to reset `data`', async () => {
Expand Down
Loading