very-express 0.5.17 → 0.5.19
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.
|
@@ -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,34 @@ 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 {
|
|
21
|
-
|
|
22
|
-
const selectString = req.query._select;
|
|
23
|
-
const ErrorArrStrMsg = "Invalid \"_select\" string, only json array accepted";
|
|
22
|
+
export function parseFieldsSelect(req: Request) : ({ [key: string]: number } | undefined) {
|
|
23
|
+
let _selectRaw: string[] = [];
|
|
24
24
|
|
|
25
|
-
if
|
|
25
|
+
if(!req.query._select && !req.body._select) {
|
|
26
26
|
return undefined;
|
|
27
27
|
}
|
|
28
|
-
else if (typeof selectString !== "string") {
|
|
29
|
-
throw new Error(ErrorArrStrMsg);
|
|
30
|
-
}
|
|
31
28
|
|
|
32
|
-
|
|
29
|
+
if(req.method === "GET") {
|
|
30
|
+
const selectString = String(req.query._select);
|
|
31
|
+
_selectRaw = JSON.parse(selectString);
|
|
32
|
+
}
|
|
33
|
+
else{
|
|
34
|
+
_selectRaw = req.body._select;
|
|
35
|
+
}
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
const ErrorDataType = new VexResponseError(400, response.code.err_validation, "Invalid field \"_select\", only json array of string accepted");
|
|
38
|
+
if (!Array.isArray(_selectRaw)) {
|
|
39
|
+
throw ErrorDataType;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
for (let i = 0; i <
|
|
39
|
-
if (typeof
|
|
40
|
-
throw
|
|
42
|
+
for (let i = 0; i < _selectRaw.length; i++) {
|
|
43
|
+
if (typeof _selectRaw[i] !== "string") {
|
|
44
|
+
throw ErrorDataType;
|
|
41
45
|
}
|
|
42
46
|
}
|
|
47
|
+
|
|
43
48
|
// Convert the fieldArr to an object that can be used in the select method
|
|
44
|
-
const selectFields =
|
|
49
|
+
const selectFields = _selectRaw.reduce((obj: any, field: string) => {
|
|
45
50
|
obj[field] = 1;
|
|
46
51
|
return obj;
|
|
47
52
|
}, {} as Record<string, number>);
|
|
@@ -50,24 +55,37 @@ export function parseFieldsSelect(req: Request) : { [key: string]: number } | un
|
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
export function parseCollectionJoin(req: Request, availablePopulateOptions:{[key:string]: string}) : { [key: string]: string } | undefined {
|
|
53
|
-
|
|
54
|
-
const joinString = req.query._join;
|
|
55
|
-
const ErrorArrStrMsg = "Invalid \"_select\" string, only json array accepted";
|
|
56
|
-
const populateOptions: any = [];
|
|
57
|
-
|
|
58
|
-
if (typeof joinString === "undefined" || joinString === "") {
|
|
58
|
+
if(!req.query._select && !req.body._select) {
|
|
59
59
|
return undefined;
|
|
60
60
|
}
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
|
|
62
|
+
let _joinRaw: string[] = [];
|
|
63
|
+
|
|
64
|
+
if(req.method === "GET") {
|
|
65
|
+
const rawString = String(req.query._join);
|
|
66
|
+
if (typeof rawString === "undefined" || rawString === "") {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
_joinRaw = JSON.parse(rawString);
|
|
70
|
+
}
|
|
71
|
+
else{
|
|
72
|
+
_joinRaw = req.body._join;
|
|
63
73
|
}
|
|
64
74
|
|
|
65
|
-
const
|
|
75
|
+
const ErrorDataType = new VexResponseError(400, response.code.err_validation, "Invalid field \"_join\", only json array of string accepted");
|
|
76
|
+
if (!Array.isArray(_joinRaw)) {
|
|
77
|
+
throw ErrorDataType;
|
|
78
|
+
}
|
|
79
|
+
for (let i = 0; i < _joinRaw.length; i++) {
|
|
80
|
+
if (typeof _joinRaw[i] !== "string") {
|
|
81
|
+
throw ErrorDataType;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
66
84
|
|
|
67
85
|
// switch joinArr's item to populate options
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
86
|
+
const populateOptions: {[key:string]:any} = [];
|
|
87
|
+
if(_joinRaw.length > 0) {
|
|
88
|
+
_joinRaw.forEach((refKey: any) => {
|
|
71
89
|
if(availablePopulateOptions[refKey]) {
|
|
72
90
|
populateOptions.push({
|
|
73
91
|
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
|
+
});
|