@rebasepro/server-core 0.3.0 → 0.4.0

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.
Files changed (40) hide show
  1. package/dist/common/src/collections/default-collections.d.ts +5 -8
  2. package/dist/common/src/data/query_builder.d.ts +6 -2
  3. package/dist/index.es.js +318 -282
  4. package/dist/index.es.js.map +1 -1
  5. package/dist/index.umd.js +318 -282
  6. package/dist/index.umd.js.map +1 -1
  7. package/dist/server-core/src/api/rest/query-parser.d.ts +2 -0
  8. package/dist/server-core/src/api/types.d.ts +2 -1
  9. package/dist/server-core/src/email/types.d.ts +1 -0
  10. package/dist/server-core/src/init.d.ts +31 -1
  11. package/dist/types/src/controllers/auth.d.ts +2 -2
  12. package/dist/types/src/controllers/client.d.ts +25 -40
  13. package/dist/types/src/controllers/data.d.ts +21 -3
  14. package/dist/types/src/controllers/data_driver.d.ts +5 -0
  15. package/dist/types/src/controllers/email.d.ts +2 -0
  16. package/dist/types/src/types/auth_adapter.d.ts +3 -56
  17. package/dist/types/src/types/backend.d.ts +2 -2
  18. package/dist/types/src/types/backend_hooks.d.ts +2 -17
  19. package/dist/types/src/types/collections.d.ts +9 -5
  20. package/dist/types/src/types/entity_views.d.ts +19 -28
  21. package/dist/types/src/types/properties.d.ts +9 -7
  22. package/dist/types/src/types/user_management_delegate.d.ts +16 -53
  23. package/dist/types/src/users/index.d.ts +0 -1
  24. package/dist/types/src/users/user.d.ts +0 -1
  25. package/package.json +5 -5
  26. package/src/api/rest/api-generator.ts +11 -9
  27. package/src/api/rest/query-parser.ts +184 -63
  28. package/src/api/types.ts +2 -1
  29. package/src/auth/admin-routes.ts +2 -91
  30. package/src/auth/builtin-auth-adapter.ts +5 -55
  31. package/src/auth/custom-auth-adapter.ts +1 -1
  32. package/src/email/smtp-email-service.ts +31 -0
  33. package/src/email/types.ts +1 -0
  34. package/src/init.ts +136 -24
  35. package/src/storage/image-transform.ts +2 -1
  36. package/test/admin-routes.test.ts +0 -169
  37. package/test/backend-hooks-admin.test.ts +0 -25
  38. package/test/custom-auth-adapter.test.ts +2 -10
  39. package/test/smtp-email-service.test.ts +169 -0
  40. package/dist/types/src/users/roles.d.ts +0 -14
@@ -540,174 +540,5 @@ displayName: "Updated" }),
540
540
  });
541
541
  });
542
542
 
543
- // ── Role CRUD ───────────────────────────────────────────────────────
544
- describe("Role Management", () => {
545
- describe("GET /admin/roles", () => {
546
- it("returns list of roles", async () => {
547
- const app = createApp();
548
- mockAuthRepo.listRoles.mockResolvedValueOnce([
549
- mockRole("admin", true),
550
- mockRole("editor"),
551
- mockRole("viewer")
552
- ]);
553
-
554
- const res = await app.request("/admin/roles", { headers: { ...adminAuth() } });
555
- expect(res.status).toBe(200);
556
- const body = await res.json() as any;
557
- expect(body.roles).toHaveLength(3);
558
- expect(body.roles[0].isAdmin).toBe(true);
559
- });
560
- });
561
-
562
- describe("GET /admin/roles/:roleId", () => {
563
- it("returns role by ID", async () => {
564
- const app = createApp();
565
- mockAuthRepo.getRoleById.mockResolvedValueOnce(mockRole("admin", true));
566
-
567
- const res = await app.request("/admin/roles/admin", { headers: { ...adminAuth() } });
568
- expect(res.status).toBe(200);
569
- const body = await res.json() as any;
570
- expect(body.role.id).toBe("admin");
571
- expect(body.role.isAdmin).toBe(true);
572
- });
573
-
574
- it("returns 404 for non-existent role", async () => {
575
- const app = createApp();
576
- mockAuthRepo.getRoleById.mockResolvedValueOnce(null);
577
-
578
- const res = await app.request("/admin/roles/missing", { headers: { ...adminAuth() } });
579
- expect(res.status).toBe(404);
580
- });
581
- });
582
-
583
- describe("POST /admin/roles", () => {
584
- it("creates a new role", async () => {
585
- const app = createApp();
586
-
587
- const res = await app.request("/admin/roles", {
588
- ...json({ id: "custom",
589
- name: "Custom Role" }),
590
- headers: { ...json({}).headers,
591
- ...adminAuth() }
592
- });
593
- expect(res.status).toBe(201);
594
- const body = await res.json() as any;
595
- expect(body.role.id).toBe("custom");
596
- });
597
543
 
598
- it("returns 400 for missing id or name", async () => {
599
- const app = createApp();
600
-
601
- const res = await app.request("/admin/roles", {
602
- ...json({ id: "nope" }),
603
- headers: { ...json({}).headers,
604
- ...adminAuth() }
605
- });
606
- expect(res.status).toBe(400);
607
- });
608
-
609
- it("returns 409 when role already exists", async () => {
610
- const app = createApp();
611
- mockAuthRepo.getRoleById.mockResolvedValueOnce(mockRole("custom"));
612
-
613
- const res = await app.request("/admin/roles", {
614
- ...json({ id: "custom",
615
- name: "Dup" }),
616
- headers: { ...json({}).headers,
617
- ...adminAuth() }
618
- });
619
- expect(res.status).toBe(409);
620
- const body = await res.json() as any;
621
- expect(body.error.code).toBe("ROLE_EXISTS");
622
- });
623
- });
624
-
625
- describe("PUT /admin/roles/:roleId", () => {
626
- it("updates an existing role", async () => {
627
- const app = createApp();
628
- mockAuthRepo.getRoleById.mockResolvedValueOnce(mockRole("editor"));
629
-
630
- const res = await app.request("/admin/roles/editor", {
631
- method: "PUT",
632
- headers: { "Content-Type": "application/json",
633
- ...adminAuth() },
634
- body: JSON.stringify({ name: "Super Editor" })
635
- });
636
- expect(res.status).toBe(200);
637
- expect(mockAuthRepo.updateRole).toHaveBeenCalledWith("editor", expect.objectContaining({
638
- name: "Super Editor"
639
- }));
640
- });
641
-
642
- it("returns 404 for non-existent role", async () => {
643
- const app = createApp();
644
- mockAuthRepo.getRoleById.mockResolvedValueOnce(null);
645
-
646
- const res = await app.request("/admin/roles/missing", {
647
- method: "PUT",
648
- headers: { "Content-Type": "application/json",
649
- ...adminAuth() },
650
- body: JSON.stringify({ name: "Nope" })
651
- });
652
- expect(res.status).toBe(404);
653
- });
654
- });
655
-
656
- describe("DELETE /admin/roles/:roleId", () => {
657
- it("deletes a custom role", async () => {
658
- const app = createApp();
659
- mockAuthRepo.getRoleById.mockResolvedValueOnce(mockRole("custom"));
660
-
661
- const res = await app.request("/admin/roles/custom", {
662
- method: "DELETE",
663
- headers: { ...adminAuth() }
664
- });
665
- expect(res.status).toBe(200);
666
- expect(mockAuthRepo.deleteRole).toHaveBeenCalledWith("custom");
667
- });
668
-
669
- it("prevents deletion of built-in admin role", async () => {
670
- const app = createApp();
671
-
672
- const res = await app.request("/admin/roles/admin", {
673
- method: "DELETE",
674
- headers: { ...adminAuth() }
675
- });
676
- expect(res.status).toBe(400);
677
- const body = await res.json() as any;
678
- expect(body.error.code).toBe("BUILTIN_ROLE");
679
- });
680
-
681
- it("prevents deletion of built-in editor role", async () => {
682
- const app = createApp();
683
-
684
- const res = await app.request("/admin/roles/editor", {
685
- method: "DELETE",
686
- headers: { ...adminAuth() }
687
- });
688
- expect(res.status).toBe(400);
689
- });
690
-
691
- it("prevents deletion of built-in viewer role", async () => {
692
- const app = createApp();
693
-
694
- const res = await app.request("/admin/roles/viewer", {
695
- method: "DELETE",
696
- headers: { ...adminAuth() }
697
- });
698
- expect(res.status).toBe(400);
699
- });
700
-
701
- it("returns 404 for non-existent role", async () => {
702
- const app = createApp();
703
- mockAuthRepo.getRoleById.mockResolvedValueOnce(null);
704
-
705
- const res = await app.request("/admin/roles/ghost", {
706
- method: "DELETE",
707
- headers: { ...adminAuth() }
708
- });
709
- expect(res.status).toBe(404);
710
- });
711
- });
712
- });
713
544
  });
@@ -362,32 +362,7 @@ describe("BackendHooks — Admin Routes", () => {
362
362
  });
363
363
  });
364
364
 
365
- // ── roles.afterRead ─────────────────────────────────────────────────
366
- describe("roles.afterRead", () => {
367
- it("filters out roles from GET /admin/roles", async () => {
368
- const hooks: BackendHooks = {
369
- roles: {
370
- afterRead(role) {
371
- // Hide internal roles
372
- if (role.id === "internal") return null;
373
- return role;
374
- }
375
- }
376
- };
377
- const app = createApp(hooks);
378
- mockAuthRepo.listRoles.mockResolvedValueOnce([
379
- mockRole("admin", true),
380
- mockRole("internal"),
381
- mockRole("editor")
382
- ]);
383
365
 
384
- const res = await app.request("/admin/roles", { headers: { ...adminAuth() } });
385
- expect(res.status).toBe(200);
386
- const body = await res.json() as any;
387
- expect(body.roles).toHaveLength(2);
388
- expect(body.roles.map((r: any) => r.id)).toEqual(["admin", "editor"]);
389
- });
390
- });
391
366
 
392
367
  // ── no hooks (passthrough) ──────────────────────────────────────────
393
368
  describe("no hooks configured", () => {
@@ -143,7 +143,7 @@ describe("createCustomAuthAdapter", () => {
143
143
  expect(adapter.serviceKey).toBe("sk_live_123");
144
144
  });
145
145
 
146
- it("passes through userManagement and roleManagement when provided", () => {
146
+ it("passes through userManagement when provided", () => {
147
147
  const userMgmt = {
148
148
  getUser: jest.fn(),
149
149
  listUsers: jest.fn(),
@@ -151,27 +151,19 @@ describe("createCustomAuthAdapter", () => {
151
151
  updateUser: jest.fn(),
152
152
  deleteUser: jest.fn(),
153
153
  };
154
- const roleMgmt = {
155
- listRoles: jest.fn(),
156
- getUserRoles: jest.fn(),
157
- setUserRoles: jest.fn(),
158
- };
159
154
 
160
155
  const adapter = createCustomAuthAdapter({
161
156
  verifyRequest: async () => null,
162
157
  userManagement: userMgmt as unknown as CustomAuthAdapterOptions["userManagement"],
163
- roleManagement: roleMgmt as unknown as CustomAuthAdapterOptions["roleManagement"],
164
158
  });
165
159
 
166
160
  expect(adapter.userManagement).toBe(userMgmt);
167
- expect(adapter.roleManagement).toBe(roleMgmt);
168
161
  });
169
162
 
170
- it("omits userManagement and roleManagement when not provided", () => {
163
+ it("omits userManagement when not provided", () => {
171
164
  const adapter = createCustomAuthAdapter({
172
165
  verifyRequest: async () => null,
173
166
  });
174
167
  expect(adapter.userManagement).toBeUndefined();
175
- expect(adapter.roleManagement).toBeUndefined();
176
168
  });
177
169
  });
@@ -0,0 +1,169 @@
1
+ import { SMTPEmailService } from "../src/email/smtp-email-service";
2
+ import nodemailer from "nodemailer";
3
+
4
+ jest.mock("nodemailer", () => {
5
+ const mockTransporter = {
6
+ verify: jest.fn().mockResolvedValue(true),
7
+ sendMail: jest.fn().mockResolvedValue(true),
8
+ };
9
+ return {
10
+ createTransport: jest.fn().mockReturnValue(mockTransporter),
11
+ };
12
+ });
13
+
14
+ describe("SMTPEmailService", () => {
15
+ let mockCreateTransport: jest.Mock;
16
+ let mockTransporter: any;
17
+
18
+ beforeEach(() => {
19
+ jest.clearAllMocks();
20
+ mockCreateTransport = nodemailer.createTransport as jest.Mock;
21
+ mockTransporter = mockCreateTransport({} as any); // gets the mocked object
22
+ mockCreateTransport.mockClear();
23
+ });
24
+
25
+ it("should create transport with explicit name", () => {
26
+ new SMTPEmailService({
27
+ from: "test@example.com",
28
+ smtp: {
29
+ host: "smtp.example.com",
30
+ port: 587,
31
+ name: "explicit-hostname",
32
+ },
33
+ });
34
+
35
+ expect(mockCreateTransport).toHaveBeenCalledWith(
36
+ expect.objectContaining({
37
+ name: "explicit-hostname",
38
+ host: "smtp.example.com",
39
+ port: 587,
40
+ })
41
+ );
42
+ });
43
+
44
+ it("should infer name from FRONTEND_URL if smtp.name is undefined", () => {
45
+ const originalFrontendUrl = process.env.FRONTEND_URL;
46
+ process.env.FRONTEND_URL = "https://frontend-url.com/some-path";
47
+
48
+ try {
49
+ new SMTPEmailService({
50
+ from: "test@example.com",
51
+ smtp: {
52
+ host: "smtp.example.com",
53
+ port: 587,
54
+ },
55
+ });
56
+
57
+ expect(mockCreateTransport).toHaveBeenCalledWith(
58
+ expect.objectContaining({
59
+ name: "frontend-url.com",
60
+ })
61
+ );
62
+ } finally {
63
+ process.env.FRONTEND_URL = originalFrontendUrl;
64
+ }
65
+ });
66
+
67
+ it("should infer name from resetPasswordUrl if FRONTEND_URL and smtp.name are undefined", () => {
68
+ const originalFrontendUrl = process.env.FRONTEND_URL;
69
+ delete process.env.FRONTEND_URL;
70
+
71
+ try {
72
+ new SMTPEmailService({
73
+ from: "test@example.com",
74
+ smtp: {
75
+ host: "smtp.example.com",
76
+ port: 587,
77
+ },
78
+ resetPasswordUrl: "http://reset-password-url.org",
79
+ });
80
+
81
+ expect(mockCreateTransport).toHaveBeenCalledWith(
82
+ expect.objectContaining({
83
+ name: "reset-password-url.org",
84
+ })
85
+ );
86
+ } finally {
87
+ process.env.FRONTEND_URL = originalFrontendUrl;
88
+ }
89
+ });
90
+
91
+ it("should infer name from verifyEmailUrl if FRONTEND_URL, resetPasswordUrl and smtp.name are undefined", () => {
92
+ const originalFrontendUrl = process.env.FRONTEND_URL;
93
+ delete process.env.FRONTEND_URL;
94
+
95
+ try {
96
+ new SMTPEmailService({
97
+ from: "test@example.com",
98
+ smtp: {
99
+ host: "smtp.example.com",
100
+ port: 587,
101
+ },
102
+ verifyEmailUrl: "verify-email-url.net/auth",
103
+ });
104
+
105
+ expect(mockCreateTransport).toHaveBeenCalledWith(
106
+ expect.objectContaining({
107
+ name: "verify-email-url.net",
108
+ })
109
+ );
110
+ } finally {
111
+ process.env.FRONTEND_URL = originalFrontendUrl;
112
+ }
113
+ });
114
+
115
+ it("should leave name undefined if no URLs are provided", () => {
116
+ const originalFrontendUrl = process.env.FRONTEND_URL;
117
+ delete process.env.FRONTEND_URL;
118
+
119
+ try {
120
+ new SMTPEmailService({
121
+ from: "test@example.com",
122
+ smtp: {
123
+ host: "smtp.example.com",
124
+ port: 587,
125
+ },
126
+ });
127
+
128
+ expect(mockCreateTransport).toHaveBeenCalledWith(
129
+ expect.objectContaining({
130
+ name: undefined,
131
+ })
132
+ );
133
+ } finally {
134
+ process.env.FRONTEND_URL = originalFrontendUrl;
135
+ }
136
+ });
137
+
138
+ it("should successfully verify SMTP connection", async () => {
139
+ mockTransporter.verify.mockResolvedValueOnce(true);
140
+
141
+ const service = new SMTPEmailService({
142
+ from: "test@example.com",
143
+ smtp: {
144
+ host: "smtp.example.com",
145
+ port: 587,
146
+ },
147
+ });
148
+
149
+ const verified = await service.verifyConnection();
150
+ expect(verified).toBe(true);
151
+ expect(mockTransporter.verify).toHaveBeenCalled();
152
+ });
153
+
154
+ it("should return false if SMTP connection verification fails", async () => {
155
+ mockTransporter.verify.mockRejectedValueOnce(new Error("Connection timeout"));
156
+
157
+ const service = new SMTPEmailService({
158
+ from: "test@example.com",
159
+ smtp: {
160
+ host: "smtp.example.com",
161
+ port: 587,
162
+ },
163
+ });
164
+
165
+ const verified = await service.verifyConnection();
166
+ expect(verified).toBe(false);
167
+ expect(mockTransporter.verify).toHaveBeenCalled();
168
+ });
169
+ });
@@ -1,14 +0,0 @@
1
- export type Role = {
2
- /**
3
- * ID of the role
4
- */
5
- id: string;
6
- /**
7
- * Name of the role
8
- */
9
- name: string;
10
- /**
11
- * If this flag is true, the user can perform any action
12
- */
13
- isAdmin?: boolean;
14
- };