react-query-kit 3.1.3 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/build/lib/createBaseQuery.esm.js +11 -21
- package/build/lib/createBaseQuery.esm.js.map +1 -1
- package/build/lib/createBaseQuery.js +11 -21
- package/build/lib/createBaseQuery.js.map +1 -1
- package/build/lib/createBaseQuery.mjs +11 -21
- package/build/lib/createBaseQuery.mjs.map +1 -1
- package/build/lib/router.d.ts +2 -1
- package/build/lib/router.esm.js +1 -1
- package/build/lib/router.esm.js.map +1 -1
- package/build/lib/router.js +1 -1
- package/build/lib/router.js.map +1 -1
- package/build/lib/router.mjs +1 -1
- package/build/lib/router.mjs.map +1 -1
- package/build/lib/types.d.ts +1 -1
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.esm.js +4 -4
- package/build/lib/utils.esm.js.map +1 -1
- package/build/lib/utils.js +4 -4
- package/build/lib/utils.js.map +1 -1
- package/build/lib/utils.mjs +4 -4
- package/build/lib/utils.mjs.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats.json +101 -101
- package/build/umd/index.development.js +16 -26
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/build/zip/zip.esm.js +1 -1
- package/package.json +1 -1
- package/src/createBaseQuery.ts +11 -27
- package/src/router.ts +5 -3
- package/src/types.ts +1 -1
- package/src/utils.ts +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.production.js","sources":["../../src/utils.ts","../../src/createBaseQuery.ts","../../src/createQuery.ts","../../src/createSuspenseQuery.ts","../../src/createInfiniteQuery.ts","../../src/createSuspenseInfiniteQuery.ts","../../src/createMutation.ts","../../src/router.ts"],"sourcesContent":["import * as TanstackReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const ReactQuery = TanstackReactQuery\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n useErrorBoundary: (_error: unknown, query: Query) =>\n typeof query.state.data === 'undefined',\n}\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [uses, opts]: [Middleware[], any] = [\n ReactQuery.useQueryClient(\n // @ts-ignore Compatible with ReactQuery v4\n isV5 ? queryClient : options\n ).getDefaultOptions()[type],\n defaultOptions,\n options,\n ].reduce(\n ([u1, o1], { use: u2 = [], ...o2 } = {}) => [\n [...u1, ...u2],\n { ...o1, ...o2 },\n ],\n [[]]\n )\n const composedHook = uses.reduceRight((next, use) => use(next), hook)\n\n return composedHook(opts, queryClient)\n }\n}\n\nexport const getKey = (queryKey: QueryKey, variables?: any): QueryKey => {\n return typeof variables === 'undefined' ? queryKey : [...queryKey, variables]\n}\n","import type {\n QueryClient,\n QueryFunctionContext,\n UseBaseQueryOptions,\n UseInfiniteQueryOptions,\n} from '@tanstack/react-query'\n\nimport type { AdditionalQueryOptions, Middleware } from './types'\nimport { getKey as getFullKey, withMiddleware } from './utils'\n\ninterface CreateBaseQueryOptions\n extends Omit<UseInfiniteQueryOptions, 'queryFn'>,\n AdditionalQueryOptions<any, any> {\n use?: Middleware[]\n variables?: any\n}\n\ntype QueryBaseHookOptions = Omit<\n UseBaseQueryOptions,\n 'queryKey' | 'queryFn'\n> & {\n fetcher?: any\n variables?: any\n}\n\nexport const createBaseQuery = (\n defaultOptions: any,\n useRQHook: (options: any, queryClient?: any) => any,\n overrideOptions?: Partial<UseInfiniteQueryOptions>\n): any => {\n if (process.env.NODE_ENV !== 'production') {\n // @ts-ignore\n if (defaultOptions.useDefaultOptions) {\n console.error(\n '[Bug] useDefaultOptions is not supported, please use middleware instead.'\n )\n }\n\n // @ts-ignore\n if (defaultOptions.queryFn) {\n console.error(\n '[Bug] queryFn is not supported, please use fetcher instead.'\n )\n }\n }\n\n const {\n queryKey,\n fetcher,\n queryKeyHashFn,\n getPreviousPageParam,\n getNextPageParam,\n initialPageParam,\n } = defaultOptions as CreateBaseQueryOptions\n\n const getQueryOptions = (fetcherFn: any, variables: any) => {\n return {\n queryFn: (context: QueryFunctionContext) => fetcherFn(variables, context),\n queryKey: getFullKey(queryKey, variables),\n }\n }\n\n const getKey = (variables?: any) => getFullKey(queryKey, variables)\n\n const getOptions = (variables: any) => {\n return {\n ...defaultOptions,\n ...getQueryOptions(fetcher, variables),\n }\n }\n\n const getFetchOptions = (variables: any) => {\n return {\n queryKeyHashFn,\n getPreviousPageParam,\n getNextPageParam,\n initialPageParam,\n ...getQueryOptions(fetcher, variables),\n }\n }\n\n const useBaseHook = (\n options: QueryBaseHookOptions,\n queryClient?: QueryClient\n ) => {\n return useRQHook(\n {\n ...options,\n ...overrideOptions,\n ...getQueryOptions(options.fetcher, options.variables),\n },\n queryClient\n )\n }\n\n return Object.assign(withMiddleware(useBaseHook, defaultOptions, 'queries'), {\n fetcher,\n getKey,\n getOptions,\n getFetchOptions,\n })\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type { CompatibleError, CreateQueryOptions, QueryHook } from './types'\nimport { ReactQuery } from './utils'\n\nexport const createQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError\n>(\n options: CreateQueryOptions<TFnData, TVariables, TError>\n): QueryHook<TFnData, TVariables, TError> => {\n return createBaseQuery(options, ReactQuery.useQuery)\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateSuspenseQueryOptions,\n SuspenseQueryHook,\n} from './types'\nimport { ReactQuery, isV5, suspenseOptions } from './utils'\n\nexport const createSuspenseQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError\n>(\n options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>\n): SuspenseQueryHook<TFnData, TVariables, TError> => {\n return isV5\n ? createBaseQuery(options, ReactQuery.useSuspenseQuery)\n : createBaseQuery(options, ReactQuery.useQuery, suspenseOptions)\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n InfiniteQueryHook,\n} from './types'\nimport { ReactQuery } from './utils'\n\nexport const createInfiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>\n): InfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return createBaseQuery(options, ReactQuery.useInfiniteQuery)\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateSuspenseInfiniteQueryOptions,\n SuspenseInfiniteQueryHook,\n} from './types'\nimport { ReactQuery, isV5, suspenseOptions } from './utils'\n\nexport const createSuspenseInfiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: CreateSuspenseInfiniteQueryOptions<\n TFnData,\n TVariables,\n TError,\n TPageParam\n >\n): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return isV5\n ? createBaseQuery(options, ReactQuery.useSuspenseInfiniteQuery)\n : createBaseQuery(options, ReactQuery.useInfiniteQuery, suspenseOptions)\n}\n","import type {\n CompatibleError,\n CreateMutationOptions,\n MutationHook,\n} from './types'\nimport { ReactQuery, withMiddleware } from './utils'\n\nexport const createMutation = <\n TData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>\n): MutationHook<TData, TVariables, TError, TContext> => {\n return Object.assign(\n withMiddleware(ReactQuery.useMutation, defaultOptions, 'mutations'),\n {\n getKey: () => defaultOptions.mutationKey,\n getOptions: () => defaultOptions,\n mutationFn: defaultOptions.mutationFn,\n }\n ) as MutationHook<TData, TVariables, TError, TContext>\n}\n","import { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterInfiniteQueryOptions,\n RouterMutation,\n RouterMutationOptions,\n RouterQuery,\n RouterQueryOptions,\n} from './types'\n\nconst buildRouter = (keys: string[], config: RouterConfig) => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n if (!opts._type) {\n acc[key] = buildRouter([...keys, key], opts)\n } else {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [...keys, key],\n }\n\n acc[key] =\n opts._type === `m`\n ? {\n useMutation: createMutation(options),\n ...createMutation(options),\n }\n : opts._type === `q`\n ? {\n useQuery: createQuery(options),\n useSuspenseQuery: createSuspenseQuery(options),\n ...createQuery(options),\n }\n : {\n useInfiniteQuery: createInfiniteQuery(options),\n useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),\n ...createInfiniteQuery(options),\n }\n }\n\n return acc\n },\n {\n getKey: () => keys,\n } as any\n )\n}\n\nexport const router = <TConfig extends RouterConfig>(\n key: string,\n config: TConfig\n): CreateRouter<TConfig> => {\n return buildRouter([key], config)\n}\n\nrouter.query = <TFnData, TVariables = void, TError = CompatibleError>(\n options: RouterQueryOptions<TFnData, TVariables, TError>\n) => {\n return {\n ...options,\n _type: 'q',\n } as RouterQuery<TFnData, TVariables, TError>\n}\n\nrouter.infiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: RouterInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>\n) => {\n return { ...options, _type: 'inf' } as RouterInfiniteQuery<\n TFnData,\n TVariables,\n TError,\n TPageParam\n >\n}\n\nrouter.mutation = <\n TFnData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n options: RouterMutationOptions<TFnData, TVariables, TError, TContext>\n) => {\n return { ...options, _type: 'm' } as RouterMutation<\n TFnData,\n TVariables,\n TError,\n TContext\n >\n}\n"],"names":["ReactQuery","isV5","useSuspenseQuery","suspenseOptions","enabled","suspense","useErrorBoundary","_error","query","state","data","withMiddleware","hook","defaultOptions","type","options","queryClient","uses","opts","useQueryClient","getDefaultOptions","reduce","u1","o1","use","u2","o2","reduceRight","next","composedHook","getKey","queryKey","variables","createBaseQuery","useRQHook","overrideOptions","fetcher","queryKeyHashFn","getPreviousPageParam","getNextPageParam","initialPageParam","getQueryOptions","fetcherFn","queryFn","context","getFullKey","Object","assign","getOptions","getFetchOptions","createQuery","useQuery","createSuspenseQuery","createInfiniteQuery","useInfiniteQuery","createSuspenseInfiniteQuery","useSuspenseInfiniteQuery","createMutation","useMutation","mutationKey","mutationFn","buildRouter","keys","config","entries","acc","key","_type","router","infiniteQuery","mutation"],"mappings":"4lBAKO,MAAMA,OAEAC,IAASD,EAAWE,iBAEpBC,EAAkB,CAC7BC,SAAS,EACTC,UAAU,EACVC,iBAAkB,CAACC,EAAiBC,SACN,IAArBA,EAAMC,MAAMC,MAGVC,EAAiB,CAC5BC,EACAC,EACAC,IAEO,SACLC,EACAC,GAEA,MAAOC,EAAMC,GAA6B,CACxClB,EAAWmB,eAETlB,EAAOe,EAAcD,GACrBK,oBAAoBN,GACtBD,EACAE,GACAM,QACA,EAAEC,EAAIC,IAAOC,IAAKC,EAAK,MAAOC,GAAO,CAAA,IAAO,CAC1C,IAAIJ,KAAOG,GACX,IAAKF,KAAOG,KAEd,CAAC,KAIH,OAFqBT,EAAKU,aAAY,CAACC,EAAMJ,IAAQA,EAAII,IAAOhB,EAEzDiB,CAAaX,EAAMF,IAIjBc,EAAS,CAACC,EAAoBC,SACb,IAAdA,EAA4BD,EAAW,IAAIA,EAAUC,GCrBxDC,EAAkB,CAC7BpB,EACAqB,EACAC,KAkBA,MAAMJ,SACJA,EADIK,QAEJA,EAFIC,eAGJA,EAHIC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pB,MADAG,EAAKiD,MACL,CACEhB,SAAUD,EAAYnC,GACtBb,iBAAkBkD,EAAoBrC,MACnCmC,EAAYnC,IAEjB,CACEuC,iBAAkBD,EAAoBtC,GACtCyC,yBAA0BD,EAA4BxC,MACnDsC,EAAoBtC,GAEhC,MAxBCkD,EAAIC,GAAOL,EAAY,IAAIC,EAAMI,GAAMhD,GA0BzC,OAAO+C,CAAP,GAEF,CACEnC,OAAQ,IAAMgC,IAKPM,EAAS,CACpBF,EACAH,IAEOF,EAAY,CAACK,GAAMH,GAG5BK,EAAO5D,MACLO,IAEO,IACFA,EACHoD,MAAO,MAIXC,EAAOC,cAMLtD,IAEO,IAAKA,EAASoD,MAAO,QAQ9BC,EAAOE,SAMLvD,IAEO,IAAKA,EAASoD,MAAO"}
|
|
1
|
+
{"version":3,"file":"index.production.js","sources":["../../src/utils.ts","../../src/createBaseQuery.ts","../../src/createQuery.ts","../../src/createSuspenseQuery.ts","../../src/createInfiniteQuery.ts","../../src/createSuspenseInfiniteQuery.ts","../../src/createMutation.ts","../../src/router.ts"],"sourcesContent":["import * as TanstackReactQuery from '@tanstack/react-query'\nimport type { Query, QueryClient, QueryKey } from '@tanstack/react-query'\n\nimport type { Middleware } from './types'\n\nexport const ReactQuery = TanstackReactQuery\n\nexport const isV5 = !!ReactQuery.useSuspenseQuery\n\nexport const suspenseOptions = {\n enabled: true,\n suspense: true,\n placeholderData: undefined,\n useErrorBoundary: (_error: unknown, query: Query) =>\n query.state.data === undefined,\n}\n\nexport const withMiddleware = (\n hook: any,\n defaultOptions: any,\n type: 'queries' | 'mutations'\n) => {\n return function useMiddleware(\n options?: { client?: QueryClient; use?: Middleware[] },\n queryClient?: QueryClient\n ) {\n const [uses, opts]: [Middleware[], any] = [\n ReactQuery.useQueryClient(\n // @ts-ignore Compatible with ReactQuery v4\n isV5 ? queryClient : options\n ).getDefaultOptions()[type],\n defaultOptions,\n options,\n ].reduce(\n ([u1, o1], { use: u2 = [], ...o2 } = {}) => [\n [...u1, ...u2],\n { ...o1, ...o2 },\n ],\n [[]]\n )\n\n return uses.reduceRight((next, use) => use(next), hook)(opts, queryClient)\n }\n}\n\nexport const getKey = (queryKey: QueryKey, variables?: any): QueryKey => {\n return variables === undefined ? queryKey : [...queryKey, variables]\n}\n","import type {\n QueryClient,\n QueryFunctionContext,\n UseBaseQueryOptions,\n UseInfiniteQueryOptions,\n} from '@tanstack/react-query'\n\nimport { getKey as getFullKey, withMiddleware } from './utils'\n\ntype QueryBaseHookOptions = Omit<\n UseBaseQueryOptions,\n 'queryKey' | 'queryFn'\n> & {\n fetcher?: any\n variables?: any\n}\n\nexport const createBaseQuery = (\n defaultOptions: any,\n useRQHook: (options: any, queryClient?: any) => any,\n overrideOptions?: Partial<UseInfiniteQueryOptions>\n): any => {\n if (process.env.NODE_ENV !== 'production') {\n // @ts-ignore\n if (defaultOptions.useDefaultOptions) {\n console.error(\n '[Bug] useDefaultOptions is not supported, please use middleware instead.'\n )\n }\n\n // @ts-ignore\n if (defaultOptions.queryFn) {\n console.error(\n '[Bug] queryFn is not supported, please use fetcher instead.'\n )\n }\n }\n\n const getQueryOptions = (fetcherFn: any, variables: any) => {\n return {\n queryFn: (context: QueryFunctionContext) => fetcherFn(variables, context),\n queryKey: getFullKey(defaultOptions.queryKey, variables),\n }\n }\n\n const getKey = (variables?: any) =>\n getFullKey(defaultOptions.queryKey, variables)\n\n const getOptions = (variables: any) => {\n return {\n ...defaultOptions,\n ...getQueryOptions(defaultOptions.fetcher, variables),\n }\n }\n\n const getFetchOptions = (variables: any) => {\n return {\n ...getQueryOptions(defaultOptions.fetcher, variables),\n queryKeyHashFn: defaultOptions.queryKeyHashFn,\n getPreviousPageParam: defaultOptions.getPreviousPageParam,\n getNextPageParam: defaultOptions.getNextPageParam,\n initialPageParam: defaultOptions.initialPageParam,\n }\n }\n\n const useBaseHook = (\n options: QueryBaseHookOptions,\n queryClient?: QueryClient\n ) => {\n return useRQHook(\n {\n ...options,\n ...getQueryOptions(options.fetcher, options.variables),\n ...overrideOptions,\n },\n queryClient\n )\n }\n\n return Object.assign(withMiddleware(useBaseHook, defaultOptions, 'queries'), {\n fetcher: defaultOptions.fetcher,\n getKey,\n getOptions,\n getFetchOptions,\n })\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type { CompatibleError, CreateQueryOptions, QueryHook } from './types'\nimport { ReactQuery } from './utils'\n\nexport const createQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError\n>(\n options: CreateQueryOptions<TFnData, TVariables, TError>\n): QueryHook<TFnData, TVariables, TError> => {\n return createBaseQuery(options, ReactQuery.useQuery)\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateSuspenseQueryOptions,\n SuspenseQueryHook,\n} from './types'\nimport { ReactQuery, isV5, suspenseOptions } from './utils'\n\nexport const createSuspenseQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError\n>(\n options: CreateSuspenseQueryOptions<TFnData, TVariables, TError>\n): SuspenseQueryHook<TFnData, TVariables, TError> => {\n return isV5\n ? createBaseQuery(options, ReactQuery.useSuspenseQuery)\n : createBaseQuery(options, ReactQuery.useQuery, suspenseOptions)\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateInfiniteQueryOptions,\n InfiniteQueryHook,\n} from './types'\nimport { ReactQuery } from './utils'\n\nexport const createInfiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: CreateInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>\n): InfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return createBaseQuery(options, ReactQuery.useInfiniteQuery)\n}\n","import { createBaseQuery } from './createBaseQuery'\nimport type {\n CompatibleError,\n CreateSuspenseInfiniteQueryOptions,\n SuspenseInfiniteQueryHook,\n} from './types'\nimport { ReactQuery, isV5, suspenseOptions } from './utils'\n\nexport const createSuspenseInfiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: CreateSuspenseInfiniteQueryOptions<\n TFnData,\n TVariables,\n TError,\n TPageParam\n >\n): SuspenseInfiniteQueryHook<TFnData, TVariables, TError, TPageParam> => {\n return isV5\n ? createBaseQuery(options, ReactQuery.useSuspenseInfiniteQuery)\n : createBaseQuery(options, ReactQuery.useInfiniteQuery, suspenseOptions)\n}\n","import type {\n CompatibleError,\n CreateMutationOptions,\n MutationHook,\n} from './types'\nimport { ReactQuery, withMiddleware } from './utils'\n\nexport const createMutation = <\n TData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n defaultOptions: CreateMutationOptions<TData, TVariables, TError, TContext>\n): MutationHook<TData, TVariables, TError, TContext> => {\n return Object.assign(\n withMiddleware(ReactQuery.useMutation, defaultOptions, 'mutations'),\n {\n getKey: () => defaultOptions.mutationKey,\n getOptions: () => defaultOptions,\n mutationFn: defaultOptions.mutationFn,\n }\n ) as MutationHook<TData, TVariables, TError, TContext>\n}\n","import { QueryKey } from '@tanstack/react-query'\n\nimport { createInfiniteQuery } from './createInfiniteQuery'\nimport { createMutation } from './createMutation'\nimport { createQuery } from './createQuery'\nimport { createSuspenseInfiniteQuery } from './createSuspenseInfiniteQuery'\nimport { createSuspenseQuery } from './createSuspenseQuery'\nimport type {\n CompatibleError,\n CreateRouter,\n RouterConfig,\n RouterInfiniteQuery,\n RouterInfiniteQueryOptions,\n RouterMutation,\n RouterMutationOptions,\n RouterQuery,\n RouterQueryOptions,\n} from './types'\n\nconst buildRouter = (keys: QueryKey, config: RouterConfig) => {\n return Object.entries(config).reduce(\n (acc, [key, opts]) => {\n if (!opts._type) {\n acc[key] = buildRouter([...keys, key], opts)\n } else {\n const options: any = {\n ...opts,\n [opts._type === `m` ? `mutationKey` : `queryKey`]: [...keys, key],\n }\n\n acc[key] =\n opts._type === `m`\n ? {\n useMutation: createMutation(options),\n ...createMutation(options),\n }\n : opts._type === `q`\n ? {\n useQuery: createQuery(options),\n useSuspenseQuery: createSuspenseQuery(options),\n ...createQuery(options),\n }\n : {\n useInfiniteQuery: createInfiniteQuery(options),\n useSuspenseInfiniteQuery: createSuspenseInfiniteQuery(options),\n ...createInfiniteQuery(options),\n }\n }\n\n return acc\n },\n {\n getKey: () => keys,\n } as any\n )\n}\n\nexport const router = <TConfig extends RouterConfig>(\n key: string | QueryKey,\n config: TConfig\n): CreateRouter<TConfig> => {\n return buildRouter(Array.isArray(key) ? key : [key], config)\n}\n\nrouter.query = <TFnData, TVariables = void, TError = CompatibleError>(\n options: RouterQueryOptions<TFnData, TVariables, TError>\n) => {\n return {\n ...options,\n _type: 'q',\n } as RouterQuery<TFnData, TVariables, TError>\n}\n\nrouter.infiniteQuery = <\n TFnData,\n TVariables = void,\n TError = CompatibleError,\n TPageParam = number\n>(\n options: RouterInfiniteQueryOptions<TFnData, TVariables, TError, TPageParam>\n) => {\n return { ...options, _type: 'inf' } as RouterInfiniteQuery<\n TFnData,\n TVariables,\n TError,\n TPageParam\n >\n}\n\nrouter.mutation = <\n TFnData = unknown,\n TVariables = void,\n TError = CompatibleError,\n TContext = unknown\n>(\n options: RouterMutationOptions<TFnData, TVariables, TError, TContext>\n) => {\n return { ...options, _type: 'm' } as RouterMutation<\n TFnData,\n TVariables,\n TError,\n TContext\n >\n}\n"],"names":["ReactQuery","isV5","useSuspenseQuery","suspenseOptions","enabled","suspense","placeholderData","undefined","useErrorBoundary","_error","query","state","data","withMiddleware","hook","defaultOptions","type","options","queryClient","uses","opts","useQueryClient","getDefaultOptions","reduce","u1","o1","use","u2","o2","reduceRight","next","getKey","queryKey","variables","createBaseQuery","useRQHook","overrideOptions","getQueryOptions","fetcherFn","queryFn","context","getFullKey","Object","assign","fetcher","getOptions","getFetchOptions","queryKeyHashFn","getPreviousPageParam","getNextPageParam","initialPageParam","createQuery","useQuery","createSuspenseQuery","createInfiniteQuery","useInfiniteQuery","createSuspenseInfiniteQuery","useSuspenseInfiniteQuery","createMutation","useMutation","mutationKey","mutationFn","buildRouter","keys","config","entries","acc","key","_type","router","Array","isArray","infiniteQuery","mutation"],"mappings":"4lBAKO,MAAMA,OAEAC,IAASD,EAAWE,iBAEpBC,EAAkB,CAC7BC,SAAS,EACTC,UAAU,EACVC,qBAAiBC,EACjBC,iBAAkB,CAACC,EAAiBC,SACbH,IAArBG,EAAMC,MAAMC,MAGHC,EAAiB,CAC5BC,EACAC,EACAC,IAEO,SACLC,EACAC,GAEA,MAAOC,EAAMC,GAA6B,CACxCpB,EAAWqB,eAETpB,EAAOiB,EAAcD,GACrBK,oBAAoBN,GACtBD,EACAE,GACAM,QACA,EAAEC,EAAIC,IAAOC,IAAKC,EAAK,MAAOC,GAAO,CAAA,IAAO,CAC1C,IAAIJ,KAAOG,GACX,IAAKF,KAAOG,KAEd,CAAC,KAGH,OAAOT,EAAKU,aAAY,CAACC,EAAMJ,IAAQA,EAAII,IAAOhB,EAA3CK,CAAiDC,EAAMF,IAIrDa,EAAS,CAACC,EAAoBC,SACpB1B,IAAd0B,EAA0BD,EAAW,IAAIA,EAAUC,GC7B/CC,EAAkB,CAC7BnB,EACAoB,EACAC,KAkBA,MAAMC,EAAkB,CAACC,EAAgBL,KAChC,CACLM,QAAUC,GAAkCF,EAAUL,EAAWO,GACjER,SAAUS,EAAW1B,EAAeiB,SAAUC,KAsClD,OAAOS,OAAOC,OAAO9B,GAdD,CAClBI,EACAC,IAEOiB,EACL,IACKlB,KACAoB,EAAgBpB,EAAQ2B,QAAS3B,EAAQgB,cACzCG,GAELlB,IAI6CH,EAAgB,WAAY,CAC3E6B,QAAS7B,EAAe6B,eAnCVX,GACdQ,EAAW1B,EAAeiB,SAAUC,GAoCpCY,WAlCkBZ,IACX,IACFlB,KACAsB,EAAgBtB,EAAe6B,QAASX,KAgC7Ca,gBA5BuBb,IAChB,IACFI,EAAgBtB,EAAe6B,QAASX,GAC3Cc,eAAgBhC,EAAegC,eAC/BC,qBAAsBjC,EAAeiC,qBACrCC,iBAAkBlC,EAAekC,iBACjCC,iBAAkBn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AAO,QAQ9BC,EAAOI,SAMLxD,IAEO,IAAKA,EAASmD,MAAO"}
|
package/build/zip/zip.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"@tanstack/react-query";const t=e,u=!!t.useSuspenseQuery,
|
|
1
|
+
import*as e from"@tanstack/react-query";const t=e,u=!!t.useSuspenseQuery,r={enabled:!0,suspense:!0,placeholderData:void 0,useErrorBoundary:(e,t)=>void 0===t.state.data},n=(e,r,n)=>function(s,a){const[i,y]=[t.useQueryClient(u?a:s).getDefaultOptions()[n],r,s].reduce((([e,t],{use:u=[],...r}={})=>[[...e,...u],{...t,...r}]),[[]]);return i.reduceRight(((e,t)=>t(e)),e)(y,a)},s=(e,t)=>void 0===t?e:[...e,t],a=(e,t,u)=>{const r=(t,u)=>({queryFn:e=>t(u,e),queryKey:s(e.queryKey,u)});return Object.assign(n(((e,n)=>t({...e,...r(e.fetcher,e.variables),...u},n)),e,"queries"),{fetcher:e.fetcher,getKey:t=>s(e.queryKey,t),getOptions:t=>({...e,...r(e.fetcher,t)}),getFetchOptions:t=>({...r(e.fetcher,t),queryKeyHashFn:e.queryKeyHashFn,getPreviousPageParam:e.getPreviousPageParam,getNextPageParam:e.getNextPageParam,initialPageParam:e.initialPageParam})})},i=e=>a(e,t.useQuery),y=e=>u?a(e,t.useSuspenseQuery):a(e,t.useQuery,r),o=e=>a(e,t.useInfiniteQuery),c=e=>u?a(e,t.useSuspenseInfiniteQuery):a(e,t.useInfiniteQuery,r),g=e=>Object.assign(n(t.useMutation,e,"mutations"),{getKey:()=>e.mutationKey,getOptions:()=>e,mutationFn:e.mutationFn}),p=(e,t)=>Object.entries(t).reduce(((t,[u,r])=>{if(r._type){const n={...r,["m"===r._type?"mutationKey":"queryKey"]:[...e,u]};t[u]="m"===r._type?{useMutation:g(n),...g(n)}:"q"===r._type?{useQuery:i(n),useSuspenseQuery:y(n),...i(n)}:{useInfiniteQuery:o(n),useSuspenseInfiniteQuery:c(n),...o(n)}}else t[u]=p([...e,u],r);return t}),{getKey:()=>e}),m=(e,t)=>p(Array.isArray(e)?e:[e],t);m.query=e=>({...e,_type:"q"}),m.infiniteQuery=e=>({...e,_type:"inf"}),m.mutation=e=>({...e,_type:"m"});export{o as createInfiniteQuery,g as createMutation,i as createQuery,c as createSuspenseInfiniteQuery,y as createSuspenseQuery,s as getKey,m as router};
|
package/package.json
CHANGED
package/src/createBaseQuery.ts
CHANGED
|
@@ -5,16 +5,8 @@ import type {
|
|
|
5
5
|
UseInfiniteQueryOptions,
|
|
6
6
|
} from '@tanstack/react-query'
|
|
7
7
|
|
|
8
|
-
import type { AdditionalQueryOptions, Middleware } from './types'
|
|
9
8
|
import { getKey as getFullKey, withMiddleware } from './utils'
|
|
10
9
|
|
|
11
|
-
interface CreateBaseQueryOptions
|
|
12
|
-
extends Omit<UseInfiniteQueryOptions, 'queryFn'>,
|
|
13
|
-
AdditionalQueryOptions<any, any> {
|
|
14
|
-
use?: Middleware[]
|
|
15
|
-
variables?: any
|
|
16
|
-
}
|
|
17
|
-
|
|
18
10
|
type QueryBaseHookOptions = Omit<
|
|
19
11
|
UseBaseQueryOptions,
|
|
20
12
|
'queryKey' | 'queryFn'
|
|
@@ -44,38 +36,30 @@ export const createBaseQuery = (
|
|
|
44
36
|
}
|
|
45
37
|
}
|
|
46
38
|
|
|
47
|
-
const {
|
|
48
|
-
queryKey,
|
|
49
|
-
fetcher,
|
|
50
|
-
queryKeyHashFn,
|
|
51
|
-
getPreviousPageParam,
|
|
52
|
-
getNextPageParam,
|
|
53
|
-
initialPageParam,
|
|
54
|
-
} = defaultOptions as CreateBaseQueryOptions
|
|
55
|
-
|
|
56
39
|
const getQueryOptions = (fetcherFn: any, variables: any) => {
|
|
57
40
|
return {
|
|
58
41
|
queryFn: (context: QueryFunctionContext) => fetcherFn(variables, context),
|
|
59
|
-
queryKey: getFullKey(queryKey, variables),
|
|
42
|
+
queryKey: getFullKey(defaultOptions.queryKey, variables),
|
|
60
43
|
}
|
|
61
44
|
}
|
|
62
45
|
|
|
63
|
-
const getKey = (variables?: any) =>
|
|
46
|
+
const getKey = (variables?: any) =>
|
|
47
|
+
getFullKey(defaultOptions.queryKey, variables)
|
|
64
48
|
|
|
65
49
|
const getOptions = (variables: any) => {
|
|
66
50
|
return {
|
|
67
51
|
...defaultOptions,
|
|
68
|
-
...getQueryOptions(fetcher, variables),
|
|
52
|
+
...getQueryOptions(defaultOptions.fetcher, variables),
|
|
69
53
|
}
|
|
70
54
|
}
|
|
71
55
|
|
|
72
56
|
const getFetchOptions = (variables: any) => {
|
|
73
57
|
return {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
58
|
+
...getQueryOptions(defaultOptions.fetcher, variables),
|
|
59
|
+
queryKeyHashFn: defaultOptions.queryKeyHashFn,
|
|
60
|
+
getPreviousPageParam: defaultOptions.getPreviousPageParam,
|
|
61
|
+
getNextPageParam: defaultOptions.getNextPageParam,
|
|
62
|
+
initialPageParam: defaultOptions.initialPageParam,
|
|
79
63
|
}
|
|
80
64
|
}
|
|
81
65
|
|
|
@@ -86,15 +70,15 @@ export const createBaseQuery = (
|
|
|
86
70
|
return useRQHook(
|
|
87
71
|
{
|
|
88
72
|
...options,
|
|
89
|
-
...overrideOptions,
|
|
90
73
|
...getQueryOptions(options.fetcher, options.variables),
|
|
74
|
+
...overrideOptions,
|
|
91
75
|
},
|
|
92
76
|
queryClient
|
|
93
77
|
)
|
|
94
78
|
}
|
|
95
79
|
|
|
96
80
|
return Object.assign(withMiddleware(useBaseHook, defaultOptions, 'queries'), {
|
|
97
|
-
fetcher,
|
|
81
|
+
fetcher: defaultOptions.fetcher,
|
|
98
82
|
getKey,
|
|
99
83
|
getOptions,
|
|
100
84
|
getFetchOptions,
|
package/src/router.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { QueryKey } from '@tanstack/react-query'
|
|
2
|
+
|
|
1
3
|
import { createInfiniteQuery } from './createInfiniteQuery'
|
|
2
4
|
import { createMutation } from './createMutation'
|
|
3
5
|
import { createQuery } from './createQuery'
|
|
@@ -15,7 +17,7 @@ import type {
|
|
|
15
17
|
RouterQueryOptions,
|
|
16
18
|
} from './types'
|
|
17
19
|
|
|
18
|
-
const buildRouter = (keys:
|
|
20
|
+
const buildRouter = (keys: QueryKey, config: RouterConfig) => {
|
|
19
21
|
return Object.entries(config).reduce(
|
|
20
22
|
(acc, [key, opts]) => {
|
|
21
23
|
if (!opts._type) {
|
|
@@ -54,10 +56,10 @@ const buildRouter = (keys: string[], config: RouterConfig) => {
|
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
export const router = <TConfig extends RouterConfig>(
|
|
57
|
-
key: string,
|
|
59
|
+
key: string | QueryKey,
|
|
58
60
|
config: TConfig
|
|
59
61
|
): CreateRouter<TConfig> => {
|
|
60
|
-
return buildRouter([key], config)
|
|
62
|
+
return buildRouter(Array.isArray(key) ? key : [key], config)
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
router.query = <TFnData, TVariables = void, TError = CompatibleError>(
|
package/src/types.ts
CHANGED
package/src/utils.ts
CHANGED
|
@@ -10,8 +10,9 @@ export const isV5 = !!ReactQuery.useSuspenseQuery
|
|
|
10
10
|
export const suspenseOptions = {
|
|
11
11
|
enabled: true,
|
|
12
12
|
suspense: true,
|
|
13
|
+
placeholderData: undefined,
|
|
13
14
|
useErrorBoundary: (_error: unknown, query: Query) =>
|
|
14
|
-
|
|
15
|
+
query.state.data === undefined,
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export const withMiddleware = (
|
|
@@ -37,12 +38,11 @@ export const withMiddleware = (
|
|
|
37
38
|
],
|
|
38
39
|
[[]]
|
|
39
40
|
)
|
|
40
|
-
const composedHook = uses.reduceRight((next, use) => use(next), hook)
|
|
41
41
|
|
|
42
|
-
return
|
|
42
|
+
return uses.reduceRight((next, use) => use(next), hook)(opts, queryClient)
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export const getKey = (queryKey: QueryKey, variables?: any): QueryKey => {
|
|
47
|
-
return
|
|
47
|
+
return variables === undefined ? queryKey : [...queryKey, variables]
|
|
48
48
|
}
|