very-express 0.5.26 → 0.6.1-alpha
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/.eslintrc.json +35 -37
- package/.gitattributes +2 -2
- package/.github/copilot-instructions.md +107 -84
- package/.github/workflows/npm-start.yml +23 -23
- package/LICENSE +21 -21
- package/README.md +75 -75
- package/dist/index.js +1039 -1149
- package/dist/templates/_controllers/_ControllerFactory.ts +7 -3
- package/dist/templates/_middlewares/Authentication.ts +1 -5
- package/dist/templates/_middlewares/tsoaAuthentication.ts +24 -0
- package/dist/templates/_projectSettings/env +25 -24
- package/dist/templates/_projectSettings/package.json +50 -54
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +48 -0
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +59 -0
- package/dist/templates/_services/VexDb.ts +14 -0
- package/dist/templates/_services/VexDbConnector.ts +108 -163
- package/dist/templates/_services/VexSystem.ts +25 -42
- package/dist/templates/_services/auth/JWTService.ts +26 -14
- package/dist/templates/_services/oauth/OAuthProfileMap.ts +6 -14
- package/dist/templates/_services/oauth/OAuthStrategyService.ts +69 -51
- package/dist/templates/_types/IVexRepository.ts +18 -0
- package/dist/templates/_types/VexRequest.ts +11 -0
- package/dist/templates/_types/VexResponse.ts +25 -0
- package/dist/templates/_types/VexResponseError.ts +4 -3
- package/dist/templates/_types/responseObject.ts +1 -1
- package/dist/templates/_utils/common.ts +50 -16
- package/dist/templates/_utils/hash.ts +6 -5
- package/dist/templates/_utils/index.ts +2 -1
- package/dist/templates/_utils/logger.ts +15 -14
- package/dist/templates/_utils/processTimer.ts +1 -0
- package/dist/templates/_utils/response.ts +4 -3
- package/dist/templates/_utils/validator.ts +1 -0
- package/dist/templates/jsonSchema/Session.json +34 -31
- package/dist/templates/jsonSchema/User.json +50 -85
- package/dist/templates/jsonSchema/UserAuthProfiles.json +48 -0
- package/dist/templates/root/Dockerfile +19 -19
- package/dist/templates/root/README.md +38 -38
- package/dist/templates/root/public/css/style.css +7 -7
- package/dist/templates/root/public/js/login.js +58 -58
- package/dist/templates/root/public/js/logincallback.js +18 -18
- package/dist/templates/root/public/js/mytokens.js +10 -10
- package/dist/templates/root/public/js/refreshtokens.js +22 -22
- package/dist/templates/root/scripts/build.js +38 -38
- package/dist/templates/root/tsconfig.json +29 -27
- package/docs/ForeignKey.md +43 -43
- package/docs/appGenerated/auth.md +22 -22
- package/docs/developmentNote.md +22 -22
- package/docs/releaseNote/v0-5-0.md +17 -17
- package/docs/roadMap/futurePlan.md +9 -9
- package/docs/roadMap/released.md +49 -49
- package/docs/roadMap/v0-6-0.md +15 -15
- package/docs/vexJsonSchema.md +55 -55
- package/package.json +59 -58
- package/scripts/build.js +13 -13
- package/scripts/bump-version.js +86 -86
- package/scripts/copyTsTemplates.js +30 -30
- package/scripts/merge.js +124 -124
- package/tsconfig.json +121 -121
- package/vex.config.json +37 -36
- package/dist/templates/_types/VexResponsePayloadError.ts +0 -20
|
@@ -1,35 +1,55 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
|
-
import {
|
|
2
|
+
import { UserEntity } from "./../../_models/UserModel.gen";
|
|
3
|
+
import { UserAuthProfilesEntity } from "./../../_models/UserAuthProfilesModel.gen";
|
|
3
4
|
import { User } from "./../../_types/User.gen";
|
|
5
|
+
import { UserAuthProfiles } from "./../../_types/UserAuthProfiles.gen";
|
|
6
|
+
import { IVexRepository } from "./../../_types/IVexRepository.gen";
|
|
4
7
|
import JWTService from "./../auth/JWTService.gen";
|
|
5
8
|
import OAuthProfileMap, { IProfile } from "./OAuthProfileMap.gen";
|
|
9
|
+
import VexDb from "./../VexDb.gen";
|
|
6
10
|
import utils from "../../_utils";
|
|
7
11
|
|
|
8
|
-
|
|
9
12
|
export default class OAuthStrategyService {
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
private get userRepo(): IVexRepository<UserEntity> {
|
|
15
|
+
return VexDb.getRepository(UserEntity);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private get uapRepo(): IVexRepository<UserAuthProfilesEntity> {
|
|
19
|
+
return VexDb.getRepository(UserAuthProfilesEntity);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
constructor() {}
|
|
23
|
+
|
|
24
|
+
public async verify(accessToken: string, refreshToken: string, profile: IProfile, done: (error: any, user?: any) => void): Promise<void> {
|
|
12
25
|
try {
|
|
13
26
|
|
|
14
|
-
let user:
|
|
27
|
+
let user: UserEntity;
|
|
15
28
|
const authUser = new OAuthProfileMap().map(profile);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
const authProfile = authUser.userAuthProfiles?.[0];
|
|
30
|
+
|
|
31
|
+
// find user by oauthId + provider, or fall back to email
|
|
32
|
+
let existingUser: UserEntity | null = null;
|
|
33
|
+
if (authProfile?.oauthId && authProfile?.provider) {
|
|
34
|
+
const matchedProfile = await this.uapRepo.findOneWhere(
|
|
35
|
+
{ oauthId: authProfile.oauthId, provider: authProfile.provider },
|
|
36
|
+
);
|
|
37
|
+
if (matchedProfile?.userId) {
|
|
38
|
+
existingUser = await this.userRepo.findOne({ _id: matchedProfile.userId });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!existingUser && authUser.email) {
|
|
42
|
+
existingUser = await this.userRepo.findOneWhere({ email: authUser.email });
|
|
43
|
+
}
|
|
24
44
|
|
|
25
45
|
// 1. create new user
|
|
26
|
-
if (
|
|
46
|
+
if (!existingUser) {
|
|
27
47
|
user = await this.createNewUser(authUser);
|
|
28
48
|
}
|
|
29
49
|
// 2. update existing user
|
|
30
50
|
else {
|
|
31
51
|
console.log("OAuthVerify UpdateUser");
|
|
32
|
-
user = await this.processExistingUser(
|
|
52
|
+
user = await this.processExistingUser(authProfile, existingUser);
|
|
33
53
|
}
|
|
34
54
|
|
|
35
55
|
// set req.user
|
|
@@ -38,55 +58,53 @@ export default class OAuthStrategyService {
|
|
|
38
58
|
profile: new JWTService().sanitizeUser(user),
|
|
39
59
|
});
|
|
40
60
|
}
|
|
41
|
-
catch(err) {
|
|
42
|
-
return done(err);
|
|
61
|
+
catch (err) {
|
|
62
|
+
return done(err);
|
|
43
63
|
}
|
|
44
64
|
}
|
|
45
65
|
|
|
46
|
-
private async createNewUser(authProfile: User): Promise<
|
|
66
|
+
private async createNewUser(authProfile: User): Promise<UserEntity> {
|
|
47
67
|
utils.log.info("OAuthVerify > createNewUser");
|
|
48
|
-
const
|
|
49
|
-
|
|
68
|
+
const user = await this.userRepo.create(authProfile as Partial<UserEntity>);
|
|
69
|
+
|
|
70
|
+
// create auth profile row linked to the new user
|
|
71
|
+
if (authProfile.userAuthProfiles?.[0]) {
|
|
72
|
+
await this.uapRepo.create({
|
|
73
|
+
...authProfile.userAuthProfiles[0],
|
|
74
|
+
userId: user._id,
|
|
75
|
+
} as Partial<UserAuthProfilesEntity>);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return user;
|
|
50
79
|
}
|
|
51
80
|
|
|
52
|
-
private async processExistingUser(
|
|
81
|
+
private async processExistingUser(incomingProfile: UserAuthProfiles | undefined, existingUser: UserEntity): Promise<UserEntity> {
|
|
53
82
|
utils.log.info("OAuthVerify > processExistingUser");
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
const userProfiles = existingUser.authProfiles as User["authProfiles"] || [];
|
|
58
|
-
|
|
59
|
-
if(!authProfile){
|
|
60
|
-
utils.log.errorNoExit("OAuthVerify > Invalid OAuth Callback \"authProfile\"");
|
|
83
|
+
|
|
84
|
+
if (!incomingProfile) {
|
|
85
|
+
utils.log.error("OAuthVerify > Invalid OAuth Callback \"authProfile\"");
|
|
61
86
|
throw new Error("Invalid OAuth Callback \"authProfile\"");
|
|
62
87
|
}
|
|
63
88
|
|
|
64
|
-
//
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
utils.log.info("OAuthVerify > add new provider",
|
|
72
|
-
|
|
73
|
-
|
|
89
|
+
// check if this provider/oauthId already tracked
|
|
90
|
+
const existing = await this.uapRepo.findOneWhere(
|
|
91
|
+
{ userId: existingUser._id, provider: incomingProfile.provider, oauthId: incomingProfile.oauthId },
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (!existing) {
|
|
95
|
+
// B.2. add new provider
|
|
96
|
+
utils.log.info("OAuthVerify > add new provider", incomingProfile.provider);
|
|
97
|
+
await this.uapRepo.create({
|
|
98
|
+
...incomingProfile,
|
|
99
|
+
userId: existingUser._id,
|
|
100
|
+
} as Partial<UserAuthProfilesEntity>);
|
|
74
101
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
utils.log.info("OAuthVerify > update username",
|
|
78
|
-
|
|
79
|
-
userUpdated = true;
|
|
102
|
+
else if (existing.username !== incomingProfile.username) {
|
|
103
|
+
// B.3. update username if changed
|
|
104
|
+
utils.log.info("OAuthVerify > update username", incomingProfile.provider);
|
|
105
|
+
await this.uapRepo.update(existing._id, { username: incomingProfile.username });
|
|
80
106
|
}
|
|
81
107
|
|
|
82
|
-
|
|
83
|
-
utils.log.info("OAuthVerify > Update user");
|
|
84
|
-
return await existingUser.updateOne({ authProfiles: userProfiles });
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
return existingUser;
|
|
88
|
-
}
|
|
108
|
+
return existingUser;
|
|
89
109
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
110
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
|
|
3
|
+
import { Select, Filter, Join } from "./VexRequest.gen";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* DB-agnostic repository interface.
|
|
7
|
+
* Controllers and services talk only to this — the adapter layer (TypeORM/Mongoose) absorbs DB specifics.
|
|
8
|
+
*/
|
|
9
|
+
export interface IVexRepository<T> {
|
|
10
|
+
find(filter: Filter, join?: Join, select?: Select): Promise<T[]>;
|
|
11
|
+
findOne(filter: Filter, join?: Join, select?: Select): Promise<T | null>;
|
|
12
|
+
findOneWhere(filter: Filter, join?: Join, select?: Select): Promise<T | null>;
|
|
13
|
+
create(data: Partial<T>): Promise<T>;
|
|
14
|
+
replace(id: string | number, data: Partial<T>): Promise<T | null>;
|
|
15
|
+
update(id: string | number, data: Partial<T>): Promise<T | null>;
|
|
16
|
+
delete(id: string | number): Promise<void>;
|
|
17
|
+
deleteWhere(filter: Filter): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
2
|
+
import { ResponseCode } from "./../_utils/response.gen";
|
|
3
|
+
|
|
4
|
+
export default class VexResponse<T = unknown> extends Error {
|
|
5
|
+
public status: number;
|
|
6
|
+
public body?: {
|
|
7
|
+
code?: ResponseCode;
|
|
8
|
+
message?: string | null;
|
|
9
|
+
result?: T;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
status: number = 200,
|
|
14
|
+
body?: {
|
|
15
|
+
code?: ResponseCode;
|
|
16
|
+
message?: string | null;
|
|
17
|
+
result?: T;
|
|
18
|
+
}
|
|
19
|
+
) {
|
|
20
|
+
super("VexResponse");
|
|
21
|
+
this.name = "VexResponse";
|
|
22
|
+
this.status = status;
|
|
23
|
+
this.body = body;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,20 +1,21 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
1
2
|
import { ResponseCode } from "./../_utils/response.gen";
|
|
2
3
|
import utils from "../_utils/index";
|
|
3
4
|
|
|
4
5
|
export default class VexResponseError extends Error {
|
|
5
6
|
public status: number;
|
|
6
|
-
public ret_code: ResponseCode;
|
|
7
|
+
public ret_code: ResponseCode | string;
|
|
7
8
|
public ret_msg: any;
|
|
8
9
|
|
|
9
10
|
constructor(
|
|
10
11
|
status: number = 200,
|
|
11
|
-
code?: ResponseCode,
|
|
12
|
+
code?: ResponseCode | null,
|
|
12
13
|
message?: string,
|
|
13
14
|
) {
|
|
14
15
|
super(message);
|
|
15
16
|
this.name = "VexResponseError";
|
|
16
17
|
this.status = status;
|
|
17
|
-
this.ret_code = code || utils.response.code.SERVER_ERROR;
|
|
18
|
+
this.ret_code = code || utils.response.statusCodeMap.get(status) || utils.response.code.SERVER_ERROR;
|
|
18
19
|
this.ret_msg = message || utils.response.msg.SERVER_ERROR;
|
|
19
20
|
}
|
|
20
21
|
}
|
|
@@ -15,11 +15,11 @@ export function loadYaml(yamlFilePath: string) {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* Parse _select query/body param into a fields array
|
|
19
19
|
* @param fieldsString json stringified array of string
|
|
20
20
|
* @error may throw an error if the fieldsString is not a valid JSON
|
|
21
21
|
*/
|
|
22
|
-
export function parseFieldsSelect(req: Request) :
|
|
22
|
+
export function parseFieldsSelect(req: Request) : string[] | undefined {
|
|
23
23
|
let _selectRaw: string[] = [];
|
|
24
24
|
|
|
25
25
|
if(!req.query._select && !req.body._select) {
|
|
@@ -45,17 +45,12 @@ export function parseFieldsSelect(req: Request) : ({ [key: string]: number } | u
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
const selectFields = _selectRaw.reduce((obj: any, field: string) => {
|
|
50
|
-
obj[field] = 1;
|
|
51
|
-
return obj;
|
|
52
|
-
}, {} as Record<string, number>);
|
|
53
|
-
|
|
54
|
-
return selectFields;
|
|
48
|
+
return _selectRaw;
|
|
55
49
|
}
|
|
56
50
|
|
|
57
|
-
export function parseCollectionJoin(req: Request, availablePopulateOptions
|
|
58
|
-
if
|
|
51
|
+
export function parseCollectionJoin(req: Request, availablePopulateOptions?: {[key:string]: string}) : Array<{ path: string; select?: string; options?: any }> | undefined {
|
|
52
|
+
// if no _join param provided, nothing to do
|
|
53
|
+
if(!req.query._join && !req.body._join) {
|
|
59
54
|
return undefined;
|
|
60
55
|
}
|
|
61
56
|
|
|
@@ -83,24 +78,63 @@ export function parseCollectionJoin(req: Request, availablePopulateOptions:{[key
|
|
|
83
78
|
}
|
|
84
79
|
|
|
85
80
|
// switch joinArr's item to populate options
|
|
86
|
-
const populateOptions: {
|
|
81
|
+
const populateOptions: Array<{ path: string; select?: string; options?: any }> = [];
|
|
82
|
+
const available = availablePopulateOptions || {};
|
|
87
83
|
if(_joinRaw.length > 0) {
|
|
88
84
|
_joinRaw.forEach((refKey: any) => {
|
|
89
|
-
if(
|
|
85
|
+
if(available[refKey]) {
|
|
90
86
|
populateOptions.push({
|
|
91
87
|
path: refKey,
|
|
92
|
-
select:
|
|
88
|
+
select: available[refKey],
|
|
93
89
|
options: { lean: true }
|
|
94
90
|
});
|
|
95
91
|
}
|
|
92
|
+
else {
|
|
93
|
+
// include basic populate entry even if no select mapping provided
|
|
94
|
+
populateOptions.push({ path: refKey, options: { lean: true } });
|
|
95
|
+
}
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
return populateOptions;
|
|
99
|
+
return populateOptions.length > 0 ? populateOptions : undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Parse _join query/body param into a TypeORM relations array
|
|
104
|
+
* @param req Express request
|
|
105
|
+
* @param allowedRelations optional allowlist — if provided, only matching relation names are returned
|
|
106
|
+
*/
|
|
107
|
+
export function parseRelations(req: Request, allowedRelations?: string[]): string[] | undefined {
|
|
108
|
+
if (!req.query._join && !req.body._join) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let _joinRaw: string[] = [];
|
|
113
|
+
|
|
114
|
+
if (req.method === "GET") {
|
|
115
|
+
const rawString = String(req.query._join);
|
|
116
|
+
if (!rawString) return undefined;
|
|
117
|
+
_joinRaw = JSON.parse(rawString);
|
|
118
|
+
} else {
|
|
119
|
+
_joinRaw = req.body._join;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const err = new VexResponseError(400, response.code.err_validation, "Invalid field \"_join\", only json array of relation names accepted");
|
|
123
|
+
if (!Array.isArray(_joinRaw)) throw err;
|
|
124
|
+
for (const item of _joinRaw) {
|
|
125
|
+
if (typeof item !== "string") throw err;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const filtered = allowedRelations && allowedRelations.length > 0
|
|
129
|
+
? _joinRaw.filter(r => allowedRelations.includes(r))
|
|
130
|
+
: _joinRaw;
|
|
131
|
+
|
|
132
|
+
return filtered.length > 0 ? filtered : undefined;
|
|
100
133
|
}
|
|
101
134
|
|
|
102
135
|
export default {
|
|
103
136
|
loadYaml,
|
|
104
137
|
parseFieldsSelect,
|
|
105
|
-
parseCollectionJoin
|
|
138
|
+
parseCollectionJoin,
|
|
139
|
+
parseRelations
|
|
106
140
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// {{headerComment}}
|
|
2
2
|
import crypto from "crypto";
|
|
3
|
+
// import log from "~/utils/logger";
|
|
3
4
|
import { User } from "../_types/User.gen";
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -19,7 +20,7 @@ export function hashPassword(
|
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* Verifies if a plain password (with salt) matches a given hashed password.
|
|
22
|
-
* @param user The user object containing email and
|
|
23
|
+
* @param user The user object containing email and userAuthProfiles.
|
|
23
24
|
* @param plainPassword The plain text password to verify.
|
|
24
25
|
* @returns True if the password matches, false otherwise.
|
|
25
26
|
*/
|
|
@@ -27,14 +28,14 @@ export function verifyPassword(
|
|
|
27
28
|
user: User,
|
|
28
29
|
plainPassword: string,
|
|
29
30
|
): boolean {
|
|
30
|
-
if(!user.email || !user.
|
|
31
|
-
console.
|
|
31
|
+
if(!user.email || !user.userAuthProfiles) {
|
|
32
|
+
console.log(`User "${user._id}" email or userAuthProfiles missing for local auth's password verification.`);
|
|
32
33
|
return false;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
const hashedPassword = user.
|
|
36
|
+
const hashedPassword = user.userAuthProfiles.find((profile:any) => profile.provider === "local")?.password;
|
|
36
37
|
if(!hashedPassword) {
|
|
37
|
-
console.
|
|
38
|
+
console.log(`User "${user._id}" Local auth profile or password missing.`);
|
|
38
39
|
return false;
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -1,43 +1,44 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
1
2
|
export function processing(msg: any, ...optionalParams: any[]): void {
|
|
2
|
-
console.log("\x1b[34m%s\x1b[0m", "[PROCESSING]" , new Date().toLocaleString() +" -", msg, optionalParams
|
|
3
|
+
console.log("\x1b[34m%s\x1b[0m", "[PROCESSING]" , new Date().toLocaleString() +" -", msg, ...optionalParams);
|
|
3
4
|
}
|
|
4
5
|
|
|
5
6
|
export function ok(msg: any, ...optionalParams: any[]): void {
|
|
6
|
-
console.log("\x1b[32m%s\x1b[0m", "[OK]" , new Date().toLocaleString() +" -", msg, optionalParams
|
|
7
|
+
console.log("\x1b[32m%s\x1b[0m", "[OK]" , new Date().toLocaleString() +" -", msg, ...optionalParams);
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
export function info(msg: any, ...optionalParams: any[]): void {
|
|
10
|
-
console.log("\x1b[36m%s\x1b[0m", "[INFO]" , new Date().toLocaleString() +" -", msg, optionalParams
|
|
11
|
+
console.log("\x1b[36m%s\x1b[0m", "[INFO]" , new Date().toLocaleString() +" -", msg, ...optionalParams);
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export function warn(msg: any, ...optionalParams: any[]): void {
|
|
14
|
-
console.log("\x1b[33m%s\x1b[0m", "[WARN]" , new Date().toLocaleString() +" -", msg, optionalParams
|
|
15
|
+
console.log("\x1b[33m%s\x1b[0m", "[WARN]" , new Date().toLocaleString() +" -", msg, ...optionalParams);
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
export function error(msg: any, ...optionalParams: any[]):
|
|
18
|
-
|
|
19
|
-
process.exit(1);
|
|
18
|
+
export function error(msg: any, ...optionalParams: any[]): void {
|
|
19
|
+
console.log("\x1b[41m%s\x1b[0m", "[ERROR]" , new Date().toLocaleString() +" -", msg, ...optionalParams);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function
|
|
23
|
-
|
|
22
|
+
export function errorExit(msg: any, ...optionalParams: any[]): never {
|
|
23
|
+
error(msg, ...optionalParams);
|
|
24
|
+
process.exit(1);
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
// wrapper
|
|
27
28
|
export function infoMongo(msg: any, ...optionalParams: any[]): void {
|
|
28
|
-
info("[MONGO]", msg, optionalParams);
|
|
29
|
+
info("[MONGO]", msg, ...optionalParams);
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
export function errorMongo(msg: any, ...optionalParams: any[]): void {
|
|
32
|
-
|
|
33
|
+
error("[MONGO]", msg, ...optionalParams);
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export function infoSql(msg: any, ...optionalParams: any[]): void {
|
|
36
|
-
info("[SQL]", msg, optionalParams);
|
|
37
|
+
info("[SQL]", msg, ...optionalParams);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
export function errorSql(msg: any, ...optionalParams: any[]): void {
|
|
40
|
-
|
|
41
|
+
error("[SQL]", msg, ...optionalParams);
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
export default {
|
|
@@ -45,8 +46,8 @@ export default {
|
|
|
45
46
|
ok,
|
|
46
47
|
info,
|
|
47
48
|
warn,
|
|
49
|
+
errorExit,
|
|
48
50
|
error,
|
|
49
|
-
errorNoExit,
|
|
50
51
|
|
|
51
52
|
infoMongo,
|
|
52
53
|
errorMongo,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// {{headerComment}}
|
|
1
2
|
import { Response } from "express";
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -23,7 +24,7 @@ export function send<T = undefined>(
|
|
|
23
24
|
return res.status(status).send();
|
|
24
25
|
}
|
|
25
26
|
else {
|
|
26
|
-
const code: ResponseCode | undefined = body?.code ?? responseStatusCodeMap.get(status)
|
|
27
|
+
const code: ResponseCode | undefined = body?.code ?? responseStatusCodeMap.get(status) ?? responseCode.SERVER_ERROR;
|
|
27
28
|
const msg: string | undefined = body?.message ?? (code ? responseMessage[code] : undefined);
|
|
28
29
|
|
|
29
30
|
return res.status(status)
|
|
@@ -39,7 +40,7 @@ export function send<T = undefined>(
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
const responseMessage = {
|
|
43
|
+
const responseMessage : Record<string, string> = {
|
|
43
44
|
|
|
44
45
|
DB_CONN_ERR: "Database connection error",
|
|
45
46
|
SERVER_ERROR: "Unknown Server Error",
|
|
@@ -133,7 +134,7 @@ const responseStatusCodeMap = new Map<number, string>([
|
|
|
133
134
|
[511, "Network Authentication Required"],
|
|
134
135
|
]);
|
|
135
136
|
|
|
136
|
-
export type ResponseCode = keyof typeof responseMessage;
|
|
137
|
+
export type ResponseCode = keyof typeof responseMessage | string;
|
|
137
138
|
|
|
138
139
|
export default {
|
|
139
140
|
send,
|
|
@@ -1,32 +1,35 @@
|
|
|
1
|
-
{
|
|
2
|
-
"type": "object",
|
|
3
|
-
"x-documentConfig": {
|
|
4
|
-
"documentName": "Session",
|
|
5
|
-
"methods": []
|
|
6
|
-
},
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
},
|
|
21
|
-
"userId":{
|
|
22
|
-
"type": "string",
|
|
23
|
-
"x-format": "ObjectId",
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"x-documentConfig": {
|
|
4
|
+
"documentName": "Session",
|
|
5
|
+
"methods": []
|
|
6
|
+
},
|
|
7
|
+
"required": ["provider", "userId", "expired"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"_id": {
|
|
10
|
+
"type": "string",
|
|
11
|
+
"x-format": "ObjectId",
|
|
12
|
+
"index": true
|
|
13
|
+
},
|
|
14
|
+
"sessionCode": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"index": true
|
|
17
|
+
},
|
|
18
|
+
"provider": {
|
|
19
|
+
"type": "string"
|
|
20
|
+
},
|
|
21
|
+
"userId":{
|
|
22
|
+
"type": "string",
|
|
23
|
+
"x-format": "ObjectId",
|
|
24
|
+
"x-foreignKey": {
|
|
25
|
+
"schemaName": "User",
|
|
26
|
+
"fieldName": "_id",
|
|
27
|
+
"relationType": "one-to-one"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"expired": {
|
|
31
|
+
"type": "number",
|
|
32
|
+
"description": "expired time in unix timestamp format (ms)"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
32
35
|
}
|