Skip to content

Commit ee868b6

Browse files
authored
chore(shared): export page and user endpoints as resource endpoints (#1560)
1 parent 66e3622 commit ee868b6

File tree

5 files changed

+82
-79
lines changed

5 files changed

+82
-79
lines changed

packages/@liexp/shared/src/endpoints/User.endpoints.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ import {
1212
} from "../io/http/User.js";
1313
import { ResourceEndpoints } from "./types.js";
1414

15-
export const UserLogin = Endpoint({
16-
Method: "POST",
17-
getPath: () => "/users/login",
18-
Input: {
19-
Body: t.strict({ username: t.string, password: t.string }),
20-
},
21-
Output: t.strict({ data: t.strict({ token: t.string }) }),
22-
});
23-
24-
export const UserCreate = Endpoint({
15+
const UserCreate = Endpoint({
2516
Method: "POST",
2617
getPath: () => "/users",
2718
Input: {
@@ -38,7 +29,7 @@ export const UserCreate = Endpoint({
3829
}),
3930
});
4031

41-
export const UserEdit = Endpoint({
32+
const UserEdit = Endpoint({
4233
Method: "PUT",
4334
getPath: ({ id }) => `/users/${id}`,
4435
Input: {
@@ -50,7 +41,7 @@ export const UserEdit = Endpoint({
5041
}),
5142
});
5243

53-
export const SignUpUser = Endpoint({
44+
const SignUpUser = Endpoint({
5445
Method: "POST",
5546
getPath: () => "/users/signup",
5647
Input: {
@@ -61,7 +52,7 @@ export const SignUpUser = Endpoint({
6152
}),
6253
});
6354

64-
export const UserGet = Endpoint({
55+
const UserGet = Endpoint({
6556
Method: "GET",
6657
getPath: ({ id }) => `/users/${id}`,
6758
Input: {
@@ -71,7 +62,7 @@ export const UserGet = Endpoint({
7162
Output: t.strict({ data: User }),
7263
});
7364

74-
export const GetUserMe = Endpoint({
65+
const GetUserMe = Endpoint({
7566
Method: "GET",
7667
getPath: () => `/users/me`,
7768
Input: {
@@ -80,7 +71,7 @@ export const GetUserMe = Endpoint({
8071
Output: Output(User, "user"),
8172
});
8273

83-
export const EditUserMe = Endpoint({
74+
const EditUserMe = Endpoint({
8475
Method: "PUT",
8576
getPath: () => `/users/me`,
8677
Input: {
@@ -89,7 +80,7 @@ export const EditUserMe = Endpoint({
8980
Output: Output(User, "user"),
9081
});
9182

92-
export const UserList = Endpoint({
83+
const UserList = Endpoint({
9384
Method: "GET",
9485
getPath: () => "/users",
9586
Input: {
@@ -101,22 +92,33 @@ export const UserList = Endpoint({
10192
Output: t.strict({ data: t.array(User), total: t.number }),
10293
});
10394

104-
const UserTGTokenGenerate = Endpoint({
95+
const UserDelete = Endpoint({
96+
Method: "DELETE",
97+
getPath: ({ id }) => `/users/${id}`,
98+
Input: {
99+
Params: t.type({ id: UUID }),
100+
},
101+
Output: t.undefined,
102+
});
103+
104+
// custom
105+
106+
const UserLogin = Endpoint({
105107
Method: "POST",
106-
getPath: () => "/users/tg/token",
108+
getPath: () => "/users/login",
107109
Input: {
108-
Body: t.strict({}),
110+
Body: t.strict({ username: t.string, password: t.string }),
109111
},
110112
Output: t.strict({ data: t.strict({ token: t.string }) }),
111113
});
112114

113-
const UserDelete = Endpoint({
114-
Method: "DELETE",
115-
getPath: ({ id }) => `/users/${id}`,
115+
const UserTGTokenGenerate = Endpoint({
116+
Method: "POST",
117+
getPath: () => "/users/tg/token",
116118
Input: {
117-
Params: t.type({ id: UUID }),
119+
Body: t.strict({}),
118120
},
119-
Output: t.undefined,
121+
Output: t.strict({ data: t.strict({ token: t.string }) }),
120122
});
121123

122124
export const users = ResourceEndpoints({
@@ -130,5 +132,6 @@ export const users = ResourceEndpoints({
130132
EditUserMe,
131133
SignUpUser,
132134
UserTGTokenGenerate,
135+
UserLogin,
133136
},
134137
});

packages/@liexp/shared/src/endpoints/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,4 @@ const AddEndpoint = GetEndpointSubscriber((e): IOError => {
119119
};
120120
});
121121

122-
export const UserLogin = User.UserLogin;
123-
export const PageDeleteMany = Page.DeleteManyPage;
124122
export { AddEndpoint, Endpoints, Uploads };

packages/@liexp/shared/src/endpoints/page.endpoints.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GetListQuery } from "../io/http/Query/index.js";
66
import { Page } from "../io/http/index.js";
77
import { ResourceEndpoints } from "./types.js";
88

9-
export const ListPages = Endpoint({
9+
const ListPages = Endpoint({
1010
Method: "GET",
1111
getPath: () => "/pages",
1212
Input: {
@@ -18,7 +18,7 @@ export const ListPages = Endpoint({
1818
Output: t.strict({ data: t.array(Page.Page) }),
1919
});
2020

21-
export const GetPage = Endpoint({
21+
const GetPage = Endpoint({
2222
Method: "GET",
2323
getPath: ({ id }) => `/pages/${id}`,
2424
Input: {
@@ -27,7 +27,7 @@ export const GetPage = Endpoint({
2727
Output: t.strict({ data: Page.Page }),
2828
});
2929

30-
export const CreatePage = Endpoint({
30+
const CreatePage = Endpoint({
3131
Method: "POST",
3232
getPath: () => `/pages`,
3333
Input: {
@@ -36,7 +36,7 @@ export const CreatePage = Endpoint({
3636
Output: t.strict({ data: Page.Page }),
3737
});
3838

39-
export const EditPage = Endpoint({
39+
const EditPage = Endpoint({
4040
Method: "PUT",
4141
getPath: ({ id }) => `/pages/${id}`,
4242
Input: {
@@ -46,7 +46,7 @@ export const EditPage = Endpoint({
4646
Output: t.strict({ data: Page.Page }),
4747
});
4848

49-
export const DeletePage = Endpoint({
49+
const DeletePage = Endpoint({
5050
Method: "DELETE",
5151
getPath: ({ id }) => `/pages/${id}`,
5252
Input: {
@@ -55,7 +55,7 @@ export const DeletePage = Endpoint({
5555
Output: t.strict({ data: Page.Page }),
5656
});
5757

58-
export const DeleteManyPage = Endpoint({
58+
const DeleteManyPage = Endpoint({
5959
Method: "DELETE",
6060
getPath: () => `/pages`,
6161
Input: {
@@ -70,5 +70,7 @@ export const pages = ResourceEndpoints({
7070
List: ListPages,
7171
Edit: EditPage,
7272
Delete: DeletePage,
73-
Custom: {},
73+
Custom: {
74+
DeleteMany: DeleteManyPage,
75+
},
7476
});

services/api/src/routes/pages/deleteManyPage.controller.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { pipe } from "@liexp/core/lib/fp/index.js";
2-
import {
3-
PageDeleteMany,
4-
AddEndpoint,
5-
} from "@liexp/shared/lib/endpoints/index.js";
2+
import { Endpoints, AddEndpoint } from "@liexp/shared/lib/endpoints/index.js";
63
import { type Router } from "express";
74
import * as TE from "fp-ts/lib/TaskEither.js";
85
import { In } from "typeorm";
@@ -12,7 +9,7 @@ import { authenticationHandler } from "#utils/authenticationHandler.js";
129

1310
export const MakeDeleteManyPageRoute = (r: Router, ctx: RouteContext): void => {
1411
AddEndpoint(r, authenticationHandler(["admin:delete"])(ctx))(
15-
PageDeleteMany,
12+
Endpoints.Page.Custom.DeleteMany,
1613
({ query: { ids } }) => {
1714
return pipe(
1815
ctx.db.find(PageEntity, { where: { id: In(ids) } }),
Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { pipe } from "@liexp/core/lib/fp/index.js";
2-
import { AddEndpoint, UserLogin } from "@liexp/shared/lib/endpoints/index.js";
2+
import { AddEndpoint, Endpoints } from "@liexp/shared/lib/endpoints/index.js";
33
import { UserStatusApproved } from "@liexp/shared/lib/io/http/User.js";
44
import { type Router } from "express";
55
import * as TE from "fp-ts/lib/TaskEither.js";
@@ -14,46 +14,49 @@ import { type RouteContext } from "#routes/route.types.js";
1414
import * as passwordUtils from "#utils/password.utils.js";
1515

1616
export const MakeUserLoginRoute = (r: Router, ctx: RouteContext): void => {
17-
AddEndpoint(r)(UserLogin, ({ body: { username, password } }) => {
18-
ctx.logger.debug.log("Login user with username or email %s", username);
19-
// void pipe(
20-
// passwordUtils.hash(password),
21-
// ctx.logger.debug.logInTaskEither('Password hash %s')
22-
// )()
23-
return pipe(
24-
ctx.db.findOneOrFail(UserEntity, {
25-
where: [{ username }, { email: username }],
26-
}),
27-
TE.mapLeft(() => NotFoundError("User")),
28-
LoggerService.TE.debug(ctx, "User %O"),
29-
TE.filterOrElse(
30-
(e) => e.status === UserStatusApproved.value,
31-
() => ServerError(["User not approved"]),
32-
),
33-
TE.chainFirst((user) =>
34-
pipe(
35-
passwordUtils.verify(password, user.passwordHash),
36-
TE.chain((isEqual) => {
37-
if (!isEqual) {
38-
return TE.left(BadRequestError("Password is wrong"));
39-
}
40-
return TE.right(isEqual);
41-
}),
17+
AddEndpoint(r)(
18+
Endpoints.User.Custom.UserLogin,
19+
({ body: { username, password } }) => {
20+
ctx.logger.debug.log("Login user with username or email %s", username);
21+
// void pipe(
22+
// passwordUtils.hash(password),
23+
// ctx.logger.debug.logInTaskEither('Password hash %s')
24+
// )()
25+
return pipe(
26+
ctx.db.findOneOrFail(UserEntity, {
27+
where: [{ username }, { email: username }],
28+
}),
29+
TE.mapLeft(() => NotFoundError("User")),
30+
LoggerService.TE.debug(ctx, "User %O"),
31+
TE.filterOrElse(
32+
(e) => e.status === UserStatusApproved.value,
33+
() => ServerError(["User not approved"]),
4234
),
43-
),
44-
TE.chain(({ passwordHash, ...user }) =>
45-
TE.fromIO(
46-
ctx.jwt.signUser({
47-
...user,
48-
createdAt: user.createdAt.toISOString(),
49-
updatedAt: user.updatedAt.toISOString(),
50-
}),
35+
TE.chainFirst((user) =>
36+
pipe(
37+
passwordUtils.verify(password, user.passwordHash),
38+
TE.chain((isEqual) => {
39+
if (!isEqual) {
40+
return TE.left(BadRequestError("Password is wrong"));
41+
}
42+
return TE.right(isEqual);
43+
}),
44+
),
5145
),
52-
),
53-
TE.map((token) => ({
54-
body: { data: { token } },
55-
statusCode: 201,
56-
})),
57-
);
58-
});
46+
TE.chain(({ passwordHash, ...user }) =>
47+
TE.fromIO(
48+
ctx.jwt.signUser({
49+
...user,
50+
createdAt: user.createdAt.toISOString(),
51+
updatedAt: user.updatedAt.toISOString(),
52+
}),
53+
),
54+
),
55+
TE.map((token) => ({
56+
body: { data: { token } },
57+
statusCode: 201,
58+
})),
59+
);
60+
},
61+
);
5962
};

0 commit comments

Comments
 (0)