From 0770c51daa02fa04ff587016d1083d60ef56e2b0 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 16 Sep 2025 14:48:04 -0700 Subject: [PATCH] fix: reduce any type in generator.ts (#4710) --- .../src/plugins/open-api/generator.ts | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/better-auth/src/plugins/open-api/generator.ts b/packages/better-auth/src/plugins/open-api/generator.ts index 7c243617f0..7c2f56f47d 100644 --- a/packages/better-auth/src/plugins/open-api/generator.ts +++ b/packages/better-auth/src/plugins/open-api/generator.ts @@ -6,7 +6,11 @@ import type { } from "better-call"; import * as z from "zod"; import { getEndpoints } from "../../api"; -import { getAuthTables } from "../../db"; +import { + type FieldAttributeConfig, + type FieldType, + getAuthTables, +} from "../../db"; import type { AuthContext, BetterAuthOptions } from "../../types"; import type { FieldAttribute } from "../../db"; @@ -76,8 +80,20 @@ function getTypeFromZodType(zodType: z.ZodType) { return allowedType.has(type) ? (type as AllowedType) : "string"; } +type FieldSchema = { + type: FieldType; + default?: FieldAttributeConfig["defaultValue"] | "Generated at runtime"; + readOnly?: boolean; +}; + +type OpenAPIModelSchema = { + type: "object"; + properties: Record; + required?: string[]; +}; + function getFieldSchema(field: FieldAttribute) { - const schema: any = { + const schema: FieldSchema = { type: field.type === "date" ? "string" : field.type, }; @@ -325,11 +341,13 @@ export async function generator(ctx: AuthContext, options: BetterAuthOptions) { }); const tables = getAuthTables(options); - const models = Object.entries(tables).reduce((acc, [key, value]) => { + const models = Object.entries(tables).reduce< + Record + >((acc, [key, value]) => { const modelName = key.charAt(0).toUpperCase() + key.slice(1); const fields = value.fields; const required: string[] = []; - const properties: Record = { + const properties: Record = { id: { type: "string" }, }; Object.entries(fields).forEach(([fieldKey, fieldValue]) => { @@ -340,7 +358,6 @@ export async function generator(ctx: AuthContext, options: BetterAuthOptions) { } }); - // @ts-expect-error acc[modelName] = { type: "object", properties,