Skip to content

Commit

Permalink
chore: format files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorales committed Feb 9, 2024
1 parent 85309bf commit e68ca7d
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 198 deletions.
133 changes: 62 additions & 71 deletions src/create-mutation-keys.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,20 @@ export type MutationFactorySchema = Record<string, MutationFactoryProperty | Mut

type InvalidSchema<Schema extends MutationFactorySchema> = Omit<Schema, InternalKey>;

export type ValidateFactory<Schema extends MutationFactorySchema> = Schema extends {
[P in ExtractInternalKeys<Schema>]: Schema[P];
}
? InvalidSchema<Schema>
export 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
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 ComposeQueryKey<BaseKey extends AnyMutableOrReadonlyArray, Key> =
Key extends KeyTuple ? readonly [...BaseKey, ...Key] : readonly [...BaseKey];

export type MutationOptionsStruct<
Keys extends AnyMutableOrReadonlyArray,
Expand All @@ -90,24 +87,25 @@ type MutationFactoryWithContextualQueriesOutput<
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'> & {
> =
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;
[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;
[P in keyof ContextMutations]: ContextMutations[P] extends MutationDynamicKey ?
DynamicMutationFactoryOutput<[...ComposedKey, P], ContextMutations[P]>
: ContextMutations[P] extends MutationFactoryProperty ?
StaticMutationFactoryOutput<[...ComposedKey, P], ContextMutations[P]>
: never;
};
};

Expand All @@ -116,8 +114,8 @@ type FactoryMutationKeyRecordOutput<
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'>
> =
SchemaMutationKey extends null ? Omit<MutationOptionsStruct<BaseKey, MutateFunction>, 'mutationFn'>
: Omit<MutationOptionsStruct<ComposedKey, MutateFunction>, 'mutationFn'> & DefinitionKey<BaseKey>;

type FactoryMutationOptionsOutput<
Expand All @@ -126,8 +124,8 @@ type FactoryMutationOptionsOutput<
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>
> =
SchemaQueryKey extends null ? MutationOptionsStruct<BaseKey, MutationFn>
: MutationOptionsStruct<ComposedKey, MutationFn> & DefinitionKey<BaseKey>;

type FactoryMutationOptionsWithContextualQueriesOutput<
Expand All @@ -137,71 +135,64 @@ type FactoryMutationOptionsWithContextualQueriesOutput<
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> & {
> =
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;
[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;
[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) &
> = ((...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>;

export type AnyMutationFactoryOutputCallback = DynamicMutationFactoryOutput<[string, ...any[]], MutationDynamicKey>;

export 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>
> =
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;
[P in keyof Schema]: Schema[P] extends MutationDynamicKey ? DynamicMutationFactoryOutput<[Key, P], Schema[P]>
: Schema[P] extends MutationFactoryProperty ? StaticMutationFactoryOutput<[Key, P], Schema[P]>
: never;
};

export type MutationKeyFactoryResult<Key extends string, Schema extends MutationFactorySchema> = MutationFactoryOutput<
Expand Down
6 changes: 3 additions & 3 deletions src/create-query-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { omitPrototype } from './internals';
type QueryKeyStoreSchema = Record<string, null | QueryFactorySchema>;

export type QueryKeyStore<StoreSchema extends QueryKeyStoreSchema> = {
[P in keyof StoreSchema & string]: StoreSchema[P] extends QueryFactorySchema
? QueryKeyFactoryResult<P, StoreSchema[P]>
: DefinitionKey<[P]>;
[P in keyof StoreSchema & string]: StoreSchema[P] extends QueryFactorySchema ?
QueryKeyFactoryResult<P, StoreSchema[P]>
: DefinitionKey<[P]>;
};

export function createQueryKeyStore<StoreSchema extends QueryKeyStoreSchema>(
Expand Down
Loading

0 comments on commit e68ca7d

Please sign in to comment.