jgloo 1.5.2 → 1.7.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.7.0"></a>
2
+
3
+ ## [1.7.0](https://github.com/zosma180/jgloo/compare/1.6.1...1.7.0) (2022-07-28)
4
+
5
+ ### Features
6
+
7
+ - Added the support for the recursive folder loading (api and middlewares)
8
+
9
+ ---
10
+
11
+ <a name="1.6.1"></a>
12
+
13
+ ## [1.6.1](https://github.com/zosma180/jgloo/compare/1.6.0...1.6.1) (2022-07-22)
14
+
15
+ ### Bug Fixes
16
+
17
+ - Updated the package-lock to fix the security issues
18
+
19
+ ---
20
+
21
+ <a name="1.6.0"></a>
22
+
23
+ ## [1.6.0](https://github.com/zosma180/jgloo/compare/1.5.2...1.6.0) (2022-02-20)
24
+
25
+ ### Features
26
+
27
+ - Added the optional "delay" property to the configuration
28
+
29
+ ---
30
+
1
31
  <a name="1.5.2"></a>
2
32
 
3
33
  ## [1.5.2](https://github.com/zosma180/jgloo/compare/1.5.1...1.5.2) (2021-02-21)
package/README.md CHANGED
@@ -27,7 +27,7 @@ This project is based on the Node framework Express. The highlights are:
27
27
  ## Installation
28
28
 
29
29
  ```bash
30
- npm i -g jgloo
30
+ npm i -D jgloo
31
31
  ```
32
32
 
33
33
  After the installation create a folder "mock" in your project root (you can use another path and folder name if you want).
@@ -45,6 +45,7 @@ Now you are ready to [create your first API](#create-a-simple-api).
45
45
  - [Where data are stored](#where-data-are-stored)
46
46
  - [Expose the static files](#expose-the-static-files)
47
47
  - [Handle requests with file uploads](#handle-requests-with-file-uploads)
48
+ - [Simulate network delay](#simulate-network-delay)
48
49
  - [Run the server](#run-the-server)
49
50
 
50
51
  ---
@@ -53,7 +54,7 @@ Now you are ready to [create your first API](#create-a-simple-api).
53
54
 
54
55
  To setup your first API create a new file "hello.js" in the "api" folder. The name of the file does not matter. Then insert the following snippet:
55
56
 
56
- ```typescript
57
+ ```javascript
57
58
  module.exports = {
58
59
  path: '/hello',
59
60
  method: 'get',
@@ -72,7 +73,7 @@ You are ready to [run the server](#run-the-server) now.
72
73
 
73
74
  To setup a ReST API, you have to create a new file in the "api" folder with the name you prefer and the following snippet:
74
75
 
75
- ```typescript
76
+ ```javascript
76
77
  module.exports = {
77
78
  path: '/user',
78
79
  method: 'resource',
@@ -90,7 +91,7 @@ With these few rows of code will be created 6 routes:
90
91
 
91
92
  If you want to skip any of the previous routes, you can add the "not" property:
92
93
 
93
- ```typescript
94
+ ```javascript
94
95
  module.exports = {
95
96
  path: '/user',
96
97
  method: 'resource',
@@ -106,7 +107,7 @@ The available values of the "not" property are ['LIST', 'READ', 'CREATE', 'UPDAT
106
107
  If you need to control the logic of your resources, you can create a custom API that read and/or write the data in the JSON database.
107
108
  To achieve it create a new file in the "api" folder with the name you prefer and the following snippet:
108
109
 
109
- ```typescript
110
+ ```javascript
110
111
  const { getResource, setResource } = require('jgloo');
111
112
 
112
113
  module.exports = {
@@ -139,7 +140,7 @@ module.exports = {
139
140
  To add a middleware, you have to create a folder "middlewares" in your chosen root path (e.g. "mock/middlewares").
140
141
  Then create a new file inside with the name you prefer and the following sample snippet:
141
142
 
142
- ```typescript
143
+ ```javascript
143
144
  module.exports = (req, res, next) => {
144
145
  const isAuthorized = req.get('Authorization') === 'my-token';
145
146
  isAuthorized ? next() : res.sendStatus(401);
@@ -156,7 +157,7 @@ The resources are stored in JSON files placed in the subfolder "db" of your chos
156
157
  The [default ReST API](#create-a-default-rest-api) store the JSON file with the name generated by resource path replacing the slashes with the minus sign (e.g. "/auth/user" will be stored as "auth-user.json").
157
158
  If you want to specify the file name of the resources, you can set it as the "name" property of the API:
158
159
 
159
- ```typescript
160
+ ```javascript
160
161
  module.exports = {
161
162
  path: '/my/long/path',
162
163
  method: 'resource',
@@ -183,24 +184,37 @@ It's recommended to add the `static` folder in the `.gitignore` file.
183
184
 
184
185
  ---
185
186
 
187
+ ### Simulate network delay
188
+
189
+ If you want to simulate a network delay, you can add the `delay` property to your API configuration:
190
+
191
+ ```javascript
192
+ module.exports = {
193
+ ...
194
+ delay: 3 // Seconds
195
+ };
196
+ ```
197
+
198
+ ---
199
+
186
200
  ### Run the server
187
201
 
188
202
  To run the server execute the following command in your project root:
189
203
 
190
204
  ```shell
191
- jgloo
205
+ npx jgloo
192
206
  ```
193
207
 
194
208
  The full optional parameters are:
195
209
 
196
210
  ```shell
197
- jgloo -f [FOLDER] -p [PORT] -s [STATIC_URL]
211
+ npx jgloo -f [FOLDER] -p [PORT] -s [STATIC_URL]
198
212
  ```
199
213
 
200
214
  For example:
201
215
 
202
216
  ```shell
203
- jgloo -f 'mock' -p 3000 -s 'static'
217
+ npx jgloo -f 'mock' -p 3000 -s 'static'
204
218
  ```
205
219
 
206
220
  - "**-f**" or "**--folder**": the folder where your mocks are placed. It's optional, by default it's the folder "mock".
package/jgloo.js CHANGED
@@ -20,5 +20,11 @@ module.exports = {
20
20
  if (!existsSync(db)) { mkdirSync(db); }
21
21
  const path = `${db}/${name}.json`;
22
22
  writeFileSync(path, JSON.stringify(value), 'utf8');
23
- }
23
+ },
24
+
25
+ getDelayMiddleware: (delay) => {
26
+ return (_, __, next) => {
27
+ setTimeout(() => next(), delay * 1000);
28
+ };
29
+ },
24
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jgloo",
3
- "version": "1.5.2",
3
+ "version": "1.7.0",
4
4
  "description": "The coldest mock server.",
5
5
  "homepage": "https://github.com/zosma180/jgloo",
6
6
  "repository": {
@@ -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,11 +1,12 @@
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');
6
6
  const multer = require('multer');
7
7
 
8
8
  const { configureResource } = require(path.join(__dirname, 'rest'));
9
+ const { getDelayMiddleware } = require(path.join(__dirname, 'jgloo'));
9
10
  const app = express();
10
11
  const params = process.argv.slice(2);
11
12
  const root = params[0];
@@ -21,6 +22,22 @@ if (!existsSync(root)) {
21
22
  process.exit(2);
22
23
  }
23
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
+
24
41
  // Config
25
42
  app.use(cors());
26
43
  app.use(bodyParser.json());
@@ -28,10 +45,10 @@ app.use(multer({ dest: staticPath }).any());
28
45
 
29
46
  // Add middlewares
30
47
  if (existsSync(middlewarePath)) {
31
- readdirSync(middlewarePath)
32
- .filter((file) => file.endsWith('.js'))
33
- .forEach((file) => {
34
- const middleware = require(path.join(middlewarePath, file));
48
+ walk(middlewarePath)
49
+ .filter(file => file.endsWith('.js'))
50
+ .forEach(file => {
51
+ const middleware = require(file);
35
52
  app.use(middleware);
36
53
  });
37
54
  }
@@ -47,7 +64,7 @@ if (!existsSync(apiPath)) {
47
64
  process.exit(2);
48
65
  }
49
66
 
50
- const api = readdirSync(apiPath).filter((file) => file.endsWith('.js'));
67
+ const api = walk(apiPath).filter(file => file.endsWith('.js') );
51
68
 
52
69
  if (!api.length) {
53
70
  console.error(`No API file defined. Create one.`);
@@ -55,7 +72,12 @@ if (!api.length) {
55
72
  }
56
73
 
57
74
  api.forEach((file) => {
58
- const config = require(path.join(apiPath, file));
75
+ const config = require(file);
76
+
77
+ // Add the API delay, if it is provided
78
+ if (config.delay) {
79
+ app.use(config.path, getDelayMiddleware(config.delay));
80
+ }
59
81
 
60
82
  if (config.method === 'resource') {
61
83
  // ReST resource