very-express 0.5.0 → 0.5.2
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/dist/index.js +666 -493
- package/dist/templates/_middlewares/Authentication.ts +45 -0
- package/dist/templates/_plugins/auth/JWTKeyStore.ts +89 -0
- package/dist/templates/_plugins/auth/jwt.ts +59 -0
- package/dist/templates/_plugins/auth/oauthVerify.ts +83 -0
- package/dist/templates/_plugins/auth/profileMapping.ts +69 -0
- package/dist/templates/_plugins/auth/token.ts +30 -0
- package/dist/templates/_routes/oauth/OAuthRouteFactory.ts +63 -0
- package/dist/templates/_services/VexDbConnector.ts +3 -1
- package/dist/templates/_types/response/index.ts +10 -0
- package/dist/templates/_types/response/responseCode.ts +13 -0
- package/dist/templates/_types/response/responseMsg.ts +21 -0
- package/dist/templates/_types/response/responseStatusToCode.ts +70 -0
- package/dist/templates/_utils/index.ts +6 -2
- package/dist/templates/_utils/logger.ts +5 -5
- package/dist/templates/_utils/processTimer.ts +6 -0
- package/dist/templates/_utils/response.ts +25 -11
- package/dist/templates/jsonSchema/User.json +41 -14
- package/dist/templates/root/.env +10 -1
- package/dist/templates/root/Dockerfile +20 -0
- package/dist/templates/root/README.md +5 -0
- package/dist/templates/root/package.json +13 -4
- package/dist/templates/root/scripts/build.js +39 -0
- package/docs/roadMap/v0-6-0.md +8 -3
- package/docs/roadMap/v0-7-0.md +2 -1
- package/package.json +4 -14
- package/scripts/copyTsTemplates.js +6 -0
- package/vex.config.json +5 -7
- package/dist/templates/_plugins/PassportGoogle.ts +0 -84
- package/dist/templates/_plugins/oauthVerify.ts +0 -39
- package/dist/templates/_types/responseMsg.ts +0 -20
- /package/docs/{roadMap → releaseNote}/v0-5-0.md +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import { Request, Response, NextFunction } from "express";
|
|
4
|
+
import * as jwt from "../_plugins/auth/jwt.gen";
|
|
5
|
+
import Log from "../_utils/logger.gen";
|
|
6
|
+
import responseGen from "../_utils/response.gen";
|
|
7
|
+
|
|
8
|
+
export default class Authentication {
|
|
9
|
+
|
|
10
|
+
public middleware = (req: Request, res: Response, next: NextFunction) => {
|
|
11
|
+
try {
|
|
12
|
+
Log.info("Authentication.middleware", req.headers.authorization);
|
|
13
|
+
|
|
14
|
+
if (!req.headers.authorization) {
|
|
15
|
+
Log.ok("no Header", req.headers.authorization);
|
|
16
|
+
responseGen.send(res, 401);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// verify token with jwt.verify
|
|
21
|
+
const token = req.headers.authorization.split(" ")[1];
|
|
22
|
+
const clientIndex = req.cookies.tokenIndex;
|
|
23
|
+
const tokenData = jwt.verifyToken(token, clientIndex);
|
|
24
|
+
|
|
25
|
+
if (!tokenData) {
|
|
26
|
+
Log.ok("tokenInvalid", tokenData);
|
|
27
|
+
throw 401;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// if token is valid, set req.user to the decoded token
|
|
31
|
+
Log.ok("tokenValid", tokenData);
|
|
32
|
+
req.user = tokenData;
|
|
33
|
+
next();
|
|
34
|
+
}
|
|
35
|
+
catch (e: any) {
|
|
36
|
+
if (typeof e === "number") {
|
|
37
|
+
responseGen.send(res, e);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
Log.errorNoExit(e);
|
|
41
|
+
responseGen.send(res, 500, e?.message);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import dotenv from "dotenv";
|
|
4
|
+
dotenv.config();
|
|
5
|
+
export interface keyObj {
|
|
6
|
+
key: string,
|
|
7
|
+
index?: number,
|
|
8
|
+
clientIndex?:string,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class JWTKeyStore{
|
|
12
|
+
readonly expireTime: string | undefined = process.env.JWT_EXPIRE_TIME;
|
|
13
|
+
private keys: string[] = [];
|
|
14
|
+
|
|
15
|
+
constructor(){
|
|
16
|
+
this.keys = [
|
|
17
|
+
process.env.JWT_KEY0 || undefined,
|
|
18
|
+
process.env.JWT_KEY1 || undefined,
|
|
19
|
+
process.env.JWT_KEY2 || undefined,
|
|
20
|
+
process.env.JWT_KEY3 || undefined,
|
|
21
|
+
process.env.JWT_KEY4 || undefined,
|
|
22
|
+
process.env.JWT_KEY5 || undefined,
|
|
23
|
+
process.env.JWT_KEY6 || undefined,
|
|
24
|
+
process.env.JWT_KEY7 || undefined,
|
|
25
|
+
process.env.JWT_KEY8 || undefined,
|
|
26
|
+
process.env.JWT_KEY9 || undefined,
|
|
27
|
+
].filter((key) => key !== undefined);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @return {number}
|
|
32
|
+
* - key
|
|
33
|
+
* - index (actual index of key)
|
|
34
|
+
* - client index (index with random string)
|
|
35
|
+
*/
|
|
36
|
+
public getRandomKey(): keyObj {
|
|
37
|
+
// get leng of keys and return random key
|
|
38
|
+
const len = this.keys.length;
|
|
39
|
+
const tokenInfo = {
|
|
40
|
+
index: 0,
|
|
41
|
+
clientIndex: "",
|
|
42
|
+
key: ""
|
|
43
|
+
};
|
|
44
|
+
if (len > 1) {
|
|
45
|
+
tokenInfo.index = Math.floor(Math.random() * len);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
tokenInfo.clientIndex = this.getRandomCharString() + tokenInfo.index;
|
|
49
|
+
tokenInfo.key = this.keys[tokenInfo.index];
|
|
50
|
+
|
|
51
|
+
return tokenInfo;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* retrive key by index or client index
|
|
56
|
+
*/
|
|
57
|
+
public getKey(index:number|string): string {
|
|
58
|
+
if (typeof index === "string") {
|
|
59
|
+
index = parseInt(index.replace(/\D/g, ""));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return this.keys[index];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* retrive key by index or client index
|
|
67
|
+
*/
|
|
68
|
+
public getKeyObj(index:number|string): keyObj {
|
|
69
|
+
if (typeof index === "string") {
|
|
70
|
+
index = parseInt(index.replace(/\D/g, ""));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
key: this.keys[index]
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private getRandomCharString(length: number = 3): string {
|
|
79
|
+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
80
|
+
let result = "";
|
|
81
|
+
for (let i = 0; i < length; i++) {
|
|
82
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
83
|
+
result += characters[randomIndex];
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export default JWTKeyStore;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import jwt from "jsonwebtoken";
|
|
4
|
+
import JWTKeyStore from "./JWTKeyStore.gen";
|
|
5
|
+
|
|
6
|
+
const keys = new JWTKeyStore();
|
|
7
|
+
|
|
8
|
+
export function generateToken(
|
|
9
|
+
data: any,
|
|
10
|
+
keyIndex?: number,
|
|
11
|
+
expiresIn?: string
|
|
12
|
+
): {
|
|
13
|
+
token: string,
|
|
14
|
+
index?: number,
|
|
15
|
+
clientIndex?: string
|
|
16
|
+
} {
|
|
17
|
+
const keyInfo = typeof keyIndex == "number" ? keys.getKeyObj(keyIndex) : keys.getRandomKey();
|
|
18
|
+
|
|
19
|
+
const token = jwt.sign(
|
|
20
|
+
data,
|
|
21
|
+
keyInfo.key,
|
|
22
|
+
{
|
|
23
|
+
expiresIn: expiresIn || keys.expireTime || "1h",
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
token: token,
|
|
29
|
+
index: keyInfo.index,
|
|
30
|
+
clientIndex: keyInfo.clientIndex
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Verifies a JSON Web Token (JWT) using the provided token and key index.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} token - The JSON Web Token to be verified.
|
|
38
|
+
* @param {number} index - The index of the key used to verify the token.
|
|
39
|
+
* @return {jwt.JwtPayload | string | false} The decoded token payload, an error message, or false if verification fails.
|
|
40
|
+
*/
|
|
41
|
+
export function verifyToken(token: string, index?: number|string): jwt.JwtPayload | string | false {
|
|
42
|
+
try {
|
|
43
|
+
if(!index) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const key = keys.getKey(index);
|
|
47
|
+
return jwt.verify(token, key);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
if(e instanceof jwt.TokenExpiredError) {
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
else if (e instanceof jwt.JsonWebTokenError) {
|
|
53
|
+
throw e;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import { UserModel, UserDocument } from "./../../_models/UserModel.gen";
|
|
4
|
+
import { User } from "./../../_types/User.gen";
|
|
5
|
+
import profileMapping, { IProfile } from "./profileMapping.gen";
|
|
6
|
+
import returnToken from "./token.gen";
|
|
7
|
+
import vex from "../../_utils/index.gen";
|
|
8
|
+
|
|
9
|
+
export default async function oauthVerify(accessToken: string, refreshToken: string, profile: IProfile, done: (error: any, user?: any) => void) : Promise<void> {
|
|
10
|
+
try {
|
|
11
|
+
|
|
12
|
+
let user: UserDocument;
|
|
13
|
+
const authUser = profileMapping(profile);
|
|
14
|
+
|
|
15
|
+
// find user by authProfiles.authId or email
|
|
16
|
+
const existingUser = await UserModel.findOne<UserDocument>({
|
|
17
|
+
email: authUser.email,
|
|
18
|
+
$or: authUser.authProfiles?.map((p) => {
|
|
19
|
+
return { "authProfiles.authId": p.authId };
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// 1. create new user
|
|
24
|
+
if ( !existingUser ){
|
|
25
|
+
user = await createNewUser(authUser);
|
|
26
|
+
}
|
|
27
|
+
// 2. update existing user
|
|
28
|
+
else {
|
|
29
|
+
console.log("OAuthVerify UpdateUser");
|
|
30
|
+
user = await processExistingUser(authUser, existingUser);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
done(null, returnToken(user));
|
|
34
|
+
}
|
|
35
|
+
catch(err) {
|
|
36
|
+
return done(err);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function createNewUser(authProfile: User): Promise<UserDocument>{
|
|
41
|
+
vex.log.info("OAuthVerify > createNewUser");
|
|
42
|
+
const newUser = new UserModel(authProfile);
|
|
43
|
+
return await newUser.save();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function processExistingUser( authUser: User, existingUser: UserDocument): Promise<UserDocument>{
|
|
47
|
+
vex.log.info("OAuthVerify > processExistingUser");
|
|
48
|
+
|
|
49
|
+
let userUpdated = false;
|
|
50
|
+
const authProfile = authUser.authProfiles?.[0] || undefined;
|
|
51
|
+
const userProfiles = existingUser.authProfiles as User["authProfiles"] || [];
|
|
52
|
+
|
|
53
|
+
if(!authProfile){
|
|
54
|
+
vex.log.errorNoExit("OAuthVerify > Invalid OAuth Callback \"authProfile\"");
|
|
55
|
+
throw new Error("Invalid OAuth Callback \"authProfile\"");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// B.1. check current provider exist in authProfiles
|
|
59
|
+
const providerExists = userProfiles.length > 0 && userProfiles.find((p) => {
|
|
60
|
+
return (p.provider === authProfile.provider) && (p.authId === authProfile.authId);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// B.2. add if not exist
|
|
64
|
+
if (!providerExists){
|
|
65
|
+
vex.log.info("OAuthVerify > add new provider",authProfile.provider);
|
|
66
|
+
userProfiles.push(authProfile);
|
|
67
|
+
userUpdated = true;
|
|
68
|
+
}
|
|
69
|
+
// B.3 update if username changed
|
|
70
|
+
else if (providerExists && providerExists.username !== authProfile.username){
|
|
71
|
+
vex.log.info("OAuthVerify > update username", authProfile.provider);
|
|
72
|
+
providerExists.username = authProfile.username;
|
|
73
|
+
userUpdated = true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (userUpdated){
|
|
77
|
+
vex.log.info("OAuthVerify > Update user");
|
|
78
|
+
return await existingUser.updateOne({ authProfiles: userProfiles });
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return existingUser;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import { Profile } from "passport";
|
|
4
|
+
import { User } from "./../../_types/User.gen";
|
|
5
|
+
|
|
6
|
+
export interface IProfile extends Profile {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
export default function profileMapping(oauthProfile: IProfile): User
|
|
10
|
+
{
|
|
11
|
+
let authProfile: User;
|
|
12
|
+
switch(oauthProfile.provider){
|
|
13
|
+
case "github":
|
|
14
|
+
authProfile = GithubProfileMapping(oauthProfile);
|
|
15
|
+
break;
|
|
16
|
+
case "google":
|
|
17
|
+
authProfile = GoogleProfileMapping(oauthProfile);
|
|
18
|
+
break;
|
|
19
|
+
default:
|
|
20
|
+
throw new Error("Invalid OAuth Provider");
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// if ( !authProfile.email){
|
|
25
|
+
// console.log('missingEmail');
|
|
26
|
+
// authProfile.profileErrors?.push('missingEmail');
|
|
27
|
+
// authProfile.active = false;
|
|
28
|
+
// }
|
|
29
|
+
return authProfile;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function GithubProfileMapping(oauthProfile: IProfile): User
|
|
33
|
+
{
|
|
34
|
+
const user: User = {
|
|
35
|
+
active: true,
|
|
36
|
+
authProfiles: [{
|
|
37
|
+
provider: oauthProfile.provider,
|
|
38
|
+
authId: oauthProfile.id,
|
|
39
|
+
username: oauthProfile.username || oauthProfile.displayName
|
|
40
|
+
}],
|
|
41
|
+
roles: undefined,
|
|
42
|
+
name: oauthProfile.username || oauthProfile.displayName,
|
|
43
|
+
email: oauthProfile._json.email || oauthProfile._json.notification_email || undefined,
|
|
44
|
+
locale: undefined,
|
|
45
|
+
profileErrors: []
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return user;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function GoogleProfileMapping(oauthProfile: IProfile): User
|
|
52
|
+
{
|
|
53
|
+
const user: User = {
|
|
54
|
+
active: true,
|
|
55
|
+
authProfiles: [{
|
|
56
|
+
provider: oauthProfile.provider,
|
|
57
|
+
authId: oauthProfile.id,
|
|
58
|
+
username: oauthProfile.username || oauthProfile.displayName
|
|
59
|
+
}],
|
|
60
|
+
roles: undefined,
|
|
61
|
+
name: oauthProfile.username || oauthProfile.displayName,
|
|
62
|
+
// data below could be missing depend on the provider
|
|
63
|
+
email: oauthProfile._json.email || oauthProfile._json.notification_email || undefined,
|
|
64
|
+
locale: oauthProfile._json.locale || undefined,
|
|
65
|
+
profileErrors: []
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return user;
|
|
69
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import { generateToken } from "./../auth/jwt.gen";
|
|
4
|
+
import { UserDocument } from "./../../_models/UserModel.gen";
|
|
5
|
+
|
|
6
|
+
export default function returnToken(userProfile: UserDocument){
|
|
7
|
+
const sanitizedProfile = sanitizeUser(userProfile);
|
|
8
|
+
const accessToken = generateToken(sanitizedProfile);
|
|
9
|
+
const refreshToken = generateToken({_id: userProfile._id }, 0);
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
profile: sanitizedProfile,
|
|
13
|
+
accessToken: accessToken.token,
|
|
14
|
+
keyIndex: accessToken.index,
|
|
15
|
+
clientIndex: accessToken.clientIndex,
|
|
16
|
+
refreshToken: refreshToken["token"]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function sanitizeUser(user: UserDocument){
|
|
21
|
+
return {
|
|
22
|
+
_id: user._id,
|
|
23
|
+
email: user.email,
|
|
24
|
+
name: user.name,
|
|
25
|
+
locale: user.locale,
|
|
26
|
+
roles: user.roles,
|
|
27
|
+
profileErrors: user.profileErrors,
|
|
28
|
+
active: user.active
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import { Router } from "express";
|
|
4
|
+
import passport, { PassportStatic, Profile } from "passport";
|
|
5
|
+
import * as oauth2 from "passport-oauth2";
|
|
6
|
+
// import responseGen from "../../_utils/response.gen";
|
|
7
|
+
// import { generateToken, verifyToken } from "../../_plugins/auth/jwt.gen";
|
|
8
|
+
|
|
9
|
+
export type { Profile };
|
|
10
|
+
|
|
11
|
+
export interface passportFactoryConfig {
|
|
12
|
+
strategyName: string,
|
|
13
|
+
strategy: oauth2.Strategy,
|
|
14
|
+
authenticateOptions?: any,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Provides a router and passport instance for Google OAuth */
|
|
18
|
+
export default class OAuthRouteFactory {
|
|
19
|
+
|
|
20
|
+
private config: passportFactoryConfig;
|
|
21
|
+
private router: Router = Router();
|
|
22
|
+
private passport: PassportStatic = passport;
|
|
23
|
+
|
|
24
|
+
constructor(config: passportFactoryConfig ) {
|
|
25
|
+
|
|
26
|
+
this.config = config;
|
|
27
|
+
this.config.authenticateOptions = this.config.authenticateOptions || {
|
|
28
|
+
session: false,
|
|
29
|
+
scope: ["profile", "email"],
|
|
30
|
+
failureRedirect: ""
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
this.initPassport(this.config.strategy);
|
|
34
|
+
this.initRoutes();
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private initPassport(strategy: passport.Strategy){
|
|
39
|
+
this.passport.use(strategy);
|
|
40
|
+
this.passport.initialize();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private initRoutes(){
|
|
44
|
+
// redirect to login
|
|
45
|
+
this.router.get("/",
|
|
46
|
+
this.passport.authenticate(this.config.strategyName, this.config.authenticateOptions)
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// back from login
|
|
50
|
+
this.router.get("/callback",
|
|
51
|
+
this.passport.authenticate(this.config.strategyName, this.config.authenticateOptions),
|
|
52
|
+
(req, res) => {
|
|
53
|
+
const user = req.user as any;
|
|
54
|
+
res.redirect(`/profile?accessToken=${user.accessToken}&tokenIndex=${user.clientIndex}&refreshToken=${user.refreshToken}`);
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public getRouter() {
|
|
61
|
+
return this.router;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -64,7 +64,9 @@ export default class VexDbConnector {
|
|
|
64
64
|
**/
|
|
65
65
|
middleware(req: Request, res: Response, next: NextFunction) {
|
|
66
66
|
if (mongoose.connection.readyState !== 1) {
|
|
67
|
-
vex.response.send(res, 503,
|
|
67
|
+
vex.response.send(res, 503, {
|
|
68
|
+
message: "Database connection error",
|
|
69
|
+
});
|
|
68
70
|
}
|
|
69
71
|
else if (this.recordAccessLog){
|
|
70
72
|
mongoose.connection.collection("accessLogs").insertOne({
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { responseMsg } from "./responseMsg.gen";
|
|
2
|
+
export { responseCode, ResponseCode } from "./responseCode.gen";
|
|
3
|
+
export { responseStatusToCode } from "./responseStatusToCode.gen";
|
|
4
|
+
|
|
5
|
+
export interface responseObject<T>{
|
|
6
|
+
ret_code : number,
|
|
7
|
+
ret_msg : string,
|
|
8
|
+
ret_time?: number,
|
|
9
|
+
result: T,
|
|
10
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { responseMsg } from "./responseMsg.gen";
|
|
2
|
+
|
|
3
|
+
/** custom response code **/
|
|
4
|
+
export type ResponseCode = keyof typeof responseMsg;
|
|
5
|
+
|
|
6
|
+
/** custom response code **/
|
|
7
|
+
export const responseCode = Object.keys(responseMsg).reduce((acc, key) => {
|
|
8
|
+
acc[key] = key as ResponseCode;
|
|
9
|
+
return acc;
|
|
10
|
+
}, {} as Record<string, ResponseCode>);
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export default responseCode;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* default response message of each return code
|
|
3
|
+
**/
|
|
4
|
+
export const responseMsg = {
|
|
5
|
+
err_perm: "Permission Denied",
|
|
6
|
+
|
|
7
|
+
err_payload: "Invalid Request Payload",
|
|
8
|
+
err_query: "Invalid Request Query",
|
|
9
|
+
err_body: "Invalid Request Body",
|
|
10
|
+
|
|
11
|
+
err_validation: "Validation Error",
|
|
12
|
+
err_query_validation: "Query Validation Error",
|
|
13
|
+
err_body_validation: "Body Validation Error",
|
|
14
|
+
|
|
15
|
+
err_create: "Falied to Insert Data",
|
|
16
|
+
err_read: "Falied to Read Data",
|
|
17
|
+
err_update: "Falied to Edit Data",
|
|
18
|
+
err_delete: "Falied to Delete Data",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default responseMsg;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import responseCode from "./responseCode.gen";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* response status default message.
|
|
5
|
+
* if set value as response code, response handler will set status, code & message.
|
|
6
|
+
**/
|
|
7
|
+
export const responseStatusToCode = new Map<number, string>();
|
|
8
|
+
// responseStatusToCode.set(100, "Continue");
|
|
9
|
+
// responseStatusToCode.set(101, "Switching Protocols");
|
|
10
|
+
// responseStatusToCode.set(102, "Processing");
|
|
11
|
+
// responseStatusToCode.set(103, "Early Hints");
|
|
12
|
+
responseStatusToCode.set(200, responseCode.success);
|
|
13
|
+
responseStatusToCode.set(201, responseCode.success);
|
|
14
|
+
responseStatusToCode.set(202, responseCode.success);
|
|
15
|
+
// responseStatusToCode.set(203, "Non-Authoritative Information");
|
|
16
|
+
// responseStatusToCode.set(204, "No Content");
|
|
17
|
+
// responseStatusToCode.set(205, "Reset Content");
|
|
18
|
+
// responseStatusToCode.set(206, "Partial Content");
|
|
19
|
+
// responseStatusToCode.set(207, "Multi-Status");
|
|
20
|
+
// responseStatusToCode.set(208, "Already Reported");
|
|
21
|
+
// responseStatusToCode.set(226, "IM Used");
|
|
22
|
+
// responseStatusToCode.set(300, "Multiple Choices");
|
|
23
|
+
// responseStatusToCode.set(301, "Moved Permanently");
|
|
24
|
+
// responseStatusToCode.set(302, "Found");
|
|
25
|
+
// responseStatusToCode.set(303, "See Other");
|
|
26
|
+
// responseStatusToCode.set(304, "Not Modified");
|
|
27
|
+
// responseStatusToCode.set(307, "Temporary Redirect");
|
|
28
|
+
// responseStatusToCode.set(308, "Permanent Redirect");
|
|
29
|
+
responseStatusToCode.set(400, responseCode.err_payload);
|
|
30
|
+
// responseStatusToCode.set(401, responseCode.err_perm);
|
|
31
|
+
// responseStatusToCode.set(402, "Payment Required");
|
|
32
|
+
// responseStatusToCode.set(403, "Forbidden");
|
|
33
|
+
// responseStatusToCode.set(404, "Not Found");
|
|
34
|
+
// responseStatusToCode.set(405, "Method Not Allowed");
|
|
35
|
+
// responseStatusToCode.set(406, "Not Acceptable");
|
|
36
|
+
// responseStatusToCode.set(407, "Proxy Authentication Required");
|
|
37
|
+
// responseStatusToCode.set(408, "Request Timeout");
|
|
38
|
+
// responseStatusToCode.set(409, "Conflict");
|
|
39
|
+
// responseStatusToCode.set(410, "Gone");
|
|
40
|
+
// responseStatusToCode.set(411, "Length Required");
|
|
41
|
+
// responseStatusToCode.set(412, "Precondition Failed");
|
|
42
|
+
// responseStatusToCode.set(413, "Content Too Large");
|
|
43
|
+
// responseStatusToCode.set(414, "URI Too Long");
|
|
44
|
+
// responseStatusToCode.set(415, "Unsupported Media Type");
|
|
45
|
+
// responseStatusToCode.set(416, "Range Not Satisfiable");
|
|
46
|
+
// responseStatusToCode.set(417, "Expectation Failed");
|
|
47
|
+
// responseStatusToCode.set(418, "I'm a teapot");
|
|
48
|
+
// responseStatusToCode.set(421, "Misdirected Request");
|
|
49
|
+
// responseStatusToCode.set(422, "Unprocessable Content");
|
|
50
|
+
// responseStatusToCode.set(423, "Locked");
|
|
51
|
+
// responseStatusToCode.set(424, "Failed Dependency");
|
|
52
|
+
// responseStatusToCode.set(425, "Too Early");
|
|
53
|
+
// responseStatusToCode.set(426, "Upgrade Required");
|
|
54
|
+
// responseStatusToCode.set(428, "Precondition Required");
|
|
55
|
+
// responseStatusToCode.set(429, "Too Many Requests");
|
|
56
|
+
// responseStatusToCode.set(431, "Request Header Fields Too Large");
|
|
57
|
+
// responseStatusToCode.set(451, "Unavailable For Legal Reasons");
|
|
58
|
+
// responseStatusToCode.set(500, "Internal Server Error");
|
|
59
|
+
// responseStatusToCode.set(501, "Not Implemented");
|
|
60
|
+
// responseStatusToCode.set(502, "Bad Gateway");
|
|
61
|
+
// responseStatusToCode.set(503, "Service Unavailable");
|
|
62
|
+
// responseStatusToCode.set(504, "Gateway Timeout");
|
|
63
|
+
// responseStatusToCode.set(505, "HTTP Version Not Supported");
|
|
64
|
+
// responseStatusToCode.set(506, "Variant Also Negotiates");
|
|
65
|
+
// responseStatusToCode.set(507, "Insufficient Storage");
|
|
66
|
+
// responseStatusToCode.set(508, "Loop Detected");
|
|
67
|
+
// responseStatusToCode.set(510, "Not Extended");
|
|
68
|
+
// responseStatusToCode.set(511, "Network Authentication Required");
|
|
69
|
+
|
|
70
|
+
export default responseStatusToCode;
|
|
@@ -1,13 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
1
3
|
import logger from "./logger.gen";
|
|
2
4
|
import response from "./response.gen";
|
|
3
5
|
import validator from "./validator.gen";
|
|
4
|
-
import { responseMsg } from "./../_types/
|
|
6
|
+
import { responseMsg, responseCode, responseStatusToCode } from "./../_types/response/index.gen";
|
|
5
7
|
import common from "./common.gen";
|
|
6
8
|
|
|
7
9
|
export default {
|
|
8
10
|
log : logger,
|
|
9
11
|
response,
|
|
10
12
|
validator,
|
|
11
|
-
responseMsg
|
|
13
|
+
responseMsg,
|
|
14
|
+
responseCode,
|
|
15
|
+
responseStatusToCode,
|
|
12
16
|
common
|
|
13
17
|
};
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
export function processing(msg: any, ...optionalParams: any[]): void {
|
|
2
|
-
console.log("\x1b[34m%s\x1b[0m", "[PROCESSING]", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
2
|
+
console.log("\x1b[34m%s\x1b[0m", "[PROCESSING]" , new Date().toLocaleString() +" -", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export function ok(msg: any, ...optionalParams: any[]): void {
|
|
6
|
-
console.log("\x1b[32m%s\x1b[0m", "[OK]", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
6
|
+
console.log("\x1b[32m%s\x1b[0m", "[OK]" , new Date().toLocaleString() +" -", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export function info(msg: any, ...optionalParams: any[]): void {
|
|
10
|
-
console.log("\x1b[36m%s\x1b[0m", "[INFO]", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
10
|
+
console.log("\x1b[36m%s\x1b[0m", "[INFO]" , new Date().toLocaleString() +" -", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function warn(msg: any, ...optionalParams: any[]): void {
|
|
14
|
-
console.log("\x1b[33m%s\x1b[0m", "[WARN]", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
14
|
+
console.log("\x1b[33m%s\x1b[0m", "[WARN]" , new Date().toLocaleString() +" -", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export function error(msg: any, ...optionalParams: any[]): never {
|
|
@@ -20,7 +20,7 @@ export function error(msg: any, ...optionalParams: any[]): never {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export function errorNoExit(msg: any, ...optionalParams: any[]): void {
|
|
23
|
-
console.log("\x1b[41m%s\x1b[0m", "[ERROR]", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
23
|
+
console.log("\x1b[41m%s\x1b[0m", "[ERROR]" , new Date().toLocaleString() +" -", msg, optionalParams.length > 0 ? optionalParams : "");
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
// wrapper
|