@mingjue8888/express-plus 1.0.0 → 1.1.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.
- package/README.md +150 -1
- package/package.json +1 -1
- package/{valid-response.ts → valid-send.ts} +1 -1
- package/demo-app.ts +0 -39
- package/demo-routerA.ts +0 -35
- package/demo-routerB.ts +0 -58
package/README.md
CHANGED
|
@@ -1 +1,150 @@
|
|
|
1
|
-
# Usage
|
|
1
|
+
# Usage
|
|
2
|
+
|
|
3
|
+
## Define User
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import LoginAccount from "@mingjue8888/express-plus/login-account";
|
|
7
|
+
|
|
8
|
+
export interface UserAccount extends LoginAccount {}
|
|
9
|
+
|
|
10
|
+
declare global {
|
|
11
|
+
namespace Express {
|
|
12
|
+
interface User extends UserAccount {}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Define Error
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
export class NotFoundUserError extends Error {}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Write Router
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import Joi from "joi";
|
|
27
|
+
import asyncMiddleware from "@mingjue8888/express-plus/async-middleware";
|
|
28
|
+
import hasLogin from "@mingjue8888/express-plus/has-login";
|
|
29
|
+
import Router from "@mingjue8888/express-plus/router";
|
|
30
|
+
import validBody from "@mingjue8888/express-plus/valid-body";
|
|
31
|
+
import validParam from "@mingjue8888/express-plus/valid-param";
|
|
32
|
+
import validSend from "@mingjue8888/express-plus/valid-send";
|
|
33
|
+
|
|
34
|
+
// File ./routers/routerA.ts
|
|
35
|
+
const login: Router = {
|
|
36
|
+
method: "POST",
|
|
37
|
+
path: "/login",
|
|
38
|
+
middlewares: [
|
|
39
|
+
validBody({
|
|
40
|
+
username: Joi.string()
|
|
41
|
+
.required(),
|
|
42
|
+
|
|
43
|
+
password: Joi.string()
|
|
44
|
+
.regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/)
|
|
45
|
+
.required()
|
|
46
|
+
}),
|
|
47
|
+
asyncMiddleware(async function (req, res) {
|
|
48
|
+
const username = req.valid.body.username as string;
|
|
49
|
+
const password = req.valid.body.password as string;
|
|
50
|
+
const id = "UUID";
|
|
51
|
+
res.signToken(id);
|
|
52
|
+
}),
|
|
53
|
+
validSend({
|
|
54
|
+
accessToken: Joi.string()
|
|
55
|
+
.required(),
|
|
56
|
+
}),
|
|
57
|
+
],
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default [
|
|
61
|
+
login,
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
// File ./routers/routerB.ts
|
|
65
|
+
const findUser: Router = {
|
|
66
|
+
method: "GET",
|
|
67
|
+
path: "/users/:userId",
|
|
68
|
+
middlewares: [
|
|
69
|
+
hasLogin,
|
|
70
|
+
validParam({
|
|
71
|
+
userId: Joi.string()
|
|
72
|
+
.required(),
|
|
73
|
+
}),
|
|
74
|
+
asyncMiddleware(async function (req, res) {
|
|
75
|
+
throw new NotFoundUserError();
|
|
76
|
+
}),
|
|
77
|
+
validSend({
|
|
78
|
+
id: Joi.string()
|
|
79
|
+
.required(),
|
|
80
|
+
|
|
81
|
+
name: Joi.string()
|
|
82
|
+
.required(),
|
|
83
|
+
}),
|
|
84
|
+
],
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const findUserPosts: Router = {
|
|
88
|
+
method: "GET",
|
|
89
|
+
path: "/users/:userId/posts",
|
|
90
|
+
middlewares: [
|
|
91
|
+
hasLogin,
|
|
92
|
+
validParam({
|
|
93
|
+
userId: Joi.string()
|
|
94
|
+
.required(),
|
|
95
|
+
}),
|
|
96
|
+
asyncMiddleware(async function (req, res) {
|
|
97
|
+
res.body = {
|
|
98
|
+
posts: []
|
|
99
|
+
}
|
|
100
|
+
}),
|
|
101
|
+
validSend({
|
|
102
|
+
posts: Joi.array().items(Joi.object().keys({
|
|
103
|
+
id: Joi.string().required(),
|
|
104
|
+
content: Joi.string().required().default(""),
|
|
105
|
+
}))
|
|
106
|
+
})
|
|
107
|
+
],
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default [
|
|
111
|
+
findUser,
|
|
112
|
+
findUserPosts,
|
|
113
|
+
];
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Start app
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import app from "@mingjue8888/express-plus";
|
|
120
|
+
import routerA from "./routers/routerA";
|
|
121
|
+
import routerB from "./routers/routerB";
|
|
122
|
+
import {NotFoundUserError} from "./errors";
|
|
123
|
+
import NotFoundResponseError from "@mingjue8888/express-plus/not-found-response-error";
|
|
124
|
+
|
|
125
|
+
const routers = [
|
|
126
|
+
routerA,
|
|
127
|
+
routerB,
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
app(routers, {
|
|
131
|
+
APP_PORT: 3000,
|
|
132
|
+
|
|
133
|
+
async authenticate<UserAccount>(token: LoginToken) {
|
|
134
|
+
return {
|
|
135
|
+
id: token.sub,
|
|
136
|
+
isLocked: false,
|
|
137
|
+
isSuspended: false,
|
|
138
|
+
isBanned: false,
|
|
139
|
+
roles: [],
|
|
140
|
+
} as UserAccount;
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
// Covert Error to ResponseError
|
|
144
|
+
onError(err: Error) {
|
|
145
|
+
if (err instanceof NotFoundUserError) {
|
|
146
|
+
return new NotFoundResponseError(err.constructor.name, "Not found user");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
```
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import Joi from "joi";
|
|
|
2
2
|
import asyncMiddleware from "./async-middleware";
|
|
3
3
|
import ServerErrorResponseError from "./server-error-response-error";
|
|
4
4
|
|
|
5
|
-
export default function
|
|
5
|
+
export default function validSend(sch: Record<string, Joi.AnySchema>) {
|
|
6
6
|
return asyncMiddleware(async function (_req, res) {
|
|
7
7
|
const { error, value } = Joi
|
|
8
8
|
.object(sch)
|
package/demo-app.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import routerA from "./demo-routerA";
|
|
2
|
-
import routerB, {NotFoundUserError} from "./demo-routerB";
|
|
3
|
-
import app from "./index";
|
|
4
|
-
import LoginAccount from "./login-account";
|
|
5
|
-
import LoginToken from "./login-token";
|
|
6
|
-
import NotFoundResponseError from "./not-found-response-error";
|
|
7
|
-
|
|
8
|
-
interface UserAccount extends LoginAccount {}
|
|
9
|
-
|
|
10
|
-
declare global {
|
|
11
|
-
namespace Express {
|
|
12
|
-
interface User extends UserAccount {}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const routers = [
|
|
17
|
-
routerA,
|
|
18
|
-
routerB,
|
|
19
|
-
];
|
|
20
|
-
|
|
21
|
-
app(routers, {
|
|
22
|
-
APP_PORT: 3000,
|
|
23
|
-
|
|
24
|
-
async authenticate<UserAccount>(token: LoginToken) {
|
|
25
|
-
return {
|
|
26
|
-
id: token.sub,
|
|
27
|
-
isLocked: false,
|
|
28
|
-
isSuspended: false,
|
|
29
|
-
isBanned: false,
|
|
30
|
-
roles: [],
|
|
31
|
-
} as UserAccount;
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
onError(err: Error) {
|
|
35
|
-
if (err instanceof NotFoundUserError) {
|
|
36
|
-
return new NotFoundResponseError(err.constructor.name, "Not found user");
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
});
|
package/demo-routerA.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import Joi from "joi";
|
|
2
|
-
import asyncMiddleware from "./async-middleware";
|
|
3
|
-
import Router from "./router";
|
|
4
|
-
import validBody from "./valid-body";
|
|
5
|
-
import validResponse from "./valid-response";
|
|
6
|
-
|
|
7
|
-
const login: Router = {
|
|
8
|
-
method: "POST",
|
|
9
|
-
path: "/login",
|
|
10
|
-
middlewares: [
|
|
11
|
-
validBody({
|
|
12
|
-
username: Joi.string()
|
|
13
|
-
.required(),
|
|
14
|
-
|
|
15
|
-
password: Joi.string()
|
|
16
|
-
.regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/)
|
|
17
|
-
.required()
|
|
18
|
-
}),
|
|
19
|
-
asyncMiddleware(async function (req, res) {
|
|
20
|
-
const username = req.valid.body.username as string;
|
|
21
|
-
const password = req.valid.body.password as string;
|
|
22
|
-
const id = "UUID";
|
|
23
|
-
res.signToken(id);
|
|
24
|
-
}),
|
|
25
|
-
validResponse({
|
|
26
|
-
accessToken: Joi.string()
|
|
27
|
-
.required(),
|
|
28
|
-
}),
|
|
29
|
-
],
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export default [
|
|
33
|
-
login,
|
|
34
|
-
];
|
|
35
|
-
|
package/demo-routerB.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import Joi from "joi";
|
|
2
|
-
import asyncMiddleware from "./async-middleware";
|
|
3
|
-
import hasLogin from "./has-login";
|
|
4
|
-
import Router from "./router";
|
|
5
|
-
import validParam from "./valid-param";
|
|
6
|
-
import validResponse from "./valid-response";
|
|
7
|
-
|
|
8
|
-
export class NotFoundUserError extends Error {}
|
|
9
|
-
|
|
10
|
-
const findUser: Router = {
|
|
11
|
-
method: "GET",
|
|
12
|
-
path: "/users/:userId",
|
|
13
|
-
middlewares: [
|
|
14
|
-
hasLogin,
|
|
15
|
-
validParam({
|
|
16
|
-
userId: Joi.string()
|
|
17
|
-
.required(),
|
|
18
|
-
}),
|
|
19
|
-
asyncMiddleware(async function (req, res) {
|
|
20
|
-
throw new NotFoundUserError();
|
|
21
|
-
}),
|
|
22
|
-
validResponse({
|
|
23
|
-
id: Joi.string()
|
|
24
|
-
.required(),
|
|
25
|
-
|
|
26
|
-
name: Joi.string()
|
|
27
|
-
.required(),
|
|
28
|
-
}),
|
|
29
|
-
],
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const findUserPosts: Router = {
|
|
33
|
-
method: "GET",
|
|
34
|
-
path: "/users/:userId/posts",
|
|
35
|
-
middlewares: [
|
|
36
|
-
hasLogin,
|
|
37
|
-
validParam({
|
|
38
|
-
userId: Joi.string()
|
|
39
|
-
.required(),
|
|
40
|
-
}),
|
|
41
|
-
asyncMiddleware(async function (req, res) {
|
|
42
|
-
res.body = {
|
|
43
|
-
posts: []
|
|
44
|
-
}
|
|
45
|
-
}),
|
|
46
|
-
validResponse({
|
|
47
|
-
posts: Joi.array().items(Joi.object().keys({
|
|
48
|
-
id: Joi.string().required(),
|
|
49
|
-
content: Joi.string().required().default(""),
|
|
50
|
-
}))
|
|
51
|
-
})
|
|
52
|
-
],
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default [
|
|
56
|
-
findUser,
|
|
57
|
-
findUserPosts,
|
|
58
|
-
];
|