notherbase-fs 4.1.0 → 4.1.1

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.
@@ -19,10 +19,20 @@ export default class SpiritWorld {
19
19
 
20
20
  this.user = new User();
21
21
  this.router = express.Router();
22
- this.router.post("/loadAll", this.loadAll);
23
- this.router.post("/load", this.load);
24
- this.router.post("/serve", this.serve);
25
- this.router.use("/user", this.user.router);
22
+ this.router.post("/loadAll", this.catchErrors, this.loadAll);
23
+ this.router.post("/load", this.catchErrors, this.load);
24
+ this.router.post("/serve", this.catchErrors, this.serve);
25
+ this.router.use("/user", this.catchErrors, this.user.router);
26
+ }
27
+
28
+ // abstract out the try catch pattern
29
+ catchErrors = async (req, res, next) => {
30
+ try {
31
+ await next();
32
+ } catch (error) {
33
+ console.log(error);
34
+ fail(res, "Server error");
35
+ }
26
36
  }
27
37
 
28
38
  /**
@@ -91,29 +101,24 @@ export default class SpiritWorld {
91
101
  * @returns {Object} The requested spirits.
92
102
  */
93
103
  loadAll = async (req, res) => {
94
- try {
95
- let parent = null;
96
- let data = req.body.data ? req.body.data : {};
97
- let id = req.body.id ? req.body.id : null;
98
-
99
- // if the scope is local, the parent is the user's id
100
- if (req.body.scope === "local") {
101
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
102
- if (user?.memory?._id) parent = user.memory._id;
103
- else {
104
- fail(res, "User had no id on load()");
105
- return;
106
- }
107
- }
104
+ let parent = null;
105
+ let data = req.body.data ? req.body.data : {};
106
+ let id = req.body.id ? req.body.id : null;
107
+
108
+ // if the scope is local, the parent is the user's id
109
+ if (req.body.scope === "local") {
110
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
111
+ if (user?.memory?._id) parent = user.memory._id;
112
+ else {
113
+ fail(res, "User had no id on load()");
114
+ return;
115
+ }
116
+ }
108
117
 
109
- // recall all spirits with the given service name and parent
110
- let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
118
+ // recall all spirits with the given service name and parent
119
+ let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
111
120
 
112
- res.send(spirits);
113
- } catch (error) {
114
- console.log(error);
115
- fail(res, "Server error");
116
- }
121
+ res.send(spirits);
117
122
  }
118
123
 
119
124
  /**
@@ -123,29 +128,24 @@ export default class SpiritWorld {
123
128
  * @returns {Object} The requested spirit.
124
129
  */
125
130
  load = async (req, res) => {
126
- try {
127
- let parent = null;
128
- let data = req.body.data ? req.body.data : {};
129
- let id = req.body.id ? req.body.id : null;
130
-
131
- // if the scope is local, the parent is the user's id
132
- if (req.body.scope === "local") {
133
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
134
- if (user?.memory?._id) parent = user.memory._id;
135
- else {
136
- fail(res, "User had no id on load()");
137
- return;
138
- }
139
- }
131
+ let parent = null;
132
+ let data = req.body.data ? req.body.data : {};
133
+ let id = req.body.id ? req.body.id : null;
134
+
135
+ // if the scope is local, the parent is the user's id
136
+ if (req.body.scope === "local") {
137
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
138
+ if (user?.memory?._id) parent = user.memory._id;
139
+ else {
140
+ fail(res, "User had no id on load()");
141
+ return;
142
+ }
143
+ }
140
144
 
141
- // recall all spirits with the given service name and parent
142
- let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
145
+ // recall all spirits with the given service name and parent
146
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
143
147
 
144
- res.send(spirit);
145
- } catch (error) {
146
- console.log(error);
147
- fail(res, "Server error");
148
- }
148
+ res.send(spirit);
149
149
  }
150
150
 
151
151
  /**
@@ -154,22 +154,17 @@ export default class SpiritWorld {
154
154
  * @param {Object} res
155
155
  */
156
156
  serve = async (req, res) => {
157
- try {
158
- let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
159
-
160
- let script, result = null;
157
+ let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
158
+
159
+ let script, result = null;
161
160
 
162
- if (fs.existsSync(scriptPath)) {
163
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
161
+ if (fs.existsSync(scriptPath)) {
162
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
164
163
 
165
- script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
166
- result = await script.default(req, user, this.io);
167
- success(res, "Served.", result);
168
- }
169
- else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
170
- } catch (error) {
171
- console.log(error);
172
- fail(res, "Server error");
164
+ script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
165
+ result = await script.default(req, user, this.io);
166
+ success(res, "Served.", result);
173
167
  }
168
+ else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
174
169
  }
175
170
  }
@@ -221,19 +221,21 @@ export default class User {
221
221
  let user = await req.db.Spirit.recallOne("user", null, { username: req.session.currentUser });
222
222
 
223
223
  if (check(res, user, "Account not found!")) {
224
- let passResult = await bcrypt.compare(req.body.password, user.memory.data.password);
225
-
226
- if (check(res, passResult, "Password doesn't match the username.")) {
227
- let data = JSON.parse(req.body.data);
228
- let imported = 0;
229
- for (let i = 0; i < data.length; i++) {
230
- if (data[i].parent != null) {
231
- let spirit = await req.db.Spirit.create(data[i].service, data[i].data, user.memory._id);
232
- if (spirit) imported++;
224
+ if (check(res, req.body.password, "Password error.")) {
225
+ let passResult = await bcrypt.compare(req.body.password, user.memory.data.password);
226
+
227
+ if (check(res, passResult, "Password doesn't match the username.")) {
228
+ let data = JSON.parse(req.body.data);
229
+ let imported = 0;
230
+ for (let i = 0; i < data.length; i++) {
231
+ if (data[i].parent != null) {
232
+ let spirit = await req.db.Spirit.create(data[i].service, data[i].data, user.memory._id);
233
+ if (spirit) imported++;
234
+ }
233
235
  }
234
- }
235
236
 
236
- success(res, "Data Imported", imported);
237
+ success(res, "Data Imported", imported);
238
+ }
237
239
  }
238
240
  }
239
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {