Skip to content

Commit 4bb3274

Browse files
committed
refactor: 💡 Users db list route now returns sanitized records
1 parent d067ddd commit 4bb3274

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

‎packages/db/src/users.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export interface User {
2323
* It validates that the input contains a nonempty `login` and `password`.
2424
*/
2525
export const UserInputSchema = z.object({
26-
login: z.string().nonempty(),
27-
password: z.union([z.string().nonempty(), z.string().startsWith('0x')]),
26+
login: z.string().min(1),
27+
password: z.union([z.string().min(1), z.string().startsWith('0x')]),
2828
});
2929

3030
/**
@@ -33,6 +33,31 @@ export const UserInputSchema = z.object({
3333
*/
3434
export type UserInputType = z.infer<typeof UserInputSchema>;
3535

36+
/**
37+
* User output schema
38+
*/
39+
export const SafeUserSchema = z.object({
40+
login: z.string().min(1),
41+
isAdmin: z.boolean().optional(),
42+
});
43+
44+
/**
45+
* Users list output schema
46+
*/
47+
export const UsersListOutputSchema = z.array(SafeUserSchema);
48+
49+
/**
50+
* Type definition for sanitized User record,
51+
* inferred from SafeUserSchema.
52+
*/
53+
export type SafeUserType = z.infer<typeof SafeUserSchema>;
54+
55+
/**
56+
* Type definition for sanitized Users records list,
57+
* inferred from UsersListOutputSchema.
58+
*/
59+
export type UsersListOutputSchema = z.infer<typeof UsersListOutputSchema>;
60+
3661
/**
3762
* Interface defining the properties of UsersDb initialization options.
3863
*/
@@ -130,7 +155,11 @@ export class UsersDb {
130155
* @throws Will throw an error if a user with the same login already exists
131156
* @memberof UsersDb
132157
*/
133-
async add(login: string, password: string, isAdmin = false): Promise<void> {
158+
async add(
159+
login: string,
160+
password: string,
161+
isAdmin: boolean = false,
162+
): Promise<void> {
134163
const knownUser = await this.storage.get<User>(`${this.prefix}${login}`);
135164

136165
// Check if the user already exists

‎packages/node-api/src/router/user.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { TRPCError } from '@trpc/server';
2-
import { User, UserInputSchema, comparePassword } from '@windingtree/sdk-db';
2+
import {
3+
User,
4+
UserInputSchema,
5+
UsersListOutputSchema,
6+
comparePassword,
7+
} from '@windingtree/sdk-db';
38
import {
49
router,
510
procedure,
@@ -40,20 +45,25 @@ export const userRouter = router({
4045
* List users records.
4146
* Throws an error if the user already exists.
4247
*/
43-
list: authAdminProcedure.query(async ({ ctx }) => {
44-
try {
45-
const { users } = ctx;
46-
const records = await users.getAll();
47-
logger.trace(`Listed #${records.length} users`);
48-
return records;
49-
} catch (error) {
50-
logger.error('user.list', error);
51-
throw new TRPCError({
52-
code: 'BAD_REQUEST',
53-
message: (error as Error).message,
54-
});
55-
}
56-
}),
48+
list: authAdminProcedure
49+
.output(UsersListOutputSchema)
50+
.query(async ({ ctx }) => {
51+
try {
52+
const { users } = ctx;
53+
const records = await users.getAll();
54+
logger.trace(`Listed #${records.length} users`);
55+
return records.map(({ login, isAdmin }) => ({
56+
login,
57+
isAdmin,
58+
}));
59+
} catch (error) {
60+
logger.error('user.list', error);
61+
throw new TRPCError({
62+
code: 'BAD_REQUEST',
63+
message: (error as Error).message,
64+
});
65+
}
66+
}),
5767

5868
/**
5969
* Log in an existing user.

0 commit comments

Comments
 (0)