fix: improve additional fields inference ts server performance

This commit is contained in:
Bereket Engida
2025-01-21 19:42:34 +03:00
parent 249ccfc2f8
commit fe0661da08
4 changed files with 17 additions and 63 deletions

View File

@@ -8,7 +8,6 @@ import type {
BetterAuthOptions,
User,
} from "../../types";
import type { toZod } from "../../types/to-zod";
import { parseUserInput } from "../../db/schema";
import { BASE_ERROR_CODES } from "../../error/codes";
import { isDevelopment } from "../../utils/env";
@@ -18,13 +17,15 @@ export const signUpEmail = <O extends BetterAuthOptions>() =>
"/sign-up/email",
{
method: "POST",
body: z.record(z.string(), z.any()) as unknown as ZodObject<{
name: ZodString;
email: ZodString;
password: ZodString;
}> &
toZod<AdditionalUserFieldsInput<O>>,
body: z.record(z.string(), z.any()),
metadata: {
$Infer: {
body: {} as {
name: string;
email: string;
password: string;
} & AdditionalUserFieldsInput<O>,
},
openapi: {
description: "Sign up a user using email and password",
requestBody: {

View File

@@ -1,11 +1,10 @@
import { z, ZodNull, ZodObject, ZodOptional, ZodString } from "zod";
import { z } from "zod";
import { createAuthEndpoint } from "../call";
import { deleteSessionCookie, setSessionCookie } from "../../cookies";
import { getSessionFromCtx, sessionMiddleware } from "./session";
import { APIError } from "better-call";
import { createEmailVerificationToken } from "./email-verification";
import type { toZod } from "../../types/to-zod";
import type { AdditionalUserFieldsInput, BetterAuthOptions } from "../../types";
import { parseUserInput } from "../../db/schema";
import { generateRandomString } from "../../crypto";
@@ -17,15 +16,15 @@ export const updateUser = <O extends BetterAuthOptions>() =>
"/update-user",
{
method: "POST",
body: z.record(z.string(), z.any()) as unknown as toZod<
AdditionalUserFieldsInput<O>
> &
ZodObject<{
name: ZodOptional<ZodString>;
image: ZodOptional<ZodString | ZodNull>;
}>,
body: z.record(z.string(), z.any()),
use: [sessionMiddleware],
metadata: {
$Infer: {
body: {} as AdditionalUserFieldsInput<O> & {
name?: string;
image?: string | null;
},
},
openapi: {
description: "Update the current user",
requestBody: {

View File

@@ -153,7 +153,7 @@ export type InferFieldsInput<Field> = Field extends Record<
? never
: key]: InferFieldInput<Field[key]>;
} & {
[key in Key as Field[key]["input"] extends false ? never : key]:
[key in Key as Field[key]["input"] extends false ? never : key]?:
| InferFieldInput<Field[key]>
| undefined
| null;

View File

@@ -1,46 +0,0 @@
//https://github.com/colinhacks/tozod/blob/master/src/index.ts
import * as z from "zod";
type isAny<T> = [any extends T ? "true" : "false"] extends ["true"]
? true
: false;
type nonoptional<T> = T extends undefined ? never : T;
type nonnullable<T> = T extends null ? never : T;
type equals<X, Y> = [X] extends [Y] ? ([Y] extends [X] ? true : false) : false;
export type toZod<T> = {
any: never;
optional: z.ZodUnion<[toZod<nonoptional<T>>, z.ZodUndefined]>;
nullable: z.ZodUnion<[toZod<nonnullable<T>>, z.ZodNull]>;
array: T extends Array<infer U> ? z.ZodArray<toZod<U>> : never;
string: z.ZodString;
bigint: z.ZodBigInt;
number: z.ZodNumber;
boolean: z.ZodBoolean;
date: z.ZodDate;
object: z.ZodObject<{ [k in keyof T]: toZod<T[k]> }>;
rest: never;
}[zodKey<T>];
type zodKey<T> = isAny<T> extends true
? "any"
: equals<T, boolean> extends true //[T] extends [booleanUtil.Type]
? "boolean"
: [undefined] extends [T]
? "optional"
: [null] extends [T]
? "nullable"
: T extends any[]
? "array"
: equals<T, string> extends true
? "string"
: equals<T, bigint> extends true //[T] extends [bigintUtil.Type]
? "bigint"
: equals<T, number> extends true //[T] extends [numberUtil.Type]
? "number"
: equals<T, Date> extends true //[T] extends [dateUtil.Type]
? "date"
: T extends { [k: string]: any } //[T] extends [structUtil.Type]
? "object"
: "rest";