UNPKG

better-auth-cloudflare

Version:

Seamlessly integrate better-auth with Cloudflare Workers, D1, Hyperdrive, KV, R2, and geolocation services.

413 lines (412 loc) 14.6 kB
import type { AuthContext } from "better-auth"; import type { FieldAttribute } from "better-auth/db"; import { z } from "zod"; import type { FileMetadata, R2Config } from "./types"; export declare const R2_ERROR_CODES: { readonly FILE_TOO_LARGE: "File is too large. Please choose a smaller file"; readonly INVALID_FILE_TYPE: "File type not supported. Please choose a different file"; readonly NO_FILE_PROVIDED: "Please select a file to upload"; readonly INVALID_REQUEST: "Invalid request. Please try again"; readonly R2_STORAGE_NOT_CONFIGURED: "File storage is temporarily unavailable. Please try again later"; readonly UPLOAD_FAILED: "Upload failed. Please check your connection and try again"; readonly FILE_ID_REQUIRED: "File not found"; readonly LIST_FILES_FAILED: "Unable to load your files. Please refresh the page"; readonly INVALID_METADATA: "Invalid file information. Please try uploading again"; readonly UPLOAD_ROLLBACK_FAILED: "Upload failed. Please try again"; readonly INVALID_FILE_RECORD: "File information is corrupted. Please contact support"; readonly DB_OPERATION_FAILED: "Service temporarily unavailable. Please try again later"; }; /** * Result type for validation operations */ export type ValidationResult<T> = { success: true; data: T; } | { success: false; error: string; code?: string; }; /** * Helper to create success result */ export declare const success: <T>(data: T) => ValidationResult<T>; /** * Helper to create error result */ export declare const error: (message: string, code?: string) => ValidationResult<never>; export declare const createFileMetadataSchema: (additionalFields?: Record<string, FieldAttribute>) => z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>> | z.ZodOptional<z.ZodObject<z.ZodRawShape, "strip", z.ZodTypeAny, { [x: string]: any; }, { [x: string]: any; }>>; export declare const fileIdSchema: z.ZodObject<{ fileId: z.ZodString; }, "strip", z.ZodTypeAny, { fileId: string; }, { fileId: string; }>; export declare const listFilesSchema: z.ZodOptional<z.ZodObject<{ limit: z.ZodOptional<z.ZodNumber>; cursor: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { limit?: number | undefined; cursor?: string | undefined; }, { limit?: number | undefined; cursor?: string | undefined; }>>; /** * Creates upload schema dynamically based on additionalFields configuration */ export declare const createUploadFileSchema: (additionalFields?: Record<string, FieldAttribute>) => z.ZodObject<z.ZodRawShape, "strip", z.ZodTypeAny, { [x: string]: any; }, { [x: string]: any; }>; /** * Validates file constraints using Result pattern */ export declare const createFileValidator: (config: R2Config) => { validateFile: (file: File, ctx?: AuthContext) => ValidationResult<true>; validateMetadata: (metadata: Record<string, any>, ctx?: AuthContext) => ValidationResult<Record<string, any> | undefined>; }; /** * Creates R2 storage utilities with Better Auth context for error handling and logging */ export declare const createR2Storage: (config: R2Config, generateId: (options: { model: string; size?: number; }) => string | false) => { /** * Uploads a file to R2 and returns metadata */ uploadFile(file: File | Blob, originalName: string, userId: string, ctx: AuthContext, customMetadata?: Record<string, any>, modelName?: string): Promise<FileMetadata>; /** * Downloads a file from R2 */ downloadFile(fileMetadata: FileMetadata, ctx: AuthContext): Promise<ReadableStream | null>; /** * Deletes a file from R2 */ deleteFile(fileMetadata: FileMetadata, ctx: AuthContext): Promise<void>; /** * Gets file metadata from R2 */ getFileInfo(r2Key: string): Promise<any>; /** * Lists files for a user */ listUserFiles(userId: string, ctx: AuthContext): Promise<{ objects: any[]; }>; }; /** * Creates R2 endpoints for Better Auth plugin */ export declare const createR2Endpoints: (getR2Storage: () => ReturnType<typeof createR2Storage> | null, r2Config?: R2Config) => { upload: { <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0?: ({ body?: undefined; } & { method?: "POST" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }) | undefined): Promise<[AsResponse] extends [true] ? Response : [ReturnHeaders] extends [true] ? { headers: Headers; response: { success: boolean; data: FileMetadata; }; } : { success: boolean; data: FileMetadata; }>; options: { method: "POST"; } & { use: any[]; }; path: "/files/upload-raw"; }; download: { <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: { body: { fileId: string; }; } & { method?: "POST" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }): Promise<[AsResponse] extends [true] ? Response : [ReturnHeaders] extends [true] ? { headers: Headers; response: Response; } : Response>; options: { method: "POST"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; name: string; emailVerified: boolean; email: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }; }; }>)[]; body: z.ZodObject<{ fileId: z.ZodString; }, "strip", z.ZodTypeAny, { fileId: string; }, { fileId: string; }>; } & { use: any[]; }; path: "/files/download"; }; delete: { <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: { body: { fileId: string; }; } & { method?: "POST" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }): Promise<[AsResponse] extends [true] ? Response : [ReturnHeaders] extends [true] ? { headers: Headers; response: { message: string; fileId: string; }; } : { message: string; fileId: string; }>; options: { method: "POST"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; name: string; emailVerified: boolean; email: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }; }; }>)[]; body: z.ZodObject<{ fileId: z.ZodString; }, "strip", z.ZodTypeAny, { fileId: string; }, { fileId: string; }>; } & { use: any[]; }; path: "/files/delete"; }; list: { <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0?: ({ body?: undefined; } & { method?: "GET" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }) | undefined): Promise<[AsResponse] extends [true] ? Response : [ReturnHeaders] extends [true] ? { headers: Headers; response: { files: FileMetadata[]; nextCursor: string | null; hasMore: boolean; }; } : { files: FileMetadata[]; nextCursor: string | null; hasMore: boolean; }>; options: { method: "GET"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; name: string; emailVerified: boolean; email: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }; path: "/files/list"; }; get: { <AsResponse extends boolean = false, ReturnHeaders extends boolean = false>(inputCtx_0: { body: { fileId: string; }; } & { method?: "POST" | undefined; } & { query?: Record<string, any> | undefined; } & { params?: Record<string, any>; } & { request?: Request; } & { headers?: HeadersInit; } & { asResponse?: boolean; returnHeaders?: boolean; use?: import("better-call").Middleware[]; path?: string; } & { asResponse?: AsResponse | undefined; returnHeaders?: ReturnHeaders | undefined; }): Promise<[AsResponse] extends [true] ? Response : [ReturnHeaders] extends [true] ? { headers: Headers; response: { data: {}; }; } : { data: {}; }>; options: { method: "POST"; use: ((inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; name: string; emailVerified: boolean; email: string; createdAt: Date; updatedAt: Date; image?: string | null | undefined; }; }; }>)[]; body: z.ZodObject<{ fileId: z.ZodString; }, "strip", z.ZodTypeAny, { fileId: string; }, { fileId: string; }>; } & { use: any[]; }; path: "/files/get"; }; };