UNPKG

@terrxo/query-key-factory

Version:

A library for creating standardized query keys, useful for cache management in @tanstack/query

211 lines (200 loc) 21.3 kB
import { QueryFunction, MutateFunction, QueryOptions } from '@tanstack/svelte-query'; /** * @internal */ type InternalKey = `_${string}`; /** * @internal */ type ExtractInternalKeys<T extends Record<string, unknown>> = Extract<keyof T, InternalKey>; type AnyMutableOrReadonlyArray = any[] | readonly any[]; type Tuple = [ValidValue | undefined, ...Array<ValidValue | undefined>]; type KeyTuple = Tuple | Readonly<Tuple>; type ValidValue = string | number | boolean | object | bigint; type Prettify<T> = { [K in keyof T]: T[K]; } & {}; type DefinitionKey<Key extends AnyMutableOrReadonlyArray> = { _def: readonly [...Key]; }; type ValidFactoryKey = 'queryKey' | 'queryFn' | 'contextQueries'; type StrictOptions<T> = T extends any[] ? T : keyof T extends ValidFactoryKey ? T : { [K in keyof T]: K extends ValidFactoryKey ? T[K] : never; }; type NullableQueryKeyRecord = Record<'queryKey', KeyTuple | null>; type QueryKeyRecord = Record<'queryKey', KeyTuple>; type KeySchemaWithContextualQueries = NullableQueryKeyRecord & { contextQueries: QueryFactorySchema; }; type $QueryFactorySchema = NullableQueryKeyRecord & { queryFn: QueryFunction; }; type QueryFactoryWithContextualQueriesSchema = NullableQueryKeyRecord & { queryFn: QueryFunction; contextQueries: QueryFactorySchema; }; type DynamicKeySchemaWithContextualQueries = QueryKeyRecord & { contextQueries: QueryFactorySchema; }; type DynamicQueryFactorySchema = QueryKeyRecord & { queryFn: QueryFunction; }; type DynamicQueryFactoryWithContextualQueriesSchema = QueryKeyRecord & { queryFn: QueryFunction; contextQueries: QueryFactorySchema; }; type FactoryProperty = null | KeyTuple | NullableQueryKeyRecord | KeySchemaWithContextualQueries | $QueryFactorySchema | QueryFactoryWithContextualQueriesSchema; type DynamicKey = (...args: any[]) => DynamicQueryFactoryWithContextualQueriesSchema | DynamicQueryFactorySchema | DynamicKeySchemaWithContextualQueries | QueryKeyRecord | KeyTuple; type QueryFactorySchema = Record<string, FactoryProperty | DynamicKey>; type InvalidSchema$1<Schema extends QueryFactorySchema> = Omit<Schema, InternalKey>; type ValidateFactory$1<Schema extends QueryFactorySchema> = ExtractInternalKeys<Schema> extends never ? { [P in keyof Schema]: Schema[P] extends (...args: infer Args) => infer R ? (...args: Args) => StrictOptions<R> : Schema[P]; } : InvalidSchema$1<Schema>; type ExtractNullableKey$1<Key extends KeyTuple | null | undefined> = Key extends [...infer Value] | readonly [...infer Value] ? Value : Key extends null | undefined | unknown ? null : never; type ComposeQueryKey$1<BaseKey extends AnyMutableOrReadonlyArray, Key> = Key extends KeyTuple ? readonly [...BaseKey, ...Key] : readonly [...BaseKey]; type QueryOptionsStruct<Keys extends AnyMutableOrReadonlyArray, Fetcher extends QueryFunction, FetcherResult extends ReturnType<Fetcher> = ReturnType<Fetcher>> = { queryKey: readonly [...Keys]; queryFn: QueryFunction<Awaited<FetcherResult>, readonly [...Keys]>; }; type FactoryWithContextualQueriesOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends KeySchemaWithContextualQueries | DynamicKeySchemaWithContextualQueries, SchemaQueryKey extends Schema['queryKey'] = Schema['queryKey'], ContextQueries extends Schema['contextQueries'] = Schema['contextQueries'], ComposedKey extends AnyMutableOrReadonlyArray = ComposeQueryKey$1<BaseKey, ExtractNullableKey$1<SchemaQueryKey>>> = SchemaQueryKey extends null ? Omit<QueryOptionsStruct<ComposedKey, QueryFunction>, 'queryFn'> & { _ctx: { [P in keyof ContextQueries]: ContextQueries[P] extends DynamicKey ? DynamicFactoryOutput<[...ComposedKey, P], ContextQueries[P]> : ContextQueries[P] extends FactoryProperty ? StaticFactoryOutput<[...ComposedKey, P], ContextQueries[P]> : never; }; } : Omit<QueryOptionsStruct<ComposedKey, QueryFunction>, 'queryFn'> & DefinitionKey<BaseKey> & { _ctx: { [P in keyof ContextQueries]: ContextQueries[P] extends DynamicKey ? DynamicFactoryOutput<[...ComposedKey, P], ContextQueries[P]> : ContextQueries[P] extends FactoryProperty ? StaticFactoryOutput<[...ComposedKey, P], ContextQueries[P]> : never; }; }; type FactoryQueryKeyRecordOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends NullableQueryKeyRecord | QueryKeyRecord, SchemaQueryKey extends Schema['queryKey'] = Schema['queryKey'], ComposedKey extends AnyMutableOrReadonlyArray = ComposeQueryKey$1<BaseKey, ExtractNullableKey$1<SchemaQueryKey>>> = SchemaQueryKey extends null ? Omit<QueryOptionsStruct<BaseKey, QueryFunction>, 'queryFn'> : Omit<QueryOptionsStruct<ComposedKey, QueryFunction>, 'queryFn'> & DefinitionKey<BaseKey>; type FactoryQueryOptionsOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends $QueryFactorySchema | DynamicQueryFactorySchema, SchemaQueryKey extends Schema['queryKey'] = Schema['queryKey'], QueryFn extends Schema['queryFn'] = Schema['queryFn'], ComposedKey extends AnyMutableOrReadonlyArray = ComposeQueryKey$1<BaseKey, ExtractNullableKey$1<SchemaQueryKey>>> = SchemaQueryKey extends null ? QueryOptionsStruct<BaseKey, QueryFn> : QueryOptionsStruct<ComposedKey, QueryFn> & DefinitionKey<BaseKey>; type FactoryQueryOptionsWithContextualQueriesOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends QueryFactoryWithContextualQueriesSchema | DynamicQueryFactoryWithContextualQueriesSchema, SchemaQueryKey extends Schema['queryKey'] = Schema['queryKey'], QueryFn extends Schema['queryFn'] = Schema['queryFn'], ContextQueries extends Schema['contextQueries'] = Schema['contextQueries'], Key extends AnyMutableOrReadonlyArray = ComposeQueryKey$1<BaseKey, ExtractNullableKey$1<SchemaQueryKey>>> = SchemaQueryKey extends null ? QueryOptionsStruct<Key, QueryFn> & { _ctx: { [P in keyof ContextQueries]: ContextQueries[P] extends DynamicKey ? DynamicFactoryOutput<[...Key, P], ContextQueries[P]> : ContextQueries[P] extends FactoryProperty ? StaticFactoryOutput<[...Key, P], ContextQueries[P]> : never; }; } : DefinitionKey<BaseKey> & QueryOptionsStruct<Key, QueryFn> & { _ctx: { [P in keyof ContextQueries]: ContextQueries[P] extends DynamicKey ? DynamicFactoryOutput<[...Key, P], ContextQueries[P]> : ContextQueries[P] extends FactoryProperty ? StaticFactoryOutput<[...Key, P], ContextQueries[P]> : never; }; }; type DynamicFactoryOutput<Keys extends AnyMutableOrReadonlyArray, Generator extends DynamicKey, Output extends ReturnType<Generator> = ReturnType<Generator>> = ((...args: Parameters<Generator>) => Output extends [...infer TupleResult] | readonly [...infer TupleResult] ? Omit<QueryOptionsStruct<[...Keys, ...TupleResult], QueryFunction>, 'queryFn'> : Output extends DynamicQueryFactoryWithContextualQueriesSchema ? Omit<FactoryQueryOptionsWithContextualQueriesOutput<Keys, Output>, '_def'> : Output extends DynamicQueryFactorySchema ? Omit<FactoryQueryOptionsOutput<Keys, Output>, '_def'> : Output extends DynamicKeySchemaWithContextualQueries ? Omit<FactoryWithContextualQueriesOutput<Keys, Output>, '_def'> : Output extends QueryKeyRecord ? Omit<FactoryQueryKeyRecordOutput<Keys, Output>, '_def'> : never) & DefinitionKey<Keys>; type AnyQueryFactoryOutputCallback = DynamicFactoryOutput<[string, ...any[]], DynamicKey>; type StaticFactoryOutput<Keys extends AnyMutableOrReadonlyArray, Property extends FactoryProperty> = Property extends null ? Omit<QueryOptionsStruct<Keys, QueryFunction>, 'queryFn'> : Property extends [...infer Result] | readonly [...infer Result] ? DefinitionKey<Keys> & Omit<QueryOptionsStruct<[...Keys, ...Result], QueryFunction>, 'queryFn'> : Property extends QueryFactoryWithContextualQueriesSchema ? FactoryQueryOptionsWithContextualQueriesOutput<Keys, Property> : Property extends $QueryFactorySchema ? FactoryQueryOptionsOutput<Keys, Property> : Property extends KeySchemaWithContextualQueries ? FactoryWithContextualQueriesOutput<Keys, Property> : Property extends NullableQueryKeyRecord ? FactoryQueryKeyRecordOutput<Keys, Property> : never; type FactoryOutput<Key extends string, Schema extends QueryFactorySchema> = DefinitionKey<[Key]> & { [P in keyof Schema]: Schema[P] extends DynamicKey ? DynamicFactoryOutput<[Key, P], Schema[P]> : Schema[P] extends FactoryProperty ? StaticFactoryOutput<[Key, P], Schema[P]> : never; }; type QueryKeyFactoryResult<Key extends string, Schema extends QueryFactorySchema> = FactoryOutput<Key, Schema>; type AnyQueryKeyFactoryResult = DefinitionKey<[string]> | QueryKeyFactoryResult<string, any>; type QueryKeyStoreSchema = Record<string, null | QueryFactorySchema>; type QueryKeyStore<StoreSchema extends QueryKeyStoreSchema> = { [P in keyof StoreSchema & string]: StoreSchema[P] extends QueryFactorySchema ? QueryKeyFactoryResult<P, StoreSchema[P]> : DefinitionKey<[P]>; }; declare function createQueryKeyStore<StoreSchema extends QueryKeyStoreSchema>(schema: StoreSchema): QueryKeyStore<StoreSchema>; type NullableMutationKeyRecord = Record<'mutationKey', KeyTuple | null>; type MutationKeyRecord = Record<'mutationKey', KeyTuple>; type MutationKeySchemaWithContextualMutations = NullableMutationKeyRecord & { contextMutations: MutationFactorySchema; }; type $MutationFactorySchema = NullableMutationKeyRecord & { mutationFn: MutateFunction; }; type MutationFactoryWithContextualMutationsSchema = NullableMutationKeyRecord & { mutationFn: MutateFunction; contextMutations: MutationFactorySchema; }; type DynamicMutationKeySchemaWithContextualMutations = MutationKeyRecord & { contextMutations: MutationFactorySchema; }; type DynamicMutationFactorySchema = MutationKeyRecord & { mutationFn: MutateFunction; }; type DynamicMutationFactoryWithContextualMutationsSchema = MutationKeyRecord & { mutationFn: MutateFunction; contextMutations: MutationFactorySchema; }; type MutationFactoryProperty = null | KeyTuple | NullableMutationKeyRecord | MutationKeySchemaWithContextualMutations | $MutationFactorySchema | MutationFactoryWithContextualMutationsSchema; type MutationDynamicKey = (...args: any[]) => DynamicMutationFactoryWithContextualMutationsSchema | DynamicMutationFactorySchema | DynamicMutationKeySchemaWithContextualMutations | MutationKeyRecord | KeyTuple; type MutationFactorySchema = Record<string, MutationFactoryProperty | MutationDynamicKey>; type InvalidSchema<Schema extends MutationFactorySchema> = Omit<Schema, InternalKey>; type ValidateFactory<Schema extends MutationFactorySchema> = Schema extends { [P in ExtractInternalKeys<Schema>]: Schema[P]; } ? InvalidSchema<Schema> : Schema; type ExtractNullableKey<Key extends KeyTuple | null | undefined> = Key extends [...infer Value] | readonly [...infer Value] ? Value : Key extends null | undefined | unknown ? null : never; type ComposeQueryKey<BaseKey extends AnyMutableOrReadonlyArray, Key> = Key extends KeyTuple ? readonly [...BaseKey, ...Key] : readonly [...BaseKey]; type MutationOptionsStruct<Keys extends AnyMutableOrReadonlyArray, Fetcher extends MutateFunction, FetcherResult extends ReturnType<Fetcher> = ReturnType<Fetcher>, FetcherVariables extends Parameters<Fetcher>[0] = Parameters<Fetcher>[0]> = { mutationKey: readonly [...Keys]; mutationFn: MutateFunction<Awaited<FetcherResult>, unknown, FetcherVariables>; }; type MutationFactoryWithContextualQueriesOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends MutationKeySchemaWithContextualMutations | DynamicMutationKeySchemaWithContextualMutations, SchemaMutationKey extends Schema['mutationKey'] = Schema['mutationKey'], ContextMutations extends Schema['contextMutations'] = Schema['contextMutations'], ComposedKey extends AnyMutableOrReadonlyArray = ComposeQueryKey<BaseKey, ExtractNullableKey<SchemaMutationKey>>> = SchemaMutationKey extends null ? Omit<MutationOptionsStruct<ComposedKey, MutateFunction>, 'mutationFn'> & { _ctx: { [P in keyof ContextMutations]: ContextMutations[P] extends MutationDynamicKey ? DynamicMutationFactoryOutput<[...ComposedKey, P], ContextMutations[P]> : ContextMutations[P] extends MutationFactoryProperty ? StaticMutationFactoryOutput<[...ComposedKey, P], ContextMutations[P]> : never; }; } : Omit<MutationOptionsStruct<ComposedKey, MutateFunction>, 'mutationFn'> & DefinitionKey<BaseKey> & { _ctx: { [P in keyof ContextMutations]: ContextMutations[P] extends MutationDynamicKey ? DynamicMutationFactoryOutput<[...ComposedKey, P], ContextMutations[P]> : ContextMutations[P] extends MutationFactoryProperty ? StaticMutationFactoryOutput<[...ComposedKey, P], ContextMutations[P]> : never; }; }; type FactoryMutationKeyRecordOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends NullableMutationKeyRecord | MutationKeyRecord, SchemaMutationKey extends Schema['mutationKey'] = Schema['mutationKey'], ComposedKey extends AnyMutableOrReadonlyArray = ComposeQueryKey<BaseKey, ExtractNullableKey<SchemaMutationKey>>> = SchemaMutationKey extends null ? Omit<MutationOptionsStruct<BaseKey, MutateFunction>, 'mutationFn'> : Omit<MutationOptionsStruct<ComposedKey, MutateFunction>, 'mutationFn'> & DefinitionKey<BaseKey>; type FactoryMutationOptionsOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends $MutationFactorySchema | DynamicMutationFactorySchema, SchemaQueryKey extends Schema['mutationKey'] = Schema['mutationKey'], MutationFn extends Schema['mutationFn'] = Schema['mutationFn'], ComposedKey extends AnyMutableOrReadonlyArray = ComposeQueryKey<BaseKey, ExtractNullableKey<SchemaQueryKey>>> = SchemaQueryKey extends null ? MutationOptionsStruct<BaseKey, MutationFn> : MutationOptionsStruct<ComposedKey, MutationFn> & DefinitionKey<BaseKey>; type FactoryMutationOptionsWithContextualQueriesOutput<BaseKey extends AnyMutableOrReadonlyArray, Schema extends MutationFactoryWithContextualMutationsSchema | DynamicMutationFactoryWithContextualMutationsSchema, SchemaQueryKey extends Schema['mutationKey'] = Schema['mutationKey'], MutationFn extends Schema['mutationFn'] = Schema['mutationFn'], ContextMutations extends Schema['contextMutations'] = Schema['contextMutations'], Key extends AnyMutableOrReadonlyArray = ComposeQueryKey<BaseKey, ExtractNullableKey<SchemaQueryKey>>> = SchemaQueryKey extends null ? MutationOptionsStruct<Key, MutationFn> & { _ctx: { [P in keyof ContextMutations]: ContextMutations[P] extends MutationDynamicKey ? DynamicMutationFactoryOutput<[...Key, P], ContextMutations[P]> : ContextMutations[P] extends MutationFactoryProperty ? StaticMutationFactoryOutput<[...Key, P], ContextMutations[P]> : never; }; } : DefinitionKey<BaseKey> & MutationOptionsStruct<Key, MutationFn> & { _ctx: { [P in keyof ContextMutations]: ContextMutations[P] extends MutationDynamicKey ? DynamicMutationFactoryOutput<[...Key, P], ContextMutations[P]> : ContextMutations[P] extends MutationFactoryProperty ? StaticMutationFactoryOutput<[...Key, P], ContextMutations[P]> : never; }; }; type DynamicMutationFactoryOutput<Keys extends AnyMutableOrReadonlyArray, Generator extends MutationDynamicKey, Output extends ReturnType<Generator> = ReturnType<Generator>> = ((...args: Parameters<Generator>) => Output extends [...infer TupleResult] | readonly [...infer TupleResult] ? Omit<MutationOptionsStruct<[...Keys, ...TupleResult], MutateFunction>, 'mutationFn'> : Output extends DynamicMutationFactoryWithContextualMutationsSchema ? Omit<FactoryMutationOptionsWithContextualQueriesOutput<Keys, Output>, '_def'> : Output extends DynamicMutationFactorySchema ? Omit<FactoryMutationOptionsOutput<Keys, Output>, '_def'> : Output extends DynamicMutationKeySchemaWithContextualMutations ? Omit<MutationFactoryWithContextualQueriesOutput<Keys, Output>, '_def'> : Output extends MutationKeyRecord ? Omit<FactoryMutationKeyRecordOutput<Keys, Output>, '_def'> : never) & DefinitionKey<Keys>; type AnyMutationFactoryOutputCallback = DynamicMutationFactoryOutput<[string, ...any[]], MutationDynamicKey>; type StaticMutationFactoryOutput<Keys extends AnyMutableOrReadonlyArray, Property extends MutationFactoryProperty> = Property extends null ? Omit<MutationOptionsStruct<Keys, MutateFunction>, 'mutationFn'> : Property extends [...infer Result] | readonly [...infer Result] ? DefinitionKey<Keys> & Omit<MutationOptionsStruct<[...Keys, ...Result], MutateFunction>, 'mutationFn'> : Property extends MutationFactoryWithContextualMutationsSchema ? FactoryMutationOptionsWithContextualQueriesOutput<Keys, Property> : Property extends $MutationFactorySchema ? FactoryMutationOptionsOutput<Keys, Property> : Property extends MutationKeySchemaWithContextualMutations ? MutationFactoryWithContextualQueriesOutput<Keys, Property> : Property extends NullableMutationKeyRecord ? FactoryMutationKeyRecordOutput<Keys, Property> : never; type MutationFactoryOutput<Key extends string, Schema extends MutationFactorySchema> = DefinitionKey<[Key]> & { [P in keyof Schema]: Schema[P] extends MutationDynamicKey ? DynamicMutationFactoryOutput<[Key, P], Schema[P]> : Schema[P] extends MutationFactoryProperty ? StaticMutationFactoryOutput<[Key, P], Schema[P]> : never; }; type MutationKeyFactoryResult<Key extends string, Schema extends MutationFactorySchema> = MutationFactoryOutput<Key, Schema>; type AnyMutationKeyFactoryResult = DefinitionKey<[string]> | MutationKeyFactoryResult<string, any>; /** * @deprecated the type inference for this function is broken and will be fixed in the next patch version * or possibly removed and implemented differently in a major version */ declare function createMutationKeys<Key extends string>(mutationDef: Key): DefinitionKey<[Key]>; /** * @deprecated the type inference for this function is broken and will be fixed in the next patch version * or possibly removed and implemented differently in a major version */ declare function createMutationKeys<Key extends string, Schema extends MutationFactorySchema>(mutationDef: Key, schema: ValidateFactory<Schema>): MutationKeyFactoryResult<Key, Schema>; declare function createQueryKeys<Key extends string>(queryDef: Key): DefinitionKey<[Key]>; declare function createQueryKeys<Key extends string, Schema extends QueryFactorySchema>(queryDef: Key, schema: ValidateFactory$1<Schema>): QueryKeyFactoryResult<Key, Schema>; type StoreFromMergedQueryKeys<QueryOrMutationKeyFactoryResults extends Array<AnyQueryKeyFactoryResult | AnyMutationKeyFactoryResult>> = QueryOrMutationKeyFactoryResults extends ([ infer First extends AnyQueryKeyFactoryResult | AnyMutationKeyFactoryResult, ...infer Rest extends AnyQueryKeyFactoryResult[] | AnyMutationKeyFactoryResult[] ]) ? { [P in First['_def'][0]]: First; } & StoreFromMergedQueryKeys<Rest> : {}; declare function mergeQueryKeys<QueryKeyFactoryResults extends Array<AnyQueryKeyFactoryResult | AnyMutationKeyFactoryResult>>(...schemas: QueryKeyFactoryResults): Prettify<StoreFromMergedQueryKeys<QueryKeyFactoryResults>>; type MergeInsertions<T> = T extends object ? { [K in keyof T]: MergeInsertions<T[K]>; } : T; type inferRecordMutationKeys<Target extends object> = { [P in Exclude<keyof Target, 'mutationFn'>]: Target[P] extends AnyMutableOrReadonlyArray ? Target[P] : Target[P] extends object ? { [K in keyof Target[P]]: inferSchemaProperty<Target[P][K]>; } : never; }; type inferRecordQueryKeys<Target extends object> = { [P in Exclude<keyof Target, 'queryFn'>]: Target[P] extends AnyMutableOrReadonlyArray ? Target[P] : Target[P] extends object ? { [K in keyof Target[P]]: inferSchemaProperty<Target[P][K]>; } : never; }; type inferSchemaProperty<Value> = Value extends AnyMutableOrReadonlyArray ? Value : Value extends StaticFactoryOutput<any[], any> ? inferRecordQueryKeys<Value> : Value extends StaticMutationFactoryOutput<any[], any> ? inferRecordMutationKeys<Value> : Value extends AnyQueryFactoryOutputCallback ? Record<'_def', Value['_def']> & inferRecordQueryKeys<ReturnType<Value>> : Value extends AnyMutationFactoryOutputCallback ? Record<'_def', Value['_def']> & inferRecordMutationKeys<ReturnType<Value>> : never; type inferQueryKeys<Schema extends AnyQueryKeyFactoryResult | AnyMutationKeyFactoryResult> = { [P in keyof Schema]: MergeInsertions<inferSchemaProperty<Schema[P]>>; }; type inferQueryKeyStore<Store extends QueryKeyStore<any>> = { [P in keyof Store]: inferQueryKeys<Store[P]>; }; type LooseQueryOptionsStruct = { queryKey: AnyMutableOrReadonlyArray; queryFn: QueryFunction<any, any>; }; type LooseQueryOptionsStructGenerator = (...args: any[]) => LooseQueryOptionsStruct; type TypedUseQueryOptions<Options extends LooseQueryOptionsStruct | LooseQueryOptionsStructGenerator, Data = Options extends LooseQueryOptionsStructGenerator ? Awaited<ReturnType<ReturnType<Options>['queryFn']>> : Options extends LooseQueryOptionsStruct ? Awaited<ReturnType<Options['queryFn']>> : never> = Options extends LooseQueryOptionsStructGenerator ? QueryOptions<Awaited<ReturnType<ReturnType<Options>['queryFn']>>, unknown, Data, ReturnType<Options>['queryKey']> : Options extends LooseQueryOptionsStruct ? QueryOptions<Awaited<ReturnType<Options['queryFn']>>, unknown, Data, Options['queryKey']> : never; export { type TypedUseQueryOptions, createMutationKeys, createQueryKeyStore, createQueryKeys, type inferQueryKeyStore, type inferQueryKeys, mergeQueryKeys };