slonik-trpc
Version:
Slonik tRPC loader
50 lines (49 loc) • 4.68 kB
TypeScript
import { SqlFragment } from "slonik";
import { z } from "zod";
export declare type Interpretors<TFilter extends Record<string, z.ZodType>, TContext = any> = {
[x in keyof z.infer<z.ZodObject<TFilter>>]?: (filter: z.infer<TFilter[x]>, allFilters: z.infer<z.ZodObject<TFilter>>, // Does this need ZodRecursive or not?
context: TContext) => Promise<SqlFragment | null | undefined | false> | SqlFragment | null | undefined | false;
};
export declare type RecursiveFilterConditions<TFilter, TDisabled extends "AND" | "OR" | "NOT" = never> = TFilter & Omit<{
AND?: RecursiveFilterConditions<TFilter>[];
OR?: RecursiveFilterConditions<TFilter>[];
NOT?: RecursiveFilterConditions<TFilter>;
}, TDisabled>;
export declare type ZodPartial<TFilter extends Record<string, z.ZodType>> = z.ZodOptional<z.ZodObject<{
[k in keyof TFilter]: z.ZodOptional<TFilter[k]>;
}>>;
export declare type FilterOptions<TFilter extends Record<string, z.ZodType>, TContext = any> = {
/** Use this to pre-process any filters to make them consistent */
preprocess?: (filters: RecursiveFilterConditions<z.infer<ZodPartial<TFilter>>>, context: TContext) => z.infer<ZodPartial<TFilter>>;
/** Use this to add any extra conditions, e.g. for forced authorization checks */
postprocess?: (conditions: SqlFragment[], filters: RecursiveFilterConditions<z.infer<ZodPartial<TFilter>>>, context: TContext) => SqlFragment[];
};
/**
* Specify context type first
*/
export declare const createFilters: <TContext = any>() => <TFilter extends Record<string, z.ZodType<any, z.ZodTypeDef, any>>>(filters: TFilter, interpreters: Interpretors<TFilter, TContext>, options?: FilterOptions<TFilter, TContext> | undefined) => {
readonly filters: TFilter;
readonly interpreters: Interpretors<TFilter, TContext>;
readonly options: FilterOptions<TFilter, TContext> | undefined;
};
export declare type Filters<T extends Record<string, z.ZodType>, TContext> = {
filters: T;
interpreters: Interpretors<T, TContext>;
};
declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
/**
* Merges two or more filter declarations to allow easy composability.
* If options is specified, it overwrites the pre and post-processing, and the original functions are NOT called.
* Otherwise the original postprocessing and preprocessing of filters is kept, and the functions are called sequentially.
*/
export declare const mergeFilters: <TFilter extends Filters<any, TContext>, TContext = any>(filters: readonly TFilter[], options?: FilterOptions<TFilter["filters"], TContext> | undefined) => UnionToIntersection<TFilter>;
export declare function makeFilter<TFilter extends Record<string, z.ZodType>, TContext = any>(interpreters: Interpretors<TFilter>, options?: FilterOptions<TFilter>): (filter: { [k_2 in keyof z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>[k_2]; } & Omit<{
AND?: ({ [k_2 in keyof z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>[k_2]; } & Omit<any, never>)[] | undefined;
OR?: ({ [k_2 in keyof z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>[k_2]; } & Omit<any, never>)[] | undefined;
NOT?: ({ [k_2 in keyof z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>]: z.objectUtil.addQuestionMarks<{ [k_1 in keyof { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }]: { [k in keyof TFilter]: z.ZodOptional<TFilter[k]>; }[k_1]["_output"]; }>[k_2]; } & Omit<any, never>) | undefined;
}, never>, context?: TContext | undefined) => Promise<Readonly<{
type: "SLONIK_TOKEN_FRAGMENT";
sql: string;
values: import("slonik").PrimitiveValueExpression[];
}>>;
export {};