very-express 0.5.17 → 0.5.18
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 +373 -386
- package/dist/templates/_utils/common.ts +37 -26
- package/dist/templates/root/public/js/login.js +59 -0
- package/package.json +1 -1
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
import jsYaml from "js-yaml";
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
import { Request } from "express";
|
|
5
|
+
import VexResponseError from "../_types/VexResponseError.gen";
|
|
6
|
+
import response from "./response.gen";
|
|
5
7
|
|
|
6
8
|
export function loadYaml(yamlFilePath: string) {
|
|
7
9
|
try {
|
|
@@ -17,31 +19,35 @@ export function loadYaml(yamlFilePath: string) {
|
|
|
17
19
|
* @param fieldsString json stringified array of string
|
|
18
20
|
* @error may throw an error if the fieldsString is not a valid JSON
|
|
19
21
|
*/
|
|
20
|
-
export function parseFieldsSelect(req: Request) : { [key: string]: number } | undefined {
|
|
22
|
+
export function parseFieldsSelect(req: Request) : ({ [key: string]: number } | undefined) {
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
+
let _selectRaw: string[] = [];
|
|
25
|
+
const ErrorDataType = new VexResponseError(400, response.code.err_validation, "Invalid field \"_select\", only json array accepted");
|
|
24
26
|
|
|
25
|
-
if
|
|
26
|
-
|
|
27
|
+
if(req.method === "GET") {
|
|
28
|
+
const selectString = String(req.query._select);
|
|
29
|
+
|
|
30
|
+
if (typeof selectString === "undefined" || selectString === "") {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
_selectRaw = JSON.parse(selectString);
|
|
27
34
|
}
|
|
28
|
-
else
|
|
29
|
-
|
|
35
|
+
else{
|
|
36
|
+
_selectRaw = req.body._select;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!Array.isArray(fieldArr)) {
|
|
35
|
-
throw new Error(ErrorArrStrMsg);
|
|
39
|
+
if (!Array.isArray(_selectRaw)) {
|
|
40
|
+
throw ErrorDataType;
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
for (let i = 0; i <
|
|
39
|
-
if (typeof
|
|
40
|
-
throw
|
|
43
|
+
for (let i = 0; i < _selectRaw.length; i++) {
|
|
44
|
+
if (typeof _selectRaw[i] !== "string") {
|
|
45
|
+
throw ErrorDataType;
|
|
41
46
|
}
|
|
42
47
|
}
|
|
48
|
+
|
|
43
49
|
// Convert the fieldArr to an object that can be used in the select method
|
|
44
|
-
const selectFields =
|
|
50
|
+
const selectFields = _selectRaw.reduce((obj: any, field: string) => {
|
|
45
51
|
obj[field] = 1;
|
|
46
52
|
return obj;
|
|
47
53
|
}, {} as Record<string, number>);
|
|
@@ -51,23 +57,28 @@ export function parseFieldsSelect(req: Request) : { [key: string]: number } | un
|
|
|
51
57
|
|
|
52
58
|
export function parseCollectionJoin(req: Request, availablePopulateOptions:{[key:string]: string}) : { [key: string]: string } | undefined {
|
|
53
59
|
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const populateOptions: any = [];
|
|
60
|
+
let _joinRaw: string[] = [];
|
|
61
|
+
const ErrorDataType = new VexResponseError(400, response.code.err_validation, "Invalid field \"_join\", only json array accepted");
|
|
57
62
|
|
|
58
|
-
if
|
|
59
|
-
|
|
63
|
+
if(req.method === "GET") {
|
|
64
|
+
const rawString = String(req.query._join);
|
|
65
|
+
if (typeof rawString === "undefined" || rawString === "") {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
_joinRaw = JSON.parse(rawString);
|
|
60
69
|
}
|
|
61
|
-
else
|
|
62
|
-
|
|
70
|
+
else{
|
|
71
|
+
_joinRaw = req.body._join;
|
|
63
72
|
}
|
|
64
73
|
|
|
65
|
-
|
|
74
|
+
if (!Array.isArray(_joinRaw)) {
|
|
75
|
+
throw ErrorDataType;
|
|
76
|
+
}
|
|
66
77
|
|
|
67
78
|
// switch joinArr's item to populate options
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
79
|
+
const populateOptions: {[key:string]:any} = [];
|
|
80
|
+
if(_joinRaw.length > 0) {
|
|
81
|
+
_joinRaw.forEach((refKey: any) => {
|
|
71
82
|
if(availablePopulateOptions[refKey]) {
|
|
72
83
|
populateOptions.push({
|
|
73
84
|
path: refKey,
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
|
|
2
|
+
document.addEventListener("DOMContentLoaded", function() {
|
|
3
|
+
// add event listener to login button
|
|
4
|
+
document.querySelector("#localLoginBtn").addEventListener("click", function() {
|
|
5
|
+
// call api /auth/local
|
|
6
|
+
fetch("/auth/local", {
|
|
7
|
+
method: "POST",
|
|
8
|
+
headers: {
|
|
9
|
+
"Content-Type": "application/json"
|
|
10
|
+
},
|
|
11
|
+
body: JSON.stringify({
|
|
12
|
+
email: document.querySelector("#email").value,
|
|
13
|
+
password: document.querySelector("#password").value
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
.then(async (res) => {
|
|
17
|
+
if(res.status === 302) {
|
|
18
|
+
const data = await res.json();
|
|
19
|
+
if(data.result?.url){
|
|
20
|
+
window.location.href = data.result?.url;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
alert("Login successful, but no redirect URL provided.");
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const data = await res.json();
|
|
28
|
+
alert("Login failed: " + data.message);
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
.catch((err) => {
|
|
32
|
+
console.error("Error during login:", err);
|
|
33
|
+
alert("An error occurred during login. Please try again later.");
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
document.querySelector("#localRegisterBtn").addEventListener("click", function() {
|
|
38
|
+
// call api /auth/local
|
|
39
|
+
fetch("/auth/register", {
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "application/json"
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify({
|
|
45
|
+
email: document.querySelector("#email").value,
|
|
46
|
+
password: document.querySelector("#password").value
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
.then(async (res) => {
|
|
50
|
+
if(res.status === 201) {
|
|
51
|
+
alert("Registration successful. You can now log in.");
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
.catch((err) => {
|
|
55
|
+
console.error("Error during registration:", err);
|
|
56
|
+
alert("An error occurred during registration. Please try again later.");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|