Skip to content

Commit

Permalink
feat(svelte-query): Simplify types (#5442)
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins authored May 21, 2023
1 parent cd84ed8 commit 9b285d9
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 95 deletions.
5 changes: 1 addition & 4 deletions packages/svelte-query-devtools/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"preserveValueImports": true,
"lib": ["esnext", "DOM", "DOM.Iterable"],
"moduleResolution": "node",
"module": "esnext",
Expand All @@ -23,8 +21,7 @@
"sourceMap": true,
"strict": true,
"strictNullChecks": true,
"target": "esnext",
"tsBuildInfoFile": "./build/.tsbuildinfo"
"target": "esnext"
},
"include": ["src/**/*.js", "src/**/*.ts", "src/**/*.svelte", ".eslintrc.cjs", "vite.config.ts"]
}
4 changes: 2 additions & 2 deletions packages/svelte-query/src/__tests__/CreateQuery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { QueryClient } from '@tanstack/query-core'
import { setQueryClientContext } from '../context'
import { createQuery } from '../createQuery'
import type { CreateQueryOptions, WritableOrVal } from '../types'
import type { CreateQueryOptions } from '../types'
export let options: WritableOrVal<CreateQueryOptions<any>>
export let options: CreateQueryOptions<any>
const queryClient = new QueryClient()
setQueryClientContext(queryClient)
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte-query/src/__tests__/createQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, waitFor } from '@testing-library/svelte'
import { writable } from 'svelte/store'
import CreateQuery from './CreateQuery.svelte'
import { sleep } from './utils'
import type { CreateQueryOptions, WritableOrVal } from '../types'
import type { CreateQueryOptions } from '../types'

describe('createQuery', () => {
it('Render and wait for success', async () => {
Expand All @@ -29,7 +29,7 @@ describe('createQuery', () => {
})

it('should keep previous data when returned as placeholder data', async () => {
const options: WritableOrVal<CreateQueryOptions> = writable({
const options: CreateQueryOptions = writable({
queryKey: ['test', [1]],
queryFn: async ({ queryKey }) => {
await sleep(10)
Expand Down
14 changes: 7 additions & 7 deletions packages/svelte-query/src/createBaseQuery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { QueryClient, QueryKey, QueryObserver } from '@tanstack/query-core'
import { notifyManager } from '@tanstack/query-core'
import type {
CreateBaseQueryOptions,
CreateBaseQueryResult,
WritableOrVal,
} from './types'
import type { CreateBaseQueryOptions, CreateBaseQueryResult } from './types'
import { useQueryClient } from './useQueryClient'
import { derived, get, readable, writable } from 'svelte/store'
import { isWritable } from './utils'
Expand All @@ -16,8 +12,12 @@ export function createBaseQuery<
TQueryData,
TQueryKey extends QueryKey,
>(
options: WritableOrVal<
CreateBaseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
options: CreateBaseQueryOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey
>,
Observer: typeof QueryObserver,
queryClient?: QueryClient,
Expand Down
17 changes: 7 additions & 10 deletions packages/svelte-query/src/createInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { InfiniteQueryObserver } from '@tanstack/query-core'
import type {
CreateInfiniteQueryOptions,
CreateInfiniteQueryResult,
WritableOrVal,
} from './types'
import { createBaseQuery } from './createBaseQuery'

Expand All @@ -20,15 +19,13 @@ export function createInfiniteQuery<
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: WritableOrVal<
CreateInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
>
options: CreateInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
>,
queryClient?: QueryClient,
): CreateInfiniteQueryResult<TData, TError> {
Expand Down
5 changes: 1 addition & 4 deletions packages/svelte-query/src/createMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
CreateMutateFunction,
CreateMutationOptions,
CreateMutationResult,
WritableOrVal,
} from './types'
import { useQueryClient } from './useQueryClient'
import { isWritable } from './utils'
Expand All @@ -16,9 +15,7 @@ export function createMutation<
TVariables = void,
TContext = unknown,
>(
options: WritableOrVal<
CreateMutationOptions<TData, TError, TVariables, TContext>
>,
options: CreateMutationOptions<TData, TError, TVariables, TContext>,
queryClient?: QueryClient,
): CreateMutationResult<TData, TError, TVariables, TContext> {
const client = useQueryClient(queryClient)
Expand Down
45 changes: 28 additions & 17 deletions packages/svelte-query/src/createQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@ import type {
QueryObserverResult,
DefaultError,
QueriesObserverOptions,
QueryObserverOptions,
} from '@tanstack/query-core'

import { notifyManager, QueriesObserver } from '@tanstack/query-core'
import { derived, get, readable, writable, type Readable } from 'svelte/store'

import type { CreateQueryOptions, WritableOrVal } from './types'
import type { WritableOrVal } from './types'
import { useQueryClient } from './useQueryClient'
import { isWritable } from './utils'

// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
// `placeholderData` function does not have a parameter
type CreateQueryOptionsForCreateQueries<
type QueryObserverOptionsForCreateQueries<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = Omit<
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
'placeholderData'
> & {
placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>
Expand All @@ -39,33 +40,38 @@ type GetOptions<T> =
error?: infer TError
data: infer TData
}
? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData>
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
? CreateQueryOptionsForCreateQueries<TQueryFnData, TError>
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
: T extends { data: infer TData; error?: infer TError }
? CreateQueryOptionsForCreateQueries<unknown, TError, TData>
? QueryObserverOptionsForCreateQueries<unknown, TError, TData>
: // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
T extends [infer TQueryFnData, infer TError, infer TData]
? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData>
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>
: T extends [infer TQueryFnData, infer TError]
? CreateQueryOptionsForCreateQueries<TQueryFnData, TError>
? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>
: T extends [infer TQueryFnData]
? CreateQueryOptionsForCreateQueries<TQueryFnData>
? QueryObserverOptionsForCreateQueries<TQueryFnData>
: // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
T extends {
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>
select: (data: any) => infer TData
}
? CreateQueryOptionsForCreateQueries<TQueryFnData, Error, TData, TQueryKey>
? QueryObserverOptionsForCreateQueries<
TQueryFnData,
Error,
TData,
TQueryKey
>
: T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }
? CreateQueryOptionsForCreateQueries<
? QueryObserverOptionsForCreateQueries<
TQueryFnData,
Error,
TQueryFnData,
TQueryKey
>
: // Fallback
CreateQueryOptionsForCreateQueries
QueryObserverOptionsForCreateQueries

type GetResults<T> =
// Part 1: responsible for mapping explicit type parameter to function result, if object
Expand Down Expand Up @@ -101,7 +107,7 @@ export type QueriesOptions<
Result extends any[] = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
? CreateQueryOptionsForCreateQueries[]
? QueryObserverOptionsForCreateQueries[]
: T extends []
? []
: T extends [infer Head]
Expand All @@ -112,15 +118,20 @@ export type QueriesOptions<
? T
: // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
// use this to infer the param types in the case of Array.map() argument
T extends CreateQueryOptionsForCreateQueries<
T extends QueryObserverOptionsForCreateQueries<
infer TQueryFnData,
infer TError,
infer TData,
infer TQueryKey
>[]
? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData, TQueryKey>[]
? QueryObserverOptionsForCreateQueries<
TQueryFnData,
TError,
TData,
TQueryKey
>[]
: // Fallback
CreateQueryOptionsForCreateQueries[]
QueryObserverOptionsForCreateQueries[]

/**
* QueriesResults reducer recursively maps type param to results
Expand All @@ -137,7 +148,7 @@ export type QueriesResults<
? [...Result, GetResults<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>
: T extends CreateQueryOptionsForCreateQueries<
: T extends QueryObserverOptionsForCreateQueries<
infer TQueryFnData,
infer TError,
infer TData,
Expand Down
13 changes: 3 additions & 10 deletions packages/svelte-query/src/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
DefinedCreateQueryResult,
CreateQueryOptions,
CreateQueryResult,
WritableOrVal,
} from './types'

type UndefinedInitialDataOptions<
Expand All @@ -32,9 +31,7 @@ export function createQuery<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: WritableOrVal<
UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
): CreateQueryResult<TData, TError>

Expand All @@ -44,9 +41,7 @@ export function createQuery<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: WritableOrVal<
DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
>,
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
): DefinedCreateQueryResult<TData, TError>

Expand All @@ -56,9 +51,7 @@ export function createQuery<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: WritableOrVal<
CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
>,
options: CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
) {
return createBaseQuery(options, QueryObserver, queryClient)
Expand Down
Loading

0 comments on commit 9b285d9

Please sign in to comment.