Skip to content

Commit

Permalink
feat: restrict type of option variables
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliao666 committed Oct 3, 2023
1 parent 5117acd commit 5ccd875
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 35 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
<a aria-label="NPM version" href="https://www.npmjs.com/package/quaere">
<img alt="" src="https://badgen.net/npm/v/quaere">
</a>
<a href="https://bundlephobia.com/package/quaere@latest" target="\_parent"><img src="https://badgen.net/bundlephobia/minzip/quaere" alt="Bundlephobia"></a>
<a href="https://bundlephobia.com/package/quaere@latest" target="\_parent"><img src="https://badgen.net/bundlephobia/tree-shaking/quaere" alt="Tree shaking available"></a>
<a href="https://unpkg.com/browse/quaere@latest/build/zip/zip.esm.js" rel="nofollow"><img src="https://img.badgesize.io/https:/unpkg.com/quaere@latest/build/zip/zip.esm.js?label=gzip%20size&compression=gzip" alt="gzip size"></a>
<a href="https://github.com/HuolalaTech/quaere"><img src="https://badgen.net/npm/types/quaere" alt="Types included" target="\_parent"></a>
<a href="https://github.com/HuolalaTech/quaere/blob/main/LICENSE"><img src="https://badgen.net/npm/license/quaere" alt="License" target="\_parent"></a>
<a href="https://github.com/HuolalaTech/quaere"><img src="https://img.shields.io/github/stars/HuolalaTech/quaere.svg?style=social&amp;label=Star" alt="GitHub Stars" target="\_parent"></a>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quaere",
"version": "0.0.8",
"version": "0.0.9",
"description": "A React Query wrapper for Suspense",
"author": "liaoliao666",
"repository": "https://github.com/HuolalaTech/quaere",
Expand Down
32 changes: 28 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import size from 'rollup-plugin-size'
import { terser } from 'rollup-plugin-terser'
import visualizer from 'rollup-plugin-visualizer'

const umdDevPlugin = type =>
const replaceDevPlugin = type =>
replace({
'process.env.NODE_ENV': `"${type}"`,
delimiters: ['', ''],
Expand Down Expand Up @@ -35,6 +35,7 @@ export default function rollup() {
cjs(options),
umdDev(options),
umdProd(options),
esmZip(options),
]
}

Expand Down Expand Up @@ -103,7 +104,7 @@ function umdDev({ input, external, globals, jsName }) {
babelPlugin,
commonJS(),
nodeResolve({ extensions }),
umdDevPlugin('development'),
replaceDevPlugin('development'),
],
}
}
Expand All @@ -124,7 +125,30 @@ function umdProd({ input, external, globals, jsName }) {
babelPlugin,
commonJS(),
nodeResolve({ extensions }),
umdDevPlugin('production'),
replaceDevPlugin('production'),
terser({
mangle: true,
compress: true,
}),
],
}
}

function esmZip({ input, external, globals, jsName }) {
return {
external,
input,
output: {
format: 'esm',
file: `build/zip/zip.esm.js`,
name: jsName,
globals,
},
plugins: [
babelPlugin,
commonJS(),
nodeResolve({ extensions }),
replaceDevPlugin('production'),
terser({
mangle: true,
compress: true,
Expand All @@ -136,7 +160,7 @@ function umdProd({ input, external, globals, jsName }) {
}),
visualizer({
filename: `build/stats.json`,
template: 'raw-data',
json: true,
gzipSize: true,
}),
],
Expand Down
8 changes: 5 additions & 3 deletions src/react/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ObservableQueryResult,
} from '../vanilla/observableQuery'
import { PrimitiveQuery } from '../vanilla/primitiveQuery'
import { GetVariablesOption } from '../vanilla/typeUtils'
import { useQueryClient } from './QueryClientProvider'
import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
import {
Expand All @@ -21,9 +22,10 @@ export type UseBseQueryOptions<
TError = Error,
TQueryData = TFetcherData,
TData = TFetcherData
> = ObservableQueryOptions<TFetcherData, TVars, TError, TQueryData, TData> & {
query: PrimitiveQuery<TFetcherData, TVars, TError, TQueryData>
}
> = ObservableQueryOptions<TFetcherData, TVars, TError, TQueryData, TData> &
GetVariablesOption<TVars> & {
query: PrimitiveQuery<TFetcherData, TVars, TError, TQueryData>
}

export type UseBaseQueryResult<
TData = unknown,
Expand Down
20 changes: 10 additions & 10 deletions src/react/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { ObservableInfiniteQueryResult } from '../vanilla/observableInfiniteQuer
import { ObservableQueryResult } from '../vanilla/observableQuery'
import { UseBseQueryOptions, useBaseQuery } from './useBaseQuery'

export interface UseQueryOptions<
export type UseQueryOptions<
TFetcherData = unknown,
TVars = unknown,
TError = Error,
TData = TFetcherData
> extends UseBseQueryOptions<TFetcherData, TVars, TError, TFetcherData, TData> {
> = UseBseQueryOptions<TFetcherData, TVars, TError, TFetcherData, TData> & {
query: Query<TFetcherData, TVars, TError>
}

Expand All @@ -17,18 +17,18 @@ export type UseQueryResult<
TError = unknown
> = ObservableQueryResult<TData, TError>

export interface UseInfiniteQueryOptions<
export type UseInfiniteQueryOptions<
TFetcherData = unknown,
TVars = unknown,
TError = Error,
TData = InfiniteData<TFetcherData>
> extends UseBseQueryOptions<
TFetcherData,
TVars,
TError,
InfiniteData<TFetcherData>,
TData
> {
> = UseBseQueryOptions<
TFetcherData,
TVars,
TError,
InfiniteData<TFetcherData>,
TData
> & {
query: InfiniteQuery<TFetcherData, TVars, TError>
}

Expand Down
12 changes: 6 additions & 6 deletions src/vanilla/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
QueryInfoState,
SetDataOptions,
} from './queryInfo'
import { DeepPartial, GetVariables, Updater } from './typeUtils'
import { DeepPartial, GetVariablesOption, Updater } from './typeUtils'
import { InfiniteData } from './types'
import {
UNDEFINED,
Expand Down Expand Up @@ -71,7 +71,7 @@ export type FetchQueryOptions<
QueryInfoOptions<TFetcherData, TVars, TError, TQueryData>,
'variables' | '_defaulted'
> &
GetVariables<TVars> & {
GetVariablesOption<TVars> & {
/**
* The time in milliseconds after data is considered stale.
* If the data is fresh it will be returned from the cache.
Expand All @@ -84,7 +84,7 @@ export type TriggerMutationOptions<
TVars = unknown,
TError = Error
> = Omit<MutationInfoOptions<TData, TVars, TError>, '_defaulted'> &
GetVariables<TVars>
GetVariablesOption<TVars>

export interface DefaultOptions {
queries?: Omit<ObservableQueryOptions<any, any, any>, 'query' | '_defaulted'>
Expand Down Expand Up @@ -215,7 +215,7 @@ export const createQueryClient = (config: QueryClientConfig = {}) => {
>(
filters: {
query: PrimitiveQuery<TFetcherData, TVars, TError, TQueryData>
} & GetVariables<TVars>
} & GetVariablesOption<TVars>
): QueryInfoState<TQueryData, TError> | undefined => {
return queryCache.find(
filters as {
Expand All @@ -233,7 +233,7 @@ export const createQueryClient = (config: QueryClientConfig = {}) => {
>(
filters: {
query: PrimitiveQuery<TFetcherData, TVars, TError, TQueryData>
} & GetVariables<TVars>
} & GetVariablesOption<TVars>
): TQueryData | undefined => {
return getQueryState(filters)?.data
}
Expand All @@ -246,7 +246,7 @@ export const createQueryClient = (config: QueryClientConfig = {}) => {
>(
filters: {
query: PrimitiveQuery<TFetcherData, TVars, TError, TQueryData>
} & GetVariables<TVars>,
} & GetVariablesOption<TVars>,
updater: Updater<TQueryData | undefined, TQueryData | undefined>,
setDataOptions?: SetDataOptions
) => {
Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type WithPatrial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

export type Override<A, B> = { [K in keyof A]: K extends keyof B ? B[K] : A[K] }

export type GetVariables<TVars> = TVars extends void
export type GetVariablesOption<TVars> = TVars extends void
? { variables?: TVars }
: { variables: TVars }

Expand Down
23 changes: 15 additions & 8 deletions src/vanilla/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Mutation } from "./mutation"
import { PrimitiveQuery } from "./primitiveQuery"
import { Mutation } from './mutation'
import { PrimitiveQuery } from './primitiveQuery'

export interface InfiniteData<TFetcherData> {
pages: TFetcherData[]
pageParams: number[]
}


export type inferVariables<T> = T extends PrimitiveQuery<any, infer TVariables, any,any>
export type inferVariables<T> = T extends PrimitiveQuery<
any,
infer TVariables,
any,
any
>
? TVariables
: T extends Mutation<any, infer TVariables, any>
? TVariables
Expand All @@ -19,10 +23,13 @@ export type inferData<T> = T extends PrimitiveQuery<infer TData, any, any, any>
? TData
: never

export type inferError<T> = T extends PrimitiveQuery<any, any, infer TError, any>
export type inferError<T> = T extends PrimitiveQuery<
any,
any,
infer TError,
any
>
? TError
: T extends Mutation<any, any, infer TError>
: T extends Mutation<any, any, infer TError>
? TError
: never


0 comments on commit 5ccd875

Please sign in to comment.