better-auth-cloudflare
Version:
Seamlessly integrate better-auth with Cloudflare Workers, D1, Hyperdrive, KV, R2, and geolocation services.
43 lines (42 loc) • 1.21 kB
JavaScript
/**
* Helper to create a fully typed R2 config with automatic type inference
*
* Usage:
* ```ts
* const r2Config = createR2Config({
* bucket,
* maxFileSize: 10 * 1024 * 1024, // 10MB built-in validation
* allowedTypes: ['.jpg', '.png', '.pdf'], // Built-in file type validation
* additionalFields: {
* category: { type: "string" },
* isPublic: { type: "boolean" },
* priority: { type: "number" }
* },
* hooks: {
* upload: {
* before: (file, ctx) => {
* if (file.metadata.category === "restricted") return null; // business logic
* },
* after: (file, ctx) => {
* file.category // string (fully typed!)
* file.priority // number (fully typed!)
* sendNotification(file.userId, `Uploaded ${file.filename}`);
* }
* },
* download: {
* before: (file, ctx) => {
* if (!file.isPublic && file.userId !== ctx.session?.userId) return null;
* }
* },
* list: {
* before: (userId, ctx) => {
* if (!userHasPermission(userId, "list_files")) return null;
* }
* }
* }
* });
* ```
*/
export function createR2Config(config) {
return config;
}