hapta 1.0.10 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,7 +72,21 @@ type AuthOptions = PasswordAuthOptions | MfaAuthOptions | OtpAuthOptions | OAuth
72
72
  // Define the final function type
73
73
  // (Assuming it returns a Promise with the Principal type from our previous discussions)
74
74
  type AuthenticateFn = (options: AuthOptions) => Promise<Principal>;
75
- type RegisterFn = (data: any ) => Promise<boolean>
75
+ type FieldError = {
76
+ code: string;
77
+ message: string;
78
+ };
79
+
80
+ type RegisterResponse = {
81
+ success: boolean;
82
+ data?: any; // The created user record
83
+ error?: {
84
+ fieldErrors?: Record<string, FieldError>;
85
+ formErrors?: string[];
86
+ };
87
+ status: number;
88
+ };
89
+ type RegisterFn = (data: { username: string; email: string; password: string }) => Promise<RegisterResponse>;
76
90
  type TokenOptions = {
77
91
  payload: {
78
92
  id: string,
package/index.ts CHANGED
@@ -401,6 +401,43 @@ function validateToken(token: string) {
401
401
  return null;
402
402
  }
403
403
  }
404
+ async function Register(data: { username: string; email: string; password: string }) {
405
+ try {
406
+ //@ts-ignore
407
+ data.passwordConfirm = password
408
+ const response = await pb.collection("users").create(data);
409
+
410
+ // If successful, return structured success response
411
+ return {
412
+ success: true,
413
+ data: response,
414
+ status: 200,
415
+ };
416
+ } catch (error: any) {
417
+ // Initialize structured error object
418
+ const fieldErrors: Record<string, { code: string; message: string }> = {};
419
+
420
+ // PocketBase returns error.data for field errors
421
+ if (error?.data) {
422
+ for (const key in error.data) {
423
+ const fieldMsg = error.data[key];
424
+ fieldErrors[key] = {
425
+ code: "validation_not_unique", // or map from PocketBase error if available
426
+ message: Array.isArray(fieldMsg) ? fieldMsg.join(" ") : String(fieldMsg),
427
+ };
428
+ }
429
+ }
430
+
431
+ return {
432
+ success: false,
433
+ error: {
434
+ fieldErrors,
435
+ formErrors: [error?.message || "Failed to create record."],
436
+ },
437
+ status: error?.status || 400,
438
+ };
439
+ }
440
+ }
404
441
 
405
442
  async function buildRequestContext(req: Request, headers: Headers): Promise<Context> {
406
443
  const reqJSON = await req.json().catch(() => ({}));
@@ -439,6 +476,8 @@ async function buildRequestContext(req: Request, headers: Headers): Promise<Cont
439
476
  }
440
477
  }
441
478
  },
479
+ Register,
480
+
442
481
  },
443
482
  metadata: {
444
483
  requestID: crypto.randomUUID(),
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "hapta": "./index.ts"
5
5
  },
6
- "version": "1.0.10",
6
+ "version": "1.0.11",
7
7
  "description": "modular, scalable, and feature-rich backend framework designed to extend Pocketbase with authentication, schema validation, caching, and tenant-based service orchestration.",
8
8
  "dependencies": {
9
9
  "jsonwebtoken": "^9.0.3",