type AsyncQuillQueryWithSchema<T> = (input: T) => Promise<unknown>;
type AsyncQuillQueryNoSchema = () => Promise<unknown>;
function createQuillRoute() {
const input = <T extends z.ZodTypeAny>(schema: T) => {
// define a fn that takes a query fn and returns a fn that parses it's input first
const typeSafeQueryInvoker = (
fn: AsyncQuillQueryWithSchema<z.infer<T>>,
) => {
return async (input: z.infer<typeof schema>) => {
try {
await schema.parseAsync(input);
return await fn(input);
} catch (error) {
throw error;
}
};
};
// return an object on that which is call-able
return {
query: typeSafeQueryInvoker,
// mutation: typeSafeMutationInvoker,
};
};
// we also want to define a query fn that doesn't take any input
const query = (fn: AsyncQuillQueryNoSchema) => {
return async () => fn();
};
return {
input,
query,
};
}
export const quill = () => ({
...createQuillRoute(),
});
type AsyncQuillQueryWithSchema<T> = (input: T) => Promise<unknown>;
type AsyncQuillQueryNoSchema = () => Promise<unknown>;
function createQuillRoute() {
const input = <T extends z.ZodTypeAny>(schema: T) => {
// define a fn that takes a query fn and returns a fn that parses it's input first
const typeSafeQueryInvoker = (
fn: AsyncQuillQueryWithSchema<z.infer<T>>,
) => {
return async (input: z.infer<typeof schema>) => {
try {
await schema.parseAsync(input);
return await fn(input);
} catch (error) {
throw error;
}
};
};
// return an object on that which is call-able
return {
query: typeSafeQueryInvoker,
// mutation: typeSafeMutationInvoker,
};
};
// we also want to define a query fn that doesn't take any input
const query = (fn: AsyncQuillQueryNoSchema) => {
return async () => fn();
};
return {
input,
query,
};
}
export const quill = () => ({
...createQuillRoute(),
});