jgloo 1.6.0 → 1.8.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,33 @@
1
+ <a name="1.8.0"></a>
2
+
3
+ ## [1.8.0](https://github.com/zosma180/jgloo/compare/1.7.0...1.8.0) (2022-07-29)
4
+
5
+ ### Features
6
+
7
+ - Added the optional "priority" property to the configuration
8
+
9
+ ---
10
+
11
+ <a name="1.7.0"></a>
12
+
13
+ ## [1.7.0](https://github.com/zosma180/jgloo/compare/1.6.1...1.7.0) (2022-07-28)
14
+
15
+ ### Features
16
+
17
+ - Added the support for the recursive folder loading (api and middlewares)
18
+
19
+ ---
20
+
21
+ <a name="1.6.1"></a>
22
+
23
+ ## [1.6.1](https://github.com/zosma180/jgloo/compare/1.6.0...1.6.1) (2022-07-22)
24
+
25
+ ### Bug Fixes
26
+
27
+ - Updated the package-lock to fix the security issues
28
+
29
+ ---
30
+
1
31
  <a name="1.6.0"></a>
2
32
 
3
33
  ## [1.6.0](https://github.com/zosma180/jgloo/compare/1.5.2...1.6.0) (2022-02-20)
package/README.md CHANGED
@@ -46,6 +46,7 @@ Now you are ready to [create your first API](#create-a-simple-api).
46
46
  - [Expose the static files](#expose-the-static-files)
47
47
  - [Handle requests with file uploads](#handle-requests-with-file-uploads)
48
48
  - [Simulate network delay](#simulate-network-delay)
49
+ - [Manage path conflicts](#manage-path-conflicts)
49
50
  - [Run the server](#run-the-server)
50
51
 
51
52
  ---
@@ -197,6 +198,25 @@ module.exports = {
197
198
 
198
199
  ---
199
200
 
201
+ ### Manage path conflicts
202
+
203
+ If you have a scenario where two or more paths have conflicting values, e.g.:
204
+ - /my-path/:id
205
+ - /my-path/my-sub-path
206
+
207
+ you can add the `priority` property to your API configuration:
208
+
209
+ ```javascript
210
+ module.exports = {
211
+ ...
212
+ priority: 2
213
+ };
214
+ ```
215
+
216
+ The default value is 0. The api with the higher value will be used.
217
+
218
+ ---
219
+
200
220
  ### Run the server
201
221
 
202
222
  To run the server execute the following command in your project root:
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "jgloo",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "description": "The coldest mock server.",
5
- "homepage": "https://github.com/zosma180/jgloo#readme",
5
+ "homepage": "https://github.com/zosma180/jgloo",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/zosma180/jgloo.git"
@@ -29,7 +29,7 @@
29
29
  "cors": "^2.8.5",
30
30
  "express": "^4.17.1",
31
31
  "minimist": "^1.2.5",
32
- "multer": "^1.4.2",
32
+ "multer": "^1.4.5-lts.1",
33
33
  "nodemon": "^2.0.4"
34
34
  }
35
35
  }
package/server.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const path = require('path');
2
- const { existsSync, readdirSync } = require('fs');
2
+ const { existsSync, readdirSync, lstatSync } = require('fs');
3
3
  const express = require('express');
4
4
  const cors = require('cors');
5
5
  const bodyParser = require('body-parser');
@@ -22,6 +22,22 @@ if (!existsSync(root)) {
22
22
  process.exit(2);
23
23
  }
24
24
 
25
+ // Utility to deep load files
26
+ const walk = (entry, level = 1) => {
27
+ let list = [];
28
+ const items = readdirSync(entry);
29
+
30
+ items.forEach(item => {
31
+ const subEntry = path.join(entry, item);
32
+
33
+ lstatSync(subEntry).isDirectory() && level < 10
34
+ ? list = list.concat(walk(subEntry, level + 1))
35
+ : list.push(subEntry);
36
+ });
37
+
38
+ return list;
39
+ };
40
+
25
41
  // Config
26
42
  app.use(cors());
27
43
  app.use(bodyParser.json());
@@ -29,10 +45,10 @@ app.use(multer({ dest: staticPath }).any());
29
45
 
30
46
  // Add middlewares
31
47
  if (existsSync(middlewarePath)) {
32
- readdirSync(middlewarePath)
33
- .filter((file) => file.endsWith('.js'))
34
- .forEach((file) => {
35
- const middleware = require(path.join(middlewarePath, file));
48
+ walk(middlewarePath)
49
+ .filter(file => file.endsWith('.js'))
50
+ .forEach(file => {
51
+ const middleware = require(file);
36
52
  app.use(middleware);
37
53
  });
38
54
  }
@@ -48,16 +64,17 @@ if (!existsSync(apiPath)) {
48
64
  process.exit(2);
49
65
  }
50
66
 
51
- const api = readdirSync(apiPath).filter((file) => file.endsWith('.js'));
67
+ const api = walk(apiPath)
68
+ .filter(file => file.endsWith('.js'))
69
+ .map(file => require(file))
70
+ .sort((a, b) => (b.priority || 0) - (a.priority || 0));
52
71
 
53
72
  if (!api.length) {
54
73
  console.error(`No API file defined. Create one.`);
55
74
  process.exit(2);
56
75
  }
57
76
 
58
- api.forEach((file) => {
59
- const config = require(path.join(apiPath, file));
60
-
77
+ api.forEach(config => {
61
78
  // Add the API delay, if it is provided
62
79
  if (config.delay) {
63
80
  app.use(config.path, getDelayMiddleware(config.delay));
@@ -74,3 +91,4 @@ api.forEach((file) => {
74
91
 
75
92
  const message = `jgloo builded on the ice shelf "${root}" and the port ${port}.`;
76
93
  app.listen(port, () => console.log('\x1b[36m', message));
94
+