| 
 | 1 | +import type { GenericFunction, GenericSchema, GenericSetofOption } from './common'  | 
 | 2 | + | 
 | 3 | +// Functions matching utils  | 
 | 4 | +type IsMatchingArgs<  | 
 | 5 | +  FnArgs extends GenericFunction['Args'],  | 
 | 6 | +  PassedArgs extends GenericFunction['Args']  | 
 | 7 | +> = [FnArgs] extends [Record<PropertyKey, never>]  | 
 | 8 | +  ? PassedArgs extends Record<PropertyKey, never>  | 
 | 9 | +    ? true  | 
 | 10 | +    : false  | 
 | 11 | +  : keyof PassedArgs extends keyof FnArgs  | 
 | 12 | +  ? PassedArgs extends FnArgs  | 
 | 13 | +    ? true  | 
 | 14 | +    : false  | 
 | 15 | +  : false  | 
 | 16 | + | 
 | 17 | +type MatchingFunctionArgs<  | 
 | 18 | +  Fn extends GenericFunction,  | 
 | 19 | +  Args extends GenericFunction['Args']  | 
 | 20 | +> = Fn extends { Args: infer A extends GenericFunction['Args'] }  | 
 | 21 | +  ? IsMatchingArgs<A, Args> extends true  | 
 | 22 | +    ? Fn  | 
 | 23 | +    : never  | 
 | 24 | +  : false  | 
 | 25 | + | 
 | 26 | +type FindMatchingFunctionByArgs<  | 
 | 27 | +  FnUnion,  | 
 | 28 | +  Args extends GenericFunction['Args']  | 
 | 29 | +> = FnUnion extends infer Fn extends GenericFunction ? MatchingFunctionArgs<Fn, Args> : false  | 
 | 30 | + | 
 | 31 | +// Types for working with database schemas  | 
 | 32 | +type TablesAndViews<Schema extends GenericSchema> = Schema['Tables'] & Exclude<Schema['Views'], ''>  | 
 | 33 | + | 
 | 34 | +// Utility types for working with unions  | 
 | 35 | +type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void  | 
 | 36 | +  ? I  | 
 | 37 | +  : never  | 
 | 38 | + | 
 | 39 | +type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R  | 
 | 40 | +  ? R  | 
 | 41 | +  : never  | 
 | 42 | + | 
 | 43 | +type IsAny<T> = 0 extends 1 & T ? true : false  | 
 | 44 | + | 
 | 45 | +type ExactMatch<T, S> = [T] extends [S] ? ([S] extends [T] ? true : false) : false  | 
 | 46 | + | 
 | 47 | +type ExtractExactFunction<Fns, Args> = Fns extends infer F  | 
 | 48 | +  ? F extends GenericFunction  | 
 | 49 | +    ? ExactMatch<F['Args'], Args> extends true  | 
 | 50 | +      ? F  | 
 | 51 | +      : never  | 
 | 52 | +    : never  | 
 | 53 | +  : never  | 
 | 54 | + | 
 | 55 | +type IsNever<T> = [T] extends [never] ? true : false  | 
 | 56 | + | 
 | 57 | +type RpcFunctionNotFound<FnName> = {  | 
 | 58 | +  Row: any  | 
 | 59 | +  Result: {  | 
 | 60 | +    error: true  | 
 | 61 | +  } & "Couldn't infer function definition matching provided arguments"  | 
 | 62 | +  RelationName: FnName  | 
 | 63 | +  Relationships: null  | 
 | 64 | +}  | 
 | 65 | + | 
 | 66 | +export type GetRpcFunctionFilterBuilderByArgs<  | 
 | 67 | +  Schema extends GenericSchema,  | 
 | 68 | +  FnName extends string & keyof Schema['Functions'],  | 
 | 69 | +  Args  | 
 | 70 | +> = {  | 
 | 71 | +  0: Schema['Functions'][FnName]  | 
 | 72 | +  // If the Args is exactly never (function call without any params)  | 
 | 73 | +  1: IsAny<Schema> extends true  | 
 | 74 | +    ? any  | 
 | 75 | +    : IsNever<Args> extends true  | 
 | 76 | +    ? // This is for retro compatibility, if the funcition is defined with an single return and an union of Args  | 
 | 77 | +      // we fallback to the last function definition matched by name  | 
 | 78 | +      IsNever<ExtractExactFunction<Schema['Functions'][FnName], Args>> extends true  | 
 | 79 | +      ? LastOf<Schema['Functions'][FnName]>  | 
 | 80 | +      : ExtractExactFunction<Schema['Functions'][FnName], Args>  | 
 | 81 | +    : Args extends Record<PropertyKey, never>  | 
 | 82 | +    ? LastOf<Schema['Functions'][FnName]>  | 
 | 83 | +    : // Otherwise, we attempt to match with one of the function definition in the union based  | 
 | 84 | +    // on the function arguments provided  | 
 | 85 | +    Args extends GenericFunction['Args']  | 
 | 86 | +    ? // This is for retro compatibility, if the funcition is defined with an single return and an union of Args  | 
 | 87 | +      // we fallback to the last function definition matched by name  | 
 | 88 | +      IsNever<LastOf<FindMatchingFunctionByArgs<Schema['Functions'][FnName], Args>>> extends true  | 
 | 89 | +      ? LastOf<Schema['Functions'][FnName]>  | 
 | 90 | +      : // Otherwise, we use the arguments based function definition narrowing to get the right value  | 
 | 91 | +        LastOf<FindMatchingFunctionByArgs<Schema['Functions'][FnName], Args>>  | 
 | 92 | +    : // If we can't find a matching function by args, we try to find one by function name  | 
 | 93 | +    ExtractExactFunction<Schema['Functions'][FnName], Args> extends GenericFunction  | 
 | 94 | +    ? ExtractExactFunction<Schema['Functions'][FnName], Args>  | 
 | 95 | +    : any  | 
 | 96 | +}[1] extends infer Fn  | 
 | 97 | +  ? // If we are dealing with an non-typed client everything is any  | 
 | 98 | +    IsAny<Fn> extends true  | 
 | 99 | +    ? { Row: any; Result: any; RelationName: FnName; Relationships: null }  | 
 | 100 | +    : // Otherwise, we use the arguments based function definition narrowing to get the rigt value  | 
 | 101 | +    Fn extends GenericFunction  | 
 | 102 | +    ? {  | 
 | 103 | +        Row: Fn['SetofOptions'] extends GenericSetofOption  | 
 | 104 | +          ? Fn['SetofOptions']['isSetofReturn'] extends true  | 
 | 105 | +            ? TablesAndViews<Schema>[Fn['SetofOptions']['to']]['Row']  | 
 | 106 | +            : TablesAndViews<Schema>[Fn['SetofOptions']['to']]['Row']  | 
 | 107 | +          : Fn['Returns'] extends any[]  | 
 | 108 | +          ? Fn['Returns'][number] extends Record<string, unknown>  | 
 | 109 | +            ? Fn['Returns'][number]  | 
 | 110 | +            : never  | 
 | 111 | +          : Fn['Returns'] extends Record<string, unknown>  | 
 | 112 | +          ? Fn['Returns']  | 
 | 113 | +          : never  | 
 | 114 | +        Result: Fn['SetofOptions'] extends GenericSetofOption  | 
 | 115 | +          ? Fn['SetofOptions']['isSetofReturn'] extends true  | 
 | 116 | +            ? Fn['SetofOptions']['isOneToOne'] extends true  | 
 | 117 | +              ? Fn['Returns'][]  | 
 | 118 | +              : Fn['Returns']  | 
 | 119 | +            : Fn['Returns']  | 
 | 120 | +          : Fn['Returns']  | 
 | 121 | +        RelationName: Fn['SetofOptions'] extends GenericSetofOption  | 
 | 122 | +          ? Fn['SetofOptions']['to']  | 
 | 123 | +          : FnName  | 
 | 124 | +        Relationships: Fn['SetofOptions'] extends GenericSetofOption  | 
 | 125 | +          ? Fn['SetofOptions']['to'] extends keyof Schema['Tables']  | 
 | 126 | +            ? Schema['Tables'][Fn['SetofOptions']['to']]['Relationships']  | 
 | 127 | +            : Schema['Views'][Fn['SetofOptions']['to']]['Relationships']  | 
 | 128 | +          : null  | 
 | 129 | +      }  | 
 | 130 | +    : // If we failed to find the function by argument, we still pass with any but also add an overridable  | 
 | 131 | +    Fn extends false  | 
 | 132 | +    ? RpcFunctionNotFound<FnName>  | 
 | 133 | +    : RpcFunctionNotFound<FnName>  | 
 | 134 | +  : RpcFunctionNotFound<FnName>  | 
0 commit comments