@astratra/saas-kit 1.0.0 → 1.0.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astratra/saas-kit",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Reusable backend starter kit that assembles Astratra core, security, and AI packages.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "dependencies": {
24
24
  "@astratra/ai": "^1.0.0",
25
25
  "@astratra/core": "^1.0.0",
26
- "@astratra/security": "^1.0.0",
26
+ "@astratra/security": "^1.0.1",
27
27
  "express": "^4.18.3",
28
28
  "express-validator": "^7.3.2",
29
29
  "jsonwebtoken": "^9.0.2"
package/src/app.js CHANGED
@@ -28,6 +28,10 @@ const DEFAULT_ROLES = {
28
28
 
29
29
  function normalizeOptions(options = {}) {
30
30
  const jwtSecret = options.jwtSecret || process.env.JWT_SECRET || DEFAULT_JWT_SECRET;
31
+ if (process.env.NODE_ENV === 'production' && jwtSecret === DEFAULT_JWT_SECRET) {
32
+ throw new Error('createSaasApp requires options.jwtSecret or JWT_SECRET in production.');
33
+ }
34
+
31
35
  const jwtAlgorithms = options.jwtAlgorithms || ['HS256'];
32
36
  const usersStore = options.usersStore || createMemoryUsersStore();
33
37
  const settingsStore = options.settingsStore || createMemorySettingsStore();
@@ -88,7 +92,8 @@ function createSaasApp(options = {}) {
88
92
 
89
93
  app.use('/users', authMiddleware, createUsersRoutes({
90
94
  usersStore: normalized.usersStore,
91
- authorizeAdmin
95
+ authorizeAdmin,
96
+ publicUserFields: normalized.publicUserFields
92
97
  }));
93
98
  app.use('/settings', authMiddleware, createSettingsRoutes({
94
99
  settingsStore: normalized.settingsStore,
package/src/index.d.ts CHANGED
@@ -30,6 +30,8 @@ export interface UsersStore {
30
30
  findById(id: string): Awaitable<SaasUser | null>;
31
31
  create(userData: SaasUser): Awaitable<SaasUser>;
32
32
  list(options?: UserListOptions): Awaitable<SaasUser[]>;
33
+ count?(options?: Pick<UserListOptions, 'role'>): Awaitable<number>;
34
+ countByRole?(): Awaitable<Record<string, number>>;
33
35
  update(id: string, patch: Partial<SaasUser>): Awaitable<SaasUser | null>;
34
36
  }
35
37
 
@@ -5,6 +5,18 @@ function createDashboardRoutes({ usersStore }) {
5
5
  const router = express.Router();
6
6
 
7
7
  router.get('/summary', asyncHandler(async (req, res) => {
8
+ if (typeof usersStore.count === 'function' && typeof usersStore.countByRole === 'function') {
9
+ const [userCount, roleBreakdown] = await Promise.all([
10
+ usersStore.count(),
11
+ usersStore.countByRole()
12
+ ]);
13
+
14
+ return apiResponse(res, 200, 'Dashboard summary', {
15
+ userCount,
16
+ roleBreakdown
17
+ });
18
+ }
19
+
8
20
  const users = await usersStore.list({ limit: Number.MAX_SAFE_INTEGER, offset: 0 });
9
21
  const roleBreakdown = users.reduce((counts, user) => {
10
22
  const role = user.role || 'unknown';
@@ -12,7 +12,7 @@ const userIdValidation = [
12
12
  param('id').notEmpty().withMessage('id is required')
13
13
  ];
14
14
 
15
- function createUsersRoutes({ usersStore, authorizeAdmin }) {
15
+ function createUsersRoutes({ usersStore, authorizeAdmin, publicUserFields }) {
16
16
  const router = express.Router();
17
17
 
18
18
  router.use(authorizeAdmin);
@@ -21,7 +21,7 @@ function createUsersRoutes({ usersStore, authorizeAdmin }) {
21
21
  const pagination = toPagination(req.query);
22
22
  const users = await usersStore.list(pagination);
23
23
  return apiResponse(res, 200, 'Users list', {
24
- items: users.map(sanitizeUser),
24
+ items: users.map((user) => sanitizeUser(user, publicUserFields)),
25
25
  limit: pagination.limit,
26
26
  offset: pagination.offset
27
27
  });
@@ -33,12 +33,12 @@ function createUsersRoutes({ usersStore, authorizeAdmin }) {
33
33
  throw new AppError('User not found.', 404);
34
34
  }
35
35
 
36
- return apiResponse(res, 200, 'User found', sanitizeUser(user));
36
+ return apiResponse(res, 200, 'User found', sanitizeUser(user, publicUserFields));
37
37
  }));
38
38
 
39
39
  router.post('/', validateMiddleware(createUserValidation), asyncHandler(async (req, res) => {
40
40
  const user = await usersStore.create(req.body);
41
- return apiResponse(res, 201, 'User created', sanitizeUser(user));
41
+ return apiResponse(res, 201, 'User created', sanitizeUser(user, publicUserFields));
42
42
  }));
43
43
 
44
44
  router.patch('/:id', validateMiddleware(userIdValidation), asyncHandler(async (req, res) => {
@@ -47,7 +47,7 @@ function createUsersRoutes({ usersStore, authorizeAdmin }) {
47
47
  throw new AppError('User not found.', 404);
48
48
  }
49
49
 
50
- return apiResponse(res, 200, 'User updated', sanitizeUser(user));
50
+ return apiResponse(res, 200, 'User updated', sanitizeUser(user, publicUserFields));
51
51
  }));
52
52
 
53
53
  return router;
@@ -65,6 +65,22 @@ function createMemoryUsersStore(options = {}) {
65
65
  const updated = { ...current, ...clone(patch), id: current.id };
66
66
  users.set(key, updated);
67
67
  return clone(updated);
68
+ },
69
+
70
+ async count({ role } = {}) {
71
+ let items = [...users.values()];
72
+ if (role) {
73
+ items = items.filter((user) => user.role === role);
74
+ }
75
+ return items.length;
76
+ },
77
+
78
+ async countByRole() {
79
+ return [...users.values()].reduce((counts, user) => {
80
+ const role = user.role || 'unknown';
81
+ counts[role] = (counts[role] || 0) + 1;
82
+ return counts;
83
+ }, {});
68
84
  }
69
85
  };
70
86
  }
package/src/utils.js CHANGED
@@ -12,15 +12,8 @@ function pickPublicUser(user, fields = DEFAULT_PUBLIC_USER_FIELDS) {
12
12
  }, {});
13
13
  }
14
14
 
15
- function sanitizeUser(user) {
16
- if (!user) return null;
17
-
18
- return Object.entries(user).reduce((safeUser, [key, value]) => {
19
- if (!SENSITIVE_USER_FIELDS.has(key)) {
20
- safeUser[key] = value;
21
- }
22
- return safeUser;
23
- }, {});
15
+ function sanitizeUser(user, fields = DEFAULT_PUBLIC_USER_FIELDS) {
16
+ return pickPublicUser(user, fields);
24
17
  }
25
18
 
26
19
  function assertAdapter(adapter, methods, name) {